Skip to content

Commit df83e3e

Browse files
authored
feat: support for typescript-eslint-parser-for-extra-files (#241)
* feat: support typescript-eslint-parser-for-extra-files * Create strange-cheetahs-help.md * refactor: getInnermostScope
1 parent 1637af4 commit df83e3e

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

.changeset/strange-cheetahs-help.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-eslint-parser": minor
3+
---
4+
5+
feat: support for `typescript-eslint-parser-for-extra-files`

src/context/index.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ import {
2020
} from "../parser/parser-object";
2121
import { sortedLastIndex } from "../utils";
2222

23+
const TS_PARSER_NAMES = [
24+
"@typescript-eslint/parser",
25+
"typescript-eslint-parser-for-extra-files",
26+
];
27+
2328
export class ScriptsSourceCode {
2429
private raw: string;
2530

@@ -241,18 +246,18 @@ export class Context {
241246
isTSESLintParserObject(parserValue));
242247
}
243248
const parserName = parserValue;
244-
if (parserName === "@typescript-eslint/parser") {
249+
if (TS_PARSER_NAMES.includes(parserName)) {
245250
return (this.state.isTypeScript = true);
246251
}
247-
if (parserName.includes("@typescript-eslint/parser")) {
252+
if (TS_PARSER_NAMES.some((nm) => parserName.includes(nm))) {
248253
let targetPath = parserName;
249254
while (targetPath) {
250255
const pkgPath = path.join(targetPath, "package.json");
251256
if (fs.existsSync(pkgPath)) {
252257
try {
253-
return (this.state.isTypeScript =
254-
JSON.parse(fs.readFileSync(pkgPath, "utf-8"))?.name ===
255-
"@typescript-eslint/parser");
258+
return (this.state.isTypeScript = TS_PARSER_NAMES.includes(
259+
JSON.parse(fs.readFileSync(pkgPath, "utf-8"))?.name
260+
));
256261
} catch {
257262
return (this.state.isTypeScript = false);
258263
}

src/parser/typescript/restore.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,9 @@ export class RestoreContext {
8686
(f) => f.start < end && end <= f.end
8787
);
8888
if (endFragment) {
89+
end = endFragment.start;
8990
if (startFragment === endFragment) {
90-
end = start;
91-
} else {
92-
end = endFragment.start;
91+
start = startFragment.start;
9392
}
9493
}
9594

src/scope/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,17 @@ export function getInnermostScope(
115115
node: ESTree.Node
116116
): Scope {
117117
const location = node.range![0];
118+
const isInRange =
119+
node.range![0] === node.range![1]
120+
? (range: [number, number]) =>
121+
range[0] <= location && location <= range[1]
122+
: (range: [number, number]) =>
123+
range[0] <= location && location < range[1];
118124

119125
for (const childScope of initialScope.childScopes) {
120126
const range = childScope.block.range!;
121127

122-
if (range[0] <= location && location < range[1]) {
128+
if (isInRange(range)) {
123129
return getInnermostScope(childScope, node);
124130
}
125131
}

0 commit comments

Comments
 (0)