Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new CLI option debug-print-ast #11514

Merged
merged 6 commits into from Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
});
});