Skip to content

Commit

Permalink
Add new CLI option debug-print-ast (#11514)
Browse files Browse the repository at this point in the history
* Add new CLI option debug-print-ast

* Add tests

* Add changelog

* Remove text from ast

* Update tests/integration/__tests__/debug-print-ast.js

Co-authored-by: Georgii Dolzhykov <thorn.mailbox@gmail.com>

* Update tests

Co-authored-by: Georgii Dolzhykov <thorn.mailbox@gmail.com>
  • Loading branch information
sosukesuzuki and thorn0 committed Sep 15, 2021
1 parent ae33e49 commit aa63269
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelog_unreleased/cli/11514.md
@@ -0,0 +1,3 @@
#### Add new CLI option `debug-print-ast` (#11514 by @sosukesuzuki)

A new `--debug-print-ast` CLI flag for debugging.
3 changes: 3 additions & 0 deletions src/cli/constant.js
Expand Up @@ -135,6 +135,9 @@ const options = {
"debug-print-comments": {
type: "boolean",
},
"debug-print-ast": {
type: "boolean",
},
"debug-repeat": {
// Repeat the formatting a few times and measure the average duration.
type: "int",
Expand Down
7 changes: 7 additions & 0 deletions src/cli/format.js
Expand Up @@ -126,6 +126,13 @@ function format(context, input, opt) {
};
}

if (context.argv["debug-print-ast"]) {
const { ast } = prettier.__debug.parse(input, opt);
return {
formatted: JSON.stringify(ast),
};
}

if (context.argv["debug-check"]) {
const pp = prettier.format(input, opt);
const pppp = prettier.format(pp, opt);
Expand Down
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`prints information for debugging AST --debug-print-ast (stdout) 1`] = `"{\\"type\\":\\"File\\",\\"start\\":0,\\"end\\":18,\\"loc\\":{\\"start\\":{\\"line\\":1,\\"column\\":0},\\"end\\":{\\"line\\":1,\\"column\\":18}},\\"range\\":[0,18],\\"errors\\":[],\\"program\\":{\\"type\\":\\"Program\\",\\"start\\":0,\\"end\\":18,\\"loc\\":{\\"start\\":{\\"line\\":1,\\"column\\":0},\\"end\\":{\\"line\\":1,\\"column\\":18}},\\"range\\":[0,18],\\"sourceType\\":\\"module\\",\\"interpreter\\":null,\\"body\\":[{\\"type\\":\\"VariableDeclaration\\",\\"start\\":0,\\"end\\":18,\\"loc\\":{\\"start\\":{\\"line\\":1,\\"column\\":0},\\"end\\":{\\"line\\":1,\\"column\\":18}},\\"range\\":[0,18],\\"declarations\\":[{\\"type\\":\\"VariableDeclarator\\",\\"start\\":6,\\"end\\":17,\\"loc\\":{\\"start\\":{\\"line\\":1,\\"column\\":6},\\"end\\":{\\"line\\":1,\\"column\\":17}},\\"range\\":[6,17],\\"id\\":{\\"type\\":\\"Identifier\\",\\"start\\":6,\\"end\\":9,\\"loc\\":{\\"start\\":{\\"line\\":1,\\"column\\":6},\\"end\\":{\\"line\\":1,\\"column\\":9},\\"identifierName\\":\\"foo\\"},\\"range\\":[6,9],\\"name\\":\\"foo\\"},\\"init\\":{\\"type\\":\\"StringLiteral\\",\\"start\\":12,\\"end\\":17,\\"loc\\":{\\"start\\":{\\"line\\":1,\\"column\\":12},\\"end\\":{\\"line\\":1,\\"column\\":17}},\\"range\\":[12,17],\\"extra\\":{\\"rawValue\\":\\"foo\\",\\"raw\\":\\"'foo'\\"},\\"value\\":\\"foo\\"}}],\\"kind\\":\\"const\\"}],\\"directives\\":[]},\\"comments\\":[],\\"tokens\\":[{\\"type\\":{\\"label\\":\\"const\\",\\"keyword\\":\\"const\\",\\"beforeExpr\\":false,\\"startsExpr\\":false,\\"rightAssociative\\":false,\\"isLoop\\":false,\\"isAssign\\":false,\\"prefix\\":false,\\"postfix\\":false,\\"binop\\":null,\\"updateContext\\":null},\\"value\\":\\"const\\",\\"start\\":0,\\"end\\":5,\\"loc\\":{\\"start\\":{\\"line\\":1,\\"column\\":0},\\"end\\":{\\"line\\":1,\\"column\\":5}}},{\\"type\\":{\\"label\\":\\"name\\",\\"beforeExpr\\":false,\\"startsExpr\\":true,\\"rightAssociative\\":false,\\"isLoop\\":false,\\"isAssign\\":false,\\"prefix\\":false,\\"postfix\\":false,\\"binop\\":null,\\"updateContext\\":null},\\"value\\":\\"foo\\",\\"start\\":6,\\"end\\":9,\\"loc\\":{\\"start\\":{\\"line\\":1,\\"column\\":6},\\"end\\":{\\"line\\":1,\\"column\\":9}}},{\\"type\\":{\\"label\\":\\"=\\",\\"beforeExpr\\":true,\\"startsExpr\\":false,\\"rightAssociative\\":false,\\"isLoop\\":false,\\"isAssign\\":true,\\"prefix\\":false,\\"postfix\\":false,\\"binop\\":null,\\"updateContext\\":null},\\"value\\":\\"=\\",\\"start\\":10,\\"end\\":11,\\"loc\\":{\\"start\\":{\\"line\\":1,\\"column\\":10},\\"end\\":{\\"line\\":1,\\"column\\":11}}},{\\"type\\":{\\"label\\":\\"string\\",\\"beforeExpr\\":false,\\"startsExpr\\":true,\\"rightAssociative\\":false,\\"isLoop\\":false,\\"isAssign\\":false,\\"prefix\\":false,\\"postfix\\":false,\\"binop\\":null,\\"updateContext\\":null},\\"value\\":\\"foo\\",\\"start\\":12,\\"end\\":17,\\"loc\\":{\\"start\\":{\\"line\\":1,\\"column\\":12},\\"end\\":{\\"line\\":1,\\"column\\":17}}},{\\"type\\":{\\"label\\":\\";\\",\\"beforeExpr\\":true,\\"startsExpr\\":false,\\"rightAssociative\\":false,\\"isLoop\\":false,\\"isAssign\\":false,\\"prefix\\":false,\\"postfix\\":false,\\"binop\\":null,\\"updateContext\\":null},\\"start\\":17,\\"end\\":18,\\"loc\\":{\\"start\\":{\\"line\\":1,\\"column\\":17},\\"end\\":{\\"line\\":1,\\"column\\":18}}},{\\"type\\":{\\"label\\":\\"eof\\",\\"beforeExpr\\":false,\\"startsExpr\\":false,\\"rightAssociative\\":false,\\"isLoop\\":false,\\"isAssign\\":false,\\"prefix\\":false,\\"postfix\\":false,\\"binop\\":null,\\"updateContext\\":null},\\"start\\":18,\\"end\\":18,\\"loc\\":{\\"start\\":{\\"line\\":1,\\"column\\":18},\\"end\\":{\\"line\\":1,\\"column\\":18}}}]}"`;

exports[`prints information for debugging AST --debug-print-ast (write) 1`] = `Array []`;
12 changes: 12 additions & 0 deletions tests/integration/__tests__/debug-print-ast.js
@@ -0,0 +1,12 @@
"use strict";

const runPrettier = require("../runPrettier.js");

describe("prints information for debugging AST --debug-print-ast", () => {
runPrettier("cli/with-shebang", ["--debug-print-ast", "--parser", "babel"], {
input: "const foo = 'foo';",
}).test({
stderr: "",
status: 0,
});
});

0 comments on commit aa63269

Please sign in to comment.