Skip to content

Commit fd85848

Browse files
authored
fix: fix ts extension detect regex, close #775 #774 #772 (#777)
1 parent c78ca5e commit fd85848

File tree

6 files changed

+60
-5
lines changed

6 files changed

+60
-5
lines changed

.changeset/ninety-singers-roll.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fake-scope/fake-pkg": patch
3+
---
4+
5+
fix: fix ts extension detect regex, close #775 #774 #772
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { AVAILABLE_EXTENSION_PATTERN, AVAILABLE_TS_EXTENSION_PATTERN } from '@swc-node/register/register.ts'
2+
import test from 'ava'
3+
import * as ts from 'typescript'
4+
5+
const tsExtensions = [ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Mts, ts.Extension.Cts]
6+
const nonTsExtensions = [ts.Extension.Js, ts.Extension.Jsx, ts.Extension.Mjs, ts.Extension.Cjs, '.es6', '.es']
7+
const defaultExtensions = [...tsExtensions, ...nonTsExtensions]
8+
const ignoreExtensions = ['.txt', '.json', '.xml']
9+
10+
test(`AVAILABLE_TS_EXTENSION_PATTERN matches TypeScript extensions`, (t) => {
11+
tsExtensions.forEach((ext) => {
12+
t.true(AVAILABLE_TS_EXTENSION_PATTERN.test(`file${ext}`))
13+
})
14+
})
15+
16+
test(`AVAILABLE_TS_EXTENSION_PATTERN does not match d.ts`, (t) => {
17+
tsExtensions.forEach((ext) => {
18+
t.false(AVAILABLE_TS_EXTENSION_PATTERN.test(`file.d${ext}`))
19+
})
20+
})
21+
22+
test(`AVAILABLE_TS_EXTENSION_PATTERN does not match non-ts extensions`, (t) => {
23+
;[...nonTsExtensions, ...ignoreExtensions].forEach((ext) => {
24+
t.false(AVAILABLE_TS_EXTENSION_PATTERN.test(`file${ext}`))
25+
})
26+
})
27+
28+
test(`AVAILABLE_EXTENSION_PATTERN matches default extensions`, (t) => {
29+
defaultExtensions.forEach((ext) => {
30+
t.true(AVAILABLE_EXTENSION_PATTERN.test(`file${ext}`))
31+
})
32+
})
33+
34+
test(`AVAILABLE_EXTENSION_PATTERN does not match non-default extensions`, (t) => {
35+
ignoreExtensions.forEach((ext) => {
36+
t.false(AVAILABLE_EXTENSION_PATTERN.test(`file${ext}`))
37+
})
38+
})

packages/integrate/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
"devDependencies": {
2222
"@swc/helpers": "^0.5.11",
2323
"@swc-node/core": "^1.13.0",
24+
"@swc-node/register": "workspace:*",
2425
"@types/jest": "^29.5.12",
2526
"@types/react": "^18.3.1",
2627
"@types/react-dom": "^18.3.0",
2728
"jest": "^29.7.0",
2829
"react": "^18.3.1",
2930
"react-dom": "^18.3.1",
30-
"sinon": "^17.0.1"
31+
"sinon": "^17.0.1",
32+
"typescript": "^5.4.5"
3133
}
3234
}

packages/integrate/tsconfig.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
"outDir": "./lib",
77
"jsx": "react-jsx",
88
"types": ["node", "jest"],
9+
"allowImportingTsExtensions": true
910
},
1011
"references": [
1112
{
12-
"path": "../core",
13+
"path": "../core"
1314
},
15+
{
16+
"path": "../register"
17+
}
1418
],
1519
"include": ["."],
1620
"files": ["./package.json"],
17-
"exclude": ["lib"],
21+
"exclude": ["lib"]
1822
}

packages/register/register.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ const DEFAULT_EXTENSIONS = [
1919
]
2020

2121
export const AVAILABLE_TS_EXTENSION_PATTERN = new RegExp(
22-
`(?<!\\.d(${[ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Mts, ts.Extension.Cts].map((ext) => ext.replace(/^\./, '\\.')).join('|')}))$`,
22+
`((?<!\\.d)(${[ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Mts, ts.Extension.Cts].map((ext) => ext.replace(/^\./, '\\.')).join('|')}))$`,
2323
'i',
2424
)
2525

2626
export const AVAILABLE_EXTENSION_PATTERN = new RegExp(
27-
`(?<!\\.d(${DEFAULT_EXTENSIONS.map((ext) => ext.replace(/^\./, '\\.')).join('|')}))$`,
27+
`((?<!\\.d)(${DEFAULT_EXTENSIONS.map((ext) => ext.replace(/^\./, '\\.')).join('|')}))$`,
2828
'i',
2929
)
3030

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)