Skip to content

feat(linter-rules/no-unused-vars): declared var must be used #2955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/core/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import linter from "./linter.js";
import { rule as localRefsExist } from "./linter-rules/local-refs-exist.js";
import { rule as noHeadinglessSectionsRule } from "./linter-rules/no-headingless-sections.js";
import { rule as noHttpPropsRule } from "./linter-rules/no-http-props.js";
import { rule as noUnusedVars } from "./linter-rules/no-unused-vars.js";
import { rule as privsecSection } from "./linter-rules/privsec-section.js";

linter.register(
noHttpPropsRule,
noHeadinglessSectionsRule,
noUnusedVars,
checkPunctuation,
localRefsExist,
checkInternalSlots,
Expand All @@ -26,6 +28,7 @@ export const coreDefaults = {
lint: {
"no-headingless-sections": true,
"no-http-props": true,
"no-unused-vars": false,
"check-punctuation": false,
"local-refs-exist": true,
"check-internal-slots": false,
Expand Down
86 changes: 86 additions & 0 deletions src/core/linter-rules/no-unused-vars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// @ts-check
/**
* Linter rule "no-unused-vars".
*
* Checks that an variable is used if declared (the first use is treated as
* declaration).
*/
import LinterRule from "../LinterRule.js";
import { lang as defaultLang } from "../l10n.js";
import { norm } from "../utils.js";

const name = "no-unused-vars";

const meta = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure the message here looks like one you would get from ESLint... that is, it should be clear that a "variable X was defined but never used".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We won't have access to "X" in linter message, so maybe:
"Variable was defined, but never used. Add a data-ignore-unused attribute to the <var>, or consider using <em>/ <code>"

Copy link
Contributor

@marcoscaceres marcoscaceres Jun 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Add a data-ignore-unused attribute to the , or consider using / "

Only the "data-ignore-unused" seems like an good suggestion... the other not so much as they are working around the problem, not addressing the problem explicitly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea what's going on with my comment's formatting above... 😂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only the "data-ignore-unused" seems like an good suggestion... the other not so much as they are working around the problem, not addressing the problem explicitly.

Maybe author used <var> for its style when they meant <em> or <code> (See https://github.com/w3c/webdriver/pull/1515/files)?

No idea what's going on with my comment's formatting above... 😂

Missed backticks on <em> 🎈

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we can't add a data-ignore-unused to the var micro-syntax.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using <var> for style is an author error, so let’s not worry about it (or encourage that).

en: {
description: "Variable was defined, but never used.",
howToFix: "Add a `data-ignore-unused` attribute to the `<var>`.",
help: "See developer console.",
},
};
// Fall back to english, if language is missing
const lang = defaultLang in meta ? defaultLang : "en";

/**
* @param {*} _
* @param {Document} doc
* @return {import("../LinterRule").LinterResult}
*/
function linterFunction(_, doc) {
const offendingElements = [];

/**
* Check if a <section> contains a `".algorithm"`
*
* The selector matches:
* ``` html
* <section><ul class="algorithm"></ul></section>
* <section><div><ul class="algorithm"></ul></div></section>
* ```
* The selector does not match:
* ``` html
* <section><section><ul class="algorithm"></ul></section></section>
* ```
* @param {HTMLElement} section
*/
const sectionContainsAlgorithm = section =>
!!section.querySelector(
":scope > :not(section) ~ .algorithm, :scope > :not(section) .algorithm"
);

for (const section of doc.querySelectorAll("section")) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we pick only those sections that contain algorithms?
See: https://github.com/w3c/payment-request/blob/e9515622/index.html#L280

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so... we might need to test out a few specs.

if (!sectionContainsAlgorithm(section)) continue;

/**
* `<var>` in this section, but excluding those in child sections.
* @type {NodeListOf<HTMLElement>}
*/
const varElems = section.querySelectorAll(":scope > :not(section) var");
if (!varElems.length) continue;

/** @type {Map<string, HTMLElement[]>} */
const varUsage = new Map();
for (const varElem of varElems) {
const key = norm(varElem.textContent);
const elems = varUsage.get(key) || varUsage.set(key, []).get(key);
elems.push(varElem);
}

for (const vars of varUsage.values()) {
if (vars.length === 1 && !vars[0].hasAttribute("data-ignore-unused")) {
offendingElements.push(vars[0]);
}
}
}

if (!offendingElements.length) {
return;
}
return {
name,
offendingElements,
occurrences: offendingElements.length,
...meta[lang],
};
}
export const rule = new LinterRule(name, linterFunction);
82 changes: 82 additions & 0 deletions tests/spec/core/linter-rules/no-unused-vars-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"use strict";

import { rule } from "../../../../src/core/linter-rules/no-unused-vars.js";

describe("Core Linter Rule - 'no-unused-vars'", () => {
const config = {
lint: { "no-unused-vars": true },
};
const doc = document.implementation.createHTMLDocument("test doc");
beforeEach(() => {
// Make sure every unordered test get an empty document
// See: https://github.com/w3c/respec/pull/1495
while (doc.body.firstChild) {
doc.body.removeChild(doc.body.firstChild);
}
});

it("skips sections without .algorithm", async () => {
doc.body.innerHTML = `
<section>
<p><var>varA</var></p>
<section>
<p><var>varC</var></p>
</section>
</section>
`;

const result = await rule.lint(config, doc);
expect(result).toBeUndefined();
});

it("complains on unused vars", async () => {
doc.body.innerHTML = `
<section>
<p><var>A</var> is unused</p>
<p><var>B</var></p>
<p><var>Z</var> is unused</p>
<ul class="algorithm">
<li><var>B</var></li>
<li><var>C</var> is unused</li>
<li><var>D</var></li>
<li><var>D</var></li>
</ul>
<section>
<p><var>E</var> is unused</p>
<ul class="algorithm">
<li><var>F</var> is unused</li>
<li><var>G</var></li>
<li><var>G</var></li>
<li><var>Z</var> is unused even though it's used in grandparent</li>
</ul>
</section>
</section>
`;

const result = await rule.lint(config, doc);
expect(result.name).toBe("no-unused-vars");
const unusedVars = result.offendingElements.map(v => v.textContent);
expect(unusedVars).toEqual(["A", "Z", "C", "E", "F", "Z"]);
expect(result.occurrences).toBe(6);
});

it("ignores unused vars with data-ignore-unused", async () => {
doc.body.innerHTML = `
<section>
<p><var data-ignore-unused>varA</var> is unused</p>
<ul class="algorithm">
<li><var>varB</var> is unused</li>
<li><var data-ignore-unused>varC</var> is unused</li>
<li><var>varD</var></li>
<li><var>varD</var></li>
</ul>
</section>
`;

const result = await rule.lint(config, doc);
expect(result.name).toBe("no-unused-vars");
expect(result.occurrences).toBe(1);
const unusedVars = result.offendingElements.map(v => v.textContent);
expect(unusedVars).toEqual(["varB"]);
});
});
2 changes: 2 additions & 0 deletions tests/spec/geonovum/defaults-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe("Geonovum — Defaults", () => {
"no-headingless-sections": true,
"privsec-section": true,
"no-http-props": true,
"no-unused-vars": false,
"local-refs-exist": true,
"check-punctuation": false,
"check-internal-slots": false,
Expand Down Expand Up @@ -53,6 +54,7 @@ describe("Geonovum — Defaults", () => {
"no-headingless-sections": true,
"privsec-section": false,
"no-http-props": false,
"no-unused-vars": false,
"local-refs-exist": true,
"check-punctuation": false,
"fake-linter-rule": "foo",
Expand Down
2 changes: 2 additions & 0 deletions tests/spec/w3c/defaults-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe("W3C — Defaults", () => {
"no-headingless-sections": true,
"privsec-section": true,
"no-http-props": true,
"no-unused-vars": false,
"local-refs-exist": true,
"check-punctuation": false,
"check-internal-slots": false,
Expand Down Expand Up @@ -53,6 +54,7 @@ describe("W3C — Defaults", () => {
"no-headingless-sections": true,
"privsec-section": false,
"no-http-props": false,
"no-unused-vars": false,
"local-refs-exist": true,
"check-punctuation": false,
"fake-linter-rule": "foo",
Expand Down