55// because we don't create the AST from text
66
77import * as ts from "typescript" ;
8+ import { castArray } from "./utils" ;
89
910export enum SyntaxKind {
1011 Block ,
@@ -236,14 +237,8 @@ export function createVariableDeclarationStatement(
236237 tsOriginal ?: ts . Node
237238) : VariableDeclarationStatement {
238239 const statement = createNode ( SyntaxKind . VariableDeclarationStatement , tsOriginal ) as VariableDeclarationStatement ;
239- statement . left = Array . isArray ( left ) ? left : [ left ] ;
240-
241- if ( Array . isArray ( right ) ) {
242- statement . right = right ;
243- } else if ( right ) {
244- statement . right = [ right ] ;
245- }
246-
240+ statement . left = castArray ( left ) ;
241+ if ( right ) statement . right = castArray ( right ) ;
247242 return statement ;
248243}
249244
@@ -264,14 +259,8 @@ export function createAssignmentStatement(
264259 tsOriginal ?: ts . Node
265260) : AssignmentStatement {
266261 const statement = createNode ( SyntaxKind . AssignmentStatement , tsOriginal ) as AssignmentStatement ;
267- statement . left = Array . isArray ( left ) ? left : [ left ] ;
268-
269- if ( Array . isArray ( right ) ) {
270- statement . right = right ;
271- } else {
272- statement . right = right ? [ right ] : [ ] ;
273- }
274-
262+ statement . left = castArray ( left ) ;
263+ statement . right = right ? castArray ( right ) : [ ] ;
275264 return statement ;
276265}
277266
@@ -429,14 +418,14 @@ export function createLabelStatement(name: string, tsOriginal?: ts.Node): LabelS
429418
430419export interface ReturnStatement extends Statement {
431420 kind : SyntaxKind . ReturnStatement ;
432- expressions ? : Expression [ ] ;
421+ expressions : Expression [ ] ;
433422}
434423
435424export function isReturnStatement ( node : Node ) : node is ReturnStatement {
436425 return node . kind === SyntaxKind . ReturnStatement ;
437426}
438427
439- export function createReturnStatement ( expressions ? : Expression [ ] , tsOriginal ?: ts . Node ) : ReturnStatement {
428+ export function createReturnStatement ( expressions : Expression [ ] , tsOriginal ?: ts . Node ) : ReturnStatement {
440429 const statement = createNode ( SyntaxKind . ReturnStatement , tsOriginal ) as ReturnStatement ;
441430 statement . expressions = expressions ;
442431 return statement ;
@@ -496,11 +485,7 @@ export function isBooleanLiteral(node: Node): node is BooleanLiteral {
496485}
497486
498487export function createBooleanLiteral ( value : boolean , tsOriginal ?: ts . Node ) : BooleanLiteral {
499- if ( value ) {
500- return createNode ( SyntaxKind . TrueKeyword , tsOriginal ) as BooleanLiteral ;
501- } else {
502- return createNode ( SyntaxKind . FalseKeyword , tsOriginal ) as BooleanLiteral ;
503- }
488+ return createNode ( value ? SyntaxKind . TrueKeyword : SyntaxKind . FalseKeyword , tsOriginal ) as BooleanLiteral ;
504489}
505490
506491// TODO Call this DotsLiteral or DotsKeyword?
@@ -518,7 +503,7 @@ export function createDotsLiteral(tsOriginal?: ts.Node): DotsLiteral {
518503
519504// StringLiteral / NumberLiteral
520505// TODO TS uses the export interface "LiteralLikeNode" with a "text: string" member
521- // but since we dont parse from text i think we can simplify by just having a value member
506+ // but since we don't parse from text I think we can simplify by just having a value member
522507
523508// TODO NumericLiteral or NumberLiteral?
524509export interface NumericLiteral extends Expression {
@@ -552,16 +537,15 @@ export function createStringLiteral(value: string, tsOriginal?: ts.Node): String
552537}
553538
554539export enum FunctionExpressionFlags {
555- None = 0x0 ,
556- Inline = 0x1 , // Keep function on same line
557- Declaration = 0x2 , // Prefer declaration syntax `function foo()` over assignment syntax `foo = function()`
540+ None = 1 << 0 ,
541+ Inline = 1 << 1 , // Keep function body on same line
542+ Declaration = 1 << 2 , // Prefer declaration syntax `function foo()` over assignment syntax `foo = function()`
558543}
559544
560545export interface FunctionExpression extends Expression {
561546 kind : SyntaxKind . FunctionExpression ;
562547 params ?: Identifier [ ] ;
563548 dots ?: DotsLiteral ;
564- restParamName ?: Identifier ;
565549 body : Block ;
566550 flags : FunctionExpressionFlags ;
567551}
@@ -574,15 +558,13 @@ export function createFunctionExpression(
574558 body : Block ,
575559 params ?: Identifier [ ] ,
576560 dots ?: DotsLiteral ,
577- restParamName ?: Identifier ,
578561 flags = FunctionExpressionFlags . None ,
579562 tsOriginal ?: ts . Node
580563) : FunctionExpression {
581564 const expression = createNode ( SyntaxKind . FunctionExpression , tsOriginal ) as FunctionExpression ;
582565 expression . body = body ;
583566 expression . params = params ;
584567 expression . dots = dots ;
585- expression . restParamName = restParamName ;
586568 expression . flags = flags ;
587569 return expression ;
588570}
@@ -671,7 +653,7 @@ export function createBinaryExpression(
671653export interface CallExpression extends Expression {
672654 kind : SyntaxKind . CallExpression ;
673655 expression : Expression ;
674- params ? : Expression [ ] ;
656+ params : Expression [ ] ;
675657}
676658
677659export function isCallExpression ( node : Node ) : node is CallExpression {
@@ -680,7 +662,7 @@ export function isCallExpression(node: Node): node is CallExpression {
680662
681663export function createCallExpression (
682664 expression : Expression ,
683- params ? : Expression [ ] ,
665+ params : Expression [ ] ,
684666 tsOriginal ?: ts . Node
685667) : CallExpression {
686668 const callExpression = createNode ( SyntaxKind . CallExpression , tsOriginal ) as CallExpression ;
@@ -693,7 +675,7 @@ export interface MethodCallExpression extends Expression {
693675 kind : SyntaxKind . MethodCallExpression ;
694676 prefixExpression : Expression ;
695677 name : Identifier ;
696- params ? : Expression [ ] ;
678+ params : Expression [ ] ;
697679}
698680
699681export function isMethodCallExpression ( node : Node ) : node is MethodCallExpression {
@@ -703,7 +685,7 @@ export function isMethodCallExpression(node: Node): node is MethodCallExpression
703685export function createMethodCallExpression (
704686 prefixExpression : Expression ,
705687 name : Identifier ,
706- params ? : Expression [ ] ,
688+ params : Expression [ ] ,
707689 tsOriginal ?: ts . Node
708690) : MethodCallExpression {
709691 const callExpression = createNode ( SyntaxKind . MethodCallExpression , tsOriginal ) as MethodCallExpression ;
0 commit comments