1 import * as ts from 'typescript';
2 import { AST_NODE_TYPES, AST_TOKEN_TYPES, TSESTree } from './ts-estree';
3 declare const SyntaxKind: typeof ts.SyntaxKind;
4 interface TokenToText {
5 [SyntaxKind.OpenBraceToken]: '{';
6 [SyntaxKind.CloseBraceToken]: '}';
7 [SyntaxKind.OpenParenToken]: '(';
8 [SyntaxKind.CloseParenToken]: ')';
9 [SyntaxKind.OpenBracketToken]: '[';
10 [SyntaxKind.CloseBracketToken]: ']';
11 [SyntaxKind.DotToken]: '.';
12 [SyntaxKind.DotDotDotToken]: '...';
13 [SyntaxKind.SemicolonToken]: ';';
14 [SyntaxKind.CommaToken]: ',';
15 [SyntaxKind.LessThanToken]: '<';
16 [SyntaxKind.GreaterThanToken]: '>';
17 [SyntaxKind.LessThanEqualsToken]: '<=';
18 [SyntaxKind.GreaterThanEqualsToken]: '>=';
19 [SyntaxKind.EqualsEqualsToken]: '==';
20 [SyntaxKind.ExclamationEqualsToken]: '!=';
21 [SyntaxKind.EqualsEqualsEqualsToken]: '===';
22 [SyntaxKind.InstanceOfKeyword]: 'instanceof';
23 [SyntaxKind.ExclamationEqualsEqualsToken]: '!==';
24 [SyntaxKind.EqualsGreaterThanToken]: '=>';
25 [SyntaxKind.PlusToken]: '+';
26 [SyntaxKind.MinusToken]: '-';
27 [SyntaxKind.AsteriskToken]: '*';
28 [SyntaxKind.AsteriskAsteriskToken]: '**';
29 [SyntaxKind.SlashToken]: '/';
30 [SyntaxKind.PercentToken]: '%';
31 [SyntaxKind.PlusPlusToken]: '++';
32 [SyntaxKind.MinusMinusToken]: '--';
33 [SyntaxKind.LessThanLessThanToken]: '<<';
34 [SyntaxKind.LessThanSlashToken]: '</';
35 [SyntaxKind.GreaterThanGreaterThanToken]: '>>';
36 [SyntaxKind.GreaterThanGreaterThanGreaterThanToken]: '>>>';
37 [SyntaxKind.AmpersandToken]: '&';
38 [SyntaxKind.BarToken]: '|';
39 [SyntaxKind.CaretToken]: '^';
40 [SyntaxKind.ExclamationToken]: '!';
41 [SyntaxKind.TildeToken]: '~';
42 [SyntaxKind.AmpersandAmpersandToken]: '&&';
43 [SyntaxKind.BarBarToken]: '||';
44 [SyntaxKind.QuestionToken]: '?';
45 [SyntaxKind.ColonToken]: ':';
46 [SyntaxKind.EqualsToken]: '=';
47 [SyntaxKind.PlusEqualsToken]: '+=';
48 [SyntaxKind.MinusEqualsToken]: '-=';
49 [SyntaxKind.AsteriskEqualsToken]: '*=';
50 [SyntaxKind.AsteriskAsteriskEqualsToken]: '**=';
51 [SyntaxKind.SlashEqualsToken]: '/=';
52 [SyntaxKind.PercentEqualsToken]: '%=';
53 [SyntaxKind.LessThanLessThanEqualsToken]: '<<=';
54 [SyntaxKind.GreaterThanGreaterThanEqualsToken]: '>>=';
55 [SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken]: '>>>=';
56 [SyntaxKind.AmpersandEqualsToken]: '&=';
57 [SyntaxKind.BarEqualsToken]: '|=';
58 [SyntaxKind.CaretEqualsToken]: '^=';
59 [SyntaxKind.AtToken]: '@';
60 [SyntaxKind.InKeyword]: 'in';
61 [SyntaxKind.UniqueKeyword]: 'unique';
62 [SyntaxKind.KeyOfKeyword]: 'keyof';
63 [SyntaxKind.NewKeyword]: 'new';
64 [SyntaxKind.ImportKeyword]: 'import';
65 [SyntaxKind.ReadonlyKeyword]: 'readonly';
66 [SyntaxKind.QuestionQuestionToken]: '??';
67 [SyntaxKind.QuestionDotToken]: '?.';
70 * Returns true if the given ts.Token is the assignment operator
71 * @param operator the operator token
72 * @returns is assignment
74 export declare function isAssignmentOperator<T extends ts.SyntaxKind>(operator: ts.Token<T>): boolean;
76 * Returns true if the given ts.Token is a logical operator
77 * @param operator the operator token
78 * @returns is a logical operator
80 export declare function isLogicalOperator<T extends ts.SyntaxKind>(operator: ts.Token<T>): boolean;
82 * Returns the string form of the given TSToken SyntaxKind
83 * @param kind the token's SyntaxKind
84 * @returns the token applicable token as a string
86 export declare function getTextForTokenKind<T extends ts.SyntaxKind>(kind: T): T extends keyof TokenToText ? TokenToText[T] : string | undefined;
88 * Returns true if the given ts.Node is a valid ESTree class member
89 * @param node TypeScript AST node
90 * @returns is valid ESTree class member
92 export declare function isESTreeClassMember(node: ts.Node): boolean;
94 * Checks if a ts.Node has a modifier
95 * @param modifierKind TypeScript SyntaxKind modifier
96 * @param node TypeScript AST node
97 * @returns has the modifier specified
99 export declare function hasModifier(modifierKind: ts.KeywordSyntaxKind, node: ts.Node): boolean;
101 * Get last last modifier in ast
102 * @param node TypeScript AST node
103 * @returns returns last modifier if present or null
105 export declare function getLastModifier(node: ts.Node): ts.Modifier | null;
107 * Returns true if the given ts.Token is a comma
108 * @param token the TypeScript token
111 export declare function isComma(token: ts.Node): boolean;
113 * Returns true if the given ts.Node is a comment
114 * @param node the TypeScript node
115 * @returns is comment
117 export declare function isComment(node: ts.Node): boolean;
119 * Returns true if the given ts.Node is a JSDoc comment
120 * @param node the TypeScript node
121 * @returns is JSDoc comment
123 export declare function isJSDocComment(node: ts.Node): boolean;
125 * Returns the binary expression type of the given ts.Token
126 * @param operator the operator token
127 * @returns the binary expression type
129 export declare function getBinaryExpressionType<T extends ts.SyntaxKind>(operator: ts.Token<T>): AST_NODE_TYPES.AssignmentExpression | AST_NODE_TYPES.LogicalExpression | AST_NODE_TYPES.BinaryExpression;
131 * Returns line and column data for the given positions,
132 * @param pos position to check
133 * @param ast the AST object
134 * @returns line and column
136 export declare function getLineAndCharacterFor(pos: number, ast: ts.SourceFile): TSESTree.LineAndColumnData;
138 * Returns line and column data for the given start and end positions,
140 * @param start start data
141 * @param end end data
142 * @param ast the AST object
143 * @returns the loc data
145 export declare function getLocFor(start: number, end: number, ast: ts.SourceFile): TSESTree.SourceLocation;
147 * Check whatever node can contain directive
149 * @returns returns true if node can contain directive
151 export declare function canContainDirective(node: ts.SourceFile | ts.Block | ts.ModuleBlock): boolean;
153 * Returns range for the given ts.Node
154 * @param node the ts.Node or ts.Token
155 * @param ast the AST object
156 * @returns the range data
158 export declare function getRange(node: ts.Node, ast: ts.SourceFile): [number, number];
160 * Returns true if a given ts.Node is a token
161 * @param node the ts.Node
162 * @returns is a token
164 export declare function isToken(node: ts.Node): boolean;
166 * Returns true if a given ts.Node is a JSX token
167 * @param node ts.Node to be checked
168 * @returns is a JSX token
170 export declare function isJSXToken(node: ts.Node): boolean;
172 * Returns the declaration kind of the given ts.Node
173 * @param node TypeScript AST node
174 * @returns declaration kind
176 export declare function getDeclarationKind(node: ts.VariableDeclarationList): 'let' | 'const' | 'var';
178 * Gets a ts.Node's accessibility level
179 * @param node The ts.Node
180 * @returns accessibility "public", "protected", "private", or null
182 export declare function getTSNodeAccessibility(node: ts.Node): 'public' | 'protected' | 'private' | null;
184 * Finds the next token based on the previous one and its parent
185 * Had to copy this from TS instead of using TS's version because theirs doesn't pass the ast to getChildren
186 * @param previousToken The previous TSToken
187 * @param parent The parent TSNode
188 * @param ast The TS AST
189 * @returns the next TSToken
191 export declare function findNextToken(previousToken: ts.TextRange, parent: ts.Node, ast: ts.SourceFile): ts.Node | undefined;
193 * Find the first matching ancestor based on the given predicate function.
194 * @param node The current ts.Node
195 * @param predicate The predicate function to apply to each checked ancestor
196 * @returns a matching parent ts.Node
198 export declare function findFirstMatchingAncestor(node: ts.Node, predicate: (node: ts.Node) => boolean): ts.Node | undefined;
200 * Returns true if a given ts.Node has a JSX token within its hierarchy
201 * @param node ts.Node to be checked
202 * @returns has JSX ancestor
204 export declare function hasJSXAncestor(node: ts.Node): boolean;
206 * Unescape the text content of string literals, e.g. & -> &
207 * @param text The escaped string literal text.
208 * @returns The unescaped string literal text.
210 export declare function unescapeStringLiteralText(text: string): string;
212 * Returns true if a given ts.Node is a computed property
213 * @param node ts.Node to be checked
214 * @returns is Computed Property
216 export declare function isComputedProperty(node: ts.Node): boolean;
218 * Returns true if a given ts.Node is optional (has QuestionToken)
219 * @param node ts.Node to be checked
220 * @returns is Optional
222 export declare function isOptional(node: {
223 questionToken?: ts.QuestionToken;
226 * Returns true if the node is an optional chain node
228 export declare function isOptionalChain(node: TSESTree.Node): node is TSESTree.OptionalCallExpression | TSESTree.OptionalMemberExpression;
230 * Returns true of the child of property access expression is an optional chain
232 export declare function isChildOptionalChain(node: ts.PropertyAccessExpression | ts.ElementAccessExpression | ts.CallExpression, object: TSESTree.LeftHandSideExpression): boolean;
234 * Returns the type of a given ts.Token
235 * @param token the ts.Token
236 * @returns the token type
238 export declare function getTokenType(token: ts.Identifier | ts.Token<ts.SyntaxKind>): Exclude<AST_TOKEN_TYPES, AST_TOKEN_TYPES.Line | AST_TOKEN_TYPES.Block>;
240 * Extends and formats a given ts.Token, for a given AST
241 * @param token the ts.Token
242 * @param ast the AST object
243 * @returns the converted Token
245 export declare function convertToken(token: ts.Node, ast: ts.SourceFile): TSESTree.Token;
247 * Converts all tokens for the given AST
248 * @param ast the AST object
249 * @returns the converted Tokens
251 export declare function convertTokens(ast: ts.SourceFile): TSESTree.Token[];
252 export interface TSError {
259 * @param ast the AST object
260 * @param start the index at which the error starts
261 * @param message the error message
262 * @returns converted error object
264 export declare function createError(ast: ts.SourceFile, start: number, message: string): TSError;
266 * @param n the TSNode
267 * @param ast the TS AST
269 export declare function nodeHasTokens(n: ts.Node, ast: ts.SourceFile): boolean;
271 * Like `forEach`, but suitable for use with numbers and strings (which may be falsy).
277 export declare function firstDefined<T, U>(array: readonly T[] | undefined, callback: (element: T, index: number) => U | undefined): U | undefined;
279 //# sourceMappingURL=node-utils.d.ts.map