|
| 1 | +// astHelpers.ts |
| 2 | +import type * as es from 'estree'; |
| 3 | +import { StatementSequence } from './types'; |
| 4 | +import { ControlItem } from './control'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Create a StatementSequence node. |
| 8 | + */ |
| 9 | +export const statementSequence = ( |
| 10 | + body: es.Statement[], |
| 11 | + loc?: es.SourceLocation | null |
| 12 | +): StatementSequence => ({ |
| 13 | + type: 'StatementSequence', |
| 14 | + body, |
| 15 | + loc, |
| 16 | + innerComments: undefined, |
| 17 | +}); |
| 18 | + |
| 19 | +export const isNode = (item: any): item is es.Node => { |
| 20 | + return typeof item === 'object' && item !== null && 'type' in item; |
| 21 | +}; |
| 22 | + |
| 23 | +export const isBlockStatement = (node: es.Node | StatementSequence): node is es.BlockStatement => { |
| 24 | + return node.type === 'BlockStatement'; |
| 25 | +}; |
| 26 | + |
| 27 | +export const hasDeclarations = (node: es.BlockStatement): boolean => { |
| 28 | + return node.body.some(stmt => stmt.type === 'VariableDeclaration' || stmt.type === 'FunctionDeclaration'); |
| 29 | +}; |
| 30 | + |
| 31 | +export const blockArrowFunction = ( |
| 32 | + params: es.Identifier[], |
| 33 | + body: es.Statement[] | es.BlockStatement | es.Expression, |
| 34 | + loc?: es.SourceLocation | null |
| 35 | +): es.ArrowFunctionExpression => ({ |
| 36 | + type: 'ArrowFunctionExpression', |
| 37 | + expression: false, |
| 38 | + generator: false, |
| 39 | + params, |
| 40 | + body: Array.isArray(body) ? blockStatement(body) : body, |
| 41 | + loc |
| 42 | +}) |
| 43 | + |
| 44 | +export const blockStatement = ( |
| 45 | + body: es.Statement[], |
| 46 | + loc?: es.SourceLocation | null |
| 47 | +): es.BlockStatement => ({ |
| 48 | + type: 'BlockStatement', |
| 49 | + body, |
| 50 | + loc |
| 51 | +}) |
| 52 | + |
| 53 | +export const constantDeclaration = ( |
| 54 | + name: string, |
| 55 | + init: es.Expression, |
| 56 | + loc?: es.SourceLocation | null |
| 57 | +) => declaration(name, 'declaration', init, loc) |
| 58 | + |
| 59 | +export const declaration = ( |
| 60 | + name: string, |
| 61 | + kind: AllowedDeclarations, |
| 62 | + init: es.Expression, |
| 63 | + loc?: es.SourceLocation | null |
| 64 | +): pyVariableDeclaration => ({ |
| 65 | + type: 'VariableDeclaration', |
| 66 | + declarations: [ |
| 67 | + { |
| 68 | + type: 'VariableDeclarator', |
| 69 | + id: identifier(name), |
| 70 | + init |
| 71 | + } |
| 72 | + ], |
| 73 | + kind: 'declaration', |
| 74 | + loc |
| 75 | +}) |
| 76 | + |
| 77 | +type AllowedDeclarations = 'declaration' | 'const' |
| 78 | + |
| 79 | +export interface pyVariableDeclaration { |
| 80 | + type: "VariableDeclaration"; |
| 81 | + declarations: pyVariableDeclarator[]; |
| 82 | + kind: "declaration" | "const"; |
| 83 | + loc?: es.SourceLocation | null | undefined; |
| 84 | + range?: [number, number] | undefined; |
| 85 | +} |
| 86 | + |
| 87 | +export interface pyVariableDeclarator { |
| 88 | + type: "VariableDeclarator"; |
| 89 | + id: Pattern; |
| 90 | + init?: es.Expression | null | undefined; |
| 91 | +} |
| 92 | + |
| 93 | +export type Pattern = es.Identifier | es.ObjectPattern | es.ArrayPattern | es.RestElement | es.AssignmentPattern | es.MemberExpression; |
| 94 | + |
| 95 | +export const identifier = (name: string, loc?: es.SourceLocation | null): es.Identifier => ({ |
| 96 | + type: 'Identifier', |
| 97 | + name, |
| 98 | + loc |
| 99 | +}) |
| 100 | + |
| 101 | +export const returnStatement = ( |
| 102 | + argument: es.Expression, |
| 103 | + loc?: es.SourceLocation | null |
| 104 | +): es.ReturnStatement => ({ |
| 105 | + type: 'ReturnStatement', |
| 106 | + argument, |
| 107 | + loc |
| 108 | +}) |
| 109 | + |
| 110 | +export const hasReturnStatement = (block: es.BlockStatement | StatementSequence): boolean => { |
| 111 | + let hasReturn = false |
| 112 | + for (const statement of block.body) { |
| 113 | + if (isReturnStatement(statement)) { |
| 114 | + hasReturn = true |
| 115 | + } else if (isIfStatement(statement)) { |
| 116 | + // Parser enforces that if/else have braces (block statement) |
| 117 | + hasReturn = hasReturn || hasReturnStatementIf(statement as es.IfStatement) |
| 118 | + } else if (isBlockStatement(statement) || isStatementSequence(statement)) { |
| 119 | + hasReturn = hasReturn && hasReturnStatement(statement) |
| 120 | + } |
| 121 | + } |
| 122 | + return hasReturn |
| 123 | +} |
| 124 | + |
| 125 | +export const isReturnStatement = (node: es.Node): node is es.ReturnStatement => { |
| 126 | + return (node as es.ReturnStatement).type == 'ReturnStatement' |
| 127 | +} |
| 128 | + |
| 129 | +export const isIfStatement = (node: es.Node): node is es.IfStatement => { |
| 130 | + return (node as es.IfStatement).type == 'IfStatement' |
| 131 | +} |
| 132 | + |
| 133 | +export const hasReturnStatementIf = (statement: es.IfStatement): boolean => { |
| 134 | + let hasReturn = true |
| 135 | + // Parser enforces that if/else have braces (block statement) |
| 136 | + hasReturn = hasReturn && hasReturnStatement(statement.consequent as es.BlockStatement) |
| 137 | + if (statement.alternate) { |
| 138 | + if (isIfStatement(statement.alternate)) { |
| 139 | + hasReturn = hasReturn && hasReturnStatementIf(statement.alternate as es.IfStatement) |
| 140 | + } else if (isBlockStatement(statement.alternate) || isStatementSequence(statement.alternate)) { |
| 141 | + hasReturn = hasReturn && hasReturnStatement(statement.alternate) |
| 142 | + } |
| 143 | + } |
| 144 | + return hasReturn |
| 145 | +} |
| 146 | + |
| 147 | +export const isStatementSequence = (node: ControlItem): node is StatementSequence => { |
| 148 | + return (node as StatementSequence).type == 'StatementSequence' |
| 149 | +} |
| 150 | + |
| 151 | +export const literal = ( |
| 152 | + value: string | number | boolean | null, |
| 153 | + loc?: es.SourceLocation | null |
| 154 | +): es.Literal => ({ |
| 155 | + type: 'Literal', |
| 156 | + value, |
| 157 | + loc |
| 158 | +}) |
0 commit comments