Skip to content

Commit 00cc8cf

Browse files
committed
feat!: change the parser to an ESM-only package
1 parent 3a0b5d1 commit 00cc8cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+325
-310
lines changed

benchmark/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/* eslint-disable no-console -- ignore */
33
import * as Benchmark from "benchmark";
44
import fs from "fs";
5-
import { parseForESLint } from "../src/index";
6-
import { parseForESLint as parseOld } from "../node_modules/svelte-eslint-parser";
5+
import { parseForESLint } from "../src/index.js";
6+
import { parseForESLint as parseOld } from "../node_modules/svelte-eslint-parser/lib/index.js";
77

88
const contents = `${fs.readFileSync(
99
require.resolve("../explorer-v2/src/lib/RulesSettings.svelte"),

explorer-v2/build-system/shim/module.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
// eslint-disable-next-line n/no-extraneous-import -- shim
2+
import * as estree from 'espree';
13
export function createRequire() {
2-
return null;
4+
function req(mod) {
5+
if (mod === 'espree') {
6+
return estree;
7+
}
8+
throw new Error(`Cannot find module '${mod}'`);
9+
}
10+
11+
req.cache = {};
12+
return req;
313
}
414
export default {
515
createRequire

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"engines": {
1515
"node": "^18.20.4 || ^20.18.0 || >=22.10.0"
1616
},
17-
"type": "commonjs",
17+
"type": "module",
1818
"main": "lib/index.js",
1919
"files": [
2020
"lib"
@@ -41,7 +41,7 @@
4141
"preversion": "pnpm run lint && pnpm run test",
4242
"release": "changeset publish",
4343
"test": "pnpm run mocha \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
44-
"ts": "node -r esbuild-register",
44+
"ts": "node --import tsx/esm",
4545
"update-fixtures": "git add package.json && pnpm i -D svelte@4 && git checkout package.json && pnpm run run-update-fixtures && pnpm i && pnpm run run-update-fixtures",
4646
"run-update-fixtures": "pnpm run ts ./tools/update-fixtures.ts",
4747
"version:ci": "env-cmd -e version-ci pnpm run build:meta && changeset version"
@@ -82,7 +82,6 @@
8282
"chai": "^4.5.0",
8383
"env-cmd": "^10.1.0",
8484
"esbuild": "^0.24.0",
85-
"esbuild-register": "^3.6.0",
8685
"eslint": "~9.16.0",
8786
"eslint-config-prettier": "^9.1.0",
8887
"eslint-plugin-eslint-comments": "^3.2.0",
@@ -109,6 +108,7 @@
109108
"semver": "^7.6.3",
110109
"svelte": "^5.2.11",
111110
"svelte2tsx": "^0.7.28",
111+
"tsx": "^4.19.2",
112112
"typescript": "~5.7.2",
113113
"typescript-eslint": "^8.16.0",
114114
"typescript-eslint-parser-for-extra-files": "^0.7.0"

src/ast/base.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Locations } from "./common";
1+
import type { Locations } from "./common.js";
22

33
// internals
44
export interface BaseNode extends Locations {

src/ast/common.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { BaseNode } from "./base";
1+
import type { BaseNode } from "./base.js";
22

33
export interface Position {
44
/** >= 1 */

src/ast/html.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type ESTree from "estree";
22
import type { TSESTree } from "@typescript-eslint/types";
3-
import type { BaseNode } from "./base";
4-
import type { Token, Comment } from "./common";
3+
import type { BaseNode } from "./base.js";
4+
import type { Token, Comment } from "./common.js";
55

66
export type SvelteHTMLNode =
77
| SvelteProgram

src/ast/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { SvelteHTMLNode } from "./html";
2-
import type { SvelteScriptNode } from "./script";
1+
import type { SvelteHTMLNode } from "./html.js";
2+
import type { SvelteScriptNode } from "./script.js";
33

4-
export * from "./common";
5-
export * from "./html";
6-
export * from "./script";
4+
export * from "./common.js";
5+
export * from "./html.js";
6+
export * from "./script.js";
77

88
export type SvelteNode = SvelteHTMLNode | SvelteScriptNode;

src/ast/script.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type ESTree from "estree";
2-
import type { BaseNode } from "./base";
2+
import type { BaseNode } from "./base.js";
33

44
export type SvelteScriptNode = SvelteReactiveStatement;
55

src/context/fix-locations.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type * as ESTree from "estree";
2-
import type { Comment, Locations, Token } from "../ast";
3-
import type { Context } from ".";
4-
import { traverseNodes } from "../traverse";
2+
import type { Comment, Locations, Token } from "../ast/index.js";
3+
import type { Context } from "./index.js";
4+
import { traverseNodes } from "../traverse.js";
55

66
/** Fix locations */
77
export function fixLocations(

src/context/index.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ import type {
99
SvelteSnippetBlock,
1010
SvelteStyleElement,
1111
Token,
12-
} from "../ast";
12+
} from "../ast/index.js";
1313
import type ESTree from "estree";
14-
import type * as SvAST from "../parser/svelte-ast-types";
15-
import type * as Compiler from "../parser/svelte-ast-types-for-v5";
16-
import { ScriptLetContext } from "./script-let";
17-
import { LetDirectiveCollections } from "./let-directive-collection";
18-
import { parseAttributes } from "../parser/html";
19-
import { sortedLastIndex } from "../utils";
14+
import type * as SvAST from "../parser/svelte-ast-types.js";
15+
import type * as Compiler from "../parser/svelte-ast-types-for-v5.js";
16+
import { ScriptLetContext } from "./script-let.js";
17+
import { LetDirectiveCollections } from "./let-directive-collection.js";
18+
import { parseAttributes } from "../parser/html.js";
19+
import { sortedLastIndex } from "../utils/index.js";
2020
import {
2121
isTypeScript,
2222
type NormalizedParserOptions,
23-
} from "../parser/parser-options";
23+
} from "../parser/parser-options.js";
2424

2525
export class ScriptsSourceCode {
2626
private raw: string;

src/context/let-directive-collection.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { SvelteLetDirective, SvelteName } from "../ast";
1+
import type { SvelteLetDirective, SvelteName } from "../ast/index.js";
22
import type * as ESTree from "estree";
3-
import type { ScriptLetBlockParam, ScriptLetCallback } from "./script-let";
3+
import type { ScriptLetBlockParam, ScriptLetCallback } from "./script-let.js";
44

55
/** A class that collects pattern nodes for Let directives. */
66
export class LetDirectiveCollection {

src/context/script-let.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ScopeManager, Scope } from "eslint-scope";
22
import type * as ESTree from "estree";
33
import type { TSESTree } from "@typescript-eslint/types";
44
import type { Scope as TSScope } from "@typescript-eslint/scope-manager";
5-
import type { Context, ScriptsSourceCode } from ".";
5+
import type { Context, ScriptsSourceCode } from "./index.js";
66
import type {
77
Comment,
88
SvelteEachBlock,
@@ -11,19 +11,19 @@ import type {
1111
SvelteNode,
1212
SvelteSnippetBlock,
1313
Token,
14-
} from "../ast";
15-
import type { ESLintExtendedProgram } from "../parser";
16-
import { getWithLoc } from "../parser/converts/common";
14+
} from "../ast/index.js";
15+
import type { ESLintExtendedProgram } from "../parser/index.js";
16+
import { getWithLoc } from "../parser/converts/common.js";
1717
import {
1818
getScopeFromNode,
1919
removeAllScopeAndVariableAndReference,
2020
removeIdentifierVariable,
2121
removeReference,
2222
removeScope,
23-
} from "../scope";
24-
import { getKeys, traverseNodes, getNodes } from "../traverse";
25-
import { UniqueIdGenerator } from "./unique";
26-
import { fixLocations } from "./fix-locations";
23+
} from "../scope/index.js";
24+
import { getKeys, traverseNodes, getNodes } from "../traverse.js";
25+
import { UniqueIdGenerator } from "./unique.js";
26+
import { fixLocations } from "./fix-locations.js";
2727

2828
type TSAsExpression = {
2929
type: "TSAsExpression";

src/errors.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Context } from "./context";
1+
import type { Context } from "./context/index.js";
22

33
/**
44
* Svelte parse errors.

src/index.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import * as AST from "./ast";
2-
import { traverseNodes } from "./traverse";
3-
import { KEYS } from "./visitor-keys";
4-
import { ParseError } from "./errors";
1+
import * as AST from "./ast/index.js";
2+
import { traverseNodes } from "./traverse.js";
3+
import { KEYS } from "./visitor-keys.js";
4+
import { ParseError } from "./errors.js";
55
export {
66
parseForESLint,
77
type StyleContext,
88
type StyleContextNoStyleElement,
99
type StyleContextParseError,
1010
type StyleContextSuccess,
1111
type StyleContextUnknownLang,
12-
} from "./parser";
13-
export * as meta from "./meta";
14-
export { name } from "./meta";
15-
export type { SvelteConfig } from "./svelte-config";
12+
} from "./parser/index.js";
13+
export * as meta from "./meta.js";
14+
export { name } from "./meta.js";
15+
export type { SvelteConfig } from "./svelte-config/index.js";
1616

1717
export { AST, ParseError };
1818

src/parser/analyze-scope.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import type ESTree from "estree";
22
import type { Scope, ScopeManager } from "eslint-scope";
33
import { Variable, Reference, analyze } from "eslint-scope";
4-
import { getFallbackKeys } from "../traverse";
4+
import { getFallbackKeys } from "../traverse.js";
55
import type {
66
SvelteReactiveStatement,
77
SvelteScriptElement,
88
SvelteSnippetBlock,
9-
} from "../ast";
10-
import { addReference, addVariable, getScopeFromNode } from "../scope";
11-
import { addElementToSortedArray } from "../utils";
12-
import type { NormalizedParserOptions } from "./parser-options";
13-
import type { SvelteParseContext } from "./svelte-parse-context";
9+
} from "../ast/index.js";
10+
import { addReference, addVariable, getScopeFromNode } from "../scope/index.js";
11+
import { addElementToSortedArray } from "../utils/index.js";
12+
import type { NormalizedParserOptions } from "./parser-options.js";
13+
import type { SvelteParseContext } from "./svelte-parse-context.js";
1414
/**
1515
* Analyze scope
1616
*/

src/parser/compat.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** Compatibility for Svelte v4 <-> v5 */
22
import type ESTree from "estree";
3-
import type * as SvAST from "./svelte-ast-types";
4-
import type * as Compiler from "./svelte-ast-types-for-v5";
3+
import type * as SvAST from "./svelte-ast-types.js";
4+
import type * as Compiler from "./svelte-ast-types-for-v5.js";
55

66
export type Child =
77
| Compiler.Text

src/parser/converts/attr.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ import type {
2020
SvelteStyleElement,
2121
SvelteElseBlock,
2222
SvelteAwaitBlock,
23-
} from "../../ast";
23+
} from "../../ast/index.js";
2424
import type ESTree from "estree";
25-
import type { Context } from "../../context";
26-
import type * as SvAST from "../svelte-ast-types";
27-
import type * as Compiler from "../svelte-ast-types-for-v5";
28-
import { getWithLoc, indexOf } from "./common";
29-
import { convertMustacheTag } from "./mustache";
30-
import { convertTextToLiteral } from "./text";
31-
import { ParseError } from "../../errors";
32-
import type { ScriptLetCallback } from "../../context/script-let";
33-
import { svelteVersion } from "../svelte-version";
34-
import { hasTypeInfo } from "../../utils";
35-
import { getModifiers } from "../compat";
25+
import type { Context } from "../../context/index.js";
26+
import type * as SvAST from "../svelte-ast-types.js";
27+
import type * as Compiler from "../svelte-ast-types-for-v5.js";
28+
import { getWithLoc, indexOf } from "./common.js";
29+
import { convertMustacheTag } from "./mustache.js";
30+
import { convertTextToLiteral } from "./text.js";
31+
import { ParseError } from "../../errors.js";
32+
import type { ScriptLetCallback } from "../../context/script-let.js";
33+
import { svelteVersion } from "../svelte-version.js";
34+
import { hasTypeInfo } from "../../utils/index.js";
35+
import { getModifiers } from "../compat.js";
3636

3737
type LetDirective = Omit<Compiler.LetDirective, "expression"> & {
3838
expression: SvAST.LetDirective["expression"];

src/parser/converts/block.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type * as SvAST from "../svelte-ast-types";
2-
import type * as Compiler from "../svelte-ast-types-for-v5";
1+
import type * as SvAST from "../svelte-ast-types.js";
2+
import type * as Compiler from "../svelte-ast-types-for-v5.js";
33
import type {
44
SvelteAwaitBlock,
55
SvelteAwaitBlockAwaitCatch,
@@ -16,10 +16,10 @@ import type {
1616
SvelteIfBlockElseIf,
1717
SvelteKeyBlock,
1818
SvelteSnippetBlock,
19-
} from "../../ast";
20-
import type { Context } from "../../context";
21-
import { convertChildren } from "./element";
22-
import { getWithLoc, indexOf, lastIndexOf } from "./common";
19+
} from "../../ast/index.js";
20+
import type { Context } from "../../context/index.js";
21+
import { convertChildren } from "./element.js";
22+
import { getWithLoc, indexOf, lastIndexOf } from "./common.js";
2323
import type * as ESTree from "estree";
2424
import {
2525
getAlternateFromIfBlock,
@@ -33,7 +33,7 @@ import {
3333
getTestFromIfBlock,
3434
getThenFromAwaitBlock,
3535
trimChildren,
36-
} from "../compat";
36+
} from "../compat.js";
3737

3838
/** Get start index of block */
3939
function startBlockIndex(

src/parser/converts/const.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { SvelteConstTag } from "../../ast";
2-
import type { Context } from "../../context";
3-
import { getDeclaratorFromConstTag } from "../compat";
4-
import type * as SvAST from "../svelte-ast-types";
5-
import type * as Compiler from "../svelte-ast-types-for-v5";
1+
import type { SvelteConstTag } from "../../ast/index.js";
2+
import type { Context } from "../../context/index.js";
3+
import { getDeclaratorFromConstTag } from "../compat.js";
4+
import type * as SvAST from "../svelte-ast-types.js";
5+
import type * as Compiler from "../svelte-ast-types-for-v5.js";
66

77
/** Convert for ConstTag */
88
export function convertConstTag(

src/parser/converts/element.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,34 @@ import type {
2626
SvelteSpecialElement,
2727
SvelteStyleElement,
2828
SvelteText,
29-
} from "../../ast";
29+
} from "../../ast/index.js";
3030
import type ESTree from "estree";
31-
import type { Context } from "../../context";
32-
import type * as SvAST from "../svelte-ast-types";
33-
import type * as Compiler from "../svelte-ast-types-for-v5";
31+
import type { Context } from "../../context/index.js";
32+
import type * as SvAST from "../svelte-ast-types.js";
33+
import type * as Compiler from "../svelte-ast-types-for-v5.js";
3434

3535
import {
3636
convertAwaitBlock,
3737
convertEachBlock,
3838
convertIfBlock,
3939
convertKeyBlock,
4040
convertSnippetBlock,
41-
} from "./block";
42-
import { getWithLoc, indexOf } from "./common";
41+
} from "./block.js";
42+
import { getWithLoc, indexOf } from "./common.js";
4343
import {
4444
convertMustacheTag,
4545
convertDebugTag,
4646
convertRawMustacheTag,
47-
} from "./mustache";
48-
import { convertText } from "./text";
49-
import { convertAttributes } from "./attr";
50-
import { convertConstTag } from "./const";
51-
import { sortNodes } from "../sort";
52-
import type { ScriptLetBlockParam } from "../../context/script-let";
53-
import { ParseError } from "../..";
54-
import { convertRenderTag } from "./render";
55-
import type { Child } from "../compat";
56-
import { getChildren, getFragment } from "../compat";
47+
} from "./mustache.js";
48+
import { convertText } from "./text.js";
49+
import { convertAttributes } from "./attr.js";
50+
import { convertConstTag } from "./const.js";
51+
import { sortNodes } from "../sort.js";
52+
import type { ScriptLetBlockParam } from "../../context/script-let.js";
53+
import { ParseError } from "../../index.js";
54+
import { convertRenderTag } from "./render.js";
55+
import type { Child } from "../compat.js";
56+
import { getChildren, getFragment } from "../compat.js";
5757

5858
/** Convert for Fragment or Element or ... */
5959
export function* convertChildren(

src/parser/converts/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { convertSvelteRoot } from "./root";
1+
export { convertSvelteRoot } from "./root.js";

0 commit comments

Comments
 (0)