Skip to content

Commit

Permalink
Make to use project: undefined when parsing template script-let.
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed May 12, 2023
1 parent fafbc7d commit 11c886c
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/html/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ export class Parser {
yield getParserLangFromSFC(doc)
},
),
project: undefined,
}
for (const proc of this.postProcessesForScript) {
proc(parserOptions)
Expand Down
12 changes: 5 additions & 7 deletions src/script-setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,12 @@ function getScriptSetupCodeBlocks(
const offsetLocationCalculator =
linesAndColumns.createOffsetLocationCalculator(scriptSetupStartOffset)

const result = parseScript(
const { ast, visitorKeys } = parseScript(
scriptCode,
parserOptions,
{ ...parserOptions, project: undefined },
offsetLocationCalculator,
)

const { ast } = result

// Holds the `import` and re-`export` statements.
// All import and re-`export` statements are hoisted to the top.
const importCodeBlocks = new CodeBlocks()
Expand Down Expand Up @@ -601,7 +599,7 @@ function getScriptSetupCodeBlocks(
}
fixNodeLocations(
body,
result.visitorKeys,
visitorKeys,
offsetLocationCalculator,
)
fixLocation(exportToken, offsetLocationCalculator)
Expand Down Expand Up @@ -698,7 +696,7 @@ function getScriptSetupCodeBlocks(
// restore
fixNodeLocations(
body,
result.visitorKeys,
visitorKeys,
offsetLocationCalculator,
)
for (const token of restoreTokens) {
Expand Down Expand Up @@ -745,7 +743,7 @@ function getScriptSetupCodeBlocks(
let start = n.range[0]
let end = n.range[1]
traverseNodes(n, {
visitorKeys: result.visitorKeys,
visitorKeys,
enterNode(c) {
start = Math.min(start, c.range[0])
end = Math.max(end, c.range[1])
Expand Down
2 changes: 1 addition & 1 deletion src/script-setup/parser-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function getScriptSetupParserOptions(

return {
...parserOptions,
ecmaVersion: espreeEcmaVersion,
ecmaVersion: espreeEcmaVersion || parserOptions.ecmaVersion,
}
}

Expand Down
129 changes: 129 additions & 0 deletions test/parser-options-project.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
"use strict"

const assert = require("assert")
const { parseForESLint } = require("../src")
const espree = require("espree")

describe("use `project: undefined` when parsing template script-let", () => {
it("should be the project option is defined only once in Simple SFC.", () => {
let projectCount = 0
parseForESLint(
`<template>
<div v-bind:class="{}">
<template v-for="item in items">
{{ 'str' }}
<button v-on:click="handler()"></button>
</template>
<MyComponent>
<template v-slot="{a}">
<div v-if="a">A</div>
</template>
</MyComponent>
</div>
</template>
<script>
export default {}
</script>
`,
{
project: true,
sourceType: "module",
ecmaVersion: 2018,
parser: {
parseForESLint(code, options) {
if (options.project) {
projectCount++
}

return {
ast: espree.parse(code, options),
}
},
},
},
)
assert.strictEqual(projectCount, 1)
})
it("should be the project option is defined only once in <script setup>.", () => {
let projectCount = 0
parseForESLint(
`<script setup>
let items = ["foo"]
</script>
<template>
<div v-bind:class="{}">
<template v-for="item in items">
{{ 'str' }}
<button v-on:click="handler()"></button>
</template>
<MyComponent>
<template v-slot="{a}">
<div v-if="a">A</div>
</template>
</MyComponent>
</div>
</template>
`,
{
project: true,
sourceType: "module",
ecmaVersion: 2018,
parser: {
parseForESLint(code, options) {
if (options.project) {
projectCount++
}

return {
ast: espree.parse(code, options),
}
},
},
},
)
assert.strictEqual(projectCount, 1)
})

it("should be the project option is defined only once in <script setup> with <script>.", () => {
let projectCount = 0
parseForESLint(
`<script>
import { ref } from 'vue'
</script>
<script setup>
let items = ref(["foo"])
</script>
<template>
<div v-bind:class="{}">
<template v-for="item in items">
{{ 'str' }}
<button v-on:click="handler()"></button>
</template>
<MyComponent>
<template v-slot="{a}">
<div v-if="a">A</div>
</template>
</MyComponent>
</div>
</template>
`,
{
project: true,
sourceType: "module",
ecmaVersion: 2018,
parser: {
parseForESLint(code, options) {
if (options.project) {
projectCount++
}

return {
ast: espree.parse(code, options),
}
},
},
},
)
assert.strictEqual(projectCount, 1)
})
})

0 comments on commit 11c886c

Please sign in to comment.