Skip to content

Commit

Permalink
feat: make inheritance fix more robust, improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
isaac-mason committed Sep 22, 2024
1 parent 5865bbb commit 93dfbdd
Show file tree
Hide file tree
Showing 6 changed files with 1,751 additions and 4,243 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
- name: Checkout Repo
uses: actions/checkout@v2

- name: Setup Node.js 20.x
- name: Setup Node.js 22.x
uses: actions/setup-node@v2
with:
node-version: 20.x
node-version: 22.x

- name: Install Dependencies
run: yarn
Expand Down
8 changes: 4 additions & 4 deletions packages/webidl-dts-gen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"lint": "tsc --noEmit",
"start": "yarn build && ./dist/cli.js",
"format": "yarn prettier --write .",
"test": "jest --coverage",
"test:watch": "jest --watch"
"test": "vitest test --coverage",
"test:watch": "vitest --watch"
},
"repository": {
"type": "git",
Expand All @@ -33,10 +33,10 @@
"@types/yargs": "^17.0.32",
"@typescript-eslint/eslint-plugin": "^7.10.0",
"@typescript-eslint/parser": "^7.2.0",
"@vitest/coverage-v8": "^2.1.1",
"eslint": "^8.57.0",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"ts-jest": "^29.2.4"
"vitest": "^2.1.1"
},
"dependencies": {
"jsdom": "^24.0.0",
Expand Down
35 changes: 24 additions & 11 deletions packages/webidl-dts-gen/src/fixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,32 @@ export const fixes = {
// Handle inheritance
const inheritance = []

idlString = idlString.replace(
/(\/\*[\s\S]*?\*\/|\/\/.*?$)|([a-zA-Z0-9]+) implements ([a-zA-Z0-9]+);/gi,
(line, comment, left, right) => {
if (comment) {
return line
}
// remove comments
const withoutComments = idlString.replace(/(\/\*[\s\S]*?\*\/|\/\/.*?$)/gm, '')

inheritance.push({ left, right })
return `// ${line}`
},
)
const lines = withoutComments.split('\n')

// Update interfaces with inheritance
// find lines with inheritance statements, comment them out and store them
for (let i = 0; i < lines.length; i++) {
const line = lines[i]

const match = /([a-zA-Z0-9]+) implements ([a-zA-Z0-9]+);/gi.exec(line)

if (!match) {
continue
}

const left = match[1]
const right = match[2]

inheritance.push({ left, right })

lines[i] = `// ${line}`
}

idlString = lines.join('\n')

// correct inheritance syntax
inheritance.forEach(({ left, right }) => {
idlString = idlString.replace(new RegExp(`interface ${left} {`), `interface ${left}: ${right} {`)
})
Expand Down
Loading

0 comments on commit 93dfbdd

Please sign in to comment.