Fix escope to take sourceType and ecmaVersion from options (babel/babel-eslint#288)

escope was hardcoded to sourcetype: "module" and ecmaVersion: "6"
This changes it to take the configuration from the eslint options and still
defaulting to "module" and "6".
This is done by having to global variables, as monkeypatch is only triggered once.
To fix scoping issues, the same logic as in eslint is applied. It disables the nodejs scope
if the sourceType is module.
This commit is contained in:
Daniel Tschinder
2016-04-20 02:56:15 +02:00
parent fd36e3cf8d
commit cf456bfe4a
2 changed files with 99 additions and 22 deletions

View File

@@ -2,27 +2,32 @@
"use strict";
var eslint = require("eslint");
function verifyAndAssertMessages(code, rules, expectedMessages, sourceType) {
var messages = eslint.linter.verify(
code,
{
parser: require.resolve(".."),
rules: rules,
env: {
node: true,
es6: true
function verifyAndAssertMessages(code, rules, expectedMessages, sourceType, overrideConfig) {
var config = {
parser: require.resolve(".."),
rules: rules,
env: {
node: true,
es6: true
},
parserOptions: {
ecmaVersion: 6,
ecmaFeatures: {
jsx: true,
experimentalObjectRestSpread: true,
globalReturn: true
},
parserOptions: {
ecmaVersion: 6,
ecmaFeatures: {
jsx: true,
experimentalObjectRestSpread: true,
globalReturn: true
},
sourceType: sourceType || "module"
}
sourceType: sourceType
}
);
};
if (overrideConfig) {
for (var key in overrideConfig) {
config[key] = overrideConfig[key]
}
}
var messages = eslint.linter.verify(code, config);
if (messages.length !== expectedMessages.length) {
throw new Error("Expected " + expectedMessages.length + " message(s), got " + messages.length + " " + JSON.stringify(messages));
@@ -1362,6 +1367,67 @@ describe("verify", function () {
);
});
it("correctly detects redeclares if in script mode #217", function () {
verifyAndAssertMessages([
"var a = 321;",
"var a = 123;",
].join("\n"),
{ "no-redeclare": 1 },
[ "2:5 'a' is already defined no-redeclare" ],
"script"
);
});
it("correctly detects redeclares if in module mode #217", function () {
verifyAndAssertMessages([
"var a = 321;",
"var a = 123;",
].join("\n"),
{ "no-redeclare": 1 },
[ "2:5 'a' is already defined no-redeclare" ],
"module"
);
});
it("no-implicit-globals in script", function () {
verifyAndAssertMessages(
"var leakedGlobal = 1;",
{ "no-implicit-globals": 1 },
[ "1:5 Implicit global variable, assign as global property instead. no-implicit-globals" ],
"script",
{
env: {},
parserOptions: { ecmaVersion: 6, sourceType: "script" }
}
);
});
it("no-implicit-globals in module", function () {
verifyAndAssertMessages(
"var leakedGlobal = 1;",
{ "no-implicit-globals": 1 },
[],
"module",
{
env: {},
parserOptions: { ecmaVersion: 6, sourceType: "module" }
}
);
});
it("no-implicit-globals in default", function () {
verifyAndAssertMessages(
"var leakedGlobal = 1;",
{ "no-implicit-globals": 1 },
[],
null,
{
env: {},
parserOptions: { ecmaVersion: 6 }
}
);
});
// it("regex with es6 unicodeCodePointEscapes", function () {
// verifyAndAssertMessages(
// "string.replace(/[\u{0000A0}-\u{10FFFF}<>\&]/gmiu, (char) => `&#x${char.codePointAt(0).toString(16)};`);",