Skip to content

Commit

Permalink
Fix: reuse the AST of source code object in verify (#9256)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea authored and not-an-aardvark committed Sep 10, 2017
1 parent cd698ba commit 981f933
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
21 changes: 10 additions & 11 deletions lib/linter.js
Expand Up @@ -750,17 +750,8 @@ module.exports = class Linter {
verify(textOrSourceCode, config, filenameOrOptions, saveState) {
let text,
parserServices,
allowInlineConfig;

if (typeof textOrSourceCode === "string") {
this.sourceCode = null;
text = textOrSourceCode;
} else {
this.sourceCode = textOrSourceCode;
text = this.sourceCode.text;
}

let providedFilename;
allowInlineConfig,
providedFilename;

// evaluate arguments
if (typeof filenameOrOptions === "object") {
Expand All @@ -777,6 +768,14 @@ module.exports = class Linter {
this.reset();
}

if (typeof textOrSourceCode === "string") {
this.sourceCode = null;
text = textOrSourceCode;
} else {
this.sourceCode = textOrSourceCode;
text = this.sourceCode.text;
}

// search and apply "eslint-env *".
const envInFile = findEslintEnv(text);

Expand Down
23 changes: 23 additions & 0 deletions tests/lib/linter.js
Expand Up @@ -3202,6 +3202,29 @@ describe("Linter", () => {
linter.verify(linter.getSourceCode(), { parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } });
});

it("should reuse the SourceCode object", () => {
let ast1 = null,
ast2 = null;

linter.defineRule("save-ast1", () => ({
Program(node) {
ast1 = node;
}
}));
linter.defineRule("save-ast2", () => ({
Program(node) {
ast2 = node;
}
}));

linter.verify("function render() { return <div className='test'>{hello}</div> }", { parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }, rules: { "save-ast1": 2 } });
linter.verify(linter.getSourceCode(), { parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }, rules: { "save-ast2": 2 } });

assert(ast1 !== null);
assert(ast2 !== null);
assert(ast1 === ast2);
});

it("should allow 'await' as a property name in modules", () => {
const result = linter.verify(
"obj.await",
Expand Down

0 comments on commit 981f933

Please sign in to comment.