Skip to content

Commit

Permalink
Dropped support TypeScript < 5.0
Browse files Browse the repository at this point in the history
Fixes #238
  • Loading branch information
timocov committed Mar 28, 2023
1 parent fb59897 commit 6b3b15b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
with:
node-version: 14
- run: npm install
- run: npm install typescript@4.5.2
- run: npm install typescript@5.0.2
- run: npm run tsc

ts-current:
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@
},
"homepage": "https://github.com/timocov/dts-bundle-generator",
"dependencies": {
"typescript": ">=4.5.2",
"typescript": ">=5.0.2",
"yargs": "^17.6.0"
},
"devDependencies": {
"@types/mocha": "~10.0.0",
"@types/node": "~14.18.26",
"@types/yargs": "~17.0.13",
"@typescript-eslint/eslint-plugin": "~5.44.0",
"@typescript-eslint/parser": "~5.44.0",
"eslint": "~8.28.0",
"eslint-plugin-deprecation": "~1.3.2",
"eslint-plugin-import": "~2.26.0",
"@typescript-eslint/eslint-plugin": "~5.57.0",
"@typescript-eslint/parser": "~5.57.0",
"eslint": "~8.36.0",
"eslint-plugin-deprecation": "~1.4.0-next.1",
"eslint-plugin-import": "~2.27.5",
"eslint-plugin-prefer-arrow": "~1.2.1",
"eslint-plugin-unicorn": "~45.0.0",
"mocha": "~10.1.0",
"eslint-plugin-unicorn": "~46.0.0",
"mocha": "~10.2.0",
"npm-run-all": "~4.1.5",
"rimraf": "~3.0.2",
"ts-compiler": "npm:typescript@4.9.3",
"rimraf": "~4.4.0",
"ts-compiler": "npm:typescript@5.0.2",
"ts-node": "~10.9.1"
},
"license": "MIT",
Expand Down
8 changes: 4 additions & 4 deletions src/compile-dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ export function compileDts(rootFiles: readonly string[], preferredConfigPath?: s
host.realpath = (p: string) => p;
}

