Skip to content

Commit 9267745

Browse files
committed
test: enable ts strict mode
1 parent 477dfe4 commit 9267745

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

src/transform.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export const MagicRegExpTransformPlugin = createUnplugin(() => {
2828
if (pathname.match(/\.((c|m)?j|t)sx?$/g)) {
2929
return true
3030
}
31+
32+
return false
3133
},
3234
transform(code, id) {
3335
if (!code.includes('magic-regexp')) return
@@ -36,7 +38,7 @@ export const MagicRegExpTransformPlugin = createUnplugin(() => {
3638
if (!statements.length) return
3739

3840
const contextMap: Context = { ...magicRegExp }
39-
const wrapperNames = []
41+
const wrapperNames: string[] = []
4042
let namespace: string
4143

4244
for (const i of statements.flatMap(i => parseStaticImport(i))) {
@@ -46,7 +48,7 @@ export const MagicRegExpTransformPlugin = createUnplugin(() => {
4648
}
4749
if (i.namedImports) {
4850
for (const key in i.namedImports) {
49-
contextMap[i.namedImports[key]] = magicRegExp[key]
51+
contextMap[i.namedImports[key]] = magicRegExp[key as keyof typeof magicRegExp]
5052
}
5153
if (i.namedImports.createRegExp) {
5254
wrapperNames.push(i.namedImports.createRegExp)
@@ -59,8 +61,9 @@ export const MagicRegExpTransformPlugin = createUnplugin(() => {
5961
const s = new MagicString(code)
6062

6163
walk(this.parse(code), {
62-
enter(node: SimpleCallExpression) {
63-
if (node.type !== 'CallExpression') return
64+
enter(_node) {
65+
if (_node.type !== 'CallExpression') return
66+
const node = _node as SimpleCallExpression
6467
if (
6568
// Normal call
6669
!wrapperNames.includes((node.callee as any).name) &&

test/index.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { createInput } from '../src/core/internal'
77
describe('magic-regexp', () => {
88
it('works as a normal regexp', () => {
99
const regExp = createRegExp('in', [global])
10-
expect('thing'.match(regExp)[0]).toMatchInlineSnapshot('"in"')
10+
expect('thing'.match(regExp)?.[0]).toMatchInlineSnapshot('"in"')
1111
expect(regExp.test('thing')).toBeTruthy()
1212
expect(regExp.lastIndex).toMatchInlineSnapshot('4')
1313
})
@@ -27,13 +27,13 @@ describe('inputs', () => {
2727
it('before', () => {
2828
const regExp = createRegExp(char.before('foo'))
2929
expect(regExp).toMatchInlineSnapshot('/\\.\\(\\?=foo\\)/')
30-
expect('bafoo'.match(regExp)[0]).toMatchInlineSnapshot('"a"')
30+
expect('bafoo'.match(regExp)?.[0]).toMatchInlineSnapshot('"a"')
3131
expect(regExp.test('foo')).toBeFalsy()
3232
})
3333
it('after', () => {
3434
const regExp = createRegExp(char.after('foo'))
3535
expect(regExp).toMatchInlineSnapshot('/\\(\\?<=foo\\)\\./')
36-
expect('fooafoo'.match(regExp)[0]).toMatchInlineSnapshot('"a"')
36+
expect('fooafoo'.match(regExp)?.[0]).toMatchInlineSnapshot('"a"')
3737
expect(regExp.test('foo')).toBeFalsy()
3838
})
3939
it('notBefore', () => {
@@ -59,21 +59,21 @@ describe('inputs', () => {
5959
it('capture groups', () => {
6060
const pattern = anyOf(anyOf('foo', 'bar').as('test'), exactly('baz').as('test2'))
6161

62-
expect('football'.match(createRegExp(pattern)).groups).toMatchInlineSnapshot(`
62+
expect('football'.match(createRegExp(pattern))?.groups).toMatchInlineSnapshot(`
6363
{
6464
"test": "foo",
6565
"test2": undefined,
6666
}
6767
`)
68-
expect('fobazzer'.match(createRegExp(pattern)).groups).toMatchInlineSnapshot(`
68+
expect('fobazzer'.match(createRegExp(pattern))?.groups).toMatchInlineSnapshot(`
6969
{
7070
"test": undefined,
7171
"test2": "baz",
7272
}
7373
`)
74-
expectTypeOf<Record<'test' | 'test2', string | undefined> | undefined>(
75-
'fobazzer'.match(createRegExp(pattern))?.groups
76-
)
74+
expectTypeOf('fobazzer'.match(createRegExp(pattern))?.groups).toMatchTypeOf<
75+
Record<'test' | 'test2', string | undefined> | undefined
76+
>()
7777
// @ts-expect-error
7878
'fobazzer'.match(createRegExp(pattern))?.groups.other
7979

@@ -84,7 +84,7 @@ describe('inputs', () => {
8484
"test2": "baz",
8585
}
8686
`)
87-
expectTypeOf<Record<'test' | 'test2', string | undefined>>(match.groups)
87+
expectTypeOf(match.groups).toMatchTypeOf<Record<'test' | 'test2', string | undefined>>()
8888
}
8989
})
9090
})

test/transform.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe('transformer', () => {
7878
})
7979

8080
const transform = (code: string | string[], id = 'some-id.js') => {
81-
const plugin = MagicRegExpTransformPlugin.vite()
81+
const plugin = MagicRegExpTransformPlugin.vite() as any
8282
return plugin.transform.call(
8383
{ parse: (code: string) => parse(code, { ecmaVersion: 2022, sourceType: 'module' }) },
8484
Array.isArray(code) ? code.join('\n') : code,

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"module": "ESNext",
55
"moduleResolution": "Node",
66
"esModuleInterop": true,
7+
"strict": true,
78
"skipLibCheck": true,
89
"paths": {
910
"magic-regexp/transform": ["./src/transform"],

0 commit comments

Comments
 (0)