From 8f41cd0708d91b8f1eac979b82465bbf3e114fdc Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Tue, 1 Nov 2022 20:18:28 +0900 Subject: [PATCH 1/3] feat: support typescript-eslint-parser-for-extra-files --- src/context/index.ts | 15 ++++++++++----- src/parser/typescript/restore.ts | 5 ++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/context/index.ts b/src/context/index.ts index 44e86d29..adfe46b9 100644 --- a/src/context/index.ts +++ b/src/context/index.ts @@ -20,6 +20,11 @@ import { } from "../parser/parser-object"; import { sortedLastIndex } from "../utils"; +const TS_PARSER_NAMES = [ + "@typescript-eslint/parser", + "typescript-eslint-parser-for-extra-files", +]; + export class ScriptsSourceCode { private raw: string; @@ -241,18 +246,18 @@ export class Context { isTSESLintParserObject(parserValue)); } const parserName = parserValue; - if (parserName === "@typescript-eslint/parser") { + if (TS_PARSER_NAMES.includes(parserName)) { return (this.state.isTypeScript = true); } - if (parserName.includes("@typescript-eslint/parser")) { + 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 (this.state.isTypeScript = - JSON.parse(fs.readFileSync(pkgPath, "utf-8"))?.name === - "@typescript-eslint/parser"); + return (this.state.isTypeScript = TS_PARSER_NAMES.includes( + JSON.parse(fs.readFileSync(pkgPath, "utf-8"))?.name + )); } catch { return (this.state.isTypeScript = false); } diff --git a/src/parser/typescript/restore.ts b/src/parser/typescript/restore.ts index 8e23c368..e66957e3 100644 --- a/src/parser/typescript/restore.ts +++ b/src/parser/typescript/restore.ts @@ -86,10 +86,9 @@ export class RestoreContext { (f) => f.start < end && end <= f.end ); if (endFragment) { + end = endFragment.start; if (startFragment === endFragment) { - end = start; - } else { - end = endFragment.start; + start = startFragment.start; } } From 53f6efe9e6431a9af998818a15c2940b48fb1054 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Tue, 1 Nov 2022 20:19:30 +0900 Subject: [PATCH 2/3] Create strange-cheetahs-help.md --- .changeset/strange-cheetahs-help.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/strange-cheetahs-help.md diff --git a/.changeset/strange-cheetahs-help.md b/.changeset/strange-cheetahs-help.md new file mode 100644 index 00000000..47eb8893 --- /dev/null +++ b/.changeset/strange-cheetahs-help.md @@ -0,0 +1,5 @@ +--- +"svelte-eslint-parser": minor +--- + +feat: support for `typescript-eslint-parser-for-extra-files` From ce50324e24eaf4713234a6173d59c01d6e17f147 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Tue, 1 Nov 2022 20:28:05 +0900 Subject: [PATCH 3/3] refactor: getInnermostScope --- src/scope/index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/scope/index.ts b/src/scope/index.ts index f2537dec..69ae2434 100644 --- a/src/scope/index.ts +++ b/src/scope/index.ts @@ -115,11 +115,17 @@ export function getInnermostScope( node: ESTree.Node ): Scope { const location = node.range![0]; + const isInRange = + node.range![0] === node.range![1] + ? (range: [number, number]) => + range[0] <= location && location <= range[1] + : (range: [number, number]) => + range[0] <= location && location < range[1]; for (const childScope of initialScope.childScopes) { const range = childScope.block.range!; - if (range[0] <= location && location < range[1]) { + if (isInRange(range)) { return getInnermostScope(childScope, node); } }