typescript-estree
Version:
A parser that converts TypeScript source code into an ESTree compatible form
231 lines (230 loc) • 9.63 kB
TypeScript
/**
* @fileoverview Utilities for finding and converting ts.Nodes into ESTreeNodes
* @author James Henry <https://github.com/JamesHenry>
* @copyright jQuery Foundation and other contributors, https://jquery.org/
* MIT License
*/
import ts from 'typescript';
import { ESTreeNodeLoc, ESTreeNode, ESTreeToken } from './temp-types-based-on-js-source';
import { AST_NODE_TYPES } from './ast-node-types';
/**
* Returns true if the given ts.Token is the assignment operator
* @param {ts.Token} operator the operator token
* @returns {boolean} is assignment
*/
export declare function isAssignmentOperator(operator: ts.Token<ts.AssignmentOperator>): boolean;
/**
* Returns true if the given ts.Token is a logical operator
* @param {ts.Token} operator the operator token
* @returns {boolean} is a logical operator
*/
export declare function isLogicalOperator(operator: ts.Token<ts.LogicalOperator>): boolean;
/**
* Returns the string form of the given TSToken SyntaxKind
* @param {number} kind the token's SyntaxKind
* @returns {string} the token applicable token as a string
*/
export declare function getTextForTokenKind(kind: ts.SyntaxKind): string | undefined;
/**
* Returns true if the given ts.Node is a valid ESTree class member
* @param {ts.Node} node TypeScript AST node
* @returns {boolean} is valid ESTree class member
*/
export declare function isESTreeClassMember(node: ts.Node): boolean;
/**
* Checks if a ts.Node has a modifier
* @param {ts.KeywordSyntaxKind} modifierKind TypeScript SyntaxKind modifier
* @param {ts.Node} node TypeScript AST node
* @returns {boolean} has the modifier specified
*/
export declare function hasModifier(modifierKind: ts.KeywordSyntaxKind, node: ts.Node): boolean;
/**
* Get last last modifier in ast
* @param node TypeScript AST node
* @returns returns last modifier if present or null
*/
export declare function getLastModifier(node: ts.Node): ts.Node | null;
/**
* Returns true if the given ts.Token is a comma
* @param {ts.Node} token the TypeScript token
* @returns {boolean} is comma
*/
export declare function isComma(token: ts.Node): boolean;
/**
* Returns true if the given ts.Node is a comment
* @param {ts.Node} node the TypeScript node
* @returns {boolean} is comment
*/
export declare function isComment(node: ts.Node): boolean;
/**
* Returns true if the given ts.Node is a JSDoc comment
* @param {ts.Node} node the TypeScript node
* @returns {boolean} is JSDoc comment
*/
export declare function isJSDocComment(node: ts.Node): boolean;
/**
* Returns the binary expression type of the given ts.Token
* @param {ts.Token} operator the operator token
* @returns {string} the binary expression type
*/
export declare function getBinaryExpressionType(operator: ts.Token<any>): AST_NODE_TYPES.AssignmentExpression | AST_NODE_TYPES.LogicalExpression | AST_NODE_TYPES.BinaryExpression;
/**
* Returns line and column data for the given start and end positions,
* for the given AST
* @param {number} start start data
* @param {number} end end data
* @param {ts.SourceFile} ast the AST object
* @returns {ESTreeNodeLoc} the loc data
*/
export declare function getLocFor(start: number, end: number, ast: ts.SourceFile): ESTreeNodeLoc;
/**
* Check whatever node can contain directive
* @param {ts.Node} node
* @returns {boolean} returns true if node can contain directive
*/
export declare function canContainDirective(node: ts.Node): boolean;
/**
* Returns line and column data for the given ts.Node or ts.Token,
* for the given AST
* @param {ts.Node} nodeOrToken the ts.Node or ts.Token
* @param {ts.SourceFile} ast the AST object
* @returns {ESTreeLoc} the loc data
*/
export declare function getLoc(nodeOrToken: ts.Node, ast: ts.SourceFile): ESTreeNodeLoc;
/**
* Returns true if a given ts.Node is a token
* @param {ts.Node} node the ts.Node
* @returns {boolean} is a token
*/
export declare function isToken(node: ts.Node): boolean;
/**
* Returns true if a given ts.Node is a JSX token
* @param {ts.Node} node ts.Node to be checked
* @returns {boolean} is a JSX token
*/
export declare function isJSXToken(node: ts.Node): boolean;
/**
* Returns the declaration kind of the given ts.Node
* @param {ts.VariableDeclarationList} node TypeScript AST node
* @returns {string} declaration kind
*/
export declare function getDeclarationKind(node: ts.VariableDeclarationList): 'let' | 'const' | 'var';
/**
* Gets a ts.Node's accessibility level
* @param {ts.Node} node The ts.Node
* @returns {string | null} accessibility "public", "protected", "private", or null
*/
export declare function getTSNodeAccessibility(node: ts.Node): 'public' | 'protected' | 'private' | null;
/**
* Finds the next token based on the previous one and its parent
* Had to copy this from TS instead of using TS's version because theirs doesn't pass the ast to getChildren
* @param {ts.Node} previousToken The previous TSToken
* @param {ts.Node} parent The parent TSNode
* @param {ts.SourceFile} ast The TS AST
* @returns {ts.Node|undefined} the next TSToken
*/
export declare function findNextToken(previousToken: ts.Node, parent: ts.Node, ast: ts.SourceFile): ts.Node | undefined;
/**
* Find the first matching token based on the given predicate function.
* @param {ts.Node} previousToken The previous ts.Token
* @param {ts.Node} parent The parent ts.Node
* @param {Function} predicate The predicate function to apply to each checked token
* @param {ts.SourceFile} ast The TS AST
* @returns {ts.Node|undefined} a matching ts.Token
*/
export declare function findFirstMatchingToken(previousToken: ts.Node | undefined, parent: ts.Node, predicate: (node: ts.Node) => boolean, ast: ts.SourceFile): ts.Node | undefined;
/**
* Find the first matching ancestor based on the given predicate function.
* @param {ts.Node} node The current ts.Node
* @param {Function} predicate The predicate function to apply to each checked ancestor
* @returns {ts.Node|undefined} a matching parent ts.Node
*/
export declare function findFirstMatchingAncestor(node: ts.Node, predicate: (node: ts.Node) => boolean): ts.Node | undefined;
/**
* Returns true if a given ts.Node has a JSX token within its hierarchy
* @param {ts.Node} node ts.Node to be checked
* @returns {boolean} has JSX ancestor
*/
export declare function hasJSXAncestor(node: ts.Node): boolean;
/**
* Unescape the text content of string literals, e.g. & -> &
* @param {string} text The escaped string literal text.
* @returns {string} The unescaped string literal text.
*/
export declare function unescapeStringLiteralText(text: string): string;
/**
* Returns true if a given ts.Node is a computed property
* @param {ts.Node} node ts.Node to be checked
* @returns {boolean} is Computed Property
*/
export declare function isComputedProperty(node: ts.Node): boolean;
/**
* Returns true if a given ts.Node is optional (has QuestionToken)
* @param {ts.Node} node ts.Node to be checked
* @returns {boolean} is Optional
*/
export declare function isOptional(node: {
questionToken?: ts.QuestionToken;
}): boolean;
/**
* Fixes the exports of the given ts.Node
* @param {ts.Node} node the ts.Node
* @param {ESTreeNode} result result
* @param {ts.SourceFile} ast the AST
* @returns {ESTreeNode} the ESTreeNode with fixed exports
*/
export declare function fixExports(node: ts.Node, result: ESTreeNode, ast: ts.SourceFile): ESTreeNode;
/**
* Returns the type of a given ts.Token
* @param {ts.Token} token the ts.Token
* @returns {string} the token type
*/
export declare function getTokenType(token: any): string;
/**
* Extends and formats a given ts.Token, for a given AST
* @param {ts.Node} token the ts.Token
* @param {ts.SourceFile} ast the AST object
* @returns {ESTreeToken} the converted ESTreeToken
*/
export declare function convertToken(token: ts.Node, ast: ts.SourceFile): ESTreeToken;
/**
* Converts all tokens for the given AST
* @param {ts.SourceFile} ast the AST object
* @returns {ESTreeToken[]} the converted ESTreeTokens
*/
export declare function convertTokens(ast: ts.SourceFile): ESTreeToken[];
/**
* Get container token node between range
* @param {ts.SourceFile} ast the AST object
* @param {number} start The index at which the comment starts.
* @param {number} end The index at which the comment ends.
* @returns {ts.Node} typescript container token
* @private
*/
export declare function getNodeContainer(ast: ts.SourceFile, start: number, end: number): ts.Node;
/**
* @param {ts.SourceFile} ast the AST object
* @param {number} start the index at which the error starts
* @param {string} message the error message
* @returns {Object} converted error object
*/
export declare function createError(ast: ts.SourceFile, start: number, message: string): {
index: number;
lineNumber: number;
column: number;
message: string;
};
/**
* @param {ts.Node} n the TSNode
* @param {ts.SourceFile} ast the TS AST
*/
export declare function nodeHasTokens(n: ts.Node, ast: ts.SourceFile): boolean;
/**
* Like `forEach`, but suitable for use with numbers and strings (which may be falsy).
* @template T
* @template U
* @param {ReadonlyArray<T>|undefined} array
* @param {(element: T, index: number) => (U|undefined)} callback
* @returns {U|undefined}
*/
export declare function firstDefined<T, U>(array: ReadonlyArray<T> | undefined, callback: (element: T, index: number) => U | undefined): U | undefined;