Skip to content

Commit 359eabf

Browse files
authored
test: add more comprehensive test to type detection (#57)
1 parent 5e41e33 commit 359eabf

File tree

16 files changed

+272
-95
lines changed

16 files changed

+272
-95
lines changed

.github/workflows/CI.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
name: CI
22
on:
3-
push:
4-
branches: [main]
53
pull_request:
64
branches:
75
- '**'

biome.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"recommended": true,
1212
"complexity": {
1313
"useLiteralKeys": "off"
14+
},
15+
"style": {
16+
"noNonNullAssertion": "off"
1417
}
1518
}
1619
},

packages/data/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,11 @@
1414
},
1515
"dependencies": {
1616
"runtime-compat-data": "0.0.5"
17+
},
18+
"peerDependencies": {
19+
"@cloudflare/workers-types": "*",
20+
"@types/bun": "*",
21+
"@types/deno": "*",
22+
"@types/node": "*"
1723
}
1824
}

packages/data/preprocess.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { writeFileSync } from 'node:fs'
22
import type { CompatStatement, Identifier, StatusBlock } from 'runtime-compat-data'
33
import rawCompatData from 'runtime-compat-data'
44
import type { PreprocessCompatData, PreprocessCompatStatement } from './src/types'
5+
import { parseJsonKeys, stringifyJsonKeys } from './src/utils'
56

67
/**
78
* Compress raw compat data to single level flatmap
@@ -48,7 +49,7 @@ const mapCompatData = new Map<string, PreprocessCompatStatement>()
4849
const subData = compatData[key]
4950
if (key === '__compat') {
5051
const preprocessCompatStatement = extractPreprocessCompatStatement(subData as never)
51-
mapCompatData.set(JSON.stringify(parentKeys), preprocessCompatStatement)
52+
mapCompatData.set(stringifyJsonKeys(parentKeys), preprocessCompatStatement)
5253
} else {
5354
// Only chain keys if "__compat" exists
5455
const nodeHasCompatData = !keys.includes('__compat')
@@ -61,7 +62,7 @@ const mapCompatData = new Map<string, PreprocessCompatStatement>()
6162
}
6263

6364
/**
64-
* Sort mapped compat data into different AST detection scenarios
65+
* Sort mapped compat data into different AST detection apiContext
6566
*/
6667
const preprocessCompatData: PreprocessCompatData = {
6768
class: {},
@@ -75,7 +76,7 @@ const preprocessCompatData: PreprocessCompatData = {
7576
const isPascalCase = (s: string | undefined) => s?.match(/^[A-Z]+.*/)
7677

7778
for (const [jsonKeys, preprocessCompatStatement] of mapCompatData.entries()) {
78-
const keys = JSON.parse(jsonKeys) as string[]
79+
const keys = parseJsonKeys(jsonKeys)
7980
if (keys.length === 1) {
8081
if (isPascalCase(keys[0])) {
8182
// PascalCase, hence a class
@@ -87,24 +88,24 @@ const preprocessCompatData: PreprocessCompatData = {
8788
} else if (keys.length === 2) {
8889
if (keys[0] === keys[1])
8990
// Duplicate keys are class constructors
90-
preprocessCompatData.class[JSON.stringify([keys[0]])] = preprocessCompatStatement
91+
preprocessCompatData.class[stringifyJsonKeys([keys[0]!])] = preprocessCompatStatement
9192
else if (keys[1]?.match('_static')) {
9293
// Static methods have '_static'
93-
const newKeys = JSON.stringify([keys[0], keys[1]?.replace('_static', '')])
94+
const newKeys = stringifyJsonKeys([keys[0]!, keys[1]?.replace('_static', '')])
9495
if (isPascalCase(keys[0]))
9596
preprocessCompatData.classProperty[newKeys] = preprocessCompatStatement
9697
else preprocessCompatData.globalClassProperty[newKeys] = preprocessCompatStatement
9798
} else if (keys[1]?.match('_event')) {
9899
// Events have '_event'
99-
const newKeys = JSON.stringify([keys[0], keys[1]?.replace('_event', '')])
100+
const newKeys = stringifyJsonKeys([keys[0]!, keys[1]?.replace('_event', '')])
100101
preprocessCompatData.eventListener[newKeys] = preprocessCompatStatement
101102
} else if (!keys[1]?.match('_'))
102103
// Normal class property
103104
preprocessCompatData.classProperty[jsonKeys] = preprocessCompatStatement
104105
else preprocessCompatData.misc[jsonKeys] = preprocessCompatStatement
105106
} else {
106107
// Not sure how to analyse
107-
preprocessCompatData.misc[JSON.stringify([keys[0]])] = preprocessCompatStatement
108+
preprocessCompatData.misc[stringifyJsonKeys([keys[0]!])] = preprocessCompatStatement
108109
}
109110
}
110111
}

packages/data/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import type { RuntimeName } from 'runtime-compat-data'
22
import { filterPreprocessCompatData, preprocessCompatData } from './runtime'
3+
import { objectKeys, parseJsonKeys, stringifyJsonKeys } from './utils'
34

45
export type { RuntimeName }
5-
export { filterPreprocessCompatData, preprocessCompatData }
6+
export {
7+
filterPreprocessCompatData,
8+
preprocessCompatData,
9+
objectKeys,
10+
parseJsonKeys,
11+
stringifyJsonKeys,
12+
}

packages/data/src/objectKeys.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/data/src/runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { RuntimeName, StatusBlock } from 'runtime-compat-data'
2-
import { objectKeys } from './objectKeys'
32
import _preprocessCompatData from './preprocessCompatData.json'
43
import type {
54
PreprocessCompatData,
65
PreprocessCompatStatement,
76
RuntimeCompatData,
87
RuntimeCompatStatement,
98
} from './types.js'
9+
import { objectKeys } from './utils'
1010

1111
const preprocessCompatData: PreprocessCompatData = _preprocessCompatData
1212
export { preprocessCompatData }

packages/data/src/tests/runtime.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { RuntimeName } from 'runtime-compat-data'
22
import { describe, expect, it } from 'vitest'
3-
import { objectKeys } from '../objectKeys'
43
import { filterPreprocessCompatData, preprocessCompatData } from '../runtime'
4+
import { objectKeys } from '../utils'
55

66
describe('filterPreprocessCompatData', () => {
77
const filterRuntimes: RuntimeName[] = ['node']

packages/data/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ type ApiClassification =
88
| 'globalClassProperty'
99
| 'misc'
1010

11+
export type JsonKeys = string
12+
1113
/**
1214
* Types for preprocessing
1315
*/

packages/data/src/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { JsonKeys } from './types'
2+
3+
/**
4+
* Extracts the keys of an object to array with correct TypeScript.
5+
* @param object - The object.
6+
* @return Object key array.
7+
*/
8+
export const objectKeys = <T extends object>(object: T) => Object.keys(object) as (keyof T)[]
9+
10+
/**
11+
* Parse stringified JSON string array.
12+
* @param jsonKeys
13+
* @returns
14+
*/
15+
export const parseJsonKeys = (jsonKeys: JsonKeys) => JSON.parse(jsonKeys) as string[]
16+
17+
/**
18+
* Stringify JSON string array.
19+
* @param stringArray
20+
* @returns
21+
*/
22+
export const stringifyJsonKeys = (stringArray: string[]) => JSON.stringify(stringArray) as JsonKeys

0 commit comments

Comments
 (0)