From d9eef9f73a96f51f1ad64c74f0d32390cc8d7648 Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Thu, 4 Jan 2024 18:02:59 +0100 Subject: [PATCH] Let downlevel-dts do most of the heavy lifting --- .gitignore | 3 +- .idea/web-streams-polyfill.iml | 1 + api-extractor.json | 7 +- build/downlevel-dts.js | 53 +------------ {types => dist/types/ts3.6}/polyfill.d.ts | 0 package-lock.json | 95 +++++++++++++++++++++++ package.json | 11 +-- test/types/polyfill.ts | 2 +- 8 files changed, 111 insertions(+), 61 deletions(-) rename {types => dist/types/ts3.6}/polyfill.d.ts (100%) diff --git a/.gitignore b/.gitignore index a4537c4..e6077b0 100644 --- a/.gitignore +++ b/.gitignore @@ -137,7 +137,6 @@ typings/ ### Project /dist/ +!/dist/types/ts3.6/polyfill.d.ts /lib/ /temp/ -/types/ponyfill.d.ts -/types/tsdoc-metadata.json diff --git a/.idea/web-streams-polyfill.iml b/.idea/web-streams-polyfill.iml index 9e9e597..c9aa0ae 100644 --- a/.idea/web-streams-polyfill.iml +++ b/.idea/web-streams-polyfill.iml @@ -9,6 +9,7 @@ + diff --git a/api-extractor.json b/api-extractor.json index edaf59a..44c283d 100644 --- a/api-extractor.json +++ b/api-extractor.json @@ -195,7 +195,7 @@ * SUPPORTED TOKENS: , , * DEFAULT VALUE: "/dist/.d.ts" */ - "untrimmedFilePath": "/types/ponyfill.d.ts", + "untrimmedFilePath": "/dist/types/ts3.6/ponyfill.d.ts", /** * Specifies the output path for a .d.ts rollup file to be generated with trimming for a "beta" release. @@ -242,7 +242,7 @@ * * DEFAULT VALUE: true */ - // "enabled": true, + "enabled": true, /** * Specifies where the TSDoc metadata file should be written. * @@ -256,9 +256,8 @@ * SUPPORTED TOKENS: , , * DEFAULT VALUE: "" */ - // "tsdocMetadataFilePath": "/dist/tsdoc-metadata.json" + "tsdocMetadataFilePath": "/dist/types/tsdoc-metadata.json" }, - /** * Specifies what type of newlines API Extractor should use when writing output files. By default, the output files * will be written with Windows-style newlines. To use POSIX-style newlines, specify "lf" instead. diff --git a/build/downlevel-dts.js b/build/downlevel-dts.js index fa5c264..cb0f936 100644 --- a/build/downlevel-dts.js +++ b/build/downlevel-dts.js @@ -1,64 +1,19 @@ // Based on downlevel-dts (MIT licensed) by Nathan Shively-Sanders // https://github.com/sandersn/downlevel-dts/blob/e7d1cb5aced5686826fe8aac4d4af2f745a9ef60/index.js -const { Project, ts } = require('ts-morph'); +const { Project } = require('ts-morph'); const path = require('path'); const project = new Project(); -const inputDir = project.addDirectoryAtPath(path.join(__dirname, '../types/')); -const outputDir = project.createDirectory(path.join(__dirname, '../dist/types/')); - -// Create output directory -const ts36Dir = outputDir.createDirectory('ts3.6'); -project.saveSync(); +const inputDir = project.addDirectoryAtPath(path.join(__dirname, '../dist/types/')); // Down-level all *.d.ts files in input directory const files = inputDir.addSourceFilesAtPaths('*.d.ts'); -for (const originalFile of files) { - // Create copy for TypeScript 3.6 and higher - originalFile.copyToDirectory(ts36Dir, { overwrite: true }); - // Downlevel and create copy for TypeScript 3.6 and lower - const downlevelFile = originalFile.copyToDirectory(outputDir, { overwrite: true }); - downlevelTS36(downlevelFile); - downlevelTS34(downlevelFile); +for (const file of files) { + downlevelTS34(file); } project.saveSync(); -/** - * Down-level TypeScript 3.6 types in the given source file - */ -function downlevelTS36(f) { - // Replace get/set accessors with (read-only) properties - const gs = f.getDescendantsOfKind(ts.SyntaxKind.GetAccessor); - for (const g of gs) { - const s = g.getSetAccessor(); - const returnTypeNode = g.getReturnTypeNode(); - const returnType = returnTypeNode ? returnTypeNode.getText() : 'any'; - g.getParent().insertProperty(g.getChildIndex(), Object.assign({}, g.getStructure(), { - type: returnType, - isReadonly: !s - })); - g.remove(); - if (s) { - s.remove(); - } - } - const ss = f.getDescendantsOfKind(ts.SyntaxKind.SetAccessor); - for (const s of ss) { - const g = s.getGetAccessor(); - if (!g) { - const firstParam = s.getParameters()[0]; - const firstParamTypeNode = firstParam && firstParam.getTypeNode(); - const firstParamType = firstParamTypeNode ? firstParamTypeNode.getText() : 'any'; - s.getParent().insertProperty(s.getChildIndex(), Object.assign({}, s.getStructure(), { - type: firstParamType, - isReadonly: false - })); - s.remove(); - } - } -} - /** * Down-level TypeScript 3.4 types in the given source file */ diff --git a/types/polyfill.d.ts b/dist/types/ts3.6/polyfill.d.ts similarity index 100% rename from types/polyfill.d.ts rename to dist/types/ts3.6/polyfill.d.ts diff --git a/package-lock.json b/package-lock.json index fe11658..c8b2575 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,6 +19,7 @@ "@typescript-eslint/eslint-plugin": "^6.17.0", "@typescript-eslint/parser": "^6.17.0", "@ungap/promise-all-settled": "^1.1.2", + "downlevel-dts": "^0.11.0", "eslint": "^8.56.0", "jasmine": "^5.1.0", "micromatch": "^4.0.5", @@ -1339,6 +1340,20 @@ "node": ">=12" } }, + "node_modules/downlevel-dts": { + "version": "0.11.0", + "resolved": "https://registry.npmjs.org/downlevel-dts/-/downlevel-dts-0.11.0.tgz", + "integrity": "sha512-vo835pntK7kzYStk7xUHDifiYJvXxVhUapt85uk2AI94gUUAQX9HNRtrcMHNSc3YHJUEHGbYIGsM99uIbgAtxw==", + "dev": true, + "dependencies": { + "semver": "^7.3.2", + "shelljs": "^0.8.3", + "typescript": "next" + }, + "bin": { + "downlevel-dts": "index.js" + } + }, "node_modules/eastasianwidth": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", @@ -2009,6 +2024,15 @@ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", "dev": true }, + "node_modules/interpret": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.4.0.tgz", + "integrity": "sha512-agE4QfB2Lkp9uICn7BAqoscw4SZP9kTE2hxiFI3jBPmXJfdqiahTbUuKGsMoN2GtqL9AxhYioAcVvgsb1HvRbA==", + "dev": true, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/is-core-module": { "version": "2.13.1", "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.13.1.tgz", @@ -2688,6 +2712,18 @@ "node": ">= 6" } }, + "node_modules/rechoir": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.6.2.tgz", + "integrity": "sha512-HFM8rkZ+i3zrV+4LQjwQ0W+ez98pApMGM3HUrN04j3CqzPOzl9nmP15Y8YXNm8QHGv/eacOVEjqhmWpkRV0NAw==", + "dev": true, + "dependencies": { + "resolve": "^1.1.6" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/recursive-readdir": { "version": "2.2.3", "resolved": "https://registry.npmjs.org/recursive-readdir/-/recursive-readdir-2.2.3.tgz", @@ -2989,6 +3025,65 @@ "node": ">=8" } }, + "node_modules/shelljs": { + "version": "0.8.5", + "resolved": "https://registry.npmjs.org/shelljs/-/shelljs-0.8.5.tgz", + "integrity": "sha512-TiwcRcrkhHvbrZbnRcFYMLl30Dfov3HKqzp5tO5b4pt6G/SezKcYhmDg15zXVBswHmctSAQKznqNW2LO5tTDow==", + "dev": true, + "dependencies": { + "glob": "^7.0.0", + "interpret": "^1.0.0", + "rechoir": "^0.6.2" + }, + "bin": { + "shjs": "bin/shjs" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/shelljs/node_modules/brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "dependencies": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "node_modules/shelljs/node_modules/glob": { + "version": "7.2.3", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", + "integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==", + "dev": true, + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.1.1", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + }, + "engines": { + "node": "*" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/shelljs/node_modules/minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dev": true, + "dependencies": { + "brace-expansion": "^1.1.7" + }, + "engines": { + "node": "*" + } + }, "node_modules/signal-exit": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-4.1.0.tgz", diff --git a/package.json b/package.json index f48a13c..2532b23 100644 --- a/package.json +++ b/package.json @@ -24,16 +24,16 @@ "lint": "eslint \"src/**/*.ts\"", "build": "npm run build:bundle && npm run build:types", "build:bundle": "rollup -c", - "build:types": "tsc --project . --emitDeclarationOnly --declarationDir ./lib && api-extractor run && node ./build/downlevel-dts.js", - "accept:types": "tsc --project . --emitDeclarationOnly --declarationDir ./lib && api-extractor run --local && node ./build/downlevel-dts.js", + "build:types": "tsc --project . --emitDeclarationOnly --declarationDir ./lib && api-extractor run", + "accept:types": "npm run build:types -- --local", + "postbuild:types": "downlevel-dts ./dist/types/ts3.6/ ./dist/types/ --to=3.5 && node ./build/downlevel-dts.js", "prepare": "npm run build" }, "files": [ "dist", "es6", "es2018", - "ponyfill", - "types" + "ponyfill" ], "engines": { "node": ">= 8" @@ -61,12 +61,13 @@ "@rollup/plugin-inject": "^5.0.5", "@rollup/plugin-replace": "^5.0.5", "@rollup/plugin-strip": "^3.0.4", - "@rollup/plugin-typescript": "^11.1.5", "@rollup/plugin-terser": "^0.4.4", + "@rollup/plugin-typescript": "^11.1.5", "@types/node": "^18.19.4", "@typescript-eslint/eslint-plugin": "^6.17.0", "@typescript-eslint/parser": "^6.17.0", "@ungap/promise-all-settled": "^1.1.2", + "downlevel-dts": "^0.11.0", "eslint": "^8.56.0", "jasmine": "^5.1.0", "micromatch": "^4.0.5", diff --git a/test/types/polyfill.ts b/test/types/polyfill.ts index 853ce25..1b600bc 100644 --- a/test/types/polyfill.ts +++ b/test/types/polyfill.ts @@ -1,7 +1,7 @@ /* * This test verifies that the polyfill's type definitions correctly augment TypeScript's built-in DOM types. */ -import '../../types/polyfill'; +import '../../dist/types/ts3.6/polyfill'; const readable = new ReadableStream({ // TODO Figure out a way to augment the type of "declare var ReadableStream"?