Skip to content

Commit

Permalink
fix: fix ts extention detect regex, close swc-project#775 swc-project…
Browse files Browse the repository at this point in the history
  • Loading branch information
yeliex committed May 27, 2024
1 parent c78ca5e commit 4d52f20
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
38 changes: 38 additions & 0 deletions packages/integrate/__tests__/extention/extention.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { AVAILABLE_EXTENSION_PATTERN, AVAILABLE_TS_EXTENSION_PATTERN } from '@swc-node/register/register.ts'
import test from 'ava'
import * as ts from 'typescript'

const tsExtensions = [ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Mts, ts.Extension.Cts]
const nonTsExtensions = [ts.Extension.Js, ts.Extension.Jsx, ts.Extension.Mjs, ts.Extension.Cjs, '.es6', '.es']
const defaultExtensions = [...tsExtensions, ...nonTsExtensions]
const ignoreExtensions = ['.txt', '.json', '.xml']

test(`AVAILABLE_TS_EXTENSION_PATTERN matches TypeScript extensions`, (t) => {
tsExtensions.forEach((ext) => {
t.true(AVAILABLE_TS_EXTENSION_PATTERN.test(`file${ext}`))
})
})

test(`AVAILABLE_TS_EXTENSION_PATTERN does not match d.ts`, (t) => {
tsExtensions.forEach((ext) => {
t.false(AVAILABLE_TS_EXTENSION_PATTERN.test(`file.d${ext}`))
})
})

test(`AVAILABLE_TS_EXTENSION_PATTERN does not match non-ts extensions`, (t) => {
;[...nonTsExtensions, ...ignoreExtensions].forEach((ext) => {
t.false(AVAILABLE_TS_EXTENSION_PATTERN.test(`file${ext}`))
})
})

test(`AVAILABLE_EXTENSION_PATTERN matches default extensions`, (t) => {
defaultExtensions.forEach((ext) => {
t.true(AVAILABLE_EXTENSION_PATTERN.test(`file${ext}`))
})
})

test(`AVAILABLE_EXTENSION_PATTERN does not match non-default extensions`, (t) => {
ignoreExtensions.forEach((ext) => {
t.false(AVAILABLE_EXTENSION_PATTERN.test(`file${ext}`))
})
})
4 changes: 3 additions & 1 deletion packages/integrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
"devDependencies": {
"@swc/helpers": "^0.5.11",
"@swc-node/core": "^1.13.0",
"@swc-node/register": "workspace:*",
"@types/jest": "^29.5.12",
"@types/react": "^18.3.1",
"@types/react-dom": "^18.3.0",
"jest": "^29.7.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"sinon": "^17.0.1"
"sinon": "^17.0.1",
"typescript": "^5.4.5"
}
}
8 changes: 6 additions & 2 deletions packages/integrate/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
"outDir": "./lib",
"jsx": "react-jsx",
"types": ["node", "jest"],
"allowImportingTsExtensions": true
},
"references": [
{
"path": "../core",
"path": "../core"
},
{
"path": "../register"
}
],
"include": ["."],
"files": ["./package.json"],
"exclude": ["lib"],
"exclude": ["lib"]
}
4 changes: 2 additions & 2 deletions packages/register/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ const DEFAULT_EXTENSIONS = [
]

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

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

Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4d52f20

Please sign in to comment.