Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: reuse the AST of source code object in verify #9256

Merged
merged 1 commit into from
Sep 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 10 additions & 11 deletions lib/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,17 +691,8 @@ 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 @@ -718,6 +709,14 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -3049,6 +3049,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