From cae678c36f3ff45055af7bf65c4c6ecc0739951a Mon Sep 17 00:00:00 2001 From: baseballyama Date: Sun, 1 Dec 2024 21:54:21 +0900 Subject: [PATCH] feat: treat jsdoc with @typescript-eslint/parser --- src/context/index.ts | 22 +- src/parser/index.ts | 47 +- src/parser/parser-options.ts | 57 +- .../ts-multiple-parser-setup.ts | 2 +- .../type-info-tests/jsdoc-input.svelte | 6 + .../type-info-tests/jsdoc-output.json | 9 + .../type-info-tests/jsdoc-setup.ts | 31 + tests/fixtures/parser/ast/jsdoc-input.svelte | 6 + .../ast/jsdoc-no-unused-vars-result.json | 8 + tests/fixtures/parser/ast/jsdoc-output.json | 606 ++++++++++++++++++ .../parser/ast/jsdoc-requirements.json | 5 + .../parser/ast/jsdoc-scope-output.json | 524 +++++++++++++++ .../parser/ast/jsdoc-type-output.svelte | 6 + .../ast/svelte5/ts-event02-type-output.svelte | 2 +- .../parser/ast/ts-event02-type-output.svelte | 4 +- tests/fixtures/tsconfig.test.json | 4 +- tools/update-fixtures.ts | 6 +- 17 files changed, 1300 insertions(+), 45 deletions(-) create mode 100644 tests/fixtures/integrations/type-info-tests/jsdoc-input.svelte create mode 100644 tests/fixtures/integrations/type-info-tests/jsdoc-output.json create mode 100644 tests/fixtures/integrations/type-info-tests/jsdoc-setup.ts create mode 100644 tests/fixtures/parser/ast/jsdoc-input.svelte create mode 100644 tests/fixtures/parser/ast/jsdoc-no-unused-vars-result.json create mode 100644 tests/fixtures/parser/ast/jsdoc-output.json create mode 100644 tests/fixtures/parser/ast/jsdoc-requirements.json create mode 100644 tests/fixtures/parser/ast/jsdoc-scope-output.json create mode 100644 tests/fixtures/parser/ast/jsdoc-type-output.svelte diff --git a/src/context/index.ts b/src/context/index.ts index 6cda47c8..efe953ef 100644 --- a/src/context/index.ts +++ b/src/context/index.ts @@ -18,13 +18,17 @@ import { LetDirectiveCollections } from "./let-directive-collection.js"; import { parseAttributes } from "../parser/html.js"; import { sortedLastIndex } from "../utils/index.js"; import { - isTypeScript, + getLanguage, type NormalizedParserOptions, } from "../parser/parser-options.js"; export class ScriptsSourceCode { private raw: string; + public getRaw(): string { + return this.raw; + } + private trimmedRaw: string; public readonly attrs: Record; @@ -175,7 +179,7 @@ export class Context { public readonly snippets: SvelteSnippetBlock[] = []; // ----- States ------ - private readonly state: { isTypeScript?: boolean } = {}; + private readonly state: { language?: "js" | "ts" | "jsdoc" | string } = {}; private readonly blocks: Block[] = []; @@ -321,11 +325,17 @@ export class Context { } public isTypeScript(): boolean { - if (this.state.isTypeScript != null) { - return this.state.isTypeScript; + return this.getLanguage() === "ts"; + } + + public getLanguage(): "js" | "ts" | "jsdoc" | string { + if (this.state.language != null) { + return this.state.language; } - const lang = this.sourceCode.scripts.attrs.lang; - return (this.state.isTypeScript = isTypeScript(this.parserOptions, lang)); + const rawLang = this.sourceCode.scripts.attrs.lang; + const code = this.sourceCode.scripts.getRaw(); + const language = getLanguage(this.parserOptions, rawLang, code); + return (this.state.language = language); } public stripScriptCode(start: number, end: number): void { diff --git a/src/parser/index.ts b/src/parser/index.ts index f5722ae9..99061ca3 100644 --- a/src/parser/index.ts +++ b/src/parser/index.ts @@ -39,7 +39,7 @@ import { } from "./style-context.js"; import { getGlobalsForSvelte, getGlobalsForSvelteScript } from "./globals.js"; import type { NormalizedParserOptions } from "./parser-options.js"; -import { isTypeScript, normalizeParserOptions } from "./parser-options.js"; +import { getLanguage, normalizeParserOptions } from "./parser-options.js"; import { getFragmentFromRoot } from "./compat.js"; import { isEnableRunes, @@ -143,18 +143,24 @@ function parseAsSvelte( ); const scripts = ctx.sourceCode.scripts; - const resultScript = ctx.isTypeScript() - ? parseTypeScriptInSvelte( - scripts.getCurrentVirtualCodeInfo(), - scripts.attrs, - parserOptions, - { slots: ctx.slots, svelteParseContext }, - ) - : parseScriptInSvelte( - scripts.getCurrentVirtualCode(), - scripts.attrs, - parserOptions, - ); + const language = ctx.getLanguage(); + + const resultScript = + language === "ts" || language === "jsdoc" + ? parseTypeScriptInSvelte( + scripts.getCurrentVirtualCodeInfo(), + { + ...scripts.attrs, + lang: language === "jsdoc" ? "ts" : scripts.attrs.lang, + }, + parserOptions, + { slots: ctx.slots, svelteParseContext }, + ) + : parseScriptInSvelte( + scripts.getCurrentVirtualCode(), + scripts.attrs, + parserOptions, + ); ctx.scriptLet.restore(resultScript); ctx.tokens.push(...resultScript.ast.tokens); ctx.comments.push(...resultScript.ast.comments); @@ -252,10 +258,17 @@ function parseAsScript( parserOptions: NormalizedParserOptions, svelteParseContext: SvelteParseContext, ): ParseResult { - const lang = parserOptions.filePath?.split(".").pop(); - const resultScript = isTypeScript(parserOptions, lang) - ? parseTypeScript(code, { lang }, parserOptions, svelteParseContext) - : parseScript(code, { lang }, parserOptions); + const rawLang = parserOptions.filePath?.split(".").pop(); + const lang = getLanguage(parserOptions, rawLang, code); + const resultScript = + lang === "ts" || lang === "jsdoc" + ? parseTypeScript( + code, + { lang: lang === "jsdoc" ? "ts" : rawLang }, + parserOptions, + svelteParseContext, + ) + : parseScript(code, { lang: rawLang }, parserOptions); // Add $$xxx variable addGlobalVariables( diff --git a/src/parser/parser-options.ts b/src/parser/parser-options.ts index fd338485..9c4cc8d6 100644 --- a/src/parser/parser-options.ts +++ b/src/parser/parser-options.ts @@ -63,23 +63,37 @@ const TS_PARSER_NAMES = [ "typescript-eslint-parser-for-extra-files", ]; -export function isTypeScript( +export function getLanguage( parserOptions: NormalizedParserOptions, lang: string | undefined, -): boolean { - if (!lang) { - return false; + code: string | undefined, +): "js" | "ts" | "jsdoc" | string { + const hasJsDoc = code ? jsdocTags.some((tag) => tag.test(code)) : false; + if (!lang && !hasJsDoc) { + return "js"; } - const parserValue = getParserForLang(lang, parserOptions?.parser); + + function getFinalLang(isTS: boolean): string { + if (isTS) { + if (lang) return lang; + return hasJsDoc ? "jsdoc" : "ts"; + } + return lang || "js"; + } + + const parserValue = getParserForLang( + lang || (hasJsDoc ? "ts" : undefined), + parserOptions?.parser, + ); if (typeof parserValue !== "string") { - return ( + const isTS = maybeTSESLintParserObject(parserValue) || - isTSESLintParserObject(parserValue) - ); + isTSESLintParserObject(parserValue); + return getFinalLang(isTS); } const parserName = parserValue; if (TS_PARSER_NAMES.includes(parserName)) { - return true; + return getFinalLang(true); } if (TS_PARSER_NAMES.some((nm) => parserName.includes(nm))) { let targetPath = parserName; @@ -87,11 +101,12 @@ export function isTypeScript( const pkgPath = path.join(targetPath, "package.json"); if (fs.existsSync(pkgPath)) { try { - return TS_PARSER_NAMES.includes( + const isTS = TS_PARSER_NAMES.includes( JSON.parse(fs.readFileSync(pkgPath, "utf-8"))?.name, ); + return getFinalLang(isTS); } catch { - return false; + return getFinalLang(false); } } const parent = path.dirname(targetPath); @@ -102,5 +117,23 @@ export function isTypeScript( } } - return false; + return getFinalLang(false); } + +const jsdocTags = [ + /@type\s/, + /@param\s/, + /@arg\s/, + /@argument\s/, + /@returns\s/, + /@return\s/, + /@typedef\s/, + /@callback\s/, + /@template\s/, + /@class\s/, + /@constructor\s/, + /@this\s/, + /@extends\s/, + /@augments\s/, + /@enum\s/, +] as const; diff --git a/tests/fixtures/integrations/parser-object-tests/ts-multiple-parser-setup.ts b/tests/fixtures/integrations/parser-object-tests/ts-multiple-parser-setup.ts index e044a48c..38b04808 100644 --- a/tests/fixtures/integrations/parser-object-tests/ts-multiple-parser-setup.ts +++ b/tests/fixtures/integrations/parser-object-tests/ts-multiple-parser-setup.ts @@ -8,7 +8,7 @@ export function getConfig(): Linter.Config { return { languageOptions: { parser, - parserOptions: generateParserOptions({ parser: { ts } }), + parserOptions: generateParserOptions({ parser: ts }), globals: { ...globals.browser, ...globals.es2021, diff --git a/tests/fixtures/integrations/type-info-tests/jsdoc-input.svelte b/tests/fixtures/integrations/type-info-tests/jsdoc-input.svelte new file mode 100644 index 00000000..438c6e87 --- /dev/null +++ b/tests/fixtures/integrations/type-info-tests/jsdoc-input.svelte @@ -0,0 +1,6 @@ + diff --git a/tests/fixtures/integrations/type-info-tests/jsdoc-output.json b/tests/fixtures/integrations/type-info-tests/jsdoc-output.json new file mode 100644 index 00000000..ccc3f0ea --- /dev/null +++ b/tests/fixtures/integrations/type-info-tests/jsdoc-output.json @@ -0,0 +1,9 @@ +[ + { + "ruleId": "@typescript-eslint/no-unsafe-return", + "code": "return x + 1;", + "line": 4, + "column": 5, + "message": "Unsafe return of a value of type `any`." + } +] \ No newline at end of file diff --git a/tests/fixtures/integrations/type-info-tests/jsdoc-setup.ts b/tests/fixtures/integrations/type-info-tests/jsdoc-setup.ts new file mode 100644 index 00000000..183f8c13 --- /dev/null +++ b/tests/fixtures/integrations/type-info-tests/jsdoc-setup.ts @@ -0,0 +1,31 @@ +import type { Linter } from "eslint"; +import { generateParserOptions } from "../../../src/parser/test-utils"; +import { rules } from "@typescript-eslint/eslint-plugin"; +import * as ts from "@typescript-eslint/parser"; +import * as parser from "../../../../src"; +import globals from "globals"; + +export function getConfig(): Linter.Config { + return { + plugins: { + "@typescript-eslint": { + rules: rules as any, + }, + }, + languageOptions: { + parser, + parserOptions: generateParserOptions({ parser: ts }), + globals: { + ...globals.browser, + ...globals.es2021, + }, + }, + rules: { + "@typescript-eslint/no-unsafe-argument": "error", + "@typescript-eslint/no-unsafe-assignment": "error", + "@typescript-eslint/no-unsafe-call": "error", + "@typescript-eslint/no-unsafe-member-access": "error", + "@typescript-eslint/no-unsafe-return": "error", + }, + }; +} diff --git a/tests/fixtures/parser/ast/jsdoc-input.svelte b/tests/fixtures/parser/ast/jsdoc-input.svelte new file mode 100644 index 00000000..438c6e87 --- /dev/null +++ b/tests/fixtures/parser/ast/jsdoc-input.svelte @@ -0,0 +1,6 @@ + diff --git a/tests/fixtures/parser/ast/jsdoc-no-unused-vars-result.json b/tests/fixtures/parser/ast/jsdoc-no-unused-vars-result.json new file mode 100644 index 00000000..74762fa5 --- /dev/null +++ b/tests/fixtures/parser/ast/jsdoc-no-unused-vars-result.json @@ -0,0 +1,8 @@ +[ + { + "ruleId": "no-unused-vars", + "code": "test", + "line": 3, + "column": 12 + } +] \ No newline at end of file diff --git a/tests/fixtures/parser/ast/jsdoc-output.json b/tests/fixtures/parser/ast/jsdoc-output.json new file mode 100644 index 00000000..a89a9c75 --- /dev/null +++ b/tests/fixtures/parser/ast/jsdoc-output.json @@ -0,0 +1,606 @@ +{ + "type": "Program", + "body": [ + { + "type": "SvelteScriptElement", + "name": { + "type": "SvelteName", + "name": "script", + "range": [ + 1, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 7 + } + } + }, + "startTag": { + "type": "SvelteStartTag", + "attributes": [], + "selfClosing": false, + "range": [ + 0, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + "body": [ + { + "type": "FunctionDeclaration", + "async": false, + "body": { + "type": "BlockStatement", + "body": [ + { + "type": "ReturnStatement", + "argument": { + "type": "BinaryExpression", + "left": { + "type": "Identifier", + "name": "x", + "range": [ + 68, + 69 + ], + "loc": { + "start": { + "line": 4, + "column": 11 + }, + "end": { + "line": 4, + "column": 12 + } + } + }, + "operator": "+", + "right": { + "type": "Literal", + "raw": "1", + "value": 1, + "range": [ + 72, + 73 + ], + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 16 + } + } + }, + "range": [ + 68, + 73 + ], + "loc": { + "start": { + "line": 4, + "column": 11 + }, + "end": { + "line": 4, + "column": 16 + } + } + }, + "range": [ + 61, + 74 + ], + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 17 + } + } + } + ], + "range": [ + 55, + 78 + ], + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 5, + "column": 3 + } + } + }, + "expression": false, + "generator": false, + "id": { + "type": "Identifier", + "name": "test", + "range": [ + 47, + 51 + ], + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 15 + } + } + }, + "params": [ + { + "type": "Identifier", + "name": "x", + "range": [ + 52, + 53 + ], + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 17 + } + } + } + ], + "range": [ + 38, + 78 + ], + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 5, + "column": 3 + } + } + } + ], + "endTag": { + "type": "SvelteEndTag", + "range": [ + 79, + 88 + ], + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 9 + } + } + }, + "range": [ + 0, + 88 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 9 + } + } + } + ], + "sourceType": "module", + "comments": [ + { + "type": "Block", + "value": "* @param {number} x ", + "range": [ + 11, + 35 + ], + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 26 + } + } + } + ], + "tokens": [ + { + "type": "Punctuator", + "value": "<", + "range": [ + 0, + 1 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "script", + "range": [ + 1, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 7 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 7, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + { + "type": "Keyword", + "value": "function", + "range": [ + 38, + 46 + ], + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 10 + } + } + }, + { + "type": "Identifier", + "value": "test", + "range": [ + 47, + 51 + ], + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 15 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 51, + 52 + ], + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 16 + } + } + }, + { + "type": "Identifier", + "value": "x", + "range": [ + 52, + 53 + ], + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 17 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 53, + 54 + ], + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 18 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 55, + 56 + ], + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 20 + } + } + }, + { + "type": "Keyword", + "value": "return", + "range": [ + 61, + 67 + ], + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 10 + } + } + }, + { + "type": "Identifier", + "value": "x", + "range": [ + 68, + 69 + ], + "loc": { + "start": { + "line": 4, + "column": 11 + }, + "end": { + "line": 4, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": "+", + "range": [ + 70, + 71 + ], + "loc": { + "start": { + "line": 4, + "column": 13 + }, + "end": { + "line": 4, + "column": 14 + } + } + }, + { + "type": "Numeric", + "value": "1", + "range": [ + 72, + 73 + ], + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 16 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 73, + 74 + ], + "loc": { + "start": { + "line": 4, + "column": 16 + }, + "end": { + "line": 4, + "column": 17 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 77, + 78 + ], + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 3 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 79, + 80 + ], + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + } + }, + { + "type": "Punctuator", + "value": "/", + "range": [ + 80, + 81 + ], + "loc": { + "start": { + "line": 6, + "column": 1 + }, + "end": { + "line": 6, + "column": 2 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "script", + "range": [ + 81, + 87 + ], + "loc": { + "start": { + "line": 6, + "column": 2 + }, + "end": { + "line": 6, + "column": 8 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 87, + 88 + ], + "loc": { + "start": { + "line": 6, + "column": 8 + }, + "end": { + "line": 6, + "column": 9 + } + } + } + ], + "range": [ + 0, + 89 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 0 + } + } +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/jsdoc-requirements.json b/tests/fixtures/parser/ast/jsdoc-requirements.json new file mode 100644 index 00000000..b0d8202a --- /dev/null +++ b/tests/fixtures/parser/ast/jsdoc-requirements.json @@ -0,0 +1,5 @@ +{ + "scope": { + "@typescript-eslint/parser": ">=6.5.0" + } +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/jsdoc-scope-output.json b/tests/fixtures/parser/ast/jsdoc-scope-output.json new file mode 100644 index 00000000..5d04531b --- /dev/null +++ b/tests/fixtures/parser/ast/jsdoc-scope-output.json @@ -0,0 +1,524 @@ +{ + "type": "global", + "variables": [ + { + "name": "$$slots", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "$$props", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "$$restProps", + "identifiers": [], + "defs": [], + "references": [] + } + ], + "references": [], + "childScopes": [ + { + "type": "module", + "variables": [ + { + "name": "test", + "identifiers": [ + { + "type": "Identifier", + "name": "test", + "range": [ + 47, + 51 + ], + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 15 + } + } + } + ], + "defs": [ + { + "type": "FunctionName", + "name": { + "type": "Identifier", + "name": "test", + "range": [ + 47, + 51 + ], + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 15 + } + } + }, + "node": { + "type": "FunctionDeclaration", + "async": false, + "body": { + "type": "BlockStatement", + "body": [ + { + "type": "ReturnStatement", + "argument": { + "type": "BinaryExpression", + "left": { + "type": "Identifier", + "name": "x", + "range": [ + 68, + 69 + ], + "loc": { + "start": { + "line": 4, + "column": 11 + }, + "end": { + "line": 4, + "column": 12 + } + } + }, + "operator": "+", + "right": { + "type": "Literal", + "raw": "1", + "value": 1, + "range": [ + 72, + 73 + ], + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 16 + } + } + }, + "range": [ + 68, + 73 + ], + "loc": { + "start": { + "line": 4, + "column": 11 + }, + "end": { + "line": 4, + "column": 16 + } + } + }, + "range": [ + 61, + 74 + ], + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 17 + } + } + } + ], + "range": [ + 55, + 78 + ], + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 5, + "column": 3 + } + } + }, + "expression": false, + "generator": false, + "id": { + "type": "Identifier", + "name": "test", + "range": [ + 47, + 51 + ], + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 15 + } + } + }, + "params": [ + { + "type": "Identifier", + "name": "x", + "range": [ + 52, + 53 + ], + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 17 + } + } + } + ], + "range": [ + 38, + 78 + ], + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 5, + "column": 3 + } + } + } + } + ], + "references": [] + } + ], + "references": [], + "childScopes": [ + { + "type": "function", + "variables": [ + { + "name": "arguments", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "x", + "identifiers": [ + { + "type": "Identifier", + "name": "x", + "range": [ + 52, + 53 + ], + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 17 + } + } + } + ], + "defs": [ + { + "type": "Parameter", + "name": { + "type": "Identifier", + "name": "x", + "range": [ + 52, + 53 + ], + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 17 + } + } + }, + "node": { + "type": "FunctionDeclaration", + "async": false, + "body": { + "type": "BlockStatement", + "body": [ + { + "type": "ReturnStatement", + "argument": { + "type": "BinaryExpression", + "left": { + "type": "Identifier", + "name": "x", + "range": [ + 68, + 69 + ], + "loc": { + "start": { + "line": 4, + "column": 11 + }, + "end": { + "line": 4, + "column": 12 + } + } + }, + "operator": "+", + "right": { + "type": "Literal", + "raw": "1", + "value": 1, + "range": [ + 72, + 73 + ], + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 16 + } + } + }, + "range": [ + 68, + 73 + ], + "loc": { + "start": { + "line": 4, + "column": 11 + }, + "end": { + "line": 4, + "column": 16 + } + } + }, + "range": [ + 61, + 74 + ], + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 17 + } + } + } + ], + "range": [ + 55, + 78 + ], + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 5, + "column": 3 + } + } + }, + "expression": false, + "generator": false, + "id": { + "type": "Identifier", + "name": "test", + "range": [ + 47, + 51 + ], + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 15 + } + } + }, + "params": [ + { + "type": "Identifier", + "name": "x", + "range": [ + 52, + 53 + ], + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 17 + } + } + } + ], + "range": [ + 38, + 78 + ], + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 5, + "column": 3 + } + } + } + } + ], + "references": [ + { + "identifier": { + "type": "Identifier", + "name": "x", + "range": [ + 68, + 69 + ], + "loc": { + "start": { + "line": 4, + "column": 11 + }, + "end": { + "line": 4, + "column": 12 + } + } + }, + "from": "function", + "init": null, + "resolved": { + "type": "Identifier", + "name": "x", + "range": [ + 52, + 53 + ], + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 17 + } + } + } + } + ] + } + ], + "references": [ + { + "identifier": { + "type": "Identifier", + "name": "x", + "range": [ + 68, + 69 + ], + "loc": { + "start": { + "line": 4, + "column": 11 + }, + "end": { + "line": 4, + "column": 12 + } + } + }, + "from": "function", + "init": null, + "resolved": { + "type": "Identifier", + "name": "x", + "range": [ + 52, + 53 + ], + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 17 + } + } + } + } + ], + "childScopes": [], + "through": [] + } + ], + "through": [] + } + ], + "through": [] +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/jsdoc-type-output.svelte b/tests/fixtures/parser/ast/jsdoc-type-output.svelte new file mode 100644 index 00000000..163827ba --- /dev/null +++ b/tests/fixtures/parser/ast/jsdoc-type-output.svelte @@ -0,0 +1,6 @@ + diff --git a/tests/fixtures/parser/ast/svelte5/ts-event02-type-output.svelte b/tests/fixtures/parser/ast/svelte5/ts-event02-type-output.svelte index 24ba5b4b..24b90511 100644 --- a/tests/fixtures/parser/ast/svelte5/ts-event02-type-output.svelte +++ b/tests/fixtures/parser/ast/svelte5/ts-event02-type-output.svelte @@ -1,5 +1,5 @@ - + diff --git a/tests/fixtures/parser/ast/ts-event02-type-output.svelte b/tests/fixtures/parser/ast/ts-event02-type-output.svelte index 2c2c348d..327c2f71 100644 --- a/tests/fixtures/parser/ast/ts-event02-type-output.svelte +++ b/tests/fixtures/parser/ast/ts-event02-type-output.svelte @@ -1,5 +1,5 @@ - - + + diff --git a/tests/fixtures/tsconfig.test.json b/tests/fixtures/tsconfig.test.json index e5167dc3..48dbe1cc 100644 --- a/tests/fixtures/tsconfig.test.json +++ b/tests/fixtures/tsconfig.test.json @@ -3,5 +3,7 @@ "strict": true, "module": "Node16", "moduleResolution": "Node16", + "allowJs": true, + "checkJs": true } -} \ No newline at end of file +} diff --git a/tools/update-fixtures.ts b/tools/update-fixtures.ts index 07532fff..5942fd80 100644 --- a/tools/update-fixtures.ts +++ b/tools/update-fixtures.ts @@ -69,8 +69,6 @@ for (const { } // if (!inputFileName.includes("test")) continue; try { - // eslint-disable-next-line no-console -- ignore - console.log(inputFileName); const result = parse(input, inputFileName, config); const astJson = astToJson(result.ast); fs.writeFileSync(outputFileName, astJson, "utf8"); @@ -279,9 +277,7 @@ function buildTypes( lineTypes.push(`${node.name}: ${typeText}`); } else { lineTypes.push( - `${input - .slice(...node.range!) - .replace(/\s*\n\s*/gu, " ")}: ${typeText}`, + `${input.slice(...node.range!).replace(/\s*\n\s*/gu, " ")}: ${typeText}`, ); } }