From 67a6b201003dc627ca96ee3be8b1abaea87a540a Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 24 Jul 2017 14:29:03 -0700 Subject: [PATCH 1/2] Remove unused internal utilities --- src/compiler/transformers/jsx.ts | 2 +- src/compiler/utilities.ts | 199 +------------------------------ src/services/symbolDisplay.ts | 2 +- 3 files changed, 7 insertions(+), 196 deletions(-) diff --git a/src/compiler/transformers/jsx.ts b/src/compiler/transformers/jsx.ts index d5bf20a525d9e..405a819991d80 100644 --- a/src/compiler/transformers/jsx.ts +++ b/src/compiler/transformers/jsx.ts @@ -114,7 +114,7 @@ namespace ts { compilerOptions.reactNamespace, tagName, objectProperties, - filter(map(children, transformJsxChildToExpression), isDefined), + children && mapDefined(children, transformJsxChildToExpression), node, location ); diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index d006a2fb6c138..5f8be1ad50773 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -26,20 +26,6 @@ namespace ts { return undefined; } - export function findDeclaration(symbol: Symbol, predicate: (node: Declaration) => node is T): T | undefined; - export function findDeclaration(symbol: Symbol, predicate: (node: Declaration) => boolean): Declaration | undefined; - export function findDeclaration(symbol: Symbol, predicate: (node: Declaration) => boolean): Declaration | undefined { - const declarations = symbol.declarations; - if (declarations) { - for (const declaration of declarations) { - if (predicate(declaration)) { - return declaration; - } - } - } - return undefined; - } - export interface StringSymbolWriter extends SymbolWriter { string(): string; } @@ -111,19 +97,16 @@ namespace ts { sourceFile.resolvedTypeReferenceDirectiveNames.set(typeReferenceDirectiveName, resolvedTypeReferenceDirective); } - /* @internal */ export function moduleResolutionIsEqualTo(oldResolution: ResolvedModuleFull, newResolution: ResolvedModuleFull): boolean { return oldResolution.isExternalLibraryImport === newResolution.isExternalLibraryImport && oldResolution.extension === newResolution.extension && oldResolution.resolvedFileName === newResolution.resolvedFileName; } - /* @internal */ export function typeDirectiveIsEqualTo(oldResolution: ResolvedTypeReferenceDirective, newResolution: ResolvedTypeReferenceDirective): boolean { return oldResolution.resolvedFileName === newResolution.resolvedFileName && oldResolution.primary === newResolution.primary; } - /* @internal */ export function hasChangesInResolutions( names: ReadonlyArray, newResolutions: ReadonlyArray, @@ -202,14 +185,6 @@ namespace ts { return `${file.fileName}(${loc.line + 1},${loc.character + 1})`; } - export function getStartPosOfNode(node: Node): number { - return node.pos; - } - - export function isDefined(value: any): boolean { - return value !== undefined; - } - export function getEndLinePosition(line: number, sourceFile: SourceFileLike): number { Debug.assert(line >= 0); const lineStarts = getLineStarts(sourceFile); @@ -436,7 +411,7 @@ namespace ts { return isExternalModule(node) || compilerOptions.isolatedModules; } - export function isBlockScope(node: Node, parentNode: Node) { + function isBlockScope(node: Node, parentNode: Node) { switch (node.kind) { case SyntaxKind.SourceFile: case SyntaxKind.CaseBlock: @@ -637,10 +612,6 @@ namespace ts { return getLeadingCommentRanges(sourceFileOfNode.text, node.pos); } - export function getLeadingCommentRangesOfNodeFromText(node: Node, text: string) { - return getLeadingCommentRanges(text, node.pos); - } - export function getJSDocCommentRanges(node: Node, text: string) { const commentRanges = (node.kind === SyntaxKind.Parameter || node.kind === SyntaxKind.TypeParameter || @@ -648,7 +619,7 @@ namespace ts { node.kind === SyntaxKind.ArrowFunction || node.kind === SyntaxKind.ParenthesizedExpression) ? concatenate(getTrailingCommentRanges(text, node.pos), getLeadingCommentRanges(text, node.pos)) : - getLeadingCommentRangesOfNodeFromText(node, text); + getLeadingCommentRanges(text, node.pos); // True if the comment starts with '/**' but not if it is '/**/' return filter(commentRanges, comment => text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk && @@ -656,9 +627,9 @@ namespace ts { text.charCodeAt(comment.pos + 3) !== CharacterCodes.slash); } - export let fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*/; - export let fullTripleSlashReferenceTypeReferenceDirectiveRegEx = /^(\/\/\/\s*/; - export let fullTripleSlashAMDReferencePathRegEx = /^(\/\/\/\s*/; + export const fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*/; + export const fullTripleSlashReferenceTypeReferenceDirectiveRegEx = /^(\/\/\/\s*/; + export const fullTripleSlashAMDReferencePathRegEx = /^(\/\/\/\s*/; export function isPartOfTypeNode(node: Node): boolean { if (SyntaxKind.FirstTypeNode <= node.kind && node.kind <= SyntaxKind.LastTypeNode) { @@ -2072,10 +2043,6 @@ namespace ts { return getParseTreeNode(sourceFile, isSourceFile) || sourceFile; } - export function getOriginalSourceFiles(sourceFiles: ReadonlyArray) { - return sameMap(sourceFiles, getOriginalSourceFile); - } - export const enum Associativity { Left, Right @@ -3068,24 +3035,6 @@ namespace ts { return false; } - // Returns false if this heritage clause element's expression contains something unsupported - // (i.e. not a name or dotted name). - export function isSupportedExpressionWithTypeArguments(node: ExpressionWithTypeArguments): boolean { - return isSupportedExpressionWithTypeArgumentsRest(node.expression); - } - - function isSupportedExpressionWithTypeArgumentsRest(node: Expression): boolean { - if (node.kind === SyntaxKind.Identifier) { - return true; - } - else if (isPropertyAccessExpression(node)) { - return isSupportedExpressionWithTypeArgumentsRest(node.expression); - } - else { - return false; - } - } - export function isExpressionWithTypeArgumentsInClassExtendsClause(node: Node): boolean { return tryGetClassExtendingExpressionWithTypeArguments(node) !== undefined; } @@ -3222,81 +3171,6 @@ namespace ts { return carriageReturnLineFeed; } - /** - * Tests whether a node and its subtree is simple enough to have its position - * information ignored when emitting source maps in a destructuring assignment. - * - * @param node The expression to test. - */ - export function isSimpleExpression(node: Expression): boolean { - return isSimpleExpressionWorker(node, 0); - } - - function isSimpleExpressionWorker(node: Expression, depth: number): boolean { - if (depth <= 5) { - const kind = node.kind; - if (kind === SyntaxKind.StringLiteral - || kind === SyntaxKind.NumericLiteral - || kind === SyntaxKind.RegularExpressionLiteral - || kind === SyntaxKind.NoSubstitutionTemplateLiteral - || kind === SyntaxKind.Identifier - || kind === SyntaxKind.ThisKeyword - || kind === SyntaxKind.SuperKeyword - || kind === SyntaxKind.TrueKeyword - || kind === SyntaxKind.FalseKeyword - || kind === SyntaxKind.NullKeyword) { - return true; - } - else if (kind === SyntaxKind.PropertyAccessExpression) { - return isSimpleExpressionWorker((node).expression, depth + 1); - } - else if (kind === SyntaxKind.ElementAccessExpression) { - return isSimpleExpressionWorker((node).expression, depth + 1) - && isSimpleExpressionWorker((node).argumentExpression, depth + 1); - } - else if (kind === SyntaxKind.PrefixUnaryExpression - || kind === SyntaxKind.PostfixUnaryExpression) { - return isSimpleExpressionWorker((node).operand, depth + 1); - } - else if (kind === SyntaxKind.BinaryExpression) { - return (node).operatorToken.kind !== SyntaxKind.AsteriskAsteriskToken - && isSimpleExpressionWorker((node).left, depth + 1) - && isSimpleExpressionWorker((node).right, depth + 1); - } - else if (kind === SyntaxKind.ConditionalExpression) { - return isSimpleExpressionWorker((node).condition, depth + 1) - && isSimpleExpressionWorker((node).whenTrue, depth + 1) - && isSimpleExpressionWorker((node).whenFalse, depth + 1); - } - else if (kind === SyntaxKind.VoidExpression - || kind === SyntaxKind.TypeOfExpression - || kind === SyntaxKind.DeleteExpression) { - return isSimpleExpressionWorker((node).expression, depth + 1); - } - else if (kind === SyntaxKind.ArrayLiteralExpression) { - return (node).elements.length === 0; - } - else if (kind === SyntaxKind.ObjectLiteralExpression) { - return (node).properties.length === 0; - } - else if (kind === SyntaxKind.CallExpression) { - if (!isSimpleExpressionWorker((node).expression, depth + 1)) { - return false; - } - - for (const argument of (node).arguments) { - if (!isSimpleExpressionWorker(argument, depth + 1)) { - return false; - } - } - - return true; - } - } - - return false; - } - /** * Formats an enum value as a string for debugging and debug assertions. */ @@ -3369,24 +3243,6 @@ namespace ts { return formatEnum(flags, (ts).ObjectFlags, /*isFlags*/ true); } - export function getRangePos(range: TextRange | undefined) { - return range ? range.pos : -1; - } - - export function getRangeEnd(range: TextRange | undefined) { - return range ? range.end : -1; - } - - /** - * Increases (or decreases) a position by the provided amount. - * - * @param pos The position. - * @param value The delta. - */ - export function movePos(pos: number, value: number) { - return positionIsSynthesized(pos) ? -1 : pos + value; - } - /** * Creates a new TextRange from the provided pos and end. * @@ -3444,26 +3300,6 @@ namespace ts { return range.pos === range.end; } - /** - * Creates a new TextRange from a provided range with its end position collapsed to its - * start position. - * - * @param range A TextRange. - */ - export function collapseRangeToStart(range: TextRange): TextRange { - return isCollapsedRange(range) ? range : moveRangeEnd(range, range.pos); - } - - /** - * Creates a new TextRange from a provided range with its start position collapsed to its - * end position. - * - * @param range A TextRange. - */ - export function collapseRangeToEnd(range: TextRange): TextRange { - return isCollapsedRange(range) ? range : moveRangePos(range, range.end); - } - /** * Creates a new TextRange for a token at the provides start position. * @@ -3527,31 +3363,6 @@ namespace ts { return node.initializer !== undefined; } - /** - * Gets a value indicating whether a node is merged with a class declaration in the same scope. - */ - export function isMergedWithClass(node: Node) { - if (node.symbol) { - for (const declaration of node.symbol.declarations) { - if (declaration.kind === SyntaxKind.ClassDeclaration && declaration !== node) { - return true; - } - } - } - - return false; - } - - /** - * Gets a value indicating whether a node is the first declaration of its kind. - * - * @param node A Declaration node. - * @param kind The SyntaxKind to find among related declarations. - */ - export function isFirstDeclarationOfKind(node: Node, kind: SyntaxKind) { - return node.symbol && getDeclarationOfKind(node.symbol, kind) === node; - } - export function isWatchSet(options: CompilerOptions) { // Firefox has Object.prototype.watch return options.watch && options.hasOwnProperty("watch"); diff --git a/src/services/symbolDisplay.ts b/src/services/symbolDisplay.ts index 0cb3916c9c5b9..303bb8395eee6 100644 --- a/src/services/symbolDisplay.ts +++ b/src/services/symbolDisplay.ts @@ -203,7 +203,7 @@ namespace ts.SymbolDisplay { // get the signature from the declaration and write it const functionDeclaration = location.parent; // Use function declaration to write the signatures only if the symbol corresponding to this declaration - const locationIsSymbolDeclaration = findDeclaration(symbol, declaration => + const locationIsSymbolDeclaration = find(symbol.declarations, declaration => declaration === (location.kind === SyntaxKind.ConstructorKeyword ? functionDeclaration.parent : functionDeclaration)); if (locationIsSymbolDeclaration) { From 71ef7e960f9eb0db2aabcd1e14b52be05d824c30 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 8 Aug 2017 13:17:38 -0700 Subject: [PATCH 2/2] Handle undefined input to `mapDefined` --- src/compiler/core.ts | 14 ++++++++------ src/compiler/transformers/jsx.ts | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 2089722e426a1..0985b30eccb25 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -515,13 +515,15 @@ namespace ts { return result || array; } - export function mapDefined(array: ReadonlyArray, mapFn: (x: T, i: number) => U | undefined): U[] { + export function mapDefined(array: ReadonlyArray | undefined, mapFn: (x: T, i: number) => U | undefined): U[] { const result: U[] = []; - for (let i = 0; i < array.length; i++) { - const item = array[i]; - const mapped = mapFn(item, i); - if (mapped !== undefined) { - result.push(mapped); + if (array) { + for (let i = 0; i < array.length; i++) { + const item = array[i]; + const mapped = mapFn(item, i); + if (mapped !== undefined) { + result.push(mapped); + } } } return result; diff --git a/src/compiler/transformers/jsx.ts b/src/compiler/transformers/jsx.ts index 405a819991d80..0d00f4d48a343 100644 --- a/src/compiler/transformers/jsx.ts +++ b/src/compiler/transformers/jsx.ts @@ -114,7 +114,7 @@ namespace ts { compilerOptions.reactNamespace, tagName, objectProperties, - children && mapDefined(children, transformJsxChildToExpression), + mapDefined(children, transformJsxChildToExpression), node, location );