Skip to content

Commit

Permalink
fix: improved loading of external modules (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Jan 24, 2023
1 parent 5933794 commit b3f6fd5
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-beans-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": minor
---

fix: improved loading of external modules
2 changes: 1 addition & 1 deletion docs-svelte-kit/src/lib/components/ESLintCodeBlock.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
postprocess,
} from "../eslint/scripts/linter.js"
import { loadTsParser } from "../eslint/scripts/ts-parser.js"
import { loadModulesForBrowser } from "../../../../src/shared/svelte-compile-warns/transform/load-module"
import { loadModulesForBrowser } from "../../../../src/utils/load-module"
const modulesForBrowser = loadModulesForBrowser()
const loadLinter = createLinter()
Expand Down
2 changes: 1 addition & 1 deletion docs-svelte-kit/src/lib/components/ESLintPlayground.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
postprocess,
} from "../eslint/scripts/linter.js"
import { loadTsParser } from "../eslint/scripts/ts-parser.js"
import { loadModulesForBrowser } from "../../../../src/shared/svelte-compile-warns/transform/load-module"
import { loadModulesForBrowser } from "../../../../src/utils/load-module"
let tsParser = null
const linter = loadModulesForBrowser()
.then(async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/svelte-compile-warns/transform/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AST } from "svelte-eslint-parser"
import type babelCore from "@babel/core"
import type { RuleContext } from "../../../types"
import type { TransformResult } from "./types"
import { loadModule } from "./load-module"
import { loadModule } from "../../../utils/load-module"

type BabelCore = typeof babelCore
/**
Expand Down
2 changes: 1 addition & 1 deletion src/shared/svelte-compile-warns/transform/less.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AST } from "svelte-eslint-parser"
import type less from "less"
import type { RuleContext } from "../../../types"
import type { TransformResult } from "./types"
import { loadModule } from "./load-module"
import { loadModule } from "../../../utils/load-module"

type Less = typeof less
/**
Expand Down
2 changes: 1 addition & 1 deletion src/shared/svelte-compile-warns/transform/sass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AST } from "svelte-eslint-parser"
import type sass from "sass"
import type { RuleContext } from "../../../types"
import type { TransformResult } from "./types"
import { loadModule } from "./load-module"
import { loadModule } from "../../../utils/load-module"

type Sass = typeof sass
/**
Expand Down
2 changes: 1 addition & 1 deletion src/shared/svelte-compile-warns/transform/stylus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type stylus from "stylus"
import type { RawSourceMap } from "source-map-js"
import type { RuleContext } from "../../../types"
import type { TransformResult } from "./types"
import { loadModule } from "./load-module"
import { loadModule } from "../../../utils/load-module"

type Stylus = typeof stylus
/**
Expand Down
2 changes: 1 addition & 1 deletion src/shared/svelte-compile-warns/transform/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AST } from "svelte-eslint-parser"
import type typescript from "typescript"
import type { RuleContext } from "../../../types"
import type { TransformResult } from "./types"
import { loadModule } from "./load-module"
import { loadModule } from "../../../utils/load-module"

type TS = typeof typescript
/**
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ export type RuleContext = {

// eslint@6 does not have this method.
getCwd?: () => string
// eslint@<7.11.0 does not have this method.
getPhysicalFilename?: () => string
}

export type NodeOrToken = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AST } from "svelte-eslint-parser"
import Module from "module"
import path from "path"
import type { RuleContext } from "../../../types"
import type { RuleContext } from "../types"
const cache = new WeakMap<AST.SvelteProgram, Record<string, unknown>>()
const cache4b = new Map<string, unknown>()
/**
Expand All @@ -17,12 +17,30 @@ export function loadModule<R>(context: RuleContext, name: string): R | null {
const mod = modules[name] || cache4b.get(name)
if (mod) return mod as R
try {
// load from cwd
const cwd = context.getCwd?.() ?? process.cwd()
const relativeTo = path.join(cwd, "__placeholder__.js")
return (modules[name] = Module.createRequire(relativeTo)(name) as R)
} catch {
return null
// ignore
}
for (const relativeTo of [
// load from lint file name
context.getFilename(),
// load from lint file name (physical)
context.getPhysicalFilename?.(),
// load from this plugin module
typeof __filename !== "undefined" ? __filename : "",
]) {
if (relativeTo) {
try {
return (modules[name] = Module.createRequire(relativeTo)(name) as R)
} catch {
// ignore
}
}
}
return null
}

/** Load modules for browser */
Expand Down
15 changes: 4 additions & 11 deletions src/utils/ts-utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { RuleContext, ASTNode } from "../../types"
import type * as TS from "typescript"
import Module from "module"
import path from "path"
import { loadModule } from "../load-module"
export type TypeScript = typeof TS
export type { TS }

Expand Down Expand Up @@ -47,21 +46,15 @@ export function getTypeScriptTools(context: RuleContext): TSTools | null {
}
}

let cacheTypeScript: TypeScript | undefined
let cacheTypeScript: TypeScript | null = null
/**
* Get TypeScript tools
*/
export function getTypeScript(context: RuleContext): TypeScript | undefined {
export function getTypeScript(context: RuleContext): TypeScript | null {
if (cacheTypeScript) {
return cacheTypeScript
}
try {
const cwd = context.getCwd?.() ?? process.cwd()
const relativeTo = path.join(cwd, "__placeholder__.js")
cacheTypeScript = Module.createRequire(relativeTo)("typescript")
} catch {
// ignore
}
cacheTypeScript = loadModule(context, "typescript")
if (cacheTypeScript) {
return cacheTypeScript
}
Expand Down

0 comments on commit b3f6fd5

Please sign in to comment.