refactor(compiler-cli): remove TypeScript 3.9 workarounds (#39586)

With this change we remove code which was used to support both TypeScript 3.9 and TypeScript 4.0

This code is now no longer needed because G3 is on TypeScript 4.0

PR Close #39586
This commit is contained in:
Alan Agius 2020-11-06 09:08:57 +01:00 committed by Misko Hevery
parent 599ca34eda
commit 0929099e41
6 changed files with 27 additions and 77 deletions

View File

@ -477,13 +477,10 @@ export class Evaluator {
case ts.SyntaxKind.UnionType: case ts.SyntaxKind.UnionType:
const unionType = <ts.UnionTypeNode>node; const unionType = <ts.UnionTypeNode>node;
// Remove null and undefined from the list of unions. // Remove null and undefined from the list of unions.
// TODO(alan-agius4): remove `n.kind !== ts.SyntaxKind.NullKeyword` when
// TS 3.9 support is dropped. In TS 4.0 NullKeyword is a child of LiteralType.
const references = const references =
unionType.types unionType.types
.filter( .filter(
n => n.kind !== ts.SyntaxKind.NullKeyword && n => n.kind !== ts.SyntaxKind.UndefinedKeyword &&
n.kind !== ts.SyntaxKind.UndefinedKeyword &&
!(ts.isLiteralTypeNode(n) && n.literal.kind === ts.SyntaxKind.NullKeyword)) !(ts.isLiteralTypeNode(n) && n.literal.kind === ts.SyntaxKind.NullKeyword))
.map(n => this.evaluateNode(n)); .map(n => this.evaluateNode(n));

View File

@ -22,25 +22,22 @@ export function extractReferencesFromType(
return []; return [];
} }
// TODO(alan-agius4): remove `def.elementTypes` and casts when TS 3.9 support is dropped and G3 is return def.elements.map(element => {
// using TS 4.0. if (!ts.isTypeQueryNode(element)) {
return (((def as any).elements || (def as any).elementTypes) as ts.NodeArray<ts.TypeNode>) throw new Error(`Expected TypeQueryNode: ${nodeDebugInfo(element)}`);
.map(element => { }
if (!ts.isTypeQueryNode(element)) { const type = element.exprName;
throw new Error(`Expected TypeQueryNode: ${nodeDebugInfo(element)}`); const {node, from} = reflectTypeEntityToDeclaration(type, checker);
} if (!isNamedClassDeclaration(node)) {
const type = element.exprName; throw new Error(`Expected named ClassDeclaration: ${nodeDebugInfo(node)}`);
const {node, from} = reflectTypeEntityToDeclaration(type, checker); }
if (!isNamedClassDeclaration(node)) { const specifier = (from !== null && !from.startsWith('.') ? from : ngModuleImportedFrom);
throw new Error(`Expected named ClassDeclaration: ${nodeDebugInfo(node)}`); if (specifier !== null) {
} return new Reference(node, {specifier, resolutionContext});
const specifier = (from !== null && !from.startsWith('.') ? from : ngModuleImportedFrom); } else {
if (specifier !== null) { return new Reference(node);
return new Reference(node, {specifier, resolutionContext}); }
} else { });
return new Reference(node);
}
});
} }
export function readStringType(type: ts.TypeNode): string|null { export function readStringType(type: ts.TypeNode): string|null {
@ -74,15 +71,12 @@ export function readStringArrayType(type: ts.TypeNode): string[] {
return []; return [];
} }
const res: string[] = []; const res: string[] = [];
// TODO(alan-agius4): remove `def.elementTypes` and casts when TS 3.9 support is dropped and G3 is type.elements.forEach(el => {
// using TS 4.0. if (!ts.isLiteralTypeNode(el) || !ts.isStringLiteral(el.literal)) {
(((type as any).elements || (type as any).elementTypes) as ts.NodeArray<ts.TypeNode>) return;
.forEach(el => { }
if (!ts.isLiteralTypeNode(el) || !ts.isStringLiteral(el.literal)) { res.push(el.literal.text);
return; });
}
res.push(el.literal.text);
});
return res; return res;
} }

View File

