JavaScript 语法解析

解析输入数据,并以结构化、便于审查的形式显示结果。

输入: 文本结果: 文本

文本输入

运行后查看结果

案例

Keep a zero-prefixed invoice ID as a string

输入

const invoice = "00123";

Expected answer

JSON
{
  "type": "Program",
  "body": [
    {
      "type": "VariableDeclaration",
      "declarations": [
        {
          "type": "VariableDeclarator",
          "id": {
            "type": "Identifier",
            "name": "invoice"
          },
          "init": {
            "type": "Literal",
            "value": "00123",
            "raw": "\"00123\""
          }
        }
      ],
      "kind": "const"
    }
  ],
  "sourceType": "script"
}
位置信息
Off
范围信息
Off
包含词法单元数组
Off
包含注释数组
Off
报告错误并尝试继续处理
Off
Find the callback inside a price total

输入

const total = prices.reduce((sum, price) => sum + price, 0);

Expected answer

JSON
{
  "type": "Program",
  "body": [
    {
      "type": "VariableDeclaration",
      "declarations": [
        {
          "type": "VariableDeclarator",
          "id": {
            "type": "Identifier",
            "name": "total"
          },
          "init": {
            "type": "CallExpression",
            "callee": {
              "type": "MemberExpression",
              "computed": false,
              "object": {
                "type": "Identifier",
                "name": "prices"
              },
              "property": {
                "type": "Identifier",
                "name": "reduce"
              }
            },
            "arguments": [
              {
                "type": "ArrowFunctionExpression",
                "id": null,
                "params": [
                  {
                    "type": "Identifier",
                    "name": "sum"
                  },
                  {
                    "type": "Identifier",
                    "name": "price"
                  }
                ],
                "body": {
                  "type": "BinaryExpression",
                  "operator": "+",
                  "left": {
                    "type": "Identifier",
                    "name": "sum"
                  },
                  "right": {
                    "type": "Identifier",
                    "name": "price"
                  }
                },
                "generator": false,
                "expression": true,
                "async": false
              },
              {
                "type": "Literal",
                "value": 0,
                "raw": "0"
              }
            ]
          }
        }
      ],
      "kind": "const"
    }
  ],
  "sourceType": "script"
}
位置信息
Off
范围信息
Off
包含词法单元数组
Off
包含注释数组
Off
报告错误并尝试继续处理
Off
Locate a price after a source comment

输入

// USD prices
const price = 19.95;

Expected answer

JSON
{
  "type": "Program",
  "body": [
    {
      "type": "VariableDeclaration",
      "declarations": [
        {
          "type": "VariableDeclarator",
          "id": {
            "type": "Identifier",
            "name": "price",
            "range": [
              20,
              25
            ],
            "loc": {
              "start": {
                "line": 2,
                "column": 6
              },
              "end": {
                "line": 2,
                "column": 11
              }
            }
          },
          "init": {
            "type": "Literal",
            "value": 19.95,
            "raw": "19.95",
            "range": [
              28,
              33
            ],
            "loc": {
              "start": {
                "line": 2,
                "column": 14
              },
              "end": {
                "line": 2,
                "column": 19
              }
            }
          },
          "range": [
            20,
            33
          ],
          "loc": {
            "start": {
              "line": 2,
              "column": 6
            },
            "end": {
              "line": 2,
              "column": 19
            }
          }
        }
      ],
      "kind": "const",
      "range": [
        14,
        34
      ],
      "loc": {
        "start": {
          "line": 2,
          "column": 0
        },
        "end": {
          "line": 2,
          "column": 20
        }
      }
    }
  ],
  "sourceType": "script",
  "range": [
    14,
    34
  ],
  "loc": {
    "start": {
      "line": 2,
      "column": 0
    },
    "end": {
      "line": 2,
      "column": 20
    }
  },
  "comments": [
    {
      "type": "Line",
      "value": " USD prices",
      "range": [
        0,
        13
      ],
      "loc": {
        "start": {
          "line": 1,
          "column": 0
        },
        "end": {
          "line": 1,
          "column": 13
        }
      }
    }
  ],
  "tokens": [
    {
      "type": "Keyword",
      "value": "const",
      "range": [
        14,
        19
      ],
      "loc": {
        "start": {
          "line": 2,
          "column": 0
        },
        "end": {
          "line": 2,
          "column": 5
        }
      }
    },
    {
      "type": "Identifier",
      "value": "price",
      "range": [
        20,
        25
      ],
      "loc": {
        "start": {
          "line": 2,
          "column": 6
        },
        "end": {
          "line": 2,
          "column": 11
        }
      }
    },
    {
      "type": "Punctuator",
      "value": "=",
      "range": [
        26,
        27
      ],
      "loc": {
        "start": {
          "line": 2,
          "column": 12
        },
        "end": {
          "line": 2,
          "column": 13
        }
      }
    },
    {
      "type": "Numeric",
      "value": "19.95",
      "range": [
        28,
        33
      ],
      "loc": {
        "start": {
          "line": 2,
          "column": 14
        },
        "end": {
          "line": 2,
          "column": 19
        }
      }
    },
    {
      "type": "Punctuator",
      "value": ";",
      "range": [
        33,
        34
      ],
      "loc": {
        "start": {
          "line": 2,
          "column": 19
        },
        "end": {
          "line": 2,
          "column": 20
        }
      }
    }
  ]
}
位置信息
On
范围信息
On
包含词法单元数组
On
包含注释数组
On
报告错误并尝试继续处理
Off
Inspect both branches without running either

输入

if (isReady) { start(); } else { retry(); }

Expected answer

JSON
{
  "type": "Program",
  "body": [
    {
      "type": "IfStatement",
      "test": {
        "type": "Identifier",
        "name": "isReady"
      },
      "consequent": {
        "type": "BlockStatement",
        "body": [
          {
            "type": "ExpressionStatement",
            "expression": {
              "type": "CallExpression",
              "callee": {
                "type": "Identifier",
                "name": "start"
              },
              "arguments": []
            }
          }
        ]
      },
      "alternate": {
        "type": "BlockStatement",
        "body": [
          {
            "type": "ExpressionStatement",
            "expression": {
              "type": "CallExpression",
              "callee": {
                "type": "Identifier",
                "name": "retry"
              },
              "arguments": []
            }
          }
        ]
      }
    }
  ],
  "sourceType": "script"
}
位置信息
Off
范围信息
Off
包含词法单元数组
Off
包含注释数组
Off
报告错误并尝试继续处理
Off
Reject a missing value instead of inventing a tree

输入

const broken = ;

Expected rejection

Message contains
Unexpected token ;
位置信息
Off
范围信息
Off
包含词法单元数组
Off
包含注释数组
Off
报告错误并尝试继续处理
Off

相关工具

代码整理

操作参考

查看原始技术文档 (English)