Skip to content

Commit

Permalink
feat: add filterPreprocessCompatData function for external runtime use
Browse files Browse the repository at this point in the history
  • Loading branch information
MengLinMaker committed Nov 9, 2024
1 parent f3d70b2 commit 8121723
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 2 deletions.
80 changes: 80 additions & 0 deletions packages/data/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import _preprocessCompatData from './preprocessCompatData.json'

import type { RuntimeName } from 'runtime-compat-data'
import { objectKeys } from './objectKeys'
import type {
PreprocessCompatData,
PreprocessCompatStatement,
RuntimeCompatData,
RuntimeCompatStatement,
} from './types.js'

const preprocessCompatData: PreprocessCompatData = _preprocessCompatData
export { preprocessCompatData }

/**
* Extract unsupported runtimes based of filter.
* @param preprocessCompatStatement
* @param filterRuntimes - Runtimes to filter for lack of support detection.
* @returns Array of unsupported runtimes.
*/
const getUnsupportedRuntimes = (
preprocessCompatStatement: PreprocessCompatStatement,
filterRuntimes: RuntimeName[],
) => {
const unsupportedRuntimes: RuntimeName[] = []

for (const filterRuntime of filterRuntimes) {
const support = preprocessCompatStatement.support[filterRuntime]
if (support === undefined) {
// Runtime not found, therefore unsupported.
unsupportedRuntimes.push(filterRuntime)
} else if (Array.isArray(support)) {
// Array format not supported by runtime-compat-data, therefore unsupported.
unsupportedRuntimes.push(filterRuntime)
} else if (support.version_added === false) {
// Only boolean is supported in runtime-compat-data.
unsupportedRuntimes.push(filterRuntime)
}
}
return unsupportedRuntimes
}

/**
* Clean flat compat data object, retaining only unsupported runtimes.
* @param flatCompatData - Flat compat data object.
* @param filterRuntimes - Runtimes to filter for lack of support detection.
* @returns Parsed unsupported runtime data.
*/
export const filterPreprocessCompatData = (
preprocessCompatData: PreprocessCompatData,
filterRuntimes: RuntimeName[],
) => {
const parsedCompatData: RuntimeCompatData = {
class: new Map<string, RuntimeCompatStatement>(),
classProperty: new Map<string, RuntimeCompatStatement>(),
eventListener: new Map<string, RuntimeCompatStatement>(),
global: new Map<string, RuntimeCompatStatement>(),
globalClassProperty: new Map<string, RuntimeCompatStatement>(),
misc: new Map<string, RuntimeCompatStatement>(),
}
for (const context of objectKeys(preprocessCompatData)) {
for (const jsonKeys of objectKeys(preprocessCompatData[context])) {
const preprocessCompatStatement = preprocessCompatData[context][jsonKeys]
if (preprocessCompatStatement) {
const unsupportedRuntimes = getUnsupportedRuntimes(
preprocessCompatStatement,
filterRuntimes,
)
if (unsupportedRuntimes.length > 0) {
parsedCompatData[context].set(jsonKeys, {
url: preprocessCompatStatement.url,
status: preprocessCompatStatement.status,
unsupported: unsupportedRuntimes,
})
}
}
}
}
return parsedCompatData
}
19 changes: 19 additions & 0 deletions packages/data/src/tests/runtime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { RuntimeName } from 'runtime-compat-data'
import { describe, expect, it } from 'vitest'
import { objectKeys } from '../objectKeys'
import { filterPreprocessCompatData, preprocessCompatData } from '../runtime'

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

it('should successfully filter for ', () => {
const runtimeCompatData = filterPreprocessCompatData(preprocessCompatData, filterRuntimes)
for (const context of objectKeys(runtimeCompatData)) {
for (const [jsonKeys, runtimeCompatStatement] of runtimeCompatData[context].entries()) {
const keys = JSON.parse(jsonKeys) as string[]
expect(keys.length).toBeGreaterThan(0)
expect(runtimeCompatStatement.unsupported).toStrictEqual(filterRuntimes)
}
}
})
})
42 changes: 40 additions & 2 deletions packages/data/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
import type { CompatStatement, RuntimeName } from 'runtime-compat-data'
import type {
CompatStatement,
RuntimeName,
StatusBlock,
SupportStatement,
} from 'runtime-compat-data'

/**
* Types for preprocessing
*/
export type PreprocessCompatStatement = {
url: string
status: StatusBlock
support: Partial<Record<RuntimeName, SupportStatement>>
}
type ApiClassification =
| 'class'
| 'classProperty'
| 'eventListener'
| 'global'
| 'globalClassProperty'
| 'misc'
export type PreprocessCompatData = Record<
ApiClassification,
Record<string, PreprocessCompatStatement>
>

/**
* Types for runtime
*/

export type RuntimeCompatStatement = {
url: string
status: StatusBlock
unsupported: RuntimeName[]
}
export type RuntimeCompatData = Record<ApiClassification, Map<string, RuntimeCompatStatement>>

/**
* Anything below is legacy
*/
export type RuleConfig = {
deprecated: boolean
experimental: boolean
}

interface NeoCompatStatement extends CompatStatement {
url?: string
}
Expand Down

0 comments on commit 8121723

Please sign in to comment.