Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix ts extension detect regex, close #775 #774 #772 #777

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-singers-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fake-scope/fake-pkg": patch
---

fix: fix ts extension detect regex, close #775 #774 #772
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.

Loading