Skip to content

Instantly share code, notes, and snippets.

@erukiti
Last active November 10, 2017 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erukiti/30c366395b3297650e7f8b210492ff51 to your computer and use it in GitHub Desktop.
Save erukiti/30c366395b3297650e7f8b210492ff51 to your computer and use it in GitHub Desktop.
定義を元に、t.hogeExpression(....) を自動生成できるようにしたやつ

ArrayExpression

aliases

Expression

AssignmentExpression

aliases

Expression

  • properties
    • operator: string
    • left: LVal
    • right: Expression

example

t.assignmentExpression('string', t.identifier('lval'), t.identifier('expr'))
{
  "type": "AssignmentExpression",
  "operator": "string",
  "left": {
    "type": "Identifier",
    "name": "lval"
  },
  "right": {
    "type": "Identifier",
    "name": "expr"
  }
}
lval string expr

BinaryExpression

aliases

Binary, Expression

  • properties
    • operator: BINARY_OPERATORS
    • left: Expression
    • right: Expression

example

t.binaryExpression('+', t.identifier('expr'), t.identifier('expr'))
{
  "type": "BinaryExpression",
  "operator": "+",
  "left": {
    "type": "Identifier",
    "name": "expr"
  },
  "right": {
    "type": "Identifier",
    "name": "expr"
  }
}
expr + expr

Directive

DirectiveLiteral

  • properties
    • value: string

example

t.directiveLiteral('string')
{
  "type": "DirectiveLiteral",
  "value": "string"
}
"string"

BlockStatement

aliases

Scopable, BlockParent, Block, Statement

  • properties
    • body: Statement
    • directives: Directive

example

t.blockStatement([t.returnStatement(t.identifier('statement'))], [])
{
  "type": "BlockStatement",
  "body": [
    {
      "type": "ReturnStatement",
      "argument": {
        "type": "Identifier",
        "name": "statement"
      }
    }
  ],
  "directives": []
}
{
  return statement;
}

BreakStatement

aliases

Statement, Terminatorless, CompletionStatement

CallExpression

aliases

Expression

  • properties
    • callee: Expression
    • arguments: Expression,SpreadElement,JSXNamespacedName

example

t.callExpression(t.identifier('expr'), [t.identifier('expr')])
{
  "type": "CallExpression",
  "callee": {
    "type": "Identifier",
    "name": "expr"
  },
  "arguments": [
    {
      "type": "Identifier",
      "name": "expr"
    }
  ]
}
expr(expr)

CatchClause

aliases

Scopable, BlockParent

ConditionalExpression

aliases

Expression, Conditional

ContinueStatement

aliases

Statement, Terminatorless, CompletionStatement

DebuggerStatement

aliases

Statement

DoWhileStatement

aliases

Statement, BlockParent, Loop, While, Scopable

EmptyStatement

aliases

Statement

ExpressionStatement

aliases

Statement, ExpressionWrapper

File

  • properties
    • program: Program

example

t.file(t.program([]))
{
  "type": "File",
  "program": {
    "type": "Program",
    "body": [],
    "directives": [],
    "sourceType": "script"
  },
  "comments": null,
  "tokens": null
}

ForInStatement

aliases

Scopable, Statement, For, BlockParent, Loop, ForXStatement

ForStatement

aliases

Scopable, Statement, For, BlockParent, Loop

FunctionDeclaration

aliases

Scopable, Function, BlockParent, FunctionParent, Statement, Pureish, Declaration

  • properties
    • id: Identifier
    • params: LVal
    • body: BlockStatement
    • generator: boolean
    • async: boolean

example

t.functionDeclaration(t.identifier('identifier'), [t.identifier('lval')], t.blockStatement([]), false, false)
{
  "type": "FunctionDeclaration",
  "id": {
    "type": "Identifier",
    "name": "identifier"
  },
  "params": [
    {
      "type": "Identifier",
      "name": "lval"
    }
  ],
  "body": {
    "type": "BlockStatement",
    "body": [],
    "directives": []
  },
  "generator": false,
  "async": false
}
function identifier(lval) {}

FunctionExpression

aliases

Scopable, Function, BlockParent, FunctionParent, Expression, Pureish

Identifier

IfStatement

aliases

Statement, Conditional

LabeledStatement

aliases

Statement

StringLiteral

aliases

Expression, Pureish, Literal, Immutable

  • properties
    • value: string

example

t.stringLiteral('string')
{
  "type": "StringLiteral",
  "value": "string"
}
"string"

NumericLiteral

aliases

Expression, Pureish, Literal, Immutable

  • properties
    • value: number

example

t.numericLiteral(42)
{
  "type": "NumericLiteral",
  "value": 42
}
42

NullLiteral

aliases

Expression, Pureish, Literal, Immutable

BooleanLiteral

aliases

Expression, Pureish, Literal, Immutable

  • properties
    • value: boolean

example

t.booleanLiteral(true)
{
  "type": "BooleanLiteral",
  "value": true
}
true

RegExpLiteral

aliases

Expression, Literal

  • properties
    • pattern: string
    • flags: string

example

t.regExpLiteral('string', "")
{
  "type": "RegExpLiteral",
  "pattern": "string",
  "flags": ""
}
/string/

LogicalExpression

aliases

Binary, Expression

  • properties
    • operator: LOGICAL_OPERATORS
    • left: Expression
    • right: Expression

example

t.logicalExpression('&&', t.identifier('expr'), t.identifier('expr'))
{
  "type": "LogicalExpression",
  "operator": "&&",
  "left": {
    "type": "Identifier",
    "name": "expr"
  },
  "right": {
    "type": "Identifier",
    "name": "expr"
  }
}
expr && expr

MemberExpression

aliases

Expression, LVal

  • properties
    • object: Expression
    • property:
    • computed:
    • optional: true,false

example

t.memberExpression(t.identifier('expr'), t.identifier('identifier'), false, true)
{
  "type": "MemberExpression",
  "object": {
    "type": "Identifier",
    "name": "expr"
  },
  "property": {
    "type": "Identifier",
    "name": "identifier"
  },
  "computed": false,
  "optional": true
}
expr?.identifier

NewExpression

Program

aliases

Scopable, BlockParent, Block

  • properties
    • body: Statement
    • directives: Directive
    • sourceType: script,module

example

t.program([t.returnStatement(t.identifier('statement'))], [], "script")
{
  "type": "Program",
  "body": [
    {
      "type": "ReturnStatement",
      "argument": {
        "type": "Identifier",
        "name": "statement"
      }
    }
  ],
  "directives": [],
  "sourceType": "script"
}
return statement;

ObjectExpression

aliases

Expression

ObjectMethod

aliases

UserWhitespacable, Function, Scopable, BlockParent, FunctionParent, Method, ObjectMember

  • properties
    • kind: method,get,set
    • key:
    • params: LVal
    • body: BlockStatement
    • computed: boolean

example

t.objectMethod("method", t.identifier('identifier'), [t.identifier('lval')], t.blockStatement([]), false)
{
  "type": "ObjectMethod",
  "kind": "method",
  "key": {
    "type": "Identifier",
    "name": "identifier"
  },
  "params": [
    {
      "type": "Identifier",
      "name": "lval"
    }
  ],
  "body": {
    "type": "BlockStatement",
    "body": [],
    "directives": []
  },
  "computed": false
}
identifier(lval) {}

ObjectProperty

aliases

UserWhitespacable, Property, ObjectMember

  • properties
    • key:
    • value: Expression,PatternLike
    • computed: boolean
    • shorthand: boolean
    • decorators: Decorator

example

t.objectProperty(t.identifier('identifier'), t.identifier('expr'), false, false, )
{
  "type": "ObjectProperty",
  "key": {
    "type": "Identifier",
    "name": "identifier"
  },
  "value": {
    "type": "Identifier",
    "name": "expr"
  },
  "computed": false,
  "shorthand": false,
  "decorators": null
}
identifier: expr

RestElement

aliases

LVal, PatternLike

  • properties
    • argument: LVal

example

t.restElement(t.identifier('lval'))
{
  "type": "RestElement",
  "argument": {
    "type": "Identifier",
    "name": "lval"
  }
}
...lval

ReturnStatement

aliases

Statement, Terminatorless, CompletionStatement

SequenceExpression

aliases

Expression

SwitchCase

SwitchStatement

aliases

Statement, BlockParent, Scopable

ThisExpression

aliases

Expression

ThrowStatement

aliases

Statement, Terminatorless, CompletionStatement

TryStatement

aliases

Statement

UnaryExpression

aliases

UnaryLike, Expression

  • properties
    • operator: UNARY_OPERATORS
    • argument: Expression
    • prefix:

example

t.unaryExpression('!', t.identifier('expr'), true)
{
  "type": "UnaryExpression",
  "operator": "!",
  "argument": {
    "type": "Identifier",
    "name": "expr"
  },
  "prefix": true
}
!expr

UpdateExpression

aliases

Expression

  • properties
    • operator: UPDATE_OPERATORS
    • argument: Expression
    • prefix:

example

t.updateExpression('++', t.identifier('expr'), false)
{
  "type": "UpdateExpression",
  "operator": "++",
  "argument": {
    "type": "Identifier",
    "name": "expr"
  },
  "prefix": false
}
expr++

VariableDeclaration

aliases

Statement, Declaration

  • properties
    • kind: var,let,const
    • declarations: VariableDeclarator

example

t.variableDeclaration('var', [t.variableDeclarator(t.identifier('hoge'), t.numericLiteral(1))])
{
  "type": "VariableDeclaration",
  "kind": "var",
  "declarations": [
    {
      "type": "VariableDeclarator",
      "id": {
        "type": "Identifier",
        "name": "hoge"
      },
      "init": {
        "type": "NumericLiteral",
        "value": 1
      }
    }
  ]
}
var hoge = 1;

VariableDeclarator

WhileStatement

aliases

Statement, BlockParent, Loop, While, Scopable

WithStatement

aliases

Statement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment