Skip to content

Commit

Permalink
Ensure emitted d.ts files do not contain extraneous import expressions
Browse files Browse the repository at this point in the history
These appear to break api-extractor, and we won't have a fix until TS
starts to emit d.ts files using `import type { ... }` for imports that
aren't explicitly written in the source files.
  • Loading branch information
jakebailey committed Sep 13, 2022
1 parent d768606 commit 638f503
Show file tree
Hide file tree
Showing 24 changed files with 350 additions and 341 deletions.
11 changes: 6 additions & 5 deletions src/compiler/factory/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import {
AccessorDeclaration, addEmitFlags, AdditiveOperator, AdditiveOperatorOrHigher, AssertionLevel,
AssignmentOperatorOrHigher, BinaryExpression, BinaryOperator, BinaryOperatorToken, BindingOrAssignmentElement,
BindingOrAssignmentElementRestIndicator, BindingOrAssignmentElementTarget, BindingOrAssignmentPattern,
BitwiseOperator, BitwiseOperatorOrHigher, BooleanLiteral, CharacterCodes, CommaListExpression,
BitwiseOperator, BitwiseOperatorOrHigher, Block, BooleanLiteral, CharacterCodes, CommaListExpression,
compareStringsCaseSensitive, CompilerOptions, Debug, Declaration, EmitFlags, EmitHelperFactory, EmitHost,
EmitResolver, EntityName, EqualityOperator, EqualityOperatorOrHigher, ExclamationToken, ExponentiationOperator,
ExportDeclaration, Expression, ExpressionStatement, externalHelpersModuleNameText, first, firstOrUndefined,
ForInitializer, GeneratedIdentifier, GeneratedIdentifierFlags, GeneratedNamePart, GeneratedPrivateIdentifier,
GetAccessorDeclaration,
getAllAccessorDeclarations, getEmitFlags, getEmitHelpers, getEmitModuleKind, getESModuleInterop,
getExternalModuleName, getExternalModuleNameFromPath, getJSDocType, getJSDocTypeTag, getModifiers,
getNamespaceDeclarationNode, getOrCreateEmitNode, getOriginalNode, getParseTreeNode,
Expand All @@ -26,7 +27,7 @@ import {
NumericLiteral, ObjectLiteralElementLike, ObjectLiteralExpression, or, OuterExpression, OuterExpressionKinds,
outFile, parseNodeFactory, PlusToken, PostfixUnaryExpression, PrefixUnaryExpression, PrivateIdentifier,
PropertyAssignment, PropertyDeclaration, PropertyName, pushIfUnique, QuestionToken, ReadonlyKeyword,
RelationalOperator, RelationalOperatorOrHigher, setOriginalNode, setParent, setStartsOnNewLine, setTextRange,
RelationalOperator, RelationalOperatorOrHigher, SetAccessorDeclaration, setOriginalNode, setParent, setStartsOnNewLine, setTextRange,
ShiftOperator, ShiftOperatorOrHigher, ShorthandPropertyAssignment, some, SourceFile, Statement, StringLiteral,
SyntaxKind, TextRange, ThisTypeNode, Token, TypeNode, TypeParameterDeclaration,
} from "../_namespaces/ts";
Expand Down Expand Up @@ -185,7 +186,7 @@ export function createForOfBindingStatement(factory: NodeFactory, node: ForIniti
}

/** @internal */
export function insertLeadingStatement(factory: NodeFactory, dest: Statement, source: Statement) {
export function insertLeadingStatement(factory: NodeFactory, dest: Statement, source: Statement): Block {
if (isBlock(dest)) {
return factory.updateBlock(dest, setTextRange(factory.createNodeArray([source, ...dest.statements]), dest.statements));
}
Expand Down Expand Up @@ -1456,7 +1457,7 @@ export function createAccessorPropertyBackingField(factory: NodeFactory, node: P
*
* @internal
*/
export function createAccessorPropertyGetRedirector(factory: NodeFactory, node: PropertyDeclaration, modifiers: ModifiersArray | undefined, name: PropertyName) {
export function createAccessorPropertyGetRedirector(factory: NodeFactory, node: PropertyDeclaration, modifiers: ModifiersArray | undefined, name: PropertyName): GetAccessorDeclaration {
return factory.createGetAccessorDeclaration(
modifiers,
name,
Expand All @@ -1478,7 +1479,7 @@ export function createAccessorPropertyGetRedirector(factory: NodeFactory, node:
*
* @internal
*/
export function createAccessorPropertySetRedirector(factory: NodeFactory, node: PropertyDeclaration, modifiers: ModifiersArray | undefined, name: PropertyName) {
export function createAccessorPropertySetRedirector(factory: NodeFactory, node: PropertyDeclaration, modifiers: ModifiersArray | undefined, name: PropertyName): SetAccessorDeclaration {
return factory.createSetAccessorDeclaration(
modifiers,
name,
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/classFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
startOnNewLine, Statement, SuperProperty, SyntaxKind, TaggedTemplateExpression, ThisExpression,
TransformationContext, TransformFlags, tryGetTextOfPropertyName, UnderscoreEscapedMap, unescapeLeadingUnderscores,
VariableStatement, visitArray, visitEachChild, visitFunctionBody, visitIterationBody, visitNode, visitNodes,
visitParameterList, VisitResult,
visitParameterList, VisitResult, Bundle,
} from "../_namespaces/ts";

const enum ClassPropertySubstitutionFlags {
Expand Down Expand Up @@ -168,7 +168,7 @@ const enum ClassFacts {
*
* @internal
*/
export function transformClassFields(context: TransformationContext) {
export function transformClassFields(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
const {
factory,
hoistVariableDeclaration,
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
SyntaxKind, TaggedTemplateExpression, takeWhile, TemplateExpression, TextRange, TokenFlags, TransformationContext,
TransformFlags, tryCast, unescapeLeadingUnderscores, unwrapInnermostStatementOfLabel, VariableDeclaration,
VariableDeclarationList, VariableStatement, visitEachChild, visitNode, visitNodes, visitParameterList, VisitResult,
VoidExpression, WhileStatement, YieldExpression,
VoidExpression, WhileStatement, YieldExpression, Bundle,
} from "../_namespaces/ts";

const enum ES2015SubstitutionFlags {
Expand Down Expand Up @@ -299,7 +299,7 @@ function createSpreadSegment(kind: SpreadSegmentKind, expression: Expression): S
}

/** @internal */
export function transformES2015(context: TransformationContext) {
export function transformES2015(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
const {
factory,
getEmitHelperFactory: emitHelpers,
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/es2016.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
BinaryExpression, chainBundle, Expression, isElementAccessExpression, isExpression, isPropertyAccessExpression,
BinaryExpression, Bundle, chainBundle, Expression, isElementAccessExpression, isExpression, isPropertyAccessExpression,
Node, setTextRange, SourceFile, SyntaxKind, TransformationContext, TransformFlags, visitEachChild, visitNode,
VisitResult,
} from "../_namespaces/ts";

/** @internal */
export function transformES2016(context: TransformationContext) {
export function transformES2016(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
const {
factory,
hoistVariableDeclaration
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/transformers/es2017.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
TypeReferenceSerializationKind, unescapeLeadingUnderscores, VariableDeclaration, VariableDeclarationList,
VariableStatement, visitEachChild, visitFunctionBody, visitIterationBody, visitNode, visitNodes, visitParameterList,
VisitResult,
Bundle,
} from "../_namespaces/ts";

type SuperContainer = ClassDeclaration | MethodDeclaration | GetAccessorDeclaration | SetAccessorDeclaration | ConstructorDeclaration;
Expand All @@ -30,7 +31,7 @@ const enum ContextFlags {
}

/** @internal */
export function transformES2017(context: TransformationContext) {
export function transformES2017(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
const {
factory,
getEmitHelperFactory: emitHelpers,
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/es2018.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
SourceFile, startOnNewLine, Statement, SyntaxKind, TaggedTemplateExpression, TextRange, Token,
TransformationContext, TransformFlags, unwrapInnermostStatementOfLabel, VariableDeclaration, VariableStatement,
visitEachChild, visitIterationBody, visitLexicalEnvironment, visitNode, visitNodes, visitParameterList, VisitResult,
VoidExpression, YieldExpression,
VoidExpression, YieldExpression, Bundle,
} from "../_namespaces/ts";

const enum ESNextSubstitutionFlags {
Expand Down Expand Up @@ -58,7 +58,7 @@ const enum HierarchyFacts {
}

/** @internal */
export function transformES2018(context: TransformationContext) {
export function transformES2018(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
const {
factory,
getEmitHelperFactory: emitHelpers,
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/transformers/es2019.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
Bundle,
CatchClause, chainBundle, isBlock, Node, SourceFile, SyntaxKind, TransformationContext, TransformFlags,
visitEachChild, visitNode, VisitResult,
} from "../_namespaces/ts";

/** @internal */
export function transformES2019(context: TransformationContext) {
export function transformES2019(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
const factory = context.factory;
return chainBundle(context, transformSourceFile);

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/es2020.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
AccessExpression, addEmitFlags, BinaryExpression, CallExpression, cast, chainBundle, Debug, DeleteExpression,
AccessExpression, addEmitFlags, BinaryExpression, Bundle, CallExpression, cast, chainBundle, Debug, DeleteExpression,
EmitFlags, Expression, isCallChain, isExpression, isGeneratedIdentifier, isIdentifier, isNonNullChain,
isOptionalChain, isParenthesizedExpression, isSimpleCopiableExpression, isSyntheticReference,
isTaggedTemplateExpression, Node, OptionalChain, OuterExpressionKinds, ParenthesizedExpression, setOriginalNode,
Expand All @@ -8,7 +8,7 @@ import {
} from "../_namespaces/ts";

/** @internal */
export function transformES2020(context: TransformationContext) {
export function transformES2020(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
const {
factory,
hoistVariableDeclaration,
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/es2021.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
AssignmentExpression, BinaryExpression, chainBundle, getNonAssignmentOperatorForCompoundAssignment,
AssignmentExpression, BinaryExpression, Bundle, chainBundle, getNonAssignmentOperatorForCompoundAssignment,
isAccessExpression, isExpression, isLeftHandSideExpression, isLogicalOrCoalescingAssignmentExpression,
isPropertyAccessExpression, isSimpleCopiableExpression, LogicalOrCoalescingAssignmentOperator, Node,
skipParentheses, SourceFile, SyntaxKind, Token, TransformationContext, TransformFlags, visitEachChild, visitNode,
VisitResult,
} from "../_namespaces/ts";

/** @internal */
export function transformES2021(context: TransformationContext) {
export function transformES2021(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
const {
hoistVariableDeclaration,
factory
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/transformers/es5.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Bundle,
chainBundle, EmitHint, Expression, getOriginalNodeId, Identifier, idText, isIdentifier, isPrivateIdentifier,
isPropertyAccessExpression, isPropertyAssignment, JsxClosingElement, JsxEmit, JsxOpeningElement,
JsxSelfClosingElement, Node, nodeIsSynthesized, PropertyAccessExpression, PropertyAssignment, setTextRange,
Expand All @@ -12,7 +13,7 @@ import {
*
* @internal
*/
export function transformES5(context: TransformationContext) {
export function transformES5(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
const { factory } = context;
const compilerOptions = context.getCompilerOptions();

Expand Down
3 changes: 2 additions & 1 deletion src/compiler/transformers/esnext.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {
Bundle,
chainBundle, Node, SourceFile, TransformationContext, TransformFlags, visitEachChild, VisitResult,
} from "../_namespaces/ts";

/** @internal */
export function transformESNext(context: TransformationContext) {
export function transformESNext(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
return chainBundle(context, transformSourceFile);

function transformSourceFile(node: SourceFile) {
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/generators.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
AccessorDeclaration, addEmitHelpers, addSyntheticTrailingComment, ArrayLiteralExpression, Associativity,
BinaryExpression, Block, BreakStatement, CallExpression, CaseClause, chainBundle, CommaListExpression,
BinaryExpression, Block, BreakStatement, Bundle, CallExpression, CaseClause, chainBundle, CommaListExpression,
ConditionalExpression, ContinueStatement, createExpressionForObjectLiteralElementLike, Debug, DoStatement,
ElementAccessExpression, EmitFlags, EmitHint, ESMap, Expression, ExpressionStatement, forEach, ForInStatement,
ForStatement, FunctionDeclaration, FunctionExpression, getEmitFlags, getEmitScriptTarget,
Expand Down Expand Up @@ -247,7 +247,7 @@ function getInstructionName(instruction: Instruction): string {
}

/** @internal */
export function transformGenerators(context: TransformationContext) {
export function transformGenerators(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
const {
factory,
getEmitHelperFactory: emitHelpers,
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/jsx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
addEmitHelpers, arrayFrom, chainBundle, createExpressionForJsxElement, createExpressionForJsxFragment,
addEmitHelpers, arrayFrom, Bundle, chainBundle, createExpressionForJsxElement, createExpressionForJsxFragment,
createExpressionFromEntityName, createJsxFactoryExpression, Debug, emptyArray, Expression, filter, find, flatten,
GeneratedIdentifierFlags, getEmitScriptTarget, getEntries, getJSXImplicitImportBase, getJSXRuntimeImport,
getLineAndCharacterOfPosition, getOriginalNode, getSemanticJsxChildren, Identifier, idText, ImportSpecifier,
Expand All @@ -14,7 +14,7 @@ import {
} from "../_namespaces/ts";

/** @internal */
export function transformJsx(context: TransformationContext) {
export function transformJsx(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
interface PerFileState {
importSpecifier?: string;
filenameDeclaration?: VariableDeclaration & { name: Identifier; };
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/legacyDecorators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
addEmitHelpers, addRange, AllDecorators, append, canHaveDecorators, chainBundle, childIsDecorated, ClassDeclaration,
addEmitHelpers, addRange, AllDecorators, append, Bundle, canHaveDecorators, chainBundle, childIsDecorated, ClassDeclaration,
ClassElement, ClassExpression, ClassLikeDeclaration, classOrConstructorParameterIsDecorated, ConstructorDeclaration,
Decorator, elideNodes, EmitFlags, EmitHint, EnumMember, Expression, filter, flatMap, GetAccessorDeclaration,
getAllDecoratorsOfClass, getAllDecoratorsOfClassElement, getEmitFlags, getEmitScriptTarget, getOriginalNodeId,
Expand All @@ -14,7 +14,7 @@ import {
} from "../_namespaces/ts";

/** @internal */
export function transformLegacyDecorators(context: TransformationContext) {
export function transformLegacyDecorators(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
const {
factory,
getEmitHelperFactory: emitHelpers,
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/module/esnextAnd2015.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
addRange, append, chainBundle, createEmptyExports, createExternalHelpersImportDeclarationIfNeeded, Debug, EmitFlags,
addRange, append, Bundle, chainBundle, createEmptyExports, createExternalHelpersImportDeclarationIfNeeded, Debug, EmitFlags,
EmitHint, ESMap, ExportAssignment, ExportDeclaration, Expression, GeneratedIdentifierFlags, getEmitFlags,
getEmitModuleKind, getEmitScriptTarget, getExternalModuleNameLiteral, hasSyntacticModifier, Identifier, idText,
ImportDeclaration, ImportEqualsDeclaration, insertStatementsAfterCustomPrologue,
Expand All @@ -10,7 +10,7 @@ import {
} from "../../_namespaces/ts";

/** @internal */
export function transformECMAScriptModule(context: TransformationContext) {
export function transformECMAScriptModule(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
const {
factory,
getEmitHelperFactory: emitHelpers,
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/transformers/module/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
addEmitFlags, addEmitHelper, addEmitHelpers, addRange, append, ArrowFunction, BinaryExpression, BindingElement,
Bundle,
CallExpression, chainBundle, ClassDeclaration, collectExternalModuleInfo, Debug, Declaration,
DestructuringAssignment, EmitFlags, EmitHelper, EmitHint, emptyArray, EndOfDeclarationMarker, ExportAssignment,
ExportDeclaration, Expression, ExpressionStatement, ExternalModuleInfo, firstOrUndefined,
Expand Down Expand Up @@ -27,7 +28,7 @@ import {
} from "../../_namespaces/ts";

/** @internal */
export function transformModule(context: TransformationContext) {
export function transformModule(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
interface AsynchronousDependencies {
aliasedModuleNames: Expression[];
unaliasedModuleNames: Expression[];
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/module/system.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
addRange, append, BinaryExpression, BindingElement, Block, CaseBlock, CaseClause, CaseOrDefaultClause, CatchClause,
addRange, append, BinaryExpression, BindingElement, Block, Bundle, CaseBlock, CaseClause, CaseOrDefaultClause, CatchClause,
chainBundle, ClassDeclaration, collectExternalModuleInfo, Debug, Declaration, DefaultClause,
DestructuringAssignment, DoStatement, EmitFlags, EmitHint, EndOfDeclarationMarker, ExportAssignment,
ExportDeclaration, Expression, ExpressionStatement, ExternalModuleInfo, firstOrUndefined,
Expand All @@ -25,7 +25,7 @@ import {
} from "../../_namespaces/ts";

/** @internal */
export function transformSystemModule(context: TransformationContext) {
export function transformSystemModule(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
interface DependencyGroup {
name: StringLiteral;
externalImports: (ImportDeclaration | ImportEqualsDeclaration | ExportDeclaration)[];
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/transformers/taggedTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
CallExpression,
Debug, Expression, factory, getSourceTextOfNodeFromSourceFile, hasInvalidEscape, Identifier, isExpression,
isExternalModule, isNoSubstitutionTemplateLiteral, NoSubstitutionTemplateLiteral, setTextRange, SourceFile,
SyntaxKind, TaggedTemplateExpression, TemplateHead, TemplateLiteralLikeNode, TemplateMiddle, TemplateTail,
Expand All @@ -18,7 +19,7 @@ export function processTaggedTemplateExpression(
visitor: Visitor,
currentSourceFile: SourceFile,
recordTaggedTemplateString: (temp: Identifier) => void,
level: ProcessLevel) {
level: ProcessLevel): TaggedTemplateExpression | CallExpression {

// Visit the tag expression
const tag = visitNode(node.tag, visitor, isExpression);
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
isPrivateIdentifier, isPropertyDeclaration, isStatic, isStringLiteralLike, isSuperCall, LogicalOperatorOrHigher,
map, Map, MethodDeclaration, ModifierFlags, NamedImportBindings, NamespaceExport, Node, NodeArray,
parameterIsThisKeyword, PrivateIdentifierAccessorDeclaration, PrivateIdentifierAutoAccessorPropertyDeclaration,
PrivateIdentifierMethodDeclaration, PropertyDeclaration, skipParentheses, some, SourceFile, Statement, SyntaxKind,
PrivateIdentifierMethodDeclaration, PropertyDeclaration, skipParentheses, some, SourceFile, Statement, SuperCall, SyntaxKind,
TransformationContext, VariableDeclaration, VariableStatement,
} from "../_namespaces/ts";

Expand Down Expand Up @@ -332,7 +332,7 @@ export function getNonAssignmentOperatorForCompoundAssignment(kind: CompoundAssi
*
* @internal
*/
export function getSuperCallFromStatement(statement: Statement) {
export function getSuperCallFromStatement(statement: Statement): SuperCall | undefined {
if (!isExpressionStatement(statement)) {
return undefined;
}
Expand Down
Loading

0 comments on commit 638f503

Please sign in to comment.