Skip to content

Commit d97fcf3

Browse files
authored
Merge branch 'main' into improve-types
2 parents 03da3af + 885012d commit d97fcf3

30 files changed

+5530
-1045
lines changed

Diff for: CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# svelte-eslint-parser
22

3+
## 0.24.0
4+
5+
### Minor Changes
6+
7+
- [#292](https://github.com/ota-meshi/svelte-eslint-parser/pull/292) [`ec061f5`](https://github.com/ota-meshi/svelte-eslint-parser/commit/ec061f574d73aa25c13a631bb3be6fa2f861e8e8) Thanks [@ota-meshi](https://github.com/ota-meshi)! - BREAKING: fix resolve to module scope for top level statements
8+
9+
This change corrects the result of `context.getScope()`, but it is a breaking change.
10+
11+
- [#294](https://github.com/ota-meshi/svelte-eslint-parser/pull/294) [`14d6e95`](https://github.com/ota-meshi/svelte-eslint-parser/commit/14d6e95773ea638855c25927c11f7a2df1632801) Thanks [@ota-meshi](https://github.com/ota-meshi)! - feat: add peerDependenciesMeta to package.json
12+
13+
- [#295](https://github.com/ota-meshi/svelte-eslint-parser/pull/295) [`924cd3e`](https://github.com/ota-meshi/svelte-eslint-parser/commit/924cd3e72db0d9d09aed1da5ec3f2e5995c9ca77) Thanks [@ota-meshi](https://github.com/ota-meshi)! - feat: export name property
14+
315
## 0.23.0
416

517
### Minor Changes

Diff for: package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-eslint-parser",
3-
"version": "0.23.0",
3+
"version": "0.24.0",
44
"description": "Svelte parser for ESLint",
55
"repository": "git+https://github.com/ota-meshi/svelte-eslint-parser.git",
66
"homepage": "https://github.com/ota-meshi/svelte-eslint-parser#readme",
@@ -45,6 +45,11 @@
4545
"peerDependencies": {
4646
"svelte": "^3.37.0"
4747
},
48+
"peerDependenciesMeta": {
49+
"svelte": {
50+
"optional": true
51+
}
52+
},
4853
"dependencies": {
4954
"eslint-scope": "^7.0.0",
5055
"eslint-visitor-keys": "^3.0.0",
@@ -63,7 +68,7 @@
6368
"@types/node": "^18.11.0",
6469
"@types/semver": "^7.3.9",
6570
"@typescript-eslint/eslint-plugin": "^5.4.0",
66-
"@typescript-eslint/parser": "~5.54.0",
71+
"@typescript-eslint/parser": "~5.55.0",
6772
"benchmark": "^2.1.4",
6873
"chai": "^4.3.4",
6974
"esbuild": "^0.17.0",

Diff for: src/context/script-let.ts

+79-35
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type ScriptLetRestoreCallbackOption = {
5959
registerNodeToScope: (node: any, scope: Scope) => void;
6060
scopeManager: ScopeManager;
6161
visitorKeys?: { [type: string]: string[] };
62+
addPostProcess: (callback: () => void) => void;
6263
};
6364

6465
/**
@@ -131,6 +132,8 @@ export class ScriptLetContext {
131132

132133
private readonly restoreCallbacks: RestoreCallback[] = [];
133134

135+
private readonly programRestoreCallbacks: ScriptLetRestoreCallback[] = [];
136+
134137
private readonly closeScopeCallbacks: (() => void)[] = [];
135138

136139
private readonly unique = new UniqueIdGenerator();
@@ -593,6 +596,10 @@ export class ScriptLetContext {
593596
this.closeScopeCallbacks.pop()!();
594597
}
595598

599+
public addProgramRestore(callback: ScriptLetRestoreCallback): void {
600+
this.programRestoreCallbacks.push(callback);
601+
}
602+
596603
private appendScript(
597604
text: string,
598605
offset: number,
@@ -650,6 +657,57 @@ export class ScriptLetContext {
650657
* Restore AST nodes
651658
*/
652659
public restore(result: ESLintExtendedProgram): void {
660+
const nodeToScope = getNodeToScope(result.scopeManager!);
661+
const postprocessList: (() => void)[] = [];
662+
663+
const callbackOption: ScriptLetRestoreCallbackOption = {
664+
getScope,
665+
getInnermostScope,
666+
registerNodeToScope,
667+
scopeManager: result.scopeManager!,
668+
visitorKeys: result.visitorKeys,
669+
addPostProcess: (cb) => postprocessList.push(cb),
670+
};
671+
672+
this.restoreNodes(result, callbackOption);
673+
this.restoreProgram(result, callbackOption);
674+
postprocessList.forEach((p) => p());
675+
676+
// Helpers
677+
/** Get scope */
678+
function getScope(node: ESTree.Node) {
679+
return getScopeFromNode(result.scopeManager!, node);
680+
}
681+
682+
/** Get innermost scope */
683+
function getInnermostScope(node: ESTree.Node) {
684+
return getInnermostScopeFromNode(result.scopeManager!, node);
685+
}
686+
687+
/** Register node to scope */
688+
function registerNodeToScope(node: any, scope: Scope): void {
689+
// If we replace the `scope.block` at this time,
690+
// the scope restore calculation will not work, so we will replace the `scope.block` later.
691+
postprocessList.push(() => {
692+
scope.block = node;
693+
});
694+
695+
const scopes = nodeToScope.get(node);
696+
if (scopes) {
697+
scopes.push(scope);
698+
} else {
699+
nodeToScope.set(node, [scope]);
700+
}
701+
}
702+
}
703+
704+
/**
705+
* Restore AST nodes
706+
*/
707+
private restoreNodes(
708+
result: ESLintExtendedProgram,
709+
callbackOption: ScriptLetRestoreCallbackOption
710+
): void {
653711
let orderedRestoreCallback = this.restoreCallbacks.shift();
654712
if (!orderedRestoreCallback) {
655713
return;
@@ -659,8 +717,6 @@ export class ScriptLetContext {
659717
const processedTokens = [];
660718
const comments = result.ast.comments;
661719
const processedComments = [];
662-
const nodeToScope = getNodeToScope(result.scopeManager!);
663-
const postprocessList: (() => void)[] = [];
664720

665721
let tok;
666722
while ((tok = tokens.shift())) {
@@ -750,13 +806,12 @@ export class ScriptLetContext {
750806
startIndex.comment,
751807
endIndex.comment - startIndex.comment
752808
);
753-
restoreCallback.callback(node, targetTokens, targetComments, {
754-
getScope,
755-
getInnermostScope,
756-
registerNodeToScope,
757-
scopeManager: result.scopeManager!,
758-
visitorKeys: result.visitorKeys,
759-
});
809+
restoreCallback.callback(
810+
node,
811+
targetTokens,
812+
targetComments,
813+
callbackOption
814+
);
760815

761816
processedTokens.push(...targetTokens);
762817
processedComments.push(...targetComments);
@@ -769,33 +824,22 @@ export class ScriptLetContext {
769824

770825
result.ast.tokens = processedTokens;
771826
result.ast.comments = processedComments;
772-
postprocessList.forEach((p) => p());
773-
774-
// Helpers
775-
/** Get scope */
776-
function getScope(node: ESTree.Node) {
777-
return getScopeFromNode(result.scopeManager!, node);
778-
}
779-
780-
/** Get innermost scope */
781-
function getInnermostScope(node: ESTree.Node) {
782-
return getInnermostScopeFromNode(result.scopeManager!, node);
783-
}
784-
785-
/** Register node to scope */
786-
function registerNodeToScope(node: any, scope: Scope): void {
787-
// If we replace the `scope.block` at this time,
788-
// the scope restore calculation will not work, so we will replace the `scope.block` later.
789-
postprocessList.push(() => {
790-
scope.block = node;
791-
});
827+
}
792828

793-
const scopes = nodeToScope.get(node);
794-
if (scopes) {
795-
scopes.push(scope);
796-
} else {
797-
nodeToScope.set(node, [scope]);
798-
}
829+
/**
830+
* Restore program node
831+
*/
832+
private restoreProgram(
833+
result: ESLintExtendedProgram,
834+
callbackOption: ScriptLetRestoreCallbackOption
835+
): void {
836+
for (const callback of this.programRestoreCallbacks) {
837+
callback(
838+
result.ast,
839+
result.ast.tokens,
840+
result.ast.comments,
841+
callbackOption
842+
);
799843
}
800844
}
801845

Diff for: src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { ParseError } from "./errors";
66

77
export { AST, ParseError };
88

9+
export const name = "svelte-eslint-parser";
10+
911
// parser
1012
export { parseForESLint };
1113
// Keys

Diff for: src/parser/converts/root.ts

+25
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {} from "./common";
99
import type { Context } from "../../context";
1010
import { convertChildren, extractElementTags } from "./element";
1111
import { convertAttributeTokens } from "./attr";
12+
import type { Scope } from "eslint-scope";
1213

1314
/**
1415
* Convert root
@@ -127,6 +128,30 @@ export function convertSvelteRoot(
127128
body.push(style);
128129
}
129130

131+
// Set the scope of the Program node.
132+
ctx.scriptLet.addProgramRestore(
133+
(
134+
node,
135+
_tokens,
136+
_comments,
137+
{ scopeManager, registerNodeToScope, addPostProcess }
138+
) => {
139+
const scopes: Scope[] = [];
140+
for (const scope of scopeManager.scopes) {
141+
if (scope.block === node) {
142+
registerNodeToScope(ast, scope);
143+
scopes.push(scope);
144+
}
145+
}
146+
addPostProcess(() => {
147+
// Reverts the node indicated by `block` to the original Program node.
148+
// This state is incorrect, but `eslint-utils`'s `referenceTracker.iterateEsmReferences()` tracks import statements
149+
// from Program nodes set to `block` in global scope. This can only be handled by the original Program node.
150+
scopeManager.globalScope.block = node;
151+
});
152+
}
153+
);
154+
130155
return ast;
131156
}
132157

0 commit comments

Comments
 (0)