UNPKG

10 kBTypeScriptView Raw
1import * as ts from 'typescript';
2import type { TSESTree, TSNode } from './ts-estree';
3import { AST_NODE_TYPES, AST_TOKEN_TYPES } from './ts-estree';
4declare const SyntaxKind: typeof ts.SyntaxKind;
5type LogicalOperatorKind = ts.SyntaxKind.AmpersandAmpersandToken | ts.SyntaxKind.BarBarToken | ts.SyntaxKind.QuestionQuestionToken;
6interface TokenToText extends TSESTree.PunctuatorTokenToText, TSESTree.BinaryOperatorToText {
7 [SyntaxKind.ImportKeyword]: 'import';
8 [SyntaxKind.NewKeyword]: 'new';
9 [SyntaxKind.KeyOfKeyword]: 'keyof';
10 [SyntaxKind.ReadonlyKeyword]: 'readonly';
11 [SyntaxKind.UniqueKeyword]: 'unique';
12}
13type AssignmentOperatorKind = keyof TSESTree.AssignmentOperatorToText;
14type BinaryOperatorKind = keyof TSESTree.BinaryOperatorToText;
15type DeclarationKind = TSESTree.VariableDeclaration['kind'];
16/**
17 * Returns true if the given ts.Token is a logical operator
18 * @param operator the operator token
19 * @returns is a logical operator
20 */
21export declare function isLogicalOperator(operator: ts.BinaryOperatorToken): operator is ts.Token<LogicalOperatorKind>;
22export declare function isESTreeBinaryOperator(operator: ts.BinaryOperatorToken): operator is ts.Token<BinaryOperatorKind>;
23type TokenForTokenKind<T extends ts.SyntaxKind> = T extends keyof TokenToText ? TokenToText[T] : string | undefined;
24/**
25 * Returns the string form of the given TSToken SyntaxKind
26 * @param kind the token's SyntaxKind
27 * @returns the token applicable token as a string
28 */
29export declare function getTextForTokenKind<T extends ts.SyntaxKind>(kind: T): TokenForTokenKind<T>;
30/**
31 * Returns true if the given ts.Node is a valid ESTree class member
32 * @param node TypeScript AST node
33 * @returns is valid ESTree class member
34 */
35export declare function isESTreeClassMember(node: ts.Node): boolean;
36/**
37 * Checks if a ts.Node has a modifier
38 * @param modifierKind TypeScript SyntaxKind modifier
39 * @param node TypeScript AST node
40 * @returns has the modifier specified
41 */
42export declare function hasModifier(modifierKind: ts.KeywordSyntaxKind, node: ts.Node): boolean;
43/**
44 * Get last last modifier in ast
45 * @param node TypeScript AST node
46 * @returns returns last modifier if present or null
47 */
48export declare function getLastModifier(node: ts.Node): ts.Modifier | null;
49/**
50 * Returns true if the given ts.Token is a comma
51 * @param token the TypeScript token
52 * @returns is comma
53 */
54export declare function isComma(token: ts.Node): token is ts.Token<ts.SyntaxKind.CommaToken>;
55/**
56 * Returns true if the given ts.Node is a comment
57 * @param node the TypeScript node
58 * @returns is comment
59 */
60export declare function isComment(node: ts.Node): boolean;
61/**
62 * Returns the binary expression type of the given ts.Token
63 * @param operator the operator token
64 * @returns the binary expression type
65 */
66export declare function getBinaryExpressionType(operator: ts.BinaryOperatorToken): {
67 type: AST_NODE_TYPES.AssignmentExpression;
68 operator: TokenForTokenKind<AssignmentOperatorKind>;
69} | {
70 type: AST_NODE_TYPES.BinaryExpression;
71 operator: TokenForTokenKind<BinaryOperatorKind>;
72} | {
73 type: AST_NODE_TYPES.LogicalExpression;
74 operator: TokenForTokenKind<LogicalOperatorKind>;
75};
76/**
77 * Returns line and column data for the given positions,
78 * @param pos position to check
79 * @param ast the AST object
80 * @returns line and column
81 */
82export declare function getLineAndCharacterFor(pos: number, ast: ts.SourceFile): TSESTree.Position;
83/**
84 * Returns line and column data for the given start and end positions,
85 * for the given AST
86 * @param range start end data
87 * @param ast the AST object
88 * @returns the loc data
89 */
90export declare function getLocFor(range: TSESTree.Range, ast: ts.SourceFile): TSESTree.SourceLocation;
91/**
92 * Check whatever node can contain directive
93 * @returns returns true if node can contain directive
94 */
95export declare function canContainDirective(node: ts.Block | ts.ClassStaticBlockDeclaration | ts.ModuleBlock | ts.SourceFile): boolean;
96/**
97 * Returns range for the given ts.Node
98 * @param node the ts.Node or ts.Token
99 * @param ast the AST object
100 * @returns the range data
101 */
102export declare function getRange(node: Pick<ts.Node, 'getEnd' | 'getStart'>, ast: ts.SourceFile): [number, number];
103/**
104 * Returns true if a given ts.Node is a JSX token
105 * @param node ts.Node to be checked
106 * @returns is a JSX token
107 */
108export declare function isJSXToken(node: ts.Node): boolean;
109/**
110 * Returns the declaration kind of the given ts.Node
111 * @param node TypeScript AST node
112 * @returns declaration kind
113 */
114export declare function getDeclarationKind(node: ts.VariableDeclarationList): DeclarationKind;
115/**
116 * Gets a ts.Node's accessibility level
117 * @param node The ts.Node
118 * @returns accessibility "public", "protected", "private", or null
119 */
120export declare function getTSNodeAccessibility(node: ts.Node): 'private' | 'protected' | 'public' | undefined;
121/**
122 * Finds the next token based on the previous one and its parent
123 * Had to copy this from TS instead of using TS's version because theirs doesn't pass the ast to getChildren
124 * @param previousToken The previous TSToken
125 * @param parent The parent TSNode
126 * @param ast The TS AST
127 * @returns the next TSToken
128 */
129export declare function findNextToken(previousToken: ts.TextRange, parent: ts.Node, ast: ts.SourceFile): ts.Node | undefined;
130/**
131 * Find the first matching ancestor based on the given predicate function.
132 * @param node The current ts.Node
133 * @param predicate The predicate function to apply to each checked ancestor
134 * @returns a matching parent ts.Node
135 */
136export declare function findFirstMatchingAncestor(node: ts.Node, predicate: (node: ts.Node) => boolean): ts.Node | undefined;
137/**
138 * Returns true if a given ts.Node has a JSX token within its hierarchy
139 * @param node ts.Node to be checked
140 * @returns has JSX ancestor
141 */
142export declare function hasJSXAncestor(node: ts.Node): boolean;
143/**
144 * Unescape the text content of string literals, e.g. &amp; -> &
145 * @param text The escaped string literal text.
146 * @returns The unescaped string literal text.
147 */
148export declare function unescapeStringLiteralText(text: string): string;
149/**
150 * Returns true if a given ts.Node is a computed property
151 * @param node ts.Node to be checked
152 * @returns is Computed Property
153 */
154export declare function isComputedProperty(node: ts.Node): node is ts.ComputedPropertyName;
155/**
156 * Returns true if a given ts.Node is optional (has QuestionToken)
157 * @param node ts.Node to be checked
158 * @returns is Optional
159 */
160export declare function isOptional(node: {
161 questionToken?: ts.QuestionToken;
162}): boolean;
163/**
164 * Returns true if the node is an optional chain node
165 */
166export declare function isChainExpression(node: TSESTree.Node): node is TSESTree.ChainExpression;
167/**
168 * Returns true of the child of property access expression is an optional chain
169 */
170export declare function isChildUnwrappableOptionalChain(node: ts.CallExpression | ts.ElementAccessExpression | ts.NonNullExpression | ts.PropertyAccessExpression, child: TSESTree.Node): boolean;
171/**
172 * Returns the type of a given ts.Token
173 * @param token the ts.Token
174 * @returns the token type
175 */
176export declare function getTokenType(token: ts.Identifier | ts.Token<ts.SyntaxKind>): Exclude<AST_TOKEN_TYPES, AST_TOKEN_TYPES.Block | AST_TOKEN_TYPES.Line>;
177/**
178 * Extends and formats a given ts.Token, for a given AST
179 * @param token the ts.Token
180 * @param ast the AST object
181 * @returns the converted Token
182 */
183export declare function convertToken(token: ts.Token<ts.TokenSyntaxKind>, ast: ts.SourceFile): TSESTree.Token;
184/**
185 * Converts all tokens for the given AST
186 * @param ast the AST object
187 * @returns the converted Tokens
188 */
189export declare function convertTokens(ast: ts.SourceFile): TSESTree.Token[];
190export declare class TSError extends Error {
191 readonly fileName: string;
192 readonly location: {
193 start: {
194 line: number;
195 column: number;
196 offset: number;
197 };
198 end: {
199 line: number;
200 column: number;
201 offset: number;
202 };
203 };
204 constructor(message: string, fileName: string, location: {
205 start: {
206 line: number;
207 column: number;
208 offset: number;
209 };
210 end: {
211 line: number;
212 column: number;
213 offset: number;
214 };
215 });
216 get index(): number;
217 get lineNumber(): number;
218 get column(): number;
219}
220/**
221 * @param message the error message
222 * @param ast the AST object
223 * @param startIndex the index at which the error starts
224 * @param endIndex the index at which the error ends
225 * @returns converted error object
226 */
227export declare function createError(message: string, ast: ts.SourceFile, startIndex: number, endIndex?: number): TSError;
228export declare function nodeHasIllegalDecorators(node: ts.Node): node is ts.Node & {
229 illegalDecorators: ts.Node[];
230};
231/**
232 * @param n the TSNode
233 * @param ast the TS AST
234 */
235export declare function nodeHasTokens(n: ts.Node, ast: ts.SourceFile): boolean;
236/**
237 * Like `forEach`, but suitable for use with numbers and strings (which may be falsy).
238 */
239export declare function firstDefined<T, U>(array: readonly T[] | undefined, callback: (element: T, index: number) => U | undefined): U | undefined;
240export declare function identifierIsThisKeyword(id: ts.Identifier): boolean;
241export declare function isThisIdentifier(node: ts.Node | undefined): node is ts.Identifier;
242export declare function isThisInTypeQuery(node: ts.Node): boolean;
243export declare function nodeIsPresent(node: ts.Node | undefined): node is ts.Node;
244export declare function getContainingFunction(node: ts.Node): ts.SignatureDeclaration | undefined;
245export declare function nodeCanBeDecorated(node: TSNode): boolean;
246export declare function isValidAssignmentTarget(node: ts.Node): boolean;
247export declare function getNamespaceModifiers(node: ts.ModuleDeclaration): ts.Modifier[] | undefined;
248export {};
249//# sourceMappingURL=node-utils.d.ts.map
\No newline at end of file