Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix createImportSpecifier with typescript 4.5+ #7426

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wowwwwww... TS doesn't follow semver?? Maybe we should pin our dependency on it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other side of that coin: #5978

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