UNPKG

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