Skip to content

Commit 1d65789

Browse files
authored
Fix parsing error when combining <script> and export in <script setup> (#115)
* Fix parsing error when combining `<script>` and `export` in `<script setup>` * update * update
1 parent 4d6e4c1 commit 1d65789

38 files changed

+8045
-152
lines changed

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464
"setup": "git submodule update --init && cd test/fixtures/eslint && npm install",
6565
"pretest": "run-s build lint",
6666
"test": "npm run -s test:mocha",
67-
"test:mocha": "nyc mocha \"test/*.js\" --reporter dot --timeout 10000",
68-
"test:debug": "mocha --inspect --require ts-node/register/transpile-only \"test/*.js\" --reporter dot --timeout 10000",
67+
"test:mocha": "nyc mocha \"test/*.js\" --reporter dot --timeout 60000",
68+
"test:debug": "mocha --inspect --require ts-node/register/transpile-only \"test/*.js\" --reporter dot --timeout 60000",
6969
"preupdate-fixtures": "npm run -s build",
7070
"update-fixtures": "node scripts/update-fixtures-ast.js && node scripts/update-fixtures-document-fragment.js",
7171
"preversion": "npm test",

Diff for: scripts/update-fixtures-ast.js

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ for (const name of TARGETS) {
228228
? JSON.parse(fs.readFileSync(optionsPath, "utf8"))
229229
: {}
230230
)
231+
// console.log("Start:", name)
231232
const actual = parser.parseForESLint(source, options)
232233
const tokenRanges = getAllTokens(actual.ast).map((t) =>
233234
source.slice(t.range[0], t.range[1])

Diff for: src/ast/nodes.ts

+1
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ export interface ESLintExportNamedDeclaration extends HasLocation, HasParent {
310310

311311
export interface ESLintExportSpecifier extends HasLocation, HasParent {
312312
type: "ExportSpecifier"
313+
local: ESLintIdentifier
313314
exported: ESLintIdentifier
314315
}
315316

Diff for: src/common/fix-locations.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {
22
ESLintExtendedProgram,
3+
ESLintNode,
34
HasLocation,
45
LocationRange,
56
Node,
@@ -20,13 +21,28 @@ import type { LocationCalculator } from "./location-calculator"
2021
export function fixLocations(
2122
result: ESLintExtendedProgram,
2223
locationCalculator: LocationCalculator,
24+
): void {
25+
fixNodeLocations(result.ast, result.visitorKeys, locationCalculator)
26+
27+
for (const token of result.ast.tokens || []) {
28+
fixLocation(token, locationCalculator)
29+
}
30+
for (const comment of result.ast.comments || []) {
31+
fixLocation(comment, locationCalculator)
32+
}
33+
}
34+
35+
export function fixNodeLocations(
36+
rootNode: ESLintNode,
37+
visitorKeys: ESLintExtendedProgram["visitorKeys"],
38+
locationCalculator: LocationCalculator,
2339
): void {
2440
// There are cases which the same node instance appears twice in the tree.
2541
// E.g. `let {a} = {}` // This `a` appears twice at `Property#key` and `Property#value`.
2642
const traversed = new Map<Node | number[] | LocationRange, Node>()
2743

28-
traverseNodes(result.ast, {
29-
visitorKeys: result.visitorKeys,
44+
traverseNodes(rootNode, {
45+
visitorKeys,
3046

3147
enterNode(node, parent) {
3248
if (!traversed.has(node)) {
@@ -65,13 +81,6 @@ export function fixLocations(
6581
// Do nothing.
6682
},
6783
})
68-
69-
for (const token of result.ast.tokens || []) {
70-
fixLocation(token, locationCalculator)
71-
}
72-
for (const comment of result.ast.comments || []) {
73-
fixLocation(comment, locationCalculator)
74-
}
7584
}
7685

7786
/**

0 commit comments

Comments
 (0)