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:
parent
fd36e3cf8d
commit
cf456bfe4a
@ -10,6 +10,7 @@ var traverse = require("babel-traverse").default;
|
|||||||
|
|
||||||
var estraverse;
|
var estraverse;
|
||||||
var hasPatched = false;
|
var hasPatched = false;
|
||||||
|
var eslintOptions = {};
|
||||||
|
|
||||||
function createModule(filename) {
|
function createModule(filename) {
|
||||||
var mod = new Module(filename);
|
var mod = new Module(filename);
|
||||||
@ -75,8 +76,11 @@ function monkeypatch() {
|
|||||||
var escope = require(escopeLoc);
|
var escope = require(escopeLoc);
|
||||||
var analyze = escope.analyze;
|
var analyze = escope.analyze;
|
||||||
escope.analyze = function (ast, opts) {
|
escope.analyze = function (ast, opts) {
|
||||||
opts.ecmaVersion = 6;
|
opts.ecmaVersion = eslintOptions.ecmaVersion;
|
||||||
opts.sourceType = "module";
|
opts.sourceType = eslintOptions.sourceType;
|
||||||
|
if (eslintOptions.globalReturn !== undefined) {
|
||||||
|
opts.nodejsScope = eslintOptions.globalReturn;
|
||||||
|
}
|
||||||
|
|
||||||
var results = analyze.call(this, ast, opts);
|
var results = analyze.call(this, ast, opts);
|
||||||
return results;
|
return results;
|
||||||
@ -353,6 +357,13 @@ function monkeypatch() {
|
|||||||
|
|
||||||
exports.parse = function (code, options) {
|
exports.parse = function (code, options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
eslintOptions.ecmaVersion = options.ecmaVersion = options.ecmaVersion || 6;
|
||||||
|
eslintOptions.sourceType = options.sourceType = options.sourceType || "module";
|
||||||
|
if (options.sourceType === "module") {
|
||||||
|
eslintOptions.globalReturn = false;
|
||||||
|
} else {
|
||||||
|
delete eslintOptions.globalReturn;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
monkeypatch();
|
monkeypatch();
|
||||||
@ -366,7 +377,7 @@ exports.parse = function (code, options) {
|
|||||||
|
|
||||||
exports.parseNoPatch = function (code, options) {
|
exports.parseNoPatch = function (code, options) {
|
||||||
var opts = {
|
var opts = {
|
||||||
sourceType: options.sourceType || "module",
|
sourceType: options.sourceType,
|
||||||
strictMode: true,
|
strictMode: true,
|
||||||
allowImportExportEverywhere: false, // consistent with espree
|
allowImportExportEverywhere: false, // consistent with espree
|
||||||
allowReturnOutsideFunction: true,
|
allowReturnOutsideFunction: true,
|
||||||
|
|||||||
@ -2,10 +2,8 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
var eslint = require("eslint");
|
var eslint = require("eslint");
|
||||||
|
|
||||||
function verifyAndAssertMessages(code, rules, expectedMessages, sourceType) {
|
function verifyAndAssertMessages(code, rules, expectedMessages, sourceType, overrideConfig) {
|
||||||
var messages = eslint.linter.verify(
|
var config = {
|
||||||
code,
|
|
||||||
{
|
|
||||||
parser: require.resolve(".."),
|
parser: require.resolve(".."),
|
||||||
rules: rules,
|
rules: rules,
|
||||||
env: {
|
env: {
|
||||||
@ -19,10 +17,17 @@ function verifyAndAssertMessages(code, rules, expectedMessages, sourceType) {
|
|||||||
experimentalObjectRestSpread: true,
|
experimentalObjectRestSpread: true,
|
||||||
globalReturn: 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) {
|
if (messages.length !== expectedMessages.length) {
|
||||||
throw new Error("Expected " + expectedMessages.length + " message(s), got " + messages.length + " " + JSON.stringify(messages));
|
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 () {
|
// it("regex with es6 unicodeCodePointEscapes", function () {
|
||||||
// verifyAndAssertMessages(
|
// verifyAndAssertMessages(
|
||||||
// "string.replace(/[\u{0000A0}-\u{10FFFF}<>\&]/gmiu, (char) => `&#x${char.codePointAt(0).toString(16)};`);",
|
// "string.replace(/[\u{0000A0}-\u{10FFFF}<>\&]/gmiu, (char) => `&#x${char.codePointAt(0).toString(16)};`);",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user