Skip to content

Commit

Permalink
Fix createImportSpecifier with typescript 4.5+ (parcel-bundler#7426)
Browse files Browse the repository at this point in the history
  • Loading branch information
astegmaier authored and bhovhannes committed Dec 23, 2021
1 parent 793acfa commit 1a2c66f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
5 changes: 3 additions & 2 deletions packages/transformers/typescript-types/src/shake.js
Expand Up @@ -4,7 +4,7 @@ import type {TSModuleGraph} from './TSModuleGraph';

import ts from 'typescript';
import nullthrows from 'nullthrows';
import {getExportedName, isDeclaration} from './utils';
import {getExportedName, isDeclaration, createImportSpecifier} from './utils';

export function shake(
moduleGraph: TSModuleGraph,
Expand Down Expand Up @@ -238,7 +238,8 @@ function generateImports(moduleGraph: TSModuleGraph) {
);
} else {
namedSpecifiers.push(
ts.createImportSpecifier(
createImportSpecifier(
ts,
name === imported ? undefined : ts.createIdentifier(imported),
ts.createIdentifier(name),
),
Expand Down
20 changes: 20 additions & 0 deletions packages/transformers/typescript-types/src/utils.js
@@ -1,5 +1,6 @@
// @flow
import typeof TypeScriptModule from 'typescript'; // eslint-disable-line import/no-extraneous-dependencies
import type {Identifier, ImportSpecifier} from 'typescript';

export function getExportedName(ts: TypeScriptModule, node: any): ?string {
if (!node.modifiers) {
Expand All @@ -26,3 +27,22 @@ export function isDeclaration(ts: TypeScriptModule, node: any): boolean {
ts.isTypeAliasDeclaration(node)
);
}

export function createImportSpecifier(
ts: TypeScriptModule,
propertyName: Identifier | void,
name: Identifier,
isTypeOnly: boolean = false,
): ImportSpecifier {
const [majorVersion, minorVersion] = ts.versionMajorMinor
.split('.')
.map(num => parseInt(num, 10));
// The signature of createImportSpecifier had a breaking change in Typescript 4.5.
// see: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#type-modifiers-on-import-names
if (majorVersion > 4 || (majorVersion === 4 && minorVersion >= 5)) {
// $FlowFixMe
return ts.createImportSpecifier(isTypeOnly, propertyName, name);
} else {
return ts.createImportSpecifier(propertyName, name);
}
}
6 changes: 3 additions & 3 deletions yarn.lock
Expand Up @@ -13024,9 +13024,9 @@ typedarray@^0.0.6:
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@>=3.0.0:
version "4.2.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.2.4.tgz#8610b59747de028fda898a8aef0e103f156d0961"
integrity sha512-V+evlYHZnQkaz8TRBuxTA92yZBPotr5H+WhQ7bD3hZUndx5tGOa1fuCgeSjxAzM1RiN5IzvadIXTVefuuwZCRg==
version "4.5.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.3.tgz#afaa858e68c7103317d89eb90c5d8906268d353c"
integrity sha512-eVYaEHALSt+s9LbvgEv4Ef+Tdq7hBiIZgii12xXJnukryt3pMgJf6aKhoCZ3FWQsu6sydEnkg11fYXLzhLBjeQ==

uglify-js@^3.1.4:
version "3.7.6"
Expand Down

0 comments on commit 1a2c66f

Please sign in to comment.