From ee3f986700cf98541a0cf1eebab9863c9fbae0da Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sat, 30 Nov 2024 08:04:43 +0900 Subject: [PATCH 1/4] feat!: change the parser to an ESM-only package --- benchmark/index.ts | 4 +- explorer-v2/build-system/shim/module.js | 12 +++++- package.json | 6 +-- src/ast/base.ts | 2 +- src/ast/common.ts | 2 +- src/ast/html.ts | 4 +- src/ast/index.ts | 10 ++--- src/ast/script.ts | 2 +- src/context/fix-locations.ts | 6 +-- src/context/index.ts | 16 ++++---- src/context/let-directive-collection.ts | 4 +- src/context/script-let.ts | 16 ++++---- src/errors.ts | 2 +- src/index.ts | 16 ++++---- src/parser/analyze-scope.ts | 12 +++--- src/parser/compat.ts | 4 +- src/parser/converts/attr.ts | 24 ++++++------ src/parser/converts/block.ts | 14 +++---- src/parser/converts/const.ts | 10 ++--- src/parser/converts/element.ts | 32 +++++++-------- src/parser/converts/index.ts | 2 +- src/parser/converts/mustache.ts | 10 ++--- src/parser/converts/render.ts | 8 ++-- src/parser/converts/root.ts | 24 ++++++------ src/parser/converts/text.ts | 6 +-- src/parser/espree.ts | 36 +++++++---------- src/parser/globals.ts | 2 +- src/parser/html.ts | 4 +- src/parser/index.ts | 43 +++++++++++---------- src/parser/parser-object.ts | 2 +- src/parser/parser-options.ts | 4 +- src/parser/resolve-parser.ts | 9 +++-- src/parser/script.ts | 12 +++--- src/parser/style-context.ts | 4 +- src/parser/svelte-parse-context.ts | 10 ++--- src/parser/template.ts | 16 ++++---- src/parser/typescript/analyze/index.ts | 33 +++++++++------- src/parser/typescript/context.ts | 6 +-- src/parser/typescript/index.ts | 19 +++++---- src/parser/typescript/restore.ts | 9 +++-- src/parser/typescript/set-parent.ts | 6 +-- src/scope/index.ts | 7 +++- src/svelte-config/index.ts | 2 +- src/svelte-config/parser.ts | 8 ++-- src/traverse.ts | 4 +- src/visitor-keys.ts | 2 +- tests/src/integrations.ts | 13 +++---- tests/src/meta.ts | 7 ++-- tests/src/parser/error.ts | 10 ++--- tests/src/parser/eslint.ts | 12 +++--- tests/src/parser/parser.ts | 17 ++++---- tests/src/parser/style-context.ts | 7 ++-- tests/src/parser/style-location-coverter.ts | 9 +++-- tests/src/parser/test-utils.ts | 29 ++++++-------- tests/src/parser/typescript/index.ts | 16 ++++---- tests/src/svelte-config/parser.ts | 2 +- tools/update-fixtures.ts | 15 +++---- tools/update-meta.ts | 8 ++-- tsconfig.json | 4 +- 59 files changed, 325 insertions(+), 310 deletions(-) diff --git a/benchmark/index.ts b/benchmark/index.ts index 46b54fcd..6e23c845 100644 --- a/benchmark/index.ts +++ b/benchmark/index.ts @@ -2,8 +2,8 @@ /* eslint-disable no-console -- ignore */ import * as Benchmark from "benchmark"; import fs from "fs"; -import { parseForESLint } from "../src/index"; -import { parseForESLint as parseOld } from "../node_modules/svelte-eslint-parser"; +import { parseForESLint } from "../src/index.js"; +import { parseForESLint as parseOld } from "../node_modules/svelte-eslint-parser/lib/index.js"; const contents = `${fs.readFileSync( require.resolve("../explorer-v2/src/lib/RulesSettings.svelte"), diff --git a/explorer-v2/build-system/shim/module.js b/explorer-v2/build-system/shim/module.js index 5cb69b5f..a75de026 100644 --- a/explorer-v2/build-system/shim/module.js +++ b/explorer-v2/build-system/shim/module.js @@ -1,5 +1,15 @@ +// eslint-disable-next-line n/no-extraneous-import -- shim +import * as estree from 'espree'; export function createRequire() { - return null; + function req(mod) { + if (mod === 'espree') { + return estree; + } + throw new Error(`Cannot find module '${mod}'`); + } + + req.cache = {}; + return req; } export default { createRequire diff --git a/package.json b/package.json index 3a2e1667..f28e04f9 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "engines": { "node": "^18.20.4 || ^20.18.0 || >=22.10.0" }, - "type": "commonjs", + "type": "module", "main": "lib/index.js", "files": [ "lib" @@ -41,7 +41,7 @@ "preversion": "pnpm run lint && pnpm run test", "release": "changeset publish", "test": "pnpm run mocha \"tests/src/**/*.ts\" --reporter dot --timeout 60000", - "ts": "node -r esbuild-register", + "ts": "node --import tsx/esm", "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", "run-update-fixtures": "pnpm run ts ./tools/update-fixtures.ts", "version:ci": "env-cmd -e version-ci pnpm run build:meta && changeset version" @@ -82,7 +82,6 @@ "chai": "^5.0.0", "env-cmd": "^10.1.0", "esbuild": "^0.24.0", - "esbuild-register": "^3.6.0", "eslint": "~9.16.0", "eslint-config-prettier": "^9.1.0", "eslint-plugin-eslint-comments": "^3.2.0", @@ -108,6 +107,7 @@ "semver": "^7.6.3", "svelte": "^5.3.1", "svelte2tsx": "^0.7.28", + "tsx": "^4.19.2", "typescript": "~5.7.2", "typescript-eslint": "^8.16.0", "typescript-eslint-parser-for-extra-files": "^0.7.0" diff --git a/src/ast/base.ts b/src/ast/base.ts index 9e931311..d231bd07 100644 --- a/src/ast/base.ts +++ b/src/ast/base.ts @@ -1,4 +1,4 @@ -import type { Locations } from "./common"; +import type { Locations } from "./common.js"; // internals export interface BaseNode extends Locations { diff --git a/src/ast/common.ts b/src/ast/common.ts index 6d80cfb0..73727723 100644 --- a/src/ast/common.ts +++ b/src/ast/common.ts @@ -1,4 +1,4 @@ -import type { BaseNode } from "./base"; +import type { BaseNode } from "./base.js"; export interface Position { /** >= 1 */ diff --git a/src/ast/html.ts b/src/ast/html.ts index efbe8f85..c39f29e0 100644 --- a/src/ast/html.ts +++ b/src/ast/html.ts @@ -1,7 +1,7 @@ import type ESTree from "estree"; import type { TSESTree } from "@typescript-eslint/types"; -import type { BaseNode } from "./base"; -import type { Token, Comment } from "./common"; +import type { BaseNode } from "./base.js"; +import type { Token, Comment } from "./common.js"; export type SvelteHTMLNode = | SvelteProgram diff --git a/src/ast/index.ts b/src/ast/index.ts index a271aaa3..84edca40 100644 --- a/src/ast/index.ts +++ b/src/ast/index.ts @@ -1,8 +1,8 @@ -import type { SvelteHTMLNode } from "./html"; -import type { SvelteScriptNode } from "./script"; +import type { SvelteHTMLNode } from "./html.js"; +import type { SvelteScriptNode } from "./script.js"; -export * from "./common"; -export * from "./html"; -export * from "./script"; +export * from "./common.js"; +export * from "./html.js"; +export * from "./script.js"; export type SvelteNode = SvelteHTMLNode | SvelteScriptNode; diff --git a/src/ast/script.ts b/src/ast/script.ts index 5cbda4d3..328af5f5 100644 --- a/src/ast/script.ts +++ b/src/ast/script.ts @@ -1,5 +1,5 @@ import type ESTree from "estree"; -import type { BaseNode } from "./base"; +import type { BaseNode } from "./base.js"; export type SvelteScriptNode = SvelteReactiveStatement; diff --git a/src/context/fix-locations.ts b/src/context/fix-locations.ts index dd1154e1..f5055784 100644 --- a/src/context/fix-locations.ts +++ b/src/context/fix-locations.ts @@ -1,7 +1,7 @@ import type * as ESTree from "estree"; -import type { Comment, Locations, Token } from "../ast"; -import type { Context } from "."; -import { traverseNodes } from "../traverse"; +import type { Comment, Locations, Token } from "../ast/index.js"; +import type { Context } from "./index.js"; +import { traverseNodes } from "../traverse.js"; /** Fix locations */ export function fixLocations( diff --git a/src/context/index.ts b/src/context/index.ts index 9dc8de3c..6cda47c8 100644 --- a/src/context/index.ts +++ b/src/context/index.ts @@ -9,18 +9,18 @@ import type { SvelteSnippetBlock, SvelteStyleElement, Token, -} from "../ast"; +} from "../ast/index.js"; import type ESTree from "estree"; -import type * as SvAST from "../parser/svelte-ast-types"; -import type * as Compiler from "../parser/svelte-ast-types-for-v5"; -import { ScriptLetContext } from "./script-let"; -import { LetDirectiveCollections } from "./let-directive-collection"; -import { parseAttributes } from "../parser/html"; -import { sortedLastIndex } from "../utils"; +import type * as SvAST from "../parser/svelte-ast-types.js"; +import type * as Compiler from "../parser/svelte-ast-types-for-v5.js"; +import { ScriptLetContext } from "./script-let.js"; +import { LetDirectiveCollections } from "./let-directive-collection.js"; +import { parseAttributes } from "../parser/html.js"; +import { sortedLastIndex } from "../utils/index.js"; import { isTypeScript, type NormalizedParserOptions, -} from "../parser/parser-options"; +} from "../parser/parser-options.js"; export class ScriptsSourceCode { private raw: string; diff --git a/src/context/let-directive-collection.ts b/src/context/let-directive-collection.ts index 6f205e38..6334af97 100644 --- a/src/context/let-directive-collection.ts +++ b/src/context/let-directive-collection.ts @@ -1,6 +1,6 @@ -import type { SvelteLetDirective, SvelteName } from "../ast"; +import type { SvelteLetDirective, SvelteName } from "../ast/index.js"; import type * as ESTree from "estree"; -import type { ScriptLetBlockParam, ScriptLetCallback } from "./script-let"; +import type { ScriptLetBlockParam, ScriptLetCallback } from "./script-let.js"; /** A class that collects pattern nodes for Let directives. */ export class LetDirectiveCollection { diff --git a/src/context/script-let.ts b/src/context/script-let.ts index cfe9ac8e..443f271c 100644 --- a/src/context/script-let.ts +++ b/src/context/script-let.ts @@ -2,7 +2,7 @@ import type { ScopeManager, Scope } from "eslint-scope"; import type * as ESTree from "estree"; import type { TSESTree } from "@typescript-eslint/types"; import type { Scope as TSScope } from "@typescript-eslint/scope-manager"; -import type { Context, ScriptsSourceCode } from "."; +import type { Context, ScriptsSourceCode } from "./index.js"; import type { Comment, SvelteEachBlock, @@ -11,19 +11,19 @@ import type { SvelteNode, SvelteSnippetBlock, Token, -} from "../ast"; -import type { ESLintExtendedProgram } from "../parser"; -import { getWithLoc } from "../parser/converts/common"; +} from "../ast/index.js"; +import type { ESLintExtendedProgram } from "../parser/index.js"; +import { getWithLoc } from "../parser/converts/common.js"; import { getScopeFromNode, removeAllScopeAndVariableAndReference, removeIdentifierVariable, removeReference, removeScope, -} from "../scope"; -import { getKeys, traverseNodes, getNodes } from "../traverse"; -import { UniqueIdGenerator } from "./unique"; -import { fixLocations } from "./fix-locations"; +} from "../scope/index.js"; +import { getKeys, traverseNodes, getNodes } from "../traverse.js"; +import { UniqueIdGenerator } from "./unique.js"; +import { fixLocations } from "./fix-locations.js"; type TSAsExpression = { type: "TSAsExpression"; diff --git a/src/errors.ts b/src/errors.ts index 65a28913..c6be5af8 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -1,4 +1,4 @@ -import type { Context } from "./context"; +import type { Context } from "./context/index.js"; /** * Svelte parse errors. diff --git a/src/index.ts b/src/index.ts index 3cb39f11..c8227535 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ -import * as AST from "./ast"; -import { traverseNodes } from "./traverse"; -import { KEYS } from "./visitor-keys"; -import { ParseError } from "./errors"; +import * as AST from "./ast/index.js"; +import { traverseNodes } from "./traverse.js"; +import { KEYS } from "./visitor-keys.js"; +import { ParseError } from "./errors.js"; export { parseForESLint, type StyleContext, @@ -9,10 +9,10 @@ export { type StyleContextParseError, type StyleContextSuccess, type StyleContextUnknownLang, -} from "./parser"; -export * as meta from "./meta"; -export { name } from "./meta"; -export type { SvelteConfig } from "./svelte-config"; +} from "./parser/index.js"; +export * as meta from "./meta.js"; +export { name } from "./meta.js"; +export type { SvelteConfig } from "./svelte-config/index.js"; export { AST, ParseError }; diff --git a/src/parser/analyze-scope.ts b/src/parser/analyze-scope.ts index caa384c6..b502b034 100644 --- a/src/parser/analyze-scope.ts +++ b/src/parser/analyze-scope.ts @@ -1,16 +1,16 @@ import type ESTree from "estree"; import type { Scope, ScopeManager } from "eslint-scope"; import { Variable, Reference, analyze } from "eslint-scope"; -import { getFallbackKeys } from "../traverse"; +import { getFallbackKeys } from "../traverse.js"; import type { SvelteReactiveStatement, SvelteScriptElement, SvelteSnippetBlock, -} from "../ast"; -import { addReference, addVariable, getScopeFromNode } from "../scope"; -import { addElementToSortedArray } from "../utils"; -import type { NormalizedParserOptions } from "./parser-options"; -import type { SvelteParseContext } from "./svelte-parse-context"; +} from "../ast/index.js"; +import { addReference, addVariable, getScopeFromNode } from "../scope/index.js"; +import { addElementToSortedArray } from "../utils/index.js"; +import type { NormalizedParserOptions } from "./parser-options.js"; +import type { SvelteParseContext } from "./svelte-parse-context.js"; /** * Analyze scope */ diff --git a/src/parser/compat.ts b/src/parser/compat.ts index 7aa30d7f..535c8821 100644 --- a/src/parser/compat.ts +++ b/src/parser/compat.ts @@ -1,7 +1,7 @@ /** Compatibility for Svelte v4 <-> v5 */ import type ESTree from "estree"; -import type * as SvAST from "./svelte-ast-types"; -import type * as Compiler from "./svelte-ast-types-for-v5"; +import type * as SvAST from "./svelte-ast-types.js"; +import type * as Compiler from "./svelte-ast-types-for-v5.js"; export type Child = | Compiler.Text diff --git a/src/parser/converts/attr.ts b/src/parser/converts/attr.ts index bc7dc3d2..e306b682 100644 --- a/src/parser/converts/attr.ts +++ b/src/parser/converts/attr.ts @@ -20,19 +20,19 @@ import type { SvelteStyleElement, SvelteElseBlock, SvelteAwaitBlock, -} from "../../ast"; +} from "../../ast/index.js"; import type ESTree from "estree"; -import type { Context } from "../../context"; -import type * as SvAST from "../svelte-ast-types"; -import type * as Compiler from "../svelte-ast-types-for-v5"; -import { getWithLoc, indexOf } from "./common"; -import { convertMustacheTag } from "./mustache"; -import { convertTextToLiteral } from "./text"; -import { ParseError } from "../../errors"; -import type { ScriptLetCallback } from "../../context/script-let"; -import { svelteVersion } from "../svelte-version"; -import { hasTypeInfo } from "../../utils"; -import { getModifiers } from "../compat"; +import type { Context } from "../../context/index.js"; +import type * as SvAST from "../svelte-ast-types.js"; +import type * as Compiler from "../svelte-ast-types-for-v5.js"; +import { getWithLoc, indexOf } from "./common.js"; +import { convertMustacheTag } from "./mustache.js"; +import { convertTextToLiteral } from "./text.js"; +import { ParseError } from "../../errors.js"; +import type { ScriptLetCallback } from "../../context/script-let.js"; +import { svelteVersion } from "../svelte-version.js"; +import { hasTypeInfo } from "../../utils/index.js"; +import { getModifiers } from "../compat.js"; type LetDirective = Omit & { expression: SvAST.LetDirective["expression"]; diff --git a/src/parser/converts/block.ts b/src/parser/converts/block.ts index 274b2df6..5ad1f310 100644 --- a/src/parser/converts/block.ts +++ b/src/parser/converts/block.ts @@ -1,5 +1,5 @@ -import type * as SvAST from "../svelte-ast-types"; -import type * as Compiler from "../svelte-ast-types-for-v5"; +import type * as SvAST from "../svelte-ast-types.js"; +import type * as Compiler from "../svelte-ast-types-for-v5.js"; import type { SvelteAwaitBlock, SvelteAwaitBlockAwaitCatch, @@ -16,10 +16,10 @@ import type { SvelteIfBlockElseIf, SvelteKeyBlock, SvelteSnippetBlock, -} from "../../ast"; -import type { Context } from "../../context"; -import { convertChildren } from "./element"; -import { getWithLoc, indexOf, lastIndexOf } from "./common"; +} from "../../ast/index.js"; +import type { Context } from "../../context/index.js"; +import { convertChildren } from "./element.js"; +import { getWithLoc, indexOf, lastIndexOf } from "./common.js"; import type * as ESTree from "estree"; import { getAlternateFromIfBlock, @@ -33,7 +33,7 @@ import { getTestFromIfBlock, getThenFromAwaitBlock, trimChildren, -} from "../compat"; +} from "../compat.js"; /** Get start index of block */ function startBlockIndex( diff --git a/src/parser/converts/const.ts b/src/parser/converts/const.ts index c236ae1c..d3c4a5d3 100644 --- a/src/parser/converts/const.ts +++ b/src/parser/converts/const.ts @@ -1,8 +1,8 @@ -import type { SvelteConstTag } from "../../ast"; -import type { Context } from "../../context"; -import { getDeclaratorFromConstTag } from "../compat"; -import type * as SvAST from "../svelte-ast-types"; -import type * as Compiler from "../svelte-ast-types-for-v5"; +import type { SvelteConstTag } from "../../ast/index.js"; +import type { Context } from "../../context/index.js"; +import { getDeclaratorFromConstTag } from "../compat.js"; +import type * as SvAST from "../svelte-ast-types.js"; +import type * as Compiler from "../svelte-ast-types-for-v5.js"; /** Convert for ConstTag */ export function convertConstTag( diff --git a/src/parser/converts/element.ts b/src/parser/converts/element.ts index b220e493..bbabaac6 100644 --- a/src/parser/converts/element.ts +++ b/src/parser/converts/element.ts @@ -26,11 +26,11 @@ import type { SvelteSpecialElement, SvelteStyleElement, SvelteText, -} from "../../ast"; +} from "../../ast/index.js"; import type ESTree from "estree"; -import type { Context } from "../../context"; -import type * as SvAST from "../svelte-ast-types"; -import type * as Compiler from "../svelte-ast-types-for-v5"; +import type { Context } from "../../context/index.js"; +import type * as SvAST from "../svelte-ast-types.js"; +import type * as Compiler from "../svelte-ast-types-for-v5.js"; import { convertAwaitBlock, @@ -38,22 +38,22 @@ import { convertIfBlock, convertKeyBlock, convertSnippetBlock, -} from "./block"; -import { getWithLoc, indexOf } from "./common"; +} from "./block.js"; +import { getWithLoc, indexOf } from "./common.js"; import { convertMustacheTag, convertDebugTag, convertRawMustacheTag, -} from "./mustache"; -import { convertText } from "./text"; -import { convertAttributes } from "./attr"; -import { convertConstTag } from "./const"; -import { sortNodes } from "../sort"; -import type { ScriptLetBlockParam } from "../../context/script-let"; -import { ParseError } from "../.."; -import { convertRenderTag } from "./render"; -import type { Child } from "../compat"; -import { getChildren, getFragment } from "../compat"; +} from "./mustache.js"; +import { convertText } from "./text.js"; +import { convertAttributes } from "./attr.js"; +import { convertConstTag } from "./const.js"; +import { sortNodes } from "../sort.js"; +import type { ScriptLetBlockParam } from "../../context/script-let.js"; +import { ParseError } from "../../index.js"; +import { convertRenderTag } from "./render.js"; +import type { Child } from "../compat.js"; +import { getChildren, getFragment } from "../compat.js"; /** Convert for Fragment or Element or ... */ export function* convertChildren( diff --git a/src/parser/converts/index.ts b/src/parser/converts/index.ts index a6874cea..748fae2a 100644 --- a/src/parser/converts/index.ts +++ b/src/parser/converts/index.ts @@ -1 +1 @@ -export { convertSvelteRoot } from "./root"; +export { convertSvelteRoot } from "./root.js"; diff --git a/src/parser/converts/mustache.ts b/src/parser/converts/mustache.ts index 60d74f94..6c8841b2 100644 --- a/src/parser/converts/mustache.ts +++ b/src/parser/converts/mustache.ts @@ -3,11 +3,11 @@ import type { SvelteMustacheTag, SvelteMustacheTagRaw, SvelteMustacheTagText, -} from "../../ast"; -import type { Context } from "../../context"; -import type * as SvAST from "../svelte-ast-types"; -import { hasTypeInfo } from "../../utils"; -import type * as Compiler from "../svelte-ast-types-for-v5"; +} from "../../ast/index.js"; +import type { Context } from "../../context/index.js"; +import type * as SvAST from "../svelte-ast-types.js"; +import { hasTypeInfo } from "../../utils/index.js"; +import type * as Compiler from "../svelte-ast-types-for-v5.js"; /** Convert for MustacheTag */ export function convertMustacheTag( diff --git a/src/parser/converts/render.ts b/src/parser/converts/render.ts index ebe92805..796701a9 100644 --- a/src/parser/converts/render.ts +++ b/src/parser/converts/render.ts @@ -1,8 +1,8 @@ import type * as ESTree from "estree"; -import type { SvelteRenderTag } from "../../ast"; -import type { Context } from "../../context"; -import { getWithLoc } from "./common"; -import type * as Compiler from "../svelte-ast-types-for-v5"; +import type { SvelteRenderTag } from "../../ast/index.js"; +import type { Context } from "../../context/index.js"; +import { getWithLoc } from "./common.js"; +import type * as Compiler from "../svelte-ast-types-for-v5.js"; /** Convert for RenderTag */ export function convertRenderTag( diff --git a/src/parser/converts/root.ts b/src/parser/converts/root.ts index 8253715a..5e5e8de3 100644 --- a/src/parser/converts/root.ts +++ b/src/parser/converts/root.ts @@ -1,5 +1,5 @@ -import type * as SvAST from "../svelte-ast-types"; -import type * as Compiler from "../svelte-ast-types-for-v5"; +import type * as SvAST from "../svelte-ast-types.js"; +import type * as Compiler from "../svelte-ast-types-for-v5.js"; import type { SvelteAttribute, SvelteGenericsDirective, @@ -8,25 +8,25 @@ import type { SvelteProgram, SvelteScriptElement, SvelteStyleElement, -} from "../../ast"; -import {} from "./common"; -import type { Context } from "../../context"; -import { convertChildren, extractElementTags } from "./element"; -import { convertAttributes } from "./attr"; +} from "../../ast/index.js"; +import {} from "./common.js"; +import type { Context } from "../../context/index.js"; +import { convertChildren, extractElementTags } from "./element.js"; +import { convertAttributes } from "./attr.js"; import type { Scope } from "eslint-scope"; -import { parseScriptWithoutAnalyzeScope } from "../script"; -import type { TSESParseForESLintResult } from "../typescript/types"; +import { parseScriptWithoutAnalyzeScope } from "../script.js"; +import type { TSESParseForESLintResult } from "../typescript/types.js"; import type * as ESTree from "estree"; import type { TSESTree } from "@typescript-eslint/types"; -import { fixLocations } from "../../context/fix-locations"; +import { fixLocations } from "../../context/fix-locations.js"; import { getChildren, getFragmentFromRoot, getInstanceFromRoot, getModuleFromRoot, getOptionsFromRoot, -} from "../compat"; -import { sortNodes } from "../sort"; +} from "../compat.js"; +import { sortNodes } from "../sort.js"; /** * Convert root diff --git a/src/parser/converts/text.ts b/src/parser/converts/text.ts index 21289a1c..87acbaea 100644 --- a/src/parser/converts/text.ts +++ b/src/parser/converts/text.ts @@ -1,6 +1,6 @@ -import type { SvelteLiteral, SvelteText } from "../../ast"; -import type { Context } from "../../context"; -import type * as SvAST from "../svelte-ast-types"; +import type { SvelteLiteral, SvelteText } from "../../ast/index.js"; +import type { Context } from "../../context/index.js"; +import type * as SvAST from "../svelte-ast-types.js"; /** Convert for Text */ export function convertText( node: SvAST.Text, diff --git a/src/parser/espree.ts b/src/parser/espree.ts index ae8dc73e..df1b6e45 100644 --- a/src/parser/espree.ts +++ b/src/parser/espree.ts @@ -1,23 +1,6 @@ import Module from "module"; import path from "path"; -import type { BasicParserObject } from "./parser-object"; - -const createRequire: (filename: string) => (modName: string) => any = - // Added in v12.2.0 - Module.createRequire || - // Added in v10.12.0, but deprecated in v12.2.0. - // @ts-expect-error -- old type - Module.createRequireFromPath || - // Polyfill - This is not executed on the tests on node@>=10. - /* istanbul ignore next */ - ((modName) => { - const mod = new Module(modName); - - mod.filename = modName; - mod.paths = (Module as any)._nodeModulePaths(path.dirname(modName)); - (mod as any)._compile("module.exports = require;", modName); - return mod.exports; - }); +import type { BasicParserObject } from "./parser-object.js"; let espreeCache: (BasicParserObject & { latestEcmaVersion: number }) | null = null; @@ -39,17 +22,26 @@ function isLinterPath(p: string): boolean { export function getEspree(): BasicParserObject & { latestEcmaVersion: number } { if (!espreeCache) { // Lookup the loaded eslint - const linterPath = Object.keys(require.cache || {}).find(isLinterPath); + const req = Module.createRequire(import.meta.url); + const linterPath = Object.keys(req.cache || {}).find(isLinterPath); if (linterPath) { try { - espreeCache = createRequire(linterPath)("espree"); + espreeCache = Module.createRequire(linterPath)("espree"); + } catch { + // ignore + } + } + if (!espreeCache) { + try { + return req("espree"); } catch { // ignore } } if (!espreeCache) { - // eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore - espreeCache = require("espree"); + if (typeof require === "function") + // eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore + espreeCache = require("espree"); } } diff --git a/src/parser/globals.ts b/src/parser/globals.ts index 29ac3c9c..be66a663 100644 --- a/src/parser/globals.ts +++ b/src/parser/globals.ts @@ -1,4 +1,4 @@ -import type { SvelteParseContext } from "./svelte-parse-context"; +import type { SvelteParseContext } from "./svelte-parse-context.js"; const globalsForSvelte = ["$$slots", "$$props", "$$restProps"] as const; export const globalsForRunes = [ diff --git a/src/parser/html.ts b/src/parser/html.ts index a4b43072..6c713bc8 100644 --- a/src/parser/html.ts +++ b/src/parser/html.ts @@ -1,6 +1,6 @@ -import type * as Compiler from "./svelte-ast-types-for-v5"; +import type * as Compiler from "./svelte-ast-types-for-v5.js"; import type ESTree from "estree"; -import { getEspree } from "./espree"; +import { getEspree } from "./espree.js"; const RE_IS_SPACE = /^\s$/u; diff --git a/src/parser/index.ts b/src/parser/index.ts index 01e4d96c..f5722ae9 100644 --- a/src/parser/index.ts +++ b/src/parser/index.ts @@ -1,29 +1,32 @@ -import { KEYS } from "../visitor-keys"; -import { Context } from "../context"; +import { KEYS } from "../visitor-keys.js"; +import { Context } from "../context/index.js"; import type { Comment, SvelteProgram, SvelteScriptElement, SvelteStyleElement, Token, -} from "../ast"; +} from "../ast/index.js"; import type { Program } from "estree"; import type { ScopeManager } from "eslint-scope"; import { Variable } from "eslint-scope"; -import { parseScript, parseScriptInSvelte } from "./script"; -import type * as SvAST from "./svelte-ast-types"; -import type * as Compiler from "./svelte-ast-types-for-v5"; -import { sortNodes } from "./sort"; -import { parseTemplate } from "./template"; +import { parseScript, parseScriptInSvelte } from "./script.js"; +import type * as SvAST from "./svelte-ast-types.js"; +import type * as Compiler from "./svelte-ast-types-for-v5.js"; +import { sortNodes } from "./sort.js"; +import { parseTemplate } from "./template.js"; import { analyzePropsScope, analyzeReactiveScope, analyzeSnippetsScope, analyzeStoreScope, -} from "./analyze-scope"; -import { ParseError } from "../errors"; -import { parseTypeScript, parseTypeScriptInSvelte } from "./typescript"; -import { addReference } from "../scope"; +} from "./analyze-scope.js"; +import { ParseError } from "../errors.js"; +import { + parseTypeScript, + parseTypeScriptInSvelte, +} from "./typescript/index.js"; +import { addReference } from "../scope/index.js"; import { parseStyleContext, type StyleContext, @@ -33,19 +36,19 @@ import { type StyleContextUnknownLang, styleNodeLoc, styleNodeRange, -} from "./style-context"; -import { getGlobalsForSvelte, getGlobalsForSvelteScript } from "./globals"; -import type { NormalizedParserOptions } from "./parser-options"; -import { isTypeScript, normalizeParserOptions } from "./parser-options"; -import { getFragmentFromRoot } from "./compat"; +} 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 { getFragmentFromRoot } from "./compat.js"; import { isEnableRunes, resolveSvelteParseContextForSvelte, resolveSvelteParseContextForSvelteScript, type SvelteParseContext, -} from "./svelte-parse-context"; -import type { SvelteConfig } from "../svelte-config"; -import { resolveSvelteConfigFromOption } from "../svelte-config"; +} from "./svelte-parse-context.js"; +import type { SvelteConfig } from "../svelte-config/index.js"; +import { resolveSvelteConfigFromOption } from "../svelte-config/index.js"; export { StyleContext, diff --git a/src/parser/parser-object.ts b/src/parser/parser-object.ts index 995efc8f..5f85b732 100644 --- a/src/parser/parser-object.ts +++ b/src/parser/parser-object.ts @@ -1,4 +1,4 @@ -import type { ESLintExtendedProgram, ESLintProgram } from "."; +import type { ESLintExtendedProgram, ESLintProgram } from "./index.js"; import type * as tsESLintParser from "@typescript-eslint/parser"; type TSESLintParser = typeof tsESLintParser; /** diff --git a/src/parser/parser-options.ts b/src/parser/parser-options.ts index c663406c..fd338485 100644 --- a/src/parser/parser-options.ts +++ b/src/parser/parser-options.ts @@ -3,8 +3,8 @@ import path from "path"; import { isTSESLintParserObject, maybeTSESLintParserObject, -} from "./parser-object"; -import { getParserForLang, type UserOptionParser } from "./resolve-parser"; +} from "./parser-object.js"; +import { getParserForLang, type UserOptionParser } from "./resolve-parser.js"; export type NormalizedParserOptions = { parser?: UserOptionParser; diff --git a/src/parser/resolve-parser.ts b/src/parser/resolve-parser.ts index 9dab4de1..76553289 100644 --- a/src/parser/resolve-parser.ts +++ b/src/parser/resolve-parser.ts @@ -1,6 +1,7 @@ -import { getEspree } from "./espree"; -import type { ParserObject } from "./parser-object"; -import { isParserObject } from "./parser-object"; +import { getEspree } from "./espree.js"; +import type { ParserObject } from "./parser-object.js"; +import { isParserObject } from "./parser-object.js"; +import Module from "module"; export type UserOptionParser = | string @@ -37,7 +38,7 @@ export function getParser( return parserValue; } if (parserValue !== "espree") { - // eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore + const require = Module.createRequire(import.meta.url); return require(parserValue); } return getEspree(); diff --git a/src/parser/script.ts b/src/parser/script.ts index 80de7662..9055455d 100644 --- a/src/parser/script.ts +++ b/src/parser/script.ts @@ -1,9 +1,9 @@ -import type { ESLintExtendedProgram } from "."; -import { analyzeScope } from "./analyze-scope"; -import { traverseNodes } from "../traverse"; -import { getParser } from "./resolve-parser"; -import { isEnhancedParserObject } from "./parser-object"; -import type { NormalizedParserOptions } from "./parser-options"; +import type { ESLintExtendedProgram } from "./index.js"; +import { analyzeScope } from "./analyze-scope.js"; +import { traverseNodes } from "../traverse.js"; +import { getParser } from "./resolve-parser.js"; +import { isEnhancedParserObject } from "./parser-object.js"; +import type { NormalizedParserOptions } from "./parser-options.js"; /** * Parse for