@ -64,9 +64,7 @@ export class TypeScriptReflectionHost implements ReflectionHost {
// optional tokes that don't have providers. // optional tokes that don't have providers.
if (typeNode && ts.isUnionTypeNode(typeNode)) { if (typeNode && ts.isUnionTypeNode(typeNode)) {
let childTypeNodes = typeNode.types.filter( let childTypeNodes = typeNode.types.filter(
// TODO(alan-agius4): remove `childTypeNode.kind !== ts.SyntaxKind.NullKeyword` when childTypeNode =>
// TS 3.9 support is dropped. In TS 4.0 NullKeyword is a child of LiteralType.
childTypeNode => childTypeNode.kind !== ts.SyntaxKind.NullKeyword &&
!(ts.isLiteralTypeNode(childTypeNode) && !(ts.isLiteralTypeNode(childTypeNode) &&
childTypeNode.literal.kind === ts.SyntaxKind.NullKeyword)); childTypeNode.literal.kind === ts.SyntaxKind.NullKeyword));

View File

@ -239,33 +239,6 @@ export class ReturnTypeTransform implements DtsTransform {
} }
transformClassElement(element: ts.ClassElement, imports: ImportManager): ts.ClassElement { transformClassElement(element: ts.ClassElement, imports: ImportManager): ts.ClassElement {
// // TODO(alan-agius4): Remove when we no longer support TS 3.9
// TS <= 3.9
if (ts.isMethodSignature(element)) {
const original = ts.getOriginalNode(element) as ts.MethodDeclaration;
if (!this.typeReplacements.has(original)) {
return element;
}
const returnType = this.typeReplacements.get(original)!;
const tsReturnType = translateType(returnType, imports);
const methodSignature = ts.updateMethodSignature(
/* node */ element,
/* typeParameters */ element.typeParameters,
/* parameters */ element.parameters,
/* type */ tsReturnType,
/* name */ element.name,
/* questionToken */ element.questionToken);
// Copy over any modifiers, these cannot be set during the `ts.updateMethodSignature` call.
(methodSignature.modifiers as ts.ModifiersArray | undefined) = element.modifiers;
// A bug in the TypeScript declaration causes `ts.MethodSignature` not to be assignable to
// `ts.ClassElement`. Since `element` was a `ts.MethodSignature` already, transforming it into
// this type is actually correct.
return methodSignature as unknown as ts.ClassElement;
}
// TS 4.0 +
if (ts.isMethodDeclaration(element)) { if (ts.isMethodDeclaration(element)) {
const original = ts.getOriginalNode(element, ts.isMethodDeclaration); const original = ts.getOriginalNode(element, ts.isMethodDeclaration);
if (!this.typeReplacements.has(original)) { if (!this.typeReplacements.has(original)) {

View File

@ -104,11 +104,7 @@ export class TypeTranslatorVisitor implements o.ExpressionVisitor, o.TypeVisitor
visitLiteralExpr(ast: o.LiteralExpr, context: Context): ts.TypeNode { visitLiteralExpr(ast: o.LiteralExpr, context: Context): ts.TypeNode {
if (ast.value === null) { if (ast.value === null) {
// TODO(alan-agius4): Remove when we no longer support TS 3.9 return ts.createLiteralTypeNode(ts.createNull());
// Use: return ts.createLiteralTypeNode(ts.createNull()) directly.
return ts.versionMajorMinor.charAt(0) === '4' ?
ts.createLiteralTypeNode(ts.createNull() as any) :
ts.createKeywordTypeNode(ts.SyntaxKind.NullKeyword as any);
} else if (ast.value === undefined) { } else if (ast.value === undefined) {
return ts.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword); return ts.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword);
} else if (typeof ast.value === 'boolean') { } else if (typeof ast.value === 'boolean') {

View File

@ -141,14 +141,9 @@ function createCtorParametersClassPropertyType(): ts.TypeNode {
])), ])),
undefined)); undefined));
// TODO(alan-agius4): Remove when we no longer support TS 3.9
const nullLiteral = ts.createNull() as any;
const nullType = ts.versionMajorMinor.charAt(0) === '4' ?
ts.createLiteralTypeNode(nullLiteral as any) :
nullLiteral;
return ts.createFunctionTypeNode(undefined, [], ts.createArrayTypeNode(ts.createUnionTypeNode([ return ts.createFunctionTypeNode(undefined, [], ts.createArrayTypeNode(ts.createUnionTypeNode([
ts.createTypeLiteralNode(typeElements), ts.createTypeLiteralNode(typeElements),
nullType, ts.createLiteralTypeNode(ts.createNull()),
]))); ])));
} }
@ -293,13 +288,10 @@ function typeReferenceToExpression(
// Ignore any generic types, just return the base type. // Ignore any generic types, just return the base type.
return entityNameToExpression(typeRef.typeName); return entityNameToExpression(typeRef.typeName);
case ts.SyntaxKind.UnionType: case ts.SyntaxKind.UnionType:
// TODO(alan-agius4): remove `t.kind !== ts.SyntaxKind.NullKeyword` when
// TS 3.9 support is dropped. In TS 4.0 NullKeyword is a child of LiteralType.
const childTypeNodes = const childTypeNodes =
(node as ts.UnionTypeNode) (node as ts.UnionTypeNode)
.types.filter( .types.filter(
t => t.kind !== ts.SyntaxKind.NullKeyword && t => !(ts.isLiteralTypeNode(t) && t.literal.kind === ts.SyntaxKind.NullKeyword));
!(ts.isLiteralTypeNode(t) && t.literal.kind === ts.SyntaxKind.NullKeyword));
return childTypeNodes.length === 1 ? return childTypeNodes.length === 1 ?
typeReferenceToExpression(entityNameToExpression, childTypeNodes[0]) : typeReferenceToExpression(entityNameToExpression, childTypeNodes[0]) :
undefined; undefined;