Skip to content

feat: treat jsdoc with @typescript-eslint/parser #607

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
22 changes: 16 additions & 6 deletions src/context/index.ts
Original file line number Diff line number Diff line change
@@ -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<string, string | undefined>;
@@ -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 {
47 changes: 30 additions & 17 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -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(
57 changes: 45 additions & 12 deletions src/parser/parser-options.ts
Original file line number Diff line number Diff line change
@@ -63,35 +63,50 @@ 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;
while (targetPath) {
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;
Original file line number Diff line number Diff line change
@@ -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,
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
/** @param {number} x */
function test(x) {
return x + 1;
}
</script>
9 changes: 9 additions & 0 deletions tests/fixtures/integrations/type-info-tests/jsdoc-output.json
Original file line number Diff line number Diff line change
@@ -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`."
}
]
31 changes: 31 additions & 0 deletions tests/fixtures/integrations/type-info-tests/jsdoc-setup.ts
Original file line number Diff line number Diff line change
@@ -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",
},
};
}
6 changes: 6 additions & 0 deletions tests/fixtures/parser/ast/jsdoc-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
/** @param {number} x */
function test(x) {
return x + 1;
}
</script>
8 changes: 8 additions & 0 deletions tests/fixtures/parser/ast/jsdoc-no-unused-vars-result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"ruleId": "no-unused-vars",
"code": "test",
"line": 3,
"column": 12
}
]
Loading

Unchanged files with check annotations Beta

(key as any).parent = sAttr;
ctx.scriptLet.addObjectShorthandProperty(attribute.key, sAttr, (es) => {
if (
// FIXME: Older parsers may use the same node. In that case, do not replace.

Check warning on line 173 in src/parser/converts/attr.ts

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: Older parsers may use the same...'
// We will drop support for ESLint v7 in the next major version and remove this branch.
es.key !== es.value
) {
import type {} from "svelte"; // FIXME: Workaround to get type information for "svelte/compiler"

Check warning on line 1 in src/parser/template.ts

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: Workaround to get type...'
import { parse } from "svelte/compiler";
import type * as Compiler from "./svelte-ast-types-for-v5.js";
import type * as SvAST from "./svelte-ast-types.js";