Skip to content

Commit 17a6f7b

Browse files
author
Andy
authored
Remove unused internal utilities (#17380)
* Remove unused internal utilities * Handle undefined input to `mapDefined`
1 parent a6f37f5 commit 17a6f7b

File tree

5 files changed

+18
-205
lines changed

5 files changed

+18
-205
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5096,8 +5096,8 @@ namespace ts {
50965096
return unknownType;
50975097
}
50985098

5099-
const declaration = <JSDocTypedefTag | TypeAliasDeclaration>findDeclaration(
5100-
symbol, d => d.kind === SyntaxKind.JSDocTypedefTag || d.kind === SyntaxKind.TypeAliasDeclaration);
5099+
const declaration = <JSDocTypedefTag | TypeAliasDeclaration>find(symbol.declarations, d =>
5100+
d.kind === SyntaxKind.JSDocTypedefTag || d.kind === SyntaxKind.TypeAliasDeclaration);
51015101
let type = getTypeFromTypeNode(declaration.kind === SyntaxKind.JSDocTypedefTag ? declaration.typeExpression : declaration.type);
51025102

51035103
if (popTypeResolution()) {

src/compiler/core.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -501,13 +501,15 @@ namespace ts {
501501
return result || array;
502502
}
503503

504-
export function mapDefined<T, U>(array: ReadonlyArray<T>, mapFn: (x: T, i: number) => U | undefined): U[] {
504+
export function mapDefined<T, U>(array: ReadonlyArray<T> | undefined, mapFn: (x: T, i: number) => U | undefined): U[] {
505505
const result: U[] = [];
506-
for (let i = 0; i < array.length; i++) {
507-
const item = array[i];
508-
const mapped = mapFn(item, i);
509-
if (mapped !== undefined) {
510-
result.push(mapped);
506+
if (array) {
507+
for (let i = 0; i < array.length; i++) {
508+
const item = array[i];
509+
const mapped = mapFn(item, i);
510+
if (mapped !== undefined) {
511+
result.push(mapped);
512+
}
511513
}
512514
}
513515
return result;

src/compiler/transformers/jsx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ namespace ts {
114114
compilerOptions.reactNamespace,
115115
tagName,
116116
objectProperties,
117-
filter(map(children, transformJsxChildToExpression), isDefined),
117+
mapDefined(children, transformJsxChildToExpression),
118118
node,
119119
location
120120
);

src/compiler/utilities.ts

Lines changed: 6 additions & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ namespace ts {
2727
return undefined;
2828
}
2929

30-
export function findDeclaration<T extends Declaration>(symbol: Symbol, predicate: (node: Declaration) => node is T): T | undefined;
31-
export function findDeclaration(symbol: Symbol, predicate: (node: Declaration) => boolean): Declaration | undefined;
32-
export function findDeclaration(symbol: Symbol, predicate: (node: Declaration) => boolean): Declaration | undefined {
33-
const declarations = symbol.declarations;
34-
if (declarations) {
35-
for (const declaration of declarations) {
36-
if (predicate(declaration)) {
37-
return declaration;
38-
}
39-
}
40-
}
41-
return undefined;
42-
}
43-
4430
export interface StringSymbolWriter extends SymbolWriter {
4531
string(): string;
4632
}
@@ -112,19 +98,16 @@ namespace ts {
11298
sourceFile.resolvedTypeReferenceDirectiveNames.set(typeReferenceDirectiveName, resolvedTypeReferenceDirective);
11399
}
114100

115-
/* @internal */
116101
export function moduleResolutionIsEqualTo(oldResolution: ResolvedModuleFull, newResolution: ResolvedModuleFull): boolean {
117102
return oldResolution.isExternalLibraryImport === newResolution.isExternalLibraryImport &&
118103
oldResolution.extension === newResolution.extension &&
119104
oldResolution.resolvedFileName === newResolution.resolvedFileName;
120105
}
121106

122-
/* @internal */
123107
export function typeDirectiveIsEqualTo(oldResolution: ResolvedTypeReferenceDirective, newResolution: ResolvedTypeReferenceDirective): boolean {
124108
return oldResolution.resolvedFileName === newResolution.resolvedFileName && oldResolution.primary === newResolution.primary;
125109
}
126110

127-
/* @internal */
128111
export function hasChangesInResolutions<T>(
129112
names: ReadonlyArray<string>,
130113
newResolutions: ReadonlyArray<T>,
@@ -203,14 +186,6 @@ namespace ts {
203186
return `${file.fileName}(${loc.line + 1},${loc.character + 1})`;
204187
}
205188

206-
export function getStartPosOfNode(node: Node): number {
207-
return node.pos;
208-
}
209-
210-
export function isDefined(value: any): boolean {
211-
return value !== undefined;
212-
}
213-
214189
export function getEndLinePosition(line: number, sourceFile: SourceFileLike): number {
215190
Debug.assert(line >= 0);
216191
const lineStarts = getLineStarts(sourceFile);
@@ -463,7 +438,7 @@ namespace ts {
463438
return isExternalModule(node) || compilerOptions.isolatedModules;
464439
}
465440

466-
export function isBlockScope(node: Node, parentNode: Node) {
441+
function isBlockScope(node: Node, parentNode: Node) {
467442
switch (node.kind) {
468443
case SyntaxKind.SourceFile:
469444
case SyntaxKind.CaseBlock:
@@ -664,29 +639,25 @@ namespace ts {
664639
return getLeadingCommentRanges(sourceFileOfNode.text, node.pos);
665640
}
666641

667-
export function getLeadingCommentRangesOfNodeFromText(node: Node, text: string) {
668-
return getLeadingCommentRanges(text, node.pos);
669-
}
670-
671642
export function getJSDocCommentRanges(node: Node, text: string) {
672643
const commentRanges = (node.kind === SyntaxKind.Parameter ||
673644
node.kind === SyntaxKind.TypeParameter ||
674645
node.kind === SyntaxKind.FunctionExpression ||
675646
node.kind === SyntaxKind.ArrowFunction ||
676647
node.kind === SyntaxKind.ParenthesizedExpression) ?
677648
concatenate(getTrailingCommentRanges(text, node.pos), getLeadingCommentRanges(text, node.pos)) :
678-
getLeadingCommentRangesOfNodeFromText(node, text);
649+
getLeadingCommentRanges(text, node.pos);
679650
// True if the comment starts with '/**' but not if it is '/**/'
680651
return filter(commentRanges, comment =>
681652
text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk &&
682653
text.charCodeAt(comment.pos + 2) === CharacterCodes.asterisk &&
683654
text.charCodeAt(comment.pos + 3) !== CharacterCodes.slash);
684655
}
685656

686-
export let fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*<reference\s+path\s*=\s*)('|")(.+?)\2.*?\/>/;
687-
export let fullTripleSlashReferenceTypeReferenceDirectiveRegEx = /^(\/\/\/\s*<reference\s+types\s*=\s*)('|")(.+?)\2.*?\/>/;
688-
export let fullTripleSlashAMDReferencePathRegEx = /^(\/\/\/\s*<amd-dependency\s+path\s*=\s*)('|")(.+?)\2.*?\/>/;
689-
export let defaultLibReferenceRegEx = /^(\/\/\/\s*<reference\s+no-default-lib\s*=\s*)('|")(.+?)\2\s*\/>/;
657+
export const fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*<reference\s+path\s*=\s*)('|")(.+?)\2.*?\/>/;
658+
const fullTripleSlashReferenceTypeReferenceDirectiveRegEx = /^(\/\/\/\s*<reference\s+types\s*=\s*)('|")(.+?)\2.*?\/>/;
659+
export const fullTripleSlashAMDReferencePathRegEx = /^(\/\/\/\s*<amd-dependency\s+path\s*=\s*)('|")(.+?)\2.*?\/>/;
660+
const defaultLibReferenceRegEx = /^(\/\/\/\s*<reference\s+no-default-lib\s*=\s*)('|")(.+?)\2\s*\/>/;
690661

691662
export function isPartOfTypeNode(node: Node): boolean {
692663
if (SyntaxKind.FirstTypeNode <= node.kind && node.kind <= SyntaxKind.LastTypeNode) {
@@ -2095,10 +2066,6 @@ namespace ts {
20952066
return getParseTreeNode(sourceFile, isSourceFile) || sourceFile;
20962067
}
20972068

2098-
export function getOriginalSourceFiles(sourceFiles: ReadonlyArray<SourceFile>) {
2099-
return sameMap(sourceFiles, getOriginalSourceFile);
2100-
}
2101-
21022069
export const enum Associativity {
21032070
Left,
21042071
Right
@@ -3090,24 +3057,6 @@ namespace ts {
30903057
return false;
30913058
}
30923059

3093-
// Returns false if this heritage clause element's expression contains something unsupported
3094-
// (i.e. not a name or dotted name).
3095-
export function isSupportedExpressionWithTypeArguments(node: ExpressionWithTypeArguments): boolean {
3096-
return isSupportedExpressionWithTypeArgumentsRest(node.expression);
3097-
}
3098-
3099-
function isSupportedExpressionWithTypeArgumentsRest(node: Expression): boolean {
3100-
if (node.kind === SyntaxKind.Identifier) {
3101-
return true;
3102-
}
3103-
else if (isPropertyAccessExpression(node)) {
3104-
return isSupportedExpressionWithTypeArgumentsRest(node.expression);
3105-
}
3106-
else {
3107-
return false;
3108-
}
3109-
}
3110-
31113060
export function isExpressionWithTypeArgumentsInClassExtendsClause(node: Node): boolean {
31123061
return tryGetClassExtendingExpressionWithTypeArguments(node) !== undefined;
31133062
}
@@ -3244,81 +3193,6 @@ namespace ts {
32443193
return carriageReturnLineFeed;
32453194
}
32463195

3247-
/**
3248-
* Tests whether a node and its subtree is simple enough to have its position
3249-
* information ignored when emitting source maps in a destructuring assignment.
3250-
*
3251-
* @param node The expression to test.
3252-
*/
3253-
export function isSimpleExpression(node: Expression): boolean {
3254-
return isSimpleExpressionWorker(node, 0);
3255-
}
3256-
3257-
function isSimpleExpressionWorker(node: Expression, depth: number): boolean {
3258-
if (depth <= 5) {
3259-
const kind = node.kind;
3260-
if (kind === SyntaxKind.StringLiteral
3261-
|| kind === SyntaxKind.NumericLiteral
3262-
|| kind === SyntaxKind.RegularExpressionLiteral
3263-
|| kind === SyntaxKind.NoSubstitutionTemplateLiteral
3264-
|| kind === SyntaxKind.Identifier
3265-
|| kind === SyntaxKind.ThisKeyword
3266-
|| kind === SyntaxKind.SuperKeyword
3267-
|| kind === SyntaxKind.TrueKeyword
3268-
|| kind === SyntaxKind.FalseKeyword
3269-
|| kind === SyntaxKind.NullKeyword) {
3270-
return true;
3271-
}
3272-
else if (kind === SyntaxKind.PropertyAccessExpression) {
3273-
return isSimpleExpressionWorker((<PropertyAccessExpression>node).expression, depth + 1);
3274-
}
3275-
else if (kind === SyntaxKind.ElementAccessExpression) {
3276-
return isSimpleExpressionWorker((<ElementAccessExpression>node).expression, depth + 1)
3277-
&& isSimpleExpressionWorker((<ElementAccessExpression>node).argumentExpression, depth + 1);
3278-
}
3279-
else if (kind === SyntaxKind.PrefixUnaryExpression
3280-
|| kind === SyntaxKind.PostfixUnaryExpression) {
3281-
return isSimpleExpressionWorker((<PrefixUnaryExpression | PostfixUnaryExpression>node).operand, depth + 1);
3282-
}
3283-
else if (kind === SyntaxKind.BinaryExpression) {
3284-
return (<BinaryExpression>node).operatorToken.kind !== SyntaxKind.AsteriskAsteriskToken
3285-
&& isSimpleExpressionWorker((<BinaryExpression>node).left, depth + 1)
3286-
&& isSimpleExpressionWorker((<BinaryExpression>node).right, depth + 1);
3287-
}
3288-
else if (kind === SyntaxKind.ConditionalExpression) {
3289-
return isSimpleExpressionWorker((<ConditionalExpression>node).condition, depth + 1)
3290-
&& isSimpleExpressionWorker((<ConditionalExpression>node).whenTrue, depth + 1)
3291-
&& isSimpleExpressionWorker((<ConditionalExpression>node).whenFalse, depth + 1);
3292-
}
3293-
else if (kind === SyntaxKind.VoidExpression
3294-
|| kind === SyntaxKind.TypeOfExpression
3295-
|| kind === SyntaxKind.DeleteExpression) {
3296-
return isSimpleExpressionWorker((<VoidExpression | TypeOfExpression | DeleteExpression>node).expression, depth + 1);
3297-
}
3298-
else if (kind === SyntaxKind.ArrayLiteralExpression) {
3299-
return (<ArrayLiteralExpression>node).elements.length === 0;
3300-
}
3301-
else if (kind === SyntaxKind.ObjectLiteralExpression) {
3302-
return (<ObjectLiteralExpression>node).properties.length === 0;
3303-
}
3304-
else if (kind === SyntaxKind.CallExpression) {
3305-
if (!isSimpleExpressionWorker((<CallExpression>node).expression, depth + 1)) {
3306-
return false;
3307-
}
3308-
3309-
for (const argument of (<CallExpression>node).arguments) {
3310-
if (!isSimpleExpressionWorker(argument, depth + 1)) {
3311-
return false;
3312-
}
3313-
}
3314-
3315-
return true;
3316-
}
3317-
}
3318-
3319-
return false;
3320-
}
3321-
33223196
/**
33233197
* Formats an enum value as a string for debugging and debug assertions.
33243198
*/
@@ -3391,24 +3265,6 @@ namespace ts {
33913265
return formatEnum(flags, (<any>ts).ObjectFlags, /*isFlags*/ true);
33923266
}
33933267

3394-
export function getRangePos(range: TextRange | undefined) {
3395-
return range ? range.pos : -1;
3396-
}
3397-
3398-
export function getRangeEnd(range: TextRange | undefined) {
3399-
return range ? range.end : -1;
3400-
}
3401-
3402-
/**
3403-
* Increases (or decreases) a position by the provided amount.
3404-
*
3405-
* @param pos The position.
3406-
* @param value The delta.
3407-
*/
3408-
export function movePos(pos: number, value: number) {
3409-
return positionIsSynthesized(pos) ? -1 : pos + value;
3410-
}
3411-
34123268
/**
34133269
* Creates a new TextRange from the provided pos and end.
34143270
*
@@ -3466,26 +3322,6 @@ namespace ts {
34663322
return range.pos === range.end;
34673323
}
34683324

3469-
/**
3470-
* Creates a new TextRange from a provided range with its end position collapsed to its
3471-
* start position.
3472-
*
3473-
* @param range A TextRange.
3474-
*/
3475-
export function collapseRangeToStart(range: TextRange): TextRange {
3476-
return isCollapsedRange(range) ? range : moveRangeEnd(range, range.pos);
3477-
}
3478-
3479-
/**
3480-
* Creates a new TextRange from a provided range with its start position collapsed to its
3481-
* end position.
3482-
*
3483-
* @param range A TextRange.
3484-
*/
3485-
export function collapseRangeToEnd(range: TextRange): TextRange {
3486-
return isCollapsedRange(range) ? range : moveRangePos(range, range.end);
3487-
}
3488-
34893325
/**
34903326
* Creates a new TextRange for a token at the provides start position.
34913327
*
@@ -3549,31 +3385,6 @@ namespace ts {
35493385
return node.initializer !== undefined;
35503386
}
35513387

3552-
/**
3553-
* Gets a value indicating whether a node is merged with a class declaration in the same scope.
3554-
*/
3555-
export function isMergedWithClass(node: Node) {
3556-
if (node.symbol) {
3557-
for (const declaration of node.symbol.declarations) {
3558-
if (declaration.kind === SyntaxKind.ClassDeclaration && declaration !== node) {
3559-
return true;
3560-
}
3561-
}
3562-
}
3563-
3564-
return false;
3565-
}
3566-
3567-
/**
3568-
* Gets a value indicating whether a node is the first declaration of its kind.
3569-
*
3570-
* @param node A Declaration node.
3571-
* @param kind The SyntaxKind to find among related declarations.
3572-
*/
3573-
export function isFirstDeclarationOfKind(node: Node, kind: SyntaxKind) {
3574-
return node.symbol && getDeclarationOfKind(node.symbol, kind) === node;
3575-
}
3576-
35773388
export function isWatchSet(options: CompilerOptions) {
35783389
// Firefox has Object.prototype.watch
35793390
return options.watch && options.hasOwnProperty("watch");

src/services/symbolDisplay.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ namespace ts.SymbolDisplay {
203203
// get the signature from the declaration and write it
204204
const functionDeclaration = <FunctionLike>location.parent;
205205
// Use function declaration to write the signatures only if the symbol corresponding to this declaration
206-
const locationIsSymbolDeclaration = findDeclaration(symbol, declaration =>
206+
const locationIsSymbolDeclaration = find(symbol.declarations, declaration =>
207207
declaration === (location.kind === SyntaxKind.ConstructorKeyword ? functionDeclaration.parent : functionDeclaration));
208208

209209
if (locationIsSymbolDeclaration) {

0 commit comments

Comments
 (0)