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

[PATCH] [10.x] Fix ESLint 6.2.0 + babel-eslint + no-unused-vars false positive with for-in loop #793

Closed
Show file tree
Hide file tree
Changes from all 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
23 changes: 14 additions & 9 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
"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, {
"no-var": 0,
"max-len": 0,
"prettier/prettier": "error",
},
env: {
}),
env: Object.assign(config.env, {
node: true,
mocha: true
}
mocha: true,
}),
parserOptions: config.parserOptions,
globals: config.globals,
};
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"
}
4 changes: 3 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ module.exports = function(code, options) {
options.ecmaFeatures && options.ecmaFeatures.legacyDecorators;

var opts = {
codeFrame: options.hasOwnProperty("codeFrame") ? options.codeFrame : true,
codeFrame: Object.prototype.hasOwnProperty.call(options, "codeFrame")
? options.codeFrame
: true,
sourceType: options.sourceType,
allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with espree
allowReturnOutsideFunction: true,
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"@babel/parser": "^7.0.0",
"@babel/traverse": "^7.0.0",
"@babel/types": "^7.0.0",
"eslint-scope": "3.7.1",
"eslint-visitor-keys": "^1.0.0"
},
"scripts": {
Expand All @@ -37,12 +36,12 @@
},
"homepage": "https://github.com/babel/babel-eslint",
"peerDependencies": {
"eslint": ">= 4.12.1"
"eslint": ">= 4.12.1",
"eslint-scope": "^3.7.1 || ^4.0.0 || ^5.0.0"
},
"devDependencies": {
"babel-eslint": "^8.2.6",
"dedent": "^0.7.0",
"eslint": "^5.6.0",
"eslint": "~5.4.0",
"eslint-config-babel": "^7.0.1",
"eslint-plugin-flowtype": "^2.30.3",
"eslint-plugin-import": "^2.14.0",
Expand Down
8 changes: 2 additions & 6 deletions test/babel-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,11 @@ describe("babylon-to-espree", () => {
});

it("export named", () => {
parseAndAssertSame("export { foo };");
parseAndAssertSame("var foo = 1;export { foo };");
});

it("export named alias", () => {
parseAndAssertSame("export { foo as bar };");
parseAndAssertSame("var foo = 1;export { foo as bar };");
});

// Espree doesn't support the optional chaining operator yet
Expand Down Expand Up @@ -470,10 +470,6 @@ describe("babylon-to-espree", () => {
parseAndAssertSame("return;");
});

it("super outside method", () => {
parseAndAssertSame("function F() { super(); }");
});

it("StringLiteral", () => {
parseAndAssertSame("");
parseAndAssertSame("");
Expand Down
33 changes: 32 additions & 1 deletion test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ var eslint = require("eslint");
var fs = require("fs");
var path = require("path");

var parser = require("../lib");

eslint.linter.defineParser("current-babel-eslint", parser);

var paths = {
fixtures: path.join(__dirname, "fixtures", "rules"),
};
Expand All @@ -11,7 +15,7 @@ var encoding = "utf8";
var errorLevel = 2;

var baseEslintOpts = {
parser: require.resolve(".."),
parser: "current-babel-eslint",
parserOptions: {
sourceType: "script",
},
Expand Down Expand Up @@ -281,3 +285,30 @@ function strictSuite() {
});
});
}

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);
});
});
9 changes: 7 additions & 2 deletions test/non-regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
var eslint = require("eslint");
var unpad = require("dedent");

var parser = require("../lib");

function verifyAndAssertMessagesWithSpecificESLint(
code,
rules,
Expand All @@ -12,7 +14,7 @@ function verifyAndAssertMessagesWithSpecificESLint(
linter
) {
var config = {
parser: require.resolve(".."),
parser: "current-babel-eslint",
rules,
env: {
node: true,
Expand Down Expand Up @@ -68,13 +70,16 @@ function verifyAndAssertMessages(
sourceType,
overrideConfig
) {
var linter = new eslint.Linter();
linter.defineParser("current-babel-eslint", parser);

verifyAndAssertMessagesWithSpecificESLint(
unpad(`${code}`),
rules || {},
expectedMessages || [],
sourceType,
overrideConfig,
new eslint.Linter()
linter
);
}

Expand Down
Loading