Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

Fix ESLint 6.2.0 + babel-eslint + no-unused-vars false positive with for-in loop #792

Closed
20 changes: 12 additions & 8 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
"use strict";

const config = require("eslint-config-babel");

module.exports = {
root: true,
extends: "babel",
plugins: ["prettier"],
rules: {
parser: "./lib",
extends: config.extends,
plugins: config.plugins.concat(["prettier"]),
rules: Object.assign(config.rules, {
"max-len": "off",
strict: "error",
"prettier/prettier": "error",
},
env: {
}),
env: Object.assign(config.env, {
node: true,
},
parserOptions: {
}),
parserOptions: Object.assign(config.parserOptions, {
sourceType: "script",
},
}),
globals: config.globals,
overrides: [
{
files: ["test/**/*"],
Expand Down
4 changes: 2 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"trailingComma": "es5"
}
"trailingComma": "es5"
}
21 changes: 21 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use strict";

module.exports = {
presets: [
["@babel/preset-env", { forceAllTransforms: true }],
["@babel/preset-flow", { all: true }],
"@babel/preset-react",
],
plugins: [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-syntax-import-meta",
"@babel/plugin-syntax-export-default-from",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-nullish-coalescing-operator",
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-syntax-numeric-separator",
"@babel/plugin-syntax-export-namespace-from",
["@babel/plugin-proposal-decorators", { decoratorsBeforeExport: false }],
["@babel/plugin-proposal-pipeline-operator", { proposal: "minimal" }],
],
};
20 changes: 0 additions & 20 deletions lib/babylon-to-espree/convertAST.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,6 @@ const astTransformVisitor = {
exit(path) {
const node = path.node;

if (path.isJSXText()) {
node.type = "Literal";
}

if (
path.isRestElement() &&
path.parent &&
path.parent.type === "ObjectPattern"
) {
node.type = "ExperimentalRestProperty";
}

if (
path.isSpreadElement() &&
path.parent &&
path.parent.type === "ObjectExpression"
) {
node.type = "ExperimentalSpreadProperty";
}

if (path.isTypeParameter()) {
node.type = "Identifier";
node.typeAnnotation = node.bound;
Expand Down
53 changes: 28 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"test-only": "cd test && mocha specs && cd -",
"lint": "eslint .",
"lint-fix": "npm run lint -- --fix",
"precommit": "lint-staged",
"preversion": "npm test",
"changelog": "git log `git describe --tags --abbrev=0`..HEAD --pretty=format:' * %s (%an)' | grep -v 'Merge pull request'"
},
Expand All @@ -33,42 +32,46 @@
"eslint": ">= 4.12.1"
},
"dependencies": {
"eslint-scope": "3.7.1",
"eslint-visitor-keys": "^1.0.0",
"semver": "^5.6.0"
"eslint-scope": "^5.0.0",
"eslint-visitor-keys": "^1.1.0",
"semver": "^6.3.0"
},
"devDependencies": {
"@babel/core": "^7.2.0",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-decorators": "^7.1.2",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0",
"@babel/plugin-proposal-optional-chaining": "^7.0.0",
"@babel/plugin-proposal-pipeline-operator": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/plugin-syntax-export-default-from": "^7.0.0",
"@babel/plugin-syntax-export-namespace-from": "^7.0.0",
"@babel/plugin-syntax-import-meta": "^7.0.0",
"@babel/plugin-syntax-numeric-separator": "^7.0.0",
"@babel/preset-env": "^7.1.5",
"@babel/core": "^7.5.5",
"@babel/plugin-proposal-class-properties": "^7.5.5",
"@babel/plugin-proposal-decorators": "^7.4.4",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.4.4",
"@babel/plugin-proposal-optional-chaining": "^7.2.0",
"@babel/plugin-proposal-pipeline-operator": "^7.5.0",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-syntax-export-default-from": "^7.2.0",
"@babel/plugin-syntax-export-namespace-from": "^7.2.0",
"@babel/plugin-syntax-import-meta": "^7.2.0",
"@babel/plugin-syntax-numeric-separator": "^7.2.0",
"@babel/preset-env": "^7.5.5",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"babel-eslint": "^10.0.2",
"dedent": "^0.7.0",
"eslint": "^6.0.1",
"eslint": "^6.2.0",
"eslint-config-babel": "^9.0.0",
"eslint-plugin-flowtype": "^3.11.1",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-flowtype": "^4.2.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-prettier": "^3.1.0",
"espree": "^3.5.2",
"husky": "^1.0.0-rc.13",
"lint-staged": "^7.2.2",
"mocha": "^6.1.4",
"prettier": "^1.4.4"
"espree": "^6.1.0",
"husky": "^3.0.4",
"lint-staged": "^9.2.3",
"mocha": "^6.2.0",
"prettier": "^1.18.2"
},
"lint-staged": {
"*.js": [
"eslint --format=codeframe --fix",
"git add"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
}
}
22 changes: 3 additions & 19 deletions test/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
"use strict";

module.exports = {
presets: [
["@babel/preset-env", { forceAllTransforms: true }],
["@babel/preset-flow", { all: true }],
"@babel/preset-react",
],
plugins: [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-syntax-import-meta",
"@babel/plugin-syntax-export-default-from",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-nullish-coalescing-operator",
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-syntax-numeric-separator",
"@babel/plugin-syntax-export-namespace-from",
["@babel/plugin-proposal-decorators", { decoratorsBeforeExport: false }],
["@babel/plugin-proposal-pipeline-operator", { proposal: "minimal" }],
],
};
const config = require("../babel.config.js");

module.exports = config;
4 changes: 3 additions & 1 deletion test/specs/babel-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ describe("babylon-to-espree", () => {
});

it("super outside method", () => {
parseAndAssertSame("function F() { super(); }");
assert.throws(() => {
parseAndAssertSame("function F() { super(); }");
}, /SyntaxError: 'super' keyword outside a method/);
});

it("StringLiteral", () => {
Expand Down
27 changes: 27 additions & 0 deletions test/specs/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,30 @@ describe("https://github.com/babel/babel-eslint/issues/558", () => {
]);
});
});

describe("no-unused-vars", () => {
const ruleId = "no-unused-vars";
const eslintOpts = Object.assign({}, baseEslintOpts, {
rules: {},
});
eslintOpts.rules[ruleId] = [errorLevel, "never"];

it("should not detect false positives inside for loop", () => {
const src =
"const nodeList = []; const visit = () => {}; for (const node of nodeList) { visit(node); }";

const report = eslint.linter.verify(src, eslintOpts);

assert(report.length === 0);
});

it("should detect unused var inside for loop", () => {
const src =
"const nodeList = []; const visit = () => {}; for (const node of nodeList) { visit(); }";

const report = eslint.linter.verify(src, eslintOpts);

assert(report.length === 1);
assert(report[0].ruleId === ruleId);
});
});
Loading