This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
/
utils.ts
438 lines (391 loc) · 18.2 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/**
* @license
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as path from "path";
import { isBlockScopedVariableDeclarationList, isPrefixUnaryExpression } from "tsutils";
import * as ts from "typescript";
import {IDisabledInterval, RuleFailure} from "./rule/rule"; // tslint:disable-line deprecation
export function getSourceFile(fileName: string, source: string): ts.SourceFile {
const normalizedName = path.normalize(fileName).replace(/\\/g, "/");
return ts.createSourceFile(normalizedName, source, ts.ScriptTarget.ES5, /*setParentNodes*/ true);
}
/** @deprecated See IDisabledInterval. */
export function doesIntersect(failure: RuleFailure, disabledIntervals: IDisabledInterval[]): boolean { // tslint:disable-line deprecation
return disabledIntervals.some((interval) => {
const maxStart = Math.max(interval.startPosition, failure.getStartPosition().getPosition());
const minEnd = Math.min(interval.endPosition, failure.getEndPosition().getPosition());
return maxStart <= minEnd;
});
}
/**
* @returns true if any modifier kinds passed along exist in the given modifiers array
*/
export function hasModifier(modifiers: ts.ModifiersArray | undefined, ...modifierKinds: ts.SyntaxKind[]): boolean {
if (modifiers === undefined || modifierKinds.length === 0) {
return false;
}
return modifiers.some((m) => {
return modifierKinds.some((k) => m.kind === k);
});
}
/**
* Determines if the appropriate bit in the parent (VariableDeclarationList) is set,
* which indicates this is a "let" or "const".
*/
export function isBlockScopedVariable(node: ts.VariableDeclaration | ts.VariableStatement): boolean {
if (node.kind === ts.SyntaxKind.VariableDeclaration) {
const parent = node.parent!;
return parent.kind === ts.SyntaxKind.CatchClause || isBlockScopedVariableDeclarationList(parent);
} else {
return isBlockScopedVariableDeclarationList(node.declarationList);
}
}
export function isBlockScopedBindingElement(node: ts.BindingElement): boolean {
const variableDeclaration = getBindingElementVariableDeclaration(node);
// if no variable declaration, it must be a function param, which is block scoped
return (variableDeclaration == null) || isBlockScopedVariable(variableDeclaration);
}
export function getBindingElementVariableDeclaration(node: ts.BindingElement): ts.VariableDeclaration | null {
let currentParent = node.parent! as ts.Node;
while (currentParent.kind !== ts.SyntaxKind.VariableDeclaration) {
if (currentParent.parent == null) {
return null; // function parameter, no variable declaration
} else {
currentParent = currentParent.parent;
}
}
return currentParent as ts.VariableDeclaration;
}
/**
* Finds a child of a given node with a given kind.
* Note: This uses `node.getChildren()`, which does extra parsing work to include tokens.
*/
export function childOfKind(node: ts.Node, kind: ts.SyntaxKind): ts.Node | undefined {
return node.getChildren().find((child) => child.kind === kind);
}
/**
* @returns true if some ancestor of `node` satisfies `predicate`, including `node` itself.
*/
export function someAncestor(node: ts.Node, predicate: (n: ts.Node) => boolean): boolean {
return predicate(node) || (node.parent != null && someAncestor(node.parent, predicate));
}
export function ancestorWhere<T extends ts.Node>(node: ts.Node, predicate: (n: ts.Node) => boolean): ts.Node | undefined {
let cur: ts.Node | undefined = node;
do {
if (predicate(cur)) {
return cur as T;
}
cur = cur.parent;
} while (cur !== undefined);
return undefined;
}
export function isAssignment(node: ts.Node) {
if (node.kind === ts.SyntaxKind.BinaryExpression) {
const binaryExpression = node as ts.BinaryExpression;
return binaryExpression.operatorToken.kind >= ts.SyntaxKind.FirstAssignment
&& binaryExpression.operatorToken.kind <= ts.SyntaxKind.LastAssignment;
} else {
return false;
}
}
/**
* Bitwise check for node flags.
*/
export function isNodeFlagSet(node: ts.Node, flagToCheck: ts.NodeFlags): boolean {
// tslint:disable-next-line:no-bitwise
return (node.flags & flagToCheck) !== 0;
}
/**
* Bitwise check for combined node flags.
*/
export function isCombinedNodeFlagSet(node: ts.Node, flagToCheck: ts.NodeFlags): boolean {
// tslint:disable-next-line:no-bitwise
return (ts.getCombinedNodeFlags(node) & flagToCheck) !== 0;
}
/**
* Bitwise check for combined modifier flags.
*/
export function isCombinedModifierFlagSet(node: ts.Node, flagToCheck: ts.ModifierFlags): boolean {
// tslint:disable-next-line:no-bitwise
return (ts.getCombinedModifierFlags(node) & flagToCheck) !== 0;
}
/**
* Bitwise check for type flags.
*/
export function isTypeFlagSet(type: ts.Type, flagToCheck: ts.TypeFlags): boolean {
// tslint:disable-next-line:no-bitwise
return (type.flags & flagToCheck) !== 0;
}
/**
* Bitwise check for symbol flags.
*/
export function isSymbolFlagSet(symbol: ts.Symbol, flagToCheck: ts.SymbolFlags): boolean {
// tslint:disable-next-line:no-bitwise
return (symbol.flags & flagToCheck) !== 0;
}
/**
* Bitwise check for object flags.
* Does not work with TypeScript 2.0.x
*/
export function isObjectFlagSet(objectType: ts.ObjectType, flagToCheck: ts.ObjectFlags): boolean {
// tslint:disable-next-line:no-bitwise
return (objectType.objectFlags & flagToCheck) !== 0;
}
/**
* @returns true if decl is a nested module declaration, i.e. represents a segment of a dotted module path.
*/
export function isNestedModuleDeclaration(decl: ts.ModuleDeclaration) {
// in a declaration expression like 'module a.b.c' - 'a' is the top level module declaration node and 'b' and 'c'
// are nested therefore we can depend that a node's position will only match with its name's position for nested
// nodes
return decl.name.pos === decl.pos;
}
export function unwrapParentheses(node: ts.Expression) {
while (node.kind === ts.SyntaxKind.ParenthesizedExpression) {
node = (node as ts.ParenthesizedExpression).expression;
}
return node;
}
export function isScopeBoundary(node: ts.Node): boolean {
return node.kind === ts.SyntaxKind.FunctionDeclaration
|| node.kind === ts.SyntaxKind.FunctionExpression
|| node.kind === ts.SyntaxKind.PropertyAssignment
|| node.kind === ts.SyntaxKind.ShorthandPropertyAssignment
|| node.kind === ts.SyntaxKind.MethodDeclaration
|| node.kind === ts.SyntaxKind.Constructor
|| node.kind === ts.SyntaxKind.ModuleDeclaration
|| node.kind === ts.SyntaxKind.ArrowFunction
|| node.kind === ts.SyntaxKind.ParenthesizedExpression
|| node.kind === ts.SyntaxKind.ClassDeclaration
|| node.kind === ts.SyntaxKind.ClassExpression
|| node.kind === ts.SyntaxKind.InterfaceDeclaration
|| node.kind === ts.SyntaxKind.GetAccessor
|| node.kind === ts.SyntaxKind.SetAccessor
|| node.kind === ts.SyntaxKind.SourceFile && ts.isExternalModule(node as ts.SourceFile);
}
export function isBlockScopeBoundary(node: ts.Node): boolean {
return isScopeBoundary(node)
|| node.kind === ts.SyntaxKind.Block
|| isLoop(node)
|| node.kind === ts.SyntaxKind.WithStatement
|| node.kind === ts.SyntaxKind.SwitchStatement
|| node.parent !== undefined
&& (node.parent.kind === ts.SyntaxKind.TryStatement
|| node.parent.kind === ts.SyntaxKind.IfStatement);
}
export function isLoop(node: ts.Node): node is ts.IterationStatement {
return node.kind === ts.SyntaxKind.DoStatement
|| node.kind === ts.SyntaxKind.WhileStatement
|| node.kind === ts.SyntaxKind.ForStatement
|| node.kind === ts.SyntaxKind.ForInStatement
|| node.kind === ts.SyntaxKind.ForOfStatement;
}
export interface TokenPosition {
/** The start of the token including all trivia before it */
fullStart: number;
/** The start of the token */
tokenStart: number;
/** The end of the token */
end: number;
}
export type ForEachTokenCallback = (fullText: string, kind: ts.SyntaxKind, pos: TokenPosition, parent: ts.Node) => void;
export type ForEachCommentCallback = (fullText: string, kind: ts.SyntaxKind, pos: TokenPosition) => void;
export type FilterCallback = (node: ts.Node) => boolean;
/**
* Iterate over all tokens of `node`
*
* @description JsDoc comments are treated like regular comments and only visited if `skipTrivia` === false.
*
* @param node The node whose tokens should be visited
* @param skipTrivia If set to false all trivia preceeding `node` or any of its children is included
* @param cb Is called for every token of `node`. It gets the full text of the SourceFile and the position of the token within that text.
* @param filter If provided, will be called for every Node and Token found. If it returns false `cb` will not be called for this subtree.
*/
export function forEachToken(node: ts.Node, skipTrivia: boolean, cb: ForEachTokenCallback, filter?: FilterCallback) {
// this function will most likely be called with SourceFile anyways, so there is no need for an additional parameter
const sourceFile = node.getSourceFile();
const fullText = sourceFile.text;
const iterateFn = filter === undefined ? iterateChildren : iterateWithFilter;
const handleTrivia = skipTrivia ? undefined : createTriviaHandler(sourceFile, cb);
iterateFn(node);
// this function is used to save the if condition for the common case where no filter is provided
function iterateWithFilter(child: ts.Node): void {
if (filter!(child)) {
return iterateChildren(child);
}
}
function iterateChildren(child: ts.Node): void {
if (child.kind < ts.SyntaxKind.FirstNode ||
// for backwards compatibility to typescript 2.0.10
// JsxText was no Token, but a Node in that version
child.kind === ts.SyntaxKind.JsxText) {
// we found a token, tokens have no children, stop recursing here
return callback(child);
}
/* Exclude everything contained in JsDoc, it will be handled with the other trivia anyway.
* When we would handle JsDoc tokens like regular ones, we would scan some trivia multiple times.
* Even worse, we would scan for trivia inside the JsDoc comment, which yields unexpected results.*/
if (child.kind !== ts.SyntaxKind.JSDocComment) {
// recurse into Node's children to find tokens
return child.getChildren(sourceFile).forEach(iterateFn);
}
}
function callback(token: ts.Node) {
const tokenStart = token.getStart(sourceFile);
if (!skipTrivia && tokenStart !== token.pos) {
// we only have to handle trivia before each token, because there is nothing after EndOfFileToken
handleTrivia!(token.pos, tokenStart, token);
}
return cb(fullText, token.kind, {tokenStart, fullStart: token.pos, end: token.end}, token.parent!);
}
}
function createTriviaHandler(sourceFile: ts.SourceFile, cb: ForEachTokenCallback) {
const fullText = sourceFile.text;
const scanner = ts.createScanner(sourceFile.languageVersion, false, sourceFile.languageVariant, fullText);
/**
* Scan the specified range to get all trivia tokens.
* This includes trailing trivia of the last token and the leading trivia of the current token
*/
function handleTrivia(start: number, end: number, token: ts.Node) {
const parent = token.parent!;
// prevent false positives by not scanning inside JsxText
if (!canHaveLeadingTrivia(token.kind, parent)) {
return;
}
scanner.setTextPos(start);
let position: number;
// we only get here if start !== end, so we can scan at least one time
do {
const kind = scanner.scan();
position = scanner.getTextPos();
cb(fullText, kind, {tokenStart: scanner.getTokenPos(), end: position, fullStart: start}, parent);
} while (position < end);
}
return handleTrivia;
}
/** Iterate over all comments owned by `node` or its children */
export function forEachComment(node: ts.Node, cb: ForEachCommentCallback) {
/* Visit all tokens and skip trivia.
Comment ranges between tokens are parsed without the need of a scanner.
forEachToken also does intentionally not pay attention to the correct comment ownership of nodes as it always
scans all trivia before each token, which could include trailing comments of the previous token.
Comment onwership is done right in this function*/
return forEachToken(node, true, (fullText, tokenKind, pos, parent) => {
// don't search for comments inside JsxText
if (canHaveLeadingTrivia(tokenKind, parent)) {
// Comments before the first token (pos.fullStart === 0) are all considered leading comments, so no need for special treatment
const comments = ts.getLeadingCommentRanges(fullText, pos.fullStart);
if (comments !== undefined) {
for (const comment of comments) {
cb(fullText, comment.kind, {fullStart: pos.fullStart, tokenStart: comment.pos, end: comment.end});
}
}
}
if (canHaveTrailingTrivia(tokenKind, parent)) {
const comments = ts.getTrailingCommentRanges(fullText, pos.end);
if (comments !== undefined) {
for (const comment of comments) {
cb(fullText, comment.kind, {fullStart: pos.fullStart, tokenStart: comment.pos, end: comment.end});
}
}
}
});
}
/** Exclude leading positions that would lead to scanning for trivia inside JsxText */
function canHaveLeadingTrivia(tokenKind: ts.SyntaxKind, parent: ts.Node): boolean {
switch (tokenKind) {
case ts.SyntaxKind.JsxText:
return false; // there is no trivia before JsxText
case ts.SyntaxKind.OpenBraceToken:
// before a JsxExpression inside a JsxElement's body can only be other JsxChild, but no trivia
return parent.kind !== ts.SyntaxKind.JsxExpression || parent.parent!.kind !== ts.SyntaxKind.JsxElement;
case ts.SyntaxKind.LessThanToken:
switch (parent.kind) {
case ts.SyntaxKind.JsxClosingElement:
return false; // would be inside the element body
case ts.SyntaxKind.JsxOpeningElement:
case ts.SyntaxKind.JsxSelfClosingElement:
// there can only be leading trivia if we are at the end of the top level element
return parent.parent!.parent!.kind !== ts.SyntaxKind.JsxElement;
default:
return true;
}
default:
return true;
}
}
/** Exclude trailing positions that would lead to scanning for trivia inside JsxText */
function canHaveTrailingTrivia(tokenKind: ts.SyntaxKind, parent: ts.Node): boolean {
switch (tokenKind) {
case ts.SyntaxKind.JsxText:
// there is no trivia after JsxText
return false;
case ts.SyntaxKind.CloseBraceToken:
// after a JsxExpression inside a JsxElement's body can only be other JsxChild, but no trivia
return parent.kind !== ts.SyntaxKind.JsxExpression || parent.parent!.kind !== ts.SyntaxKind.JsxElement;
case ts.SyntaxKind.GreaterThanToken:
switch (parent.kind) {
case ts.SyntaxKind.JsxOpeningElement:
return false; // would be inside the element
case ts.SyntaxKind.JsxClosingElement:
case ts.SyntaxKind.JsxSelfClosingElement:
// there can only be trailing trivia if we are at the end of the top level element
return parent.parent!.parent!.kind !== ts.SyntaxKind.JsxElement;
default:
return true;
}
default:
return true;
}
}
/**
* Checks if there are any comments between `position` and the next non-trivia token
*
* @param text The text to scan
* @param position The position inside `text` where to start scanning. Make sure that this is a valid start position.
* This value is typically obtained from `node.getFullStart()` or `node.getEnd()`
*/
export function hasCommentAfterPosition(text: string, position: number): boolean {
return ts.getTrailingCommentRanges(text, position) !== undefined ||
ts.getLeadingCommentRanges(text, position) !== undefined;
}
export interface EqualsKind {
isPositive: boolean; // True for "===" and "=="
isStrict: boolean; // True for "===" and "!=="
}
export function getEqualsKind(node: ts.BinaryOperatorToken): EqualsKind | undefined {
switch (node.kind) {
case ts.SyntaxKind.EqualsEqualsToken:
return { isPositive: true, isStrict: false };
case ts.SyntaxKind.EqualsEqualsEqualsToken:
return { isPositive: true, isStrict: true };
case ts.SyntaxKind.ExclamationEqualsToken:
return { isPositive: false, isStrict: false };
case ts.SyntaxKind.ExclamationEqualsEqualsToken:
return { isPositive: false, isStrict: true };
default:
return undefined;
}
}
export function isStrictNullChecksEnabled(options: ts.CompilerOptions): boolean {
return options.strictNullChecks === true ||
(options.strict === true && options.strictNullChecks !== false);
}
export function isNegativeNumberLiteral(node: ts.Node): node is ts.PrefixUnaryExpression & { operand: ts.NumericLiteral } {
return isPrefixUnaryExpression(node) &&
node.operator === ts.SyntaxKind.MinusToken &&
node.operand.kind === ts.SyntaxKind.NumericLiteral;
}