Skip to content

Commit 812fd21

Browse files
committed
Fix: didn't work if AST is reused
1 parent 7a4717a commit 812fd21

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

src/parser-services.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,20 @@ export function define(rootAST: ESLintProgram) {
4646

4747
const programExitHandler = scriptVisitor["Program:exit"]
4848
scriptVisitor["Program:exit"] = function() {
49-
if (typeof programExitHandler === "function") {
50-
programExitHandler.apply(this, arguments) //eslint-disable-line prefer-rest-params
51-
}
49+
try {
50+
if (typeof programExitHandler === "function") {
51+
programExitHandler.apply(this, arguments) //eslint-disable-line prefer-rest-params
52+
}
5253

53-
// Traverse template body.
54-
const generator = new NodeEventGenerator(emitter as EventEmitter)
55-
traverseNodes(rootAST.templateBody as VElement, generator)
54+
// Traverse template body.
55+
const generator = new NodeEventGenerator(emitter as EventEmitter)
56+
traverseNodes(rootAST.templateBody as VElement, generator)
57+
}
58+
finally {
59+
// @ts-ignore
60+
scriptVisitor["Program:exit"] = programExitHandler
61+
emitters.delete(rootAST)
62+
}
5663
}
5764
}
5865

test/index.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ const path = require("path")
1414
const fs = require("fs-extra")
1515
const parse = require("..").parse
1616
const parseForESLint = require("..").parseForESLint
17-
const CLIEngine = require("./fixtures/eslint").CLIEngine
17+
const eslint = require("./fixtures/eslint")
18+
const CLIEngine = eslint.CLIEngine
19+
const Linter = eslint.Linter
1820

1921
//------------------------------------------------------------------------------
2022
// Helpers
@@ -513,4 +515,32 @@ describe("Basic tests", () => {
513515
)
514516
})
515517
})
518+
519+
describe("parserServices.defineTemplateBodyVisitor", () => {
520+
it("should work even if AST object was reused.", () => {
521+
const code = "<template><div/></template>"
522+
const config = {
523+
parser: PARSER_PATH,
524+
rules: {
525+
"test-rule": "error",
526+
},
527+
}
528+
const linter = new Linter()
529+
530+
//eslint-disable-next-line no-shadow
531+
linter.defineRule("test-rule", (context) => context.parserServices.defineTemplateBodyVisitor({
532+
"VElement[name='div']"(node) {
533+
context.report({ node, message: "OK" })
534+
},
535+
}))
536+
537+
const messages1 = linter.verify(code, config)
538+
const messages2 = linter.verify(linter.getSourceCode(), config)
539+
540+
assert.equal(messages1.length, 1)
541+
assert.equal(messages1[0].message, "OK")
542+
assert.equal(messages2.length, 1)
543+
assert.equal(messages1[0].message, "OK")
544+
})
545+
})
516546
})

0 commit comments

Comments
 (0)