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

Make to use project: undefined when parsing script-fragments in <template>. #195

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/html/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ export class Parser {
yield getParserLangFromSFC(doc)
},
),
project: undefined,
}
const scriptParserOptions = {
...this.baseParserOptions,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ function parseAsSFC(code: string, options: ParserOptions) {
yield "<template>"
yield getParserLangFromSFC(rootAST)
}),
project: undefined,
})
}
result.ast.templateBody = templateBody
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 @@ -513,14 +513,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 @@ -594,7 +592,7 @@ function getScriptSetupCodeBlocks(
}
fixNodeLocations(
body,
result.visitorKeys,
visitorKeys,
offsetLocationCalculator,
)
fixLocation(exportToken, offsetLocationCalculator)
Expand Down Expand Up @@ -692,7 +690,7 @@ function getScriptSetupCodeBlocks(
// restore
fixNodeLocations(
body,
result.visitorKeys,
visitorKeys,
offsetLocationCalculator,
)
for (const token of restoreTokens) {
Expand Down Expand Up @@ -823,7 +821,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
134 changes: 134 additions & 0 deletions test/parser-options-project.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
"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>
<style scoped>
.a {
color: v-bind(color)
}
</style>
`,
{
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)
})
})