Skip to content

Commit

Permalink
Let downlevel-dts do most of the heavy lifting
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiasBuelens committed Jan 4, 2024
1 parent a1d052e commit d9eef9f
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 61 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ typings/

### Project
/dist/
!/dist/types/ts3.6/polyfill.d.ts
/lib/
/temp/
/types/ponyfill.d.ts
/types/tsdoc-metadata.json
1 change: 1 addition & 0 deletions .idea/web-streams-polyfill.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions api-extractor.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
* SUPPORTED TOKENS: <projectFolder>, <packageName>, <unscopedPackageName>
* DEFAULT VALUE: "<projectFolder>/dist/<unscopedPackageName>.d.ts"
*/
"untrimmedFilePath": "<projectFolder>/types/ponyfill.d.ts",
"untrimmedFilePath": "<projectFolder>/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.
Expand Down Expand Up @@ -242,7 +242,7 @@
*
* DEFAULT VALUE: true
*/
// "enabled": true,
"enabled": true,
/**
* Specifies where the TSDoc metadata file should be written.
*
Expand All @@ -256,9 +256,8 @@
* SUPPORTED TOKENS: <projectFolder>, <packageName>, <unscopedPackageName>
* DEFAULT VALUE: "<lookup>"
*/
// "tsdocMetadataFilePath": "<projectFolder>/dist/tsdoc-metadata.json"
"tsdocMetadataFilePath": "<projectFolder>/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.
Expand Down
53 changes: 4 additions & 49 deletions build/downlevel-dts.js
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down
File renamed without changes.
95 changes: 95 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion test/types/polyfill.ts
Original file line number Diff line number Diff line change
@@ -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<Uint8Array>({
// TODO Figure out a way to augment the type of "declare var ReadableStream"?
Expand Down

0 comments on commit d9eef9f

Please sign in to comment.