Skip to content

Commit 2f78cfd

Browse files
committed
chore: use defineLazyProperty instead, assert options.tsconfig in test cases
1 parent 8887aa6 commit 2f78cfd

File tree

7 files changed

+60
-17
lines changed

7 files changed

+60
-17
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ jobs:
6363
PARSER_NO_WATCH: true
6464

6565
- name: Codecov
66-
uses: codecov/codecov-action@ad3126e916f78f00edff4ed0317cf185271ccc2d # v5.4.2
66+
uses: codecov/codecov-action@18283e04ce6e62d37312384ff67231eb8fd56d24 # v5.4.3
6767
with:
6868
token: ${{ secrets.CODECOV_TOKEN }}
6969

src/utils/export-map.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function getTsconfigWithContext(context: ChildContext | RuleContext) {
9494
const parserOptions = context.parserOptions || {}
9595
let tsconfigRootDir = parserOptions.tsconfigRootDir
9696
const project = parserOptions.project
97-
const cacheKey = stableHash({ tsconfigRootDir, project })
97+
const cacheKey = stableHash([tsconfigRootDir, project])
9898
let tsConfig: TsConfigJsonResolved | null | undefined
9999
if (tsconfigCache.has(cacheKey)) {
100100
tsConfig = tsconfigCache.get(cacheKey)!
@@ -195,11 +195,11 @@ export class ExportMap {
195195
static get(source: string, context: RuleContext) {
196196
const tsconfig = lazy(() => getTsconfigWithContext(context))
197197

198-
const path = resolve(source, context, {
199-
get tsconfig() {
200-
return tsconfig()
201-
},
202-
})
198+
const path = resolve(
199+
source,
200+
context,
201+
defineLazyProperty({}, 'tsconfig', tsconfig),
202+
)
203203
if (path == null) {
204204
return null
205205
}
@@ -286,11 +286,13 @@ export class ExportMap {
286286
const namespaces = new Map</* identifier */ string, /* source */ string>()
287287

288288
function remotePath(value: string) {
289-
return relative(value, filepath, context.settings, context, {
290-
get tsconfig() {
291-
return tsconfig()
292-
},
293-
})
289+
return relative(
290+
value,
291+
filepath,
292+
context.settings,
293+
context,
294+
defineLazyProperty({}, 'tsconfig', tsconfig),
295+
)
294296
}
295297

296298
function resolveImport(value: string) {

src/utils/resolve.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
RuleContext,
1515
} from '../types.js'
1616

17+
import { defineLazyProperty } from './lazy-value.js'
1718
import {
1819
normalizeConfigResolvers,
1920
resolveWithLegacyResolver,
@@ -294,11 +295,7 @@ export function importXResolverCompat(
294295
modulePath,
295296
sourceFile,
296297
options.context,
297-
{
298-
get tsconfig() {
299-
return options.tsconfig
300-
},
301-
},
298+
defineLazyProperty({}, 'tsconfig', () => options.tsconfig),
302299
)
303300
return resolved
304301
},

test/fixtures/foo-bar-resolver-no-version.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,15 @@ exports.resolveImport = function (modulePath, sourceFile, config, _, options) {
1111
}
1212
assert.ok(!_, 'the 4th argument must be undefined')
1313
assert.ok(options.context.cwd, 'the `context.cwd` must be present')
14+
if (options.context.parserOptions?.project) {
15+
assert.ok(
16+
options.tsconfig,
17+
'the `tsconfig` must be present when `parserOptions.project` is set'
18+
)
19+
} else {
20+
assert.ok(
21+
!options.tsconfig,
22+
'the `tsconfig` must not be present when `parserOptions.project` is not set'
23+
)
24+
}
1425
}

test/fixtures/foo-bar-resolver-v1.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ exports.resolveImport = function (modulePath, sourceFile, config, _, options) {
1111
}
1212
assert.ok(!_, 'the 4th argument must be undefined')
1313
assert.ok(options.context.cwd, 'the `context` must be present')
14+
if (options.context.parserOptions?.project) {
15+
assert.ok(
16+
options.tsconfig,
17+
'the `tsconfig` must be present when `parserOptions.project` is set'
18+
)
19+
} else {
20+
assert.ok(
21+
!options.tsconfig,
22+
'the `tsconfig` must not be present when `parserOptions.project` is not set'
23+
)
24+
}
1425
}
1526

1627
exports.interfaceVersion = 1

test/fixtures/foo-bar-resolver-v2.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ exports.resolve = function (modulePath, sourceFile, config, _, options) {
1111
}
1212
assert.ok(!_, 'the 4th argument must be undefined')
1313
assert.ok(options.context.cwd, 'the `context.cwd` must be present')
14+
if (options.context.parserOptions?.project) {
15+
assert.ok(
16+
options.tsconfig,
17+
'the `tsconfig` must be present when `parserOptions.project` is set'
18+
)
19+
} else {
20+
assert.ok(
21+
!options.tsconfig,
22+
'the `tsconfig` must not be present when `parserOptions.project` is not set'
23+
)
24+
}
1425
return { found: false }
1526
}
1627

test/fixtures/foo-bar-resolver-v3.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ exports.foobarResolver = {
1616
throw new Error('foo-bar-resolver-v3 resolve test exception')
1717
}
1818
assert.ok(options.context.cwd, 'the `context.cwd` must be present')
19+
if (options.context.parserOptions?.project) {
20+
assert.ok(
21+
options.tsconfig,
22+
'the `tsconfig` must be present when `parserOptions.project` is set'
23+
)
24+
} else {
25+
assert.ok(
26+
!options.tsconfig,
27+
'the `tsconfig` must not be present when `parserOptions.project` is not set'
28+
)
29+
}
1930
return { found: false }
2031
},
2132
}

0 commit comments

Comments
 (0)