eslint-codemod-utils
Version:
A collection of AST helper functions for more complex ESLint rule fixes.
417 lines (416 loc) • 13 kB
TypeScript
import { TSESTree as ESTree } from '@typescript-eslint/types';
import type { Loose, StringableASTNode, StringableASTNodeFn, WithoutType } from './types';
/**
* __CallExpression__
*
* @example
*
* Usage
* ```
* const call = callExpression({ callee: identifier({ name: 'normalCallExpression' }) })
* ```
*
* Produces
*
* @example
*
* ```js
* normalCallExpression()
* ```
*
* @returns {ESTree.CallExpression}
*/
export declare const callExpression: StringableASTNodeFn<ESTree.CallExpression>;
/**
* __Super__
*
* @example
*
* ```
* // note the whole expression is a `CallExpression`
* // super is simply the callee / identifier
* super()
* ^^^^^
* ```
*
* @returns {ESTree.Super}
*/
export declare const superCallExpression: StringableASTNodeFn<ESTree.Super>;
export declare const chainExpression: StringableASTNodeFn<ESTree.ChainExpression>;
/**
* __BinaryExpression__
*
* @example
* ```ts
* const x = 'left' + 'right'
* ^^^^^^^^^^^^^^^^
* ```
*/
export declare const binaryExpression: StringableASTNodeFn<ESTree.BinaryExpression>;
/**
* __SequenceExpression__
*
* @example
* ```ts
* const x = (4, 8)
* ^^^^^^
* ```
*/
export declare const sequenceExpression: StringableASTNodeFn<ESTree.SequenceExpression>;
/**
* __ArrowFunctionExpression__
*
* @example
* ```js
* const arrow = () => 42
* ⌃⌃⌃⌃⌃⌃⌃⌃
* ```
* @returns {ESTree.ArrowFunctionExpression}
*/
export declare const arrowFunctionExpression: StringableASTNodeFn<ESTree.ArrowFunctionExpression>;
/**
* __TaggedTemplateExpression__
*
* @example
* ```ts
* const style = css`color: red;`
* ^^^^^^^^^^^
* ```
*/
export declare const taggedTemplateExpression: StringableASTNodeFn<ESTree.TaggedTemplateExpression>;
export declare const functionExpression: StringableASTNodeFn<ESTree.FunctionExpression>;
export declare const blockStatement: StringableASTNodeFn<ESTree.BlockStatement>;
export declare const returnStatement: StringableASTNodeFn<ESTree.ReturnStatement>;
export declare const throwStatement: StringableASTNodeFn<ESTree.ThrowStatement>;
/**
* __UnaryExpression__
*
* @example
*
* ```ts
* const y = typeof x
* ^^^^^^
* ++x
* ^^
* ```
*
* @returns {ESTree.UnaryExpression}
*/
export declare const unaryExpression: StringableASTNodeFn<ESTree.UnaryExpression>;
/**
* __ThisExpression__
*
* @example
*
* ```js
* // In `this.self` 'this' is a ThisExpression.
* this.self
* ⌃⌃⌃⌃
* ```
*
* @returns {ESTree.ThisExpression}
*/
export declare const thisExpression: StringableASTNodeFn<ESTree.ThisExpression>;
/**
* __IfStatement__
*
* @example
*
* ```ts
* if (test) {
* // consequant
* } else {
* // alternate
* }
* ⌃⌃⌃⌃^^^^^^^^
* ```
*
* @returns {ESTree.IfStatement}
*/
export declare const ifStatement: StringableASTNodeFn<ESTree.IfStatement>;
/**
* __CatchClause__
*
* @example
*
* ```ts
* // always inside a try statement
* catch (e) {}
* ⌃⌃⌃⌃^^^^^^^^
* ```
*
* @returns {ESTree.CatchClause}
*/
export declare const catchClause: StringableASTNodeFn<ESTree.CatchClause>;
/**
* __TryStatement__
*
* @example
*
* ```ts
* try {
* // block
* } catch(e) { // <--- handler
*
* } finally {} // <--- finalizer
* ⌃⌃⌃⌃^^^^^^^^
* ```
*
* @returns {ESTree.TryStatement}
*/
export declare const tryStatement: StringableASTNodeFn<ESTree.TryStatement>;
/**
* __WithStatement__
*
* @example
*
* ```ts
* with (Math) {
* a = PI * r * r;
* x = r * cos(PI);
* y = r * sin(PI / 2);
* }
* ```
*
* @returns {ESTree.WithStatement}
*/
export declare const withStatement: StringableASTNodeFn<ESTree.WithStatement>;
/**
* __ImportExpression__
*
* @example
*
* ```ts
* import('some-path')
* ⌃⌃⌃⌃^^^^^^^^^^^^^^^
* ```
*
* @returns {ESTree.ImportExpression}
*/
export declare const importExpression: StringableASTNodeFn<ESTree.ImportExpression>;
/**
* __ImportDefaultSpecifier__
*
* @example
*
* ```ts
* import Hello from 'world'
* ^^^^^
* ```
*
* @returns {ESTree.ImportDefaultSpecifier}
*/
export declare const importDefaultSpecifier: StringableASTNodeFn<ESTree.ImportDefaultSpecifier>;
/**
* __ExportNamedDeclaration__
*
* @example
*
* ```ts
* export { Hello } from 'world'
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* ```
*
* @returns {ESTree.ExportNamedDeclaration}
*/
export declare const exportNamedDeclaration: StringableASTNodeFn<ESTree.ExportNamedDeclaration>;
/**
* __ExportDefaultDeclaration__
*
* @example
*
* ```ts
* export default HelloWorld
* ^^^^^^^^^^^^^^^^^^^^^^^^^
* ```
*
* @returns {ESTree.ExportDefaultDeclaration}
*/
export declare const exportDefaultDeclaration: StringableASTNodeFn<ESTree.ExportDefaultDeclaration>;
/**
* __ExportAllDeclaration__
*
* @example
*
* ```ts
* export * from 'world'
* ^^^^^^^^^^^^^^^^^^^^^^^^^
* ```
* ```ts
* export * as Hello from 'world'
* ^^^^^^^^^^^^^^^^^^^^^^^^^
* ```
*
* @returns {ESTree.ExportAllDeclaration}
*/
/**
* `exported` is made optional here (defaults to `null`) via a narrowed input
* type so callers don't have to write `exported: null` for the common
* `export * from '…'` case. It stays required on the underlying TSESTree type
* (and on `ExportSpecifier`, which is why it's deliberately excluded from the
* shared `DefaultableFields` list).
*/
export declare const exportAllDeclaration: (node: Omit<WithoutType<ESTree.ExportAllDeclaration>, 'exported'> & {
exported?: WithoutType<ESTree.ExportAllDeclaration>['exported'];
}) => StringableASTNode<ESTree.ExportAllDeclaration>;
export declare const exportSpecifier: StringableASTNodeFn<ESTree.ExportSpecifier>;
export declare const importSpecifier: StringableASTNodeFn<ESTree.ImportSpecifier>;
/**
* __YieldExpression__
*
* @example
*
* ```ts
* const thing = yield someYieldExpression
* ⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃^^^^^^^^^^^^^
* ```
*
* @returns {ESTree.YieldExpression}
*/
export declare const yieldExpression: StringableASTNodeFn<ESTree.YieldExpression>;
export declare const arrayExpression: StringableASTNodeFn<ESTree.ArrayExpression>;
export declare const arrayPattern: StringableASTNodeFn<ESTree.ArrayPattern>;
export declare const updateExpression: StringableASTNodeFn<ESTree.UpdateExpression>;
export declare const expressionStatement: StringableASTNodeFn<ESTree.ExpressionStatement>;
/**
* __NewExpression__
*
* @example
* ```ts
* new SomeThing()
* ^^^^^^^^^^^^^^^
* ```
*/
export declare const newExpression: StringableASTNodeFn<ESTree.NewExpression>;
export declare const property: StringableASTNodeFn<ESTree.Property>;
/**
* __ObjectPattern__
*
* @example
* ```ts
* function App({ a }) {}
* ^^^^^
* ```
* @returns
*/
export declare const objectPattern: StringableASTNodeFn<ESTree.ObjectPattern>;
/**
* __SpreadElement__
*
* @example
* ```ts
* const obj = {
* ...spread
* ^^^^^^^^^
* }
* ```
*
* @returns {ESTree.SpreadElement}
*/
export declare const spreadElement: StringableASTNodeFn<ESTree.SpreadElement>;
/**
* __RestElement__
*
* @example
* ```ts
* const [a, ...b] = c
* ^^^^
* ```
*
* * @example
* ```ts
* const { a, ...b } = c
* ^^^^
* ```
*
* @returns {ESTree.RestElement}
*/
export declare const restElement: StringableASTNodeFn<ESTree.RestElement>;
/**
* __ObjectExpression__
* @example
* ```ts
* const x = {
* key: value,
* get x() { return 1 },
* }
* ^^^^^^^^^^^^
* ```
*/
export declare const objectExpression: StringableASTNodeFn<ESTree.ObjectExpression>;
export declare const emptyStatement: StringableASTNodeFn<ESTree.EmptyStatement>;
export declare const memberExpression: StringableASTNodeFn<ESTree.MemberExpression>;
export declare const logicalExpression: StringableASTNodeFn<ESTree.LogicalExpression>;
export declare const variableDeclarator: StringableASTNodeFn<ESTree.VariableDeclarator>;
export declare const variableDeclaration: StringableASTNodeFn<ESTree.VariableDeclaration>;
export declare const importNamespaceSpecifier: StringableASTNodeFn<ESTree.ImportNamespaceSpecifier>;
export declare const templateElement: StringableASTNodeFn<ESTree.TemplateElement>;
export declare const importDeclaration: StringableASTNodeFn<ESTree.ImportDeclaration>;
export declare const bigIntLiteral: StringableASTNodeFn<ESTree.BigIntLiteral>;
export declare const regExpLiteral: StringableASTNodeFn<ESTree.RegExpLiteral>;
export declare const stringLiteral: StringableASTNodeFn<ESTree.StringLiteral>;
export declare const booleanLiteral: (n: boolean | WithoutType<ESTree.BooleanLiteral>) => StringableASTNode<ESTree.BooleanLiteral>;
export declare const numberLiteral: (n: number | WithoutType<ESTree.NumberLiteral>) => StringableASTNode<ESTree.NumberLiteral>;
export declare const nullLiteral: (n: null | WithoutType<ESTree.NullLiteral>) => StringableASTNode<ESTree.NullLiteral>;
/**
* __Literal__
*
* Polymorphic helper for building a `Literal` node. Call it with a raw JS
* primitive (`'x'`, `42`, `true`, `null`) or with a pre-built `WithoutType<…>`
* node to construct the corresponding `StringLiteral`, `NumberLiteral`,
* `BooleanLiteral`, `NullLiteral`, `BigIntLiteral`, or `RegExpLiteral`.
*
* Overloads are used so that call sites get a narrowed return type:
* `literal('x')` is `StringableASTNode<StringLiteral>`, not a wide union —
* this matters when passing the result into helpers that expect a specific
* variant (e.g. `importDeclaration({ source: literal('x') })` where `source`
* is typed as `StringLiteral`).
*/
export declare function literal(n: string): StringableASTNode<ESTree.StringLiteral>;
export declare function literal(n: number): StringableASTNode<ESTree.NumberLiteral>;
export declare function literal(n: boolean): StringableASTNode<ESTree.BooleanLiteral>;
export declare function literal(n: null): StringableASTNode<ESTree.NullLiteral>;
export declare function literal(n: WithoutType<ESTree.BigIntLiteral>): StringableASTNode<ESTree.BigIntLiteral>;
export declare function literal(n: WithoutType<ESTree.RegExpLiteral>): StringableASTNode<ESTree.RegExpLiteral>;
export declare function literal(n: WithoutType<ESTree.StringLiteral>): StringableASTNode<ESTree.StringLiteral>;
export declare function literal(n: WithoutType<ESTree.NumberLiteral>): StringableASTNode<ESTree.NumberLiteral>;
export declare function literal(n: WithoutType<ESTree.BooleanLiteral>): StringableASTNode<ESTree.BooleanLiteral>;
export declare function literal(n: WithoutType<ESTree.NullLiteral>): StringableASTNode<ESTree.NullLiteral>;
export declare const identifier: (param: WithoutType<ESTree.Identifier> | string) => StringableASTNode<ESTree.Identifier>;
export declare const doWhileStatement: StringableASTNodeFn<ESTree.DoWhileStatement>;
export declare const whileStatement: StringableASTNodeFn<ESTree.WhileStatement>;
export declare const switchCase: StringableASTNodeFn<ESTree.SwitchCase>;
export declare const switchStatement: StringableASTNodeFn<ESTree.SwitchStatement>;
export declare const templateLiteral: StringableASTNodeFn<ESTree.TemplateLiteral>;
export declare const forStatement: StringableASTNodeFn<ESTree.ForStatement>;
export declare const forInStatement: StringableASTNodeFn<ESTree.ForInStatement>;
export declare const forOfStatement: StringableASTNodeFn<ESTree.ForOfStatement>;
export declare const continueStatement: StringableASTNodeFn<ESTree.ContinueStatement>;
export declare const breakStatement: StringableASTNodeFn<ESTree.BreakStatement>;
export declare const debuggerStatement: StringableASTNodeFn<ESTree.DebuggerStatement>;
export declare const conditionalExpression: StringableASTNodeFn<ESTree.ConditionalExpression>;
export declare const assignmentExpression: StringableASTNodeFn<ESTree.AssignmentExpression>;
export declare const awaitExpression: StringableASTNodeFn<ESTree.AwaitExpression>;
/**
* __StaticBlock__
*
* @example
* ```ts
* class A {
* // only applicable inside a class
* static { }
* ^^^^^^^^^^
* }
* ```
*/
export declare const staticBlock: StringableASTNodeFn<ESTree.StaticBlock>;
export declare const functionDeclaration: StringableASTNodeFn<ESTree.FunctionDeclaration>;
export declare const methodOrPropertyFn: ({ params, body, }: Loose<ESTree.FunctionExpression> | Loose<ESTree.TSEmptyBodyFunctionExpression>) => string;
export declare const methodDefinition: StringableASTNodeFn<ESTree.MethodDefinition>;
export declare const propertyDefinition: StringableASTNodeFn<ESTree.PropertyDefinition>;
export declare const classBody: StringableASTNodeFn<ESTree.ClassBody>;
export declare const classDeclaration: StringableASTNodeFn<ESTree.ClassDeclaration>;
export declare const classExpression: StringableASTNodeFn<ESTree.ClassExpression>;
export declare const program: StringableASTNodeFn<ESTree.Program>;
export declare const privateIdentifier: StringableASTNodeFn<ESTree.PrivateIdentifier>;
export declare const decorator: StringableASTNodeFn<ESTree.Decorator>;