Skip to content

Commit

Permalink
feat: use new data api (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
MengLinMaker authored Nov 9, 2024
1 parent 5e6a53d commit 6e6c38a
Show file tree
Hide file tree
Showing 19 changed files with 116 additions and 26,603 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ jobs:
with:
node-version: latest
cache: 'pnpm'
# config recommended by pnpm error
- run: pnpm config set store-dir "/home/runner/setup-pnpm/node_modules/.bin/store/v3" --global

- name: Build library
run: pnpm install && pnpm build && pnpm run version
- run: pnpm install
- run: pnpm build

- name: Create Release Pull Request or Publish to npm
id: changesets
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ jobs:
with:
node-version: latest
cache: 'pnpm'
# comfig recommended by pnpm error
# config recommended by pnpm error
- run: pnpm config set store-dir "/home/runner/setup-pnpm/node_modules/.bin/store/v3" --global

- run: pnpm install
- run: pnpm build
- run: pnpm lint
Expand Down
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The linter config should target the server bundle, not client.
npm install @menglinmaker/eslint-plugin-runtime-compat
```

2. Add `eslint.config.mjs` to root
2. Add `eslint.config.mjs` to root. This detects incompatible APIs for all runtimes in the dataset.
```Bash
import runtimeCompat from "@menglinmaker/eslint-plugin-runtime-compat";

Expand All @@ -38,10 +38,7 @@ export default [runtimeCompat.configs.strict];

Alternatively, you can load a custom config:
```Bash
export default [runtimeCompat.configs.custom(['node', 'bun', 'deno'], {
deprecated: true,
experimental: true,
})];
export default [runtimeCompat.configs.custom(['node', 'bun', 'deno'])];
```

## Limitations:
Expand Down
25 changes: 13 additions & 12 deletions packages/data/preprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ const mapCompatData = new Map<string, PreprocessCompatStatement>()
for (const key of keys) {
const subData = compatData[key]
if (key === '__compat') {
const finalCompatStatement = extractPreprocessCompatStatement(subData as never)
mapCompatData.set(JSON.stringify(parentKeys), finalCompatStatement)
const preprocessCompatStatement = extractPreprocessCompatStatement(subData as never)
mapCompatData.set(JSON.stringify(parentKeys), preprocessCompatStatement)
} else {
// Only chain keys if "__compat" exists
const nodeHasCompatData = !keys.includes('__compat')
Expand All @@ -73,37 +73,38 @@ const preprocessCompatData: PreprocessCompatData = {
}
{
const isPascalCase = (s: string | undefined) => s?.match(/^[A-Z]+.*/)
for (const [jsonKeys, finalCompatStatement] of mapCompatData.entries()) {

for (const [jsonKeys, preprocessCompatStatement] of mapCompatData.entries()) {
const keys = JSON.parse(jsonKeys) as string[]
if (keys.length === 1) {
if (isPascalCase(keys[0])) {
// PascalCase, hence a class
preprocessCompatData.class[jsonKeys] = finalCompatStatement
preprocessCompatData.class[jsonKeys] = preprocessCompatStatement
} else {
// camelCase, hence a variable or function
preprocessCompatData.global[jsonKeys] = finalCompatStatement
preprocessCompatData.global[jsonKeys] = preprocessCompatStatement
}
} else if (keys.length === 2) {
if (keys[0] === keys[1])
// Duplicate keys are class constructors
preprocessCompatData.class[JSON.stringify([keys[0]])] = finalCompatStatement
preprocessCompatData.class[JSON.stringify([keys[0]])] = preprocessCompatStatement
else if (keys[1]?.match('_static')) {
// Static methods have '_static'
const newKeys = JSON.stringify([keys[0], keys[1]?.replace('_static', '')])
if (isPascalCase(keys[0]))
preprocessCompatData.classProperty[newKeys] = finalCompatStatement
else preprocessCompatData.globalClassProperty[newKeys] = finalCompatStatement
preprocessCompatData.classProperty[newKeys] = preprocessCompatStatement
else preprocessCompatData.globalClassProperty[newKeys] = preprocessCompatStatement
} else if (keys[1]?.match('_event')) {
// Events have '_event'
const newKeys = JSON.stringify([keys[0], keys[1]?.replace('_event', '')])
preprocessCompatData.eventListener[newKeys] = finalCompatStatement
preprocessCompatData.eventListener[newKeys] = preprocessCompatStatement
} else if (!keys[1]?.match('_'))
// Normal class property
preprocessCompatData.classProperty[jsonKeys] = finalCompatStatement
else preprocessCompatData.misc[jsonKeys] = finalCompatStatement
preprocessCompatData.classProperty[jsonKeys] = preprocessCompatStatement
else preprocessCompatData.misc[jsonKeys] = preprocessCompatStatement
} else {
// Not sure how to analyse
preprocessCompatData.misc[JSON.stringify([keys[0]])] = finalCompatStatement
preprocessCompatData.misc[JSON.stringify([keys[0]])] = preprocessCompatStatement
}
}
}
Expand Down
55 changes: 0 additions & 55 deletions packages/data/src/filterSupportCompatData.ts

This file was deleted.

29 changes: 0 additions & 29 deletions packages/data/src/getUnsupportedRuntimes.ts

This file was deleted.

9 changes: 3 additions & 6 deletions packages/data/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import type { RuntimeName } from 'runtime-compat-data'
import data from 'runtime-compat-data'
import { filterSupportCompatData } from './filterSupportCompatData.js'
import { mapCompatData } from './mapCompatData.js'
import type { ParsedCompatData, RuleConfig } from './types.js'
import { filterPreprocessCompatData, preprocessCompatData } from './runtime'

export type { RuleConfig, ParsedCompatData, RuntimeName }
export { filterSupportCompatData, mapCompatData, data }
export type { RuntimeName }
export { filterPreprocessCompatData, preprocessCompatData }
46 changes: 0 additions & 46 deletions packages/data/src/mapCompatData.ts

This file was deleted.

32 changes: 30 additions & 2 deletions packages/data/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import _preprocessCompatData from './preprocessCompatData.json'

import type { RuntimeName } from 'runtime-compat-data'
import type { RuntimeName, StatusBlock } from 'runtime-compat-data'
import { objectKeys } from './objectKeys'
import type {
PreprocessCompatData,
Expand Down Expand Up @@ -40,6 +39,28 @@ const getUnsupportedRuntimes = (
return unsupportedRuntimes
}

/**
* Generates error message from parsed compat data.
* @param keys
* @param url - Doc url.
* @param status - status block of API.
* @param unsupported - Unsupported runtimes.
* @returns Error message.
*/
export const errorMessage = (
keys: string[],
url: string,
status: StatusBlock,
unsupported: RuntimeName[],
) => {
let apiStatus = status.standard_track ? 'standard' : ''
apiStatus = status.deprecated ? 'deprecated' : apiStatus
apiStatus = status.experimental ? 'experimental' : apiStatus

const docString = `Docs - ${url}`
return `[${keys}] - Unsupported ${apiStatus} API for [${unsupported}]\n${docString}`
}

/**
* Clean flat compat data object, retaining only unsupported runtimes.
* @param flatCompatData - Flat compat data object.
Expand Down Expand Up @@ -67,10 +88,17 @@ export const filterPreprocessCompatData = (
filterRuntimes,
)
if (unsupportedRuntimes.length > 0) {
const error = errorMessage(
JSON.parse(jsonKeys),
preprocessCompatStatement.url,
preprocessCompatStatement.status,
unsupportedRuntimes,
)
parsedCompatData[context].set(jsonKeys, {
url: preprocessCompatStatement.url,
status: preprocessCompatStatement.status,
unsupported: unsupportedRuntimes,
error,
})
}
}
Expand Down
89 changes: 0 additions & 89 deletions packages/data/src/tests/filterSupportCompatData.test.ts

This file was deleted.

Loading

0 comments on commit 6e6c38a

Please sign in to comment.