Skip to content

Revert default option for sourceType: module on <script setup>. #111

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

Merged
merged 5 commits into from
Jul 2, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,32 @@ export function parseForESLint(
options = Object.assign(
{
comment: true,
ecmaVersion: 2017,
loc: true,
range: true,
tokens: true,
},
options || {},
)

const optionsWithEcmaVersion = {
...options,
ecmaVersion: options.ecmaVersion || 2017,
}

let result: AST.ESLintExtendedProgram
let document: AST.VDocumentFragment | null
let locationCalculator: LocationCalculatorForHtml | null
if (!isVueFile(code, options)) {
result = parseScript(code, options)
result = parseScript(code, optionsWithEcmaVersion)
document = null
locationCalculator = null
} else {
const skipParsingScript = options.parser === false
const tokenizer = new HTMLTokenizer(code, options)
const rootAST = new HTMLParser(tokenizer, options).parse()
const tokenizer = new HTMLTokenizer(code, optionsWithEcmaVersion)
const rootAST = new HTMLParser(
tokenizer,
optionsWithEcmaVersion,
).parse()

locationCalculator = new LocationCalculatorForHtml(
tokenizer.gaps,
Expand All @@ -124,7 +131,7 @@ export function parseForESLint(

let scriptSetup: VElement | undefined
if (skipParsingScript || !scripts.length) {
result = parseScript("", options)
result = parseScript("", optionsWithEcmaVersion)
} else if (
scripts.length === 2 &&
(scriptSetup = scripts.find(isScriptSetup))
Expand Down
4 changes: 1 addition & 3 deletions src/script-setup/parser-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export function getScriptSetupParserOptions(
)

return {
// Script setup requires module support, so set module to sourceType.
sourceType: "module",
...parserOptions,
ecmaVersion: espreeEcmaVersion,
}
Expand All @@ -28,5 +26,5 @@ function getDefaultEcmaVersion(def: number) {
// Script setup requires top level await support, so default the ecma version to 2022.
return getEspreeFromLinter().latestEcmaVersion!
}
return Math.max(def, 2015)
return Math.max(def, 2017)
}
5 changes: 4 additions & 1 deletion src/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,10 @@ export function parseScriptElement(
): ESLintExtendedProgram {
const parserOptions: ParserOptions = isScriptSetup(node)
? getScriptSetupParserOptions(originalParserOptions)
: originalParserOptions
: {
...originalParserOptions,
ecmaVersion: originalParserOptions.ecmaVersion || 2017,
}

const text = node.children[0]
const { code, offset } =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"sourceType": "module",
"ecmaVersion": "latest"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sourceType": "module"
}
16 changes: 16 additions & 0 deletions test/parser-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,21 @@ describe("parserOptions", () => {
assert.strictEqual(messages.length, 1)
assert.strictEqual(messages[0].ruleId, "vue/valid-template-root")
})

it("Fail in <script setup> without sourceType.", () => {
const code = `<template>Hello</template>
<script setup>import Foo from './foo'</script>`
const config = {
parser: "vue-eslint-parser",
parserOptions: {},
rules: {
"vue/valid-template-root": "error",
},
}
const messages = linter.verify(code, config, "test.vue")

assert.strictEqual(messages.length, 1)
assert.strictEqual(messages[0].fatal, true)
})
})
})
2 changes: 2 additions & 0 deletions typings/eslint-scope/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* See LICENSE file in root directory for full license.
*/
import type * as estree from "estree"
import type { VisitorKeys } from "eslint-visitor-keys"

export interface AnalysisOptions {
optimistic?: boolean
Expand All @@ -14,6 +15,7 @@ export interface AnalysisOptions {
fallback?: string | Function
sourceType?: "script" | "module"
ecmaVersion?: number
childVisitorKeys?: VisitorKeys
}

export interface ScopeManager {
Expand Down