host.resolveModuleNames = (moduleNames: string[], containingFile: string) => {
return moduleNames.map((moduleName: string) => {
const resolvedModule = ts.resolveModuleName(moduleName, containingFile, compilerOptions, host).resolvedModule;
host.resolveModuleNameLiterals = (moduleLiterals: readonly ts.StringLiteralLike[], containingFile: string): ts.ResolvedModuleWithFailedLookupLocations[] => {
return moduleLiterals.map((moduleLiteral: ts.StringLiteralLike): ts.ResolvedModuleWithFailedLookupLocations => {
const resolvedModule = ts.resolveModuleName(moduleLiteral.text, containingFile, compilerOptions, host).resolvedModule;
if (resolvedModule && !resolvedModule.isExternalLibraryImport && resolvedModule.extension !== ts.Extension.Dts) {
resolvedModule.extension = ts.Extension.Dts;

Expand All @@ -49,7 +49,7 @@ export function compileDts(rootFiles: readonly string[], preferredConfigPath?: s
resolvedModule.resolvedFileName = changeExtensionToDts(resolvedModule.resolvedFileName);
}

return resolvedModule as ts.ResolvedModule;
return { resolvedModule };
});
};

Expand Down
86 changes: 15 additions & 71 deletions src/helpers/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export function isNodeNamedDeclaration(node: ts.Node): node is ts.NamedDeclarati

export function hasNodeModifier(node: ts.Node, modifier: ts.SyntaxKind): boolean {
const modifiers = getModifiers(node);
return Boolean(modifiers && modifiers.some((nodeModifier: NonNullable<ts.Node['modifiers']>[number]) => nodeModifier.kind === modifier));
return Boolean(modifiers && modifiers.some((nodeModifier: ts.Modifier) => nodeModifier.kind === modifier));
}

export function getNodeName(node: ts.Node): NodeName | undefined {
const nodeName = (node as unknown as ts.NamedDeclaration).name;
if (nodeName === undefined) {
const modifiers = getModifiers(node);
const defaultModifier = modifiers?.find((mod: NonNullable<ts.Node['modifiers']>[number]) => mod.kind === ts.SyntaxKind.DefaultKeyword);
const defaultModifier = modifiers?.find((mod: ts.Modifier) => mod.kind === ts.SyntaxKind.DefaultKeyword);
if (defaultModifier !== undefined) {
return defaultModifier as NodeName;
}
Expand Down Expand Up @@ -326,24 +326,6 @@ export function recreateRootLevelNodeWithModifiers(node: ts.Node, modifiersMap:
return newNode;
}

function prependEmptyDecoratorsIfNeeded<
// eslint-disable-next-line space-before-function-paren, @typescript-eslint/no-shadow
T extends (decorators: readonly ts.Decorator[] | undefined, ...args: A) => R,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
A extends any[],
R extends ts.Node,
>(func: T, ...args: A): R {
const tsVersion = parseFloat(ts.versionMajorMinor);
if (tsVersion < 4.8) {
// decorators don't exist in the declaration files anyway so we can ignore them
return func([], ...args);
} else {
// just pass args as is in
// eslint-disable-next-line prefer-spread
return func.apply(null, args as unknown as Parameters<typeof func>);
}
}

// eslint-disable-next-line complexity
function recreateRootLevelNodeWithModifiersImpl(node: ts.Node, modifiersMap: ModifiersMap): ts.Node {
const modifiers = modifiersMapToArray(modifiersMap);
Expand All @@ -360,9 +342,7 @@ function recreateRootLevelNodeWithModifiersImpl(node: ts.Node, modifiersMap: Mod
}

if (ts.isClassDeclaration(node)) {
return prependEmptyDecoratorsIfNeeded(
// eslint-disable-next-line deprecation/deprecation
ts.factory.createClassDeclaration,
return ts.factory.createClassDeclaration(
modifiers,
node.name,
node.typeParameters,
Expand All @@ -372,9 +352,7 @@ function recreateRootLevelNodeWithModifiersImpl(node: ts.Node, modifiersMap: Mod
}

if (ts.isClassExpression(node)) {
return prependEmptyDecoratorsIfNeeded(
// eslint-disable-next-line deprecation/deprecation
ts.factory.createClassExpression,
return ts.factory.createClassExpression(
modifiers,
node.name,
node.typeParameters,
Expand All @@ -384,29 +362,23 @@ function recreateRootLevelNodeWithModifiersImpl(node: ts.Node, modifiersMap: Mod
}

if (ts.isEnumDeclaration(node)) {
return prependEmptyDecoratorsIfNeeded(
// eslint-disable-next-line deprecation/deprecation
ts.factory.createEnumDeclaration,
return ts.factory.createEnumDeclaration(
modifiers,
node.name,
node.members
);
}

if (ts.isExportAssignment(node)) {
return prependEmptyDecoratorsIfNeeded(
// eslint-disable-next-line deprecation/deprecation
ts.factory.createExportAssignment,
return ts.factory.createExportAssignment(
modifiers,
node.isExportEquals,
node.expression
);
}

if (ts.isExportDeclaration(node)) {
return prependEmptyDecoratorsIfNeeded(
// eslint-disable-next-line deprecation/deprecation
ts.factory.createExportDeclaration,
return ts.factory.createExportDeclaration(
modifiers,
node.isTypeOnly,
node.exportClause,
Expand All @@ -416,9 +388,7 @@ function recreateRootLevelNodeWithModifiersImpl(node: ts.Node, modifiersMap: Mod
}

if (ts.isFunctionDeclaration(node)) {
return prependEmptyDecoratorsIfNeeded(
// eslint-disable-next-line deprecation/deprecation
ts.factory.createFunctionDeclaration,
return ts.factory.createFunctionDeclaration(
modifiers,
node.asteriskToken,
node.name,
Expand All @@ -442,9 +412,7 @@ function recreateRootLevelNodeWithModifiersImpl(node: ts.Node, modifiersMap: Mod
}

if (ts.isImportDeclaration(node)) {
return prependEmptyDecoratorsIfNeeded(
// eslint-disable-next-line deprecation/deprecation
ts.factory.createImportDeclaration,
return ts.factory.createImportDeclaration(
modifiers,
node.importClause,
node.moduleSpecifier,
Expand All @@ -453,9 +421,7 @@ function recreateRootLevelNodeWithModifiersImpl(node: ts.Node, modifiersMap: Mod
}

if (ts.isImportEqualsDeclaration(node)) {
return prependEmptyDecoratorsIfNeeded(
// eslint-disable-next-line deprecation/deprecation
ts.factory.createImportEqualsDeclaration,
return ts.factory.createImportEqualsDeclaration(
modifiers,
node.isTypeOnly,
node.name,
Expand All @@ -464,9 +430,7 @@ function recreateRootLevelNodeWithModifiersImpl(node: ts.Node, modifiersMap: Mod
}

if (ts.isInterfaceDeclaration(node)) {
return prependEmptyDecoratorsIfNeeded(
// eslint-disable-next-line deprecation/deprecation
ts.factory.createInterfaceDeclaration,
return ts.factory.createInterfaceDeclaration(
modifiers,
node.name,
node.typeParameters,
Expand All @@ -476,9 +440,7 @@ function recreateRootLevelNodeWithModifiersImpl(node: ts.Node, modifiersMap: Mod
}

if (ts.isModuleDeclaration(node)) {
return prependEmptyDecoratorsIfNeeded(
// eslint-disable-next-line deprecation/deprecation
ts.factory.createModuleDeclaration,
return ts.factory.createModuleDeclaration(
modifiers,
node.name,
node.body,
Expand All @@ -487,9 +449,7 @@ function recreateRootLevelNodeWithModifiersImpl(node: ts.Node, modifiersMap: Mod
}

if (ts.isTypeAliasDeclaration(node)) {
return prependEmptyDecoratorsIfNeeded(
// eslint-disable-next-line deprecation/deprecation
ts.factory.createTypeAliasDeclaration,
return ts.factory.createTypeAliasDeclaration(
modifiers,
node.name,
node.typeParameters,
Expand All @@ -508,26 +468,10 @@ function recreateRootLevelNodeWithModifiersImpl(node: ts.Node, modifiersMap: Mod
If you're seeing this error, please report a bug on https://github.com/timocov/dts-bundle-generator/issues`);
}

interface TsCompatWith48 {
canHaveModifiers?(node: ts.Node): boolean;
getModifiers?(node: ts.Node): readonly ts.Modifier[] | undefined;
}

function canHaveModifiersCompat(node: ts.Node): boolean {
const compatTs = ts as TsCompatWith48;
return compatTs.canHaveModifiers !== undefined ? compatTs.canHaveModifiers(node) : true;
}

function getModifiersCompat(node: ts.Node): readonly ts.Modifier[] | undefined {
const compatTs = ts as TsCompatWith48;
// eslint-disable-next-line deprecation/deprecation
return compatTs.getModifiers !== undefined ? compatTs.getModifiers(node) : node.modifiers as readonly ts.Modifier[] | undefined;
}

export function getModifiers(node: ts.Node): readonly ts.Modifier[] | undefined {
if (!canHaveModifiersCompat(node)) {
if (!ts.canHaveModifiers(node)) {
throw new Error(`Node kind=${ts.SyntaxKind[node.kind]} cannot have modifiers`);
}

return getModifiersCompat(node);
return ts.getModifiers(node);
}
3 changes: 1 addition & 2 deletions src/types-usage-evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ export class TypesUsageEvaluator {
private computeUsagesRecursively(parent: ts.Node, parentSymbol: ts.Symbol): void {
const queue = parent.getChildren();
for (const child of queue) {
// eslint-disable-next-line deprecation/deprecation
if (child.kind === ts.SyntaxKind.JSDocComment) {
if (child.kind === ts.SyntaxKind.JSDoc) {
continue;
}

Expand Down

0 comments on commit 6b3b15b

Please sign in to comment.