Skip to content

Commit b3f6fd5

Browse files
authored
fix: improved loading of external modules (#349)
1 parent 5933794 commit b3f6fd5

File tree

11 files changed

+38
-20
lines changed

11 files changed

+38
-20
lines changed

.changeset/sharp-beans-design.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-svelte": minor
3+
---
4+
5+
fix: improved loading of external modules

docs-svelte-kit/src/lib/components/ESLintCodeBlock.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
postprocess,
88
} from "../eslint/scripts/linter.js"
99
import { loadTsParser } from "../eslint/scripts/ts-parser.js"
10-
import { loadModulesForBrowser } from "../../../../src/shared/svelte-compile-warns/transform/load-module"
10+
import { loadModulesForBrowser } from "../../../../src/utils/load-module"
1111
1212
const modulesForBrowser = loadModulesForBrowser()
1313
const loadLinter = createLinter()

docs-svelte-kit/src/lib/components/ESLintPlayground.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
postprocess,
1111
} from "../eslint/scripts/linter.js"
1212
import { loadTsParser } from "../eslint/scripts/ts-parser.js"
13-
import { loadModulesForBrowser } from "../../../../src/shared/svelte-compile-warns/transform/load-module"
13+
import { loadModulesForBrowser } from "../../../../src/utils/load-module"
1414
let tsParser = null
1515
const linter = loadModulesForBrowser()
1616
.then(async () => {

src/shared/svelte-compile-warns/transform/babel.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { AST } from "svelte-eslint-parser"
22
import type babelCore from "@babel/core"
33
import type { RuleContext } from "../../../types"
44
import type { TransformResult } from "./types"
5-
import { loadModule } from "./load-module"
5+
import { loadModule } from "../../../utils/load-module"
66

77
type BabelCore = typeof babelCore
88
/**

src/shared/svelte-compile-warns/transform/less.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { AST } from "svelte-eslint-parser"
22
import type less from "less"
33
import type { RuleContext } from "../../../types"
44
import type { TransformResult } from "./types"
5-
import { loadModule } from "./load-module"
5+
import { loadModule } from "../../../utils/load-module"
66

77
type Less = typeof less
88
/**

src/shared/svelte-compile-warns/transform/sass.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { AST } from "svelte-eslint-parser"
22
import type sass from "sass"
33
import type { RuleContext } from "../../../types"
44
import type { TransformResult } from "./types"
5-
import { loadModule } from "./load-module"
5+
import { loadModule } from "../../../utils/load-module"
66

77
type Sass = typeof sass
88
/**

src/shared/svelte-compile-warns/transform/stylus.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type stylus from "stylus"
33
import type { RawSourceMap } from "source-map-js"
44
import type { RuleContext } from "../../../types"
55
import type { TransformResult } from "./types"
6-
import { loadModule } from "./load-module"
6+
import { loadModule } from "../../../utils/load-module"
77

88
type Stylus = typeof stylus
99
/**

src/shared/svelte-compile-warns/transform/typescript.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { AST } from "svelte-eslint-parser"
22
import type typescript from "typescript"
33
import type { RuleContext } from "../../../types"
44
import type { TransformResult } from "./types"
5-
import { loadModule } from "./load-module"
5+
import { loadModule } from "../../../utils/load-module"
66

77
type TS = typeof typescript
88
/**

src/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ export type RuleContext = {
154154

155155
// eslint@6 does not have this method.
156156
getCwd?: () => string
157+
// eslint@<7.11.0 does not have this method.
158+
getPhysicalFilename?: () => string
157159
}
158160

159161
export type NodeOrToken = {

src/shared/svelte-compile-warns/transform/load-module.ts renamed to src/utils/load-module.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { AST } from "svelte-eslint-parser"
22
import Module from "module"
33
import path from "path"
4-
import type { RuleContext } from "../../../types"
4+
import type { RuleContext } from "../types"
55
const cache = new WeakMap<AST.SvelteProgram, Record<string, unknown>>()
66
const cache4b = new Map<string, unknown>()
77
/**
@@ -17,12 +17,30 @@ export function loadModule<R>(context: RuleContext, name: string): R | null {
1717
const mod = modules[name] || cache4b.get(name)
1818
if (mod) return mod as R
1919
try {
20+
// load from cwd
2021
const cwd = context.getCwd?.() ?? process.cwd()
2122
const relativeTo = path.join(cwd, "__placeholder__.js")
2223
return (modules[name] = Module.createRequire(relativeTo)(name) as R)
2324
} catch {
24-
return null
25+
// ignore
2526
}
27+
for (const relativeTo of [
28+
// load from lint file name
29+
context.getFilename(),
30+
// load from lint file name (physical)
31+
context.getPhysicalFilename?.(),
32+
// load from this plugin module
33+
typeof __filename !== "undefined" ? __filename : "",
34+
]) {
35+
if (relativeTo) {
36+
try {
37+
return (modules[name] = Module.createRequire(relativeTo)(name) as R)
38+
} catch {
39+
// ignore
40+
}
41+
}
42+
}
43+
return null
2644
}
2745

2846
/** Load modules for browser */

src/utils/ts-utils/index.ts

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { RuleContext, ASTNode } from "../../types"
22
import type * as TS from "typescript"
3-
import Module from "module"
4-
import path from "path"
3+
import { loadModule } from "../load-module"
54
export type TypeScript = typeof TS
65
export type { TS }
76

@@ -47,21 +46,15 @@ export function getTypeScriptTools(context: RuleContext): TSTools | null {
4746
}
4847
}
4948

50-
let cacheTypeScript: TypeScript | undefined
49+
let cacheTypeScript: TypeScript | null = null
5150
/**
5251
* Get TypeScript tools
5352
*/
54-
export function getTypeScript(context: RuleContext): TypeScript | undefined {
53+
export function getTypeScript(context: RuleContext): TypeScript | null {
5554
if (cacheTypeScript) {
5655
return cacheTypeScript
5756
}
58-
try {
59-
const cwd = context.getCwd?.() ?? process.cwd()
60-
const relativeTo = path.join(cwd, "__placeholder__.js")
61-
cacheTypeScript = Module.createRequire(relativeTo)("typescript")
62-
} catch {
63-
// ignore
64-
}
57+
cacheTypeScript = loadModule(context, "typescript")
6558
if (cacheTypeScript) {
6659
return cacheTypeScript
6760
}

0 commit comments

Comments
 (0)