LoginSignup
10
9

More than 5 years have passed since last update.

「ES6 クイズ」を AST で見てみる

Last updated at Posted at 2015-04-28

ES6 クイズ - Qiita の二番目の問題に自信が無かったので AST を見てみる。

クイズの問題はこれ

jxck_es6_quiz.js
var a = 1, b = 2, c = 3, d = 4;
var f = a => b ? c: d;
// f = ?

AST をダンプするプログラムを書く。使うのは acorn, espurify, concat-stream.

$ mkdir es6_quiz && cd es6_quiz
$ npm init
$ npm install acorn espurify concat-stream
dump_ast.js
var acorn = require('acorn');
var espurify = require('espurify');
var concat = require('concat-stream');
var parserOptions = { ecmaVersion: 6 };

process.stdin.pipe(concat(function (buf) {
    var jsCode = buf.toString('utf-8');
    var ast = acorn.parse(jsCode, parserOptions);
    console.log(JSON.stringify(espurify(ast), null, 2));
}));

実行して AST を見てみる

$ cat jxck_es6_quiz.js | node dump_ast.js

出力結果:

{
  "type": "Program",
  "body": [
    {
      "type": "VariableDeclaration",
      "declarations": [
        {
          "type": "VariableDeclarator",
          "id": {
            "type": "Identifier",
            "name": "a"
          },
          "init": {
            "type": "Literal",
            "value": 1
          }
        },
        {
          "type": "VariableDeclarator",
          "id": {
            "type": "Identifier",
            "name": "b"
          },
          "init": {
            "type": "Literal",
            "value": 2
          }
        },
        {
          "type": "VariableDeclarator",
          "id": {
            "type": "Identifier",
            "name": "c"
          },
          "init": {
            "type": "Literal",
            "value": 3
          }
        },
        {
          "type": "VariableDeclarator",
          "id": {
            "type": "Identifier",
            "name": "d"
          },
          "init": {
            "type": "Literal",
            "value": 4
          }
        }
      ],
      "kind": "var"
    },
    {
      "type": "VariableDeclaration",
      "declarations": [
        {
          "type": "VariableDeclarator",
          "id": {
            "type": "Identifier",
            "name": "f"
          },
          "init": {
            "type": "ArrowFunctionExpression",
            "id": null,
            "params": [
              {
                "type": "Identifier",
                "name": "a"
              }
            ],
            "body": {
              "type": "ConditionalExpression",
              "test": {
                "type": "Identifier",
                "name": "b"
              },
              "alternate": {
                "type": "Identifier",
                "name": "d"
              },
              "consequent": {
                "type": "Identifier",
                "name": "c"
              }
            },
            "generator": false,
            "expression": true
          }
        }
      ],
      "kind": "var"
    }
  ],
  "sourceType": "script"
}

なるほど〜〜

10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9