Skip to content

Commit

Permalink
shermes: add --dump-transpiled-ast
Browse files Browse the repository at this point in the history
Summary:
I noticed that `--dump-ast` was outputting AST after TS-to-Flow
transpilation, and also after IIFE wrapping.

I found that to be confusing, since I expect "--dump-ast" to literally
print what was parsed, before any transformations have been performed on
it.

Move the dumping of the parsed AST earlier in the code and add a new
dumping stage `--dump-transpiled-ast`, which prints the AST after the
optional early transpilation to Flow and IIFE wrapping.

Reviewed By: avp

Differential Revision: D52149761

fbshipit-source-id: 56a83e9c42e0286faa164f0bdefe94883eaf11da
  • Loading branch information
Tzvetan Mikov authored and facebook-github-bot committed Dec 15, 2023
1 parent 5b803e4 commit 40748fa
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions test/AST/script-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

// RUN: %shermes -dump-ast -typed -script %s | %FileCheck --match-full-lines -check-prefix SM %s
// RUN: %shermes -dump-ast -typed %s | %FileCheck --match-full-lines -check-prefix MM %s
// RUN: %shermes -dump-transpiled-ast -typed -script %s | %FileCheck --match-full-lines -check-prefix SM %s
// RUN: %shermes -dump-transpiled-ast -typed %s | %FileCheck --match-full-lines -check-prefix MM %s

print("hi");

Expand Down
2 changes: 1 addition & 1 deletion test/AST/ts2flow/function-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

// RUN: %shermes -typed -parse-ts -dump-ast -pretty -script %s | %FileCheck %s --match-full-lines
// RUN: %shermes -typed -parse-ts -dump-transpiled-ast -pretty -script %s | %FileCheck %s --match-full-lines

// CHECK-LABEL: {
// CHECK-NEXT: "type": "Program",
Expand Down
2 changes: 1 addition & 1 deletion test/AST/ts2flow/literal-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

// RUN: %shermes -typed -parse-ts -dump-ast -pretty -script %s | %FileCheck %s --match-full-lines
// RUN: %shermes -typed -parse-ts -dump-transpiled-ast -pretty -script %s | %FileCheck %s --match-full-lines

// CHECK-LABEL: {
// CHECK-NEXT: "type": "Program",
Expand Down
2 changes: 1 addition & 1 deletion test/AST/ts2flow/nbody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

// RUN: %shermes -typed -parse-ts -dump-ast -pretty -script %s | %FileCheck --match-full-lines %s
// RUN: %shermes -typed -parse-ts -dump-transpiled-ast -pretty -script %s | %FileCheck --match-full-lines %s

"use strict";
(function main() {
Expand Down
2 changes: 1 addition & 1 deletion test/AST/ts2flow/primitive-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

// RUN: %shermes -typed -parse-ts -dump-ast -pretty -script %s | %FileCheck %s --match-full-lines
// RUN: %shermes -typed -parse-ts -dump-transpiled-ast -pretty -script %s | %FileCheck %s --match-full-lines

// CHECK-LABEL: {
// CHECK-NEXT: "type": "Program",
Expand Down
1 change: 1 addition & 0 deletions tools/shermes/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
enum class OutputLevelKind {
None,
AST,
TranspiledAST,
TransformedAST,
Sema,
CFG,
Expand Down
21 changes: 20 additions & 1 deletion tools/shermes/shermes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ OutputFormatKind toOutputFormatKind(OutputLevelKind level) {
case OutputLevelKind::AST:
return OutputFormatKind::DumpAST;
case OutputLevelKind::Sema:
case OutputLevelKind::TranspiledAST:
case OutputLevelKind::TransformedAST:
return OutputFormatKind::DumpTransformedAST;
case OutputLevelKind::CFG:
Expand Down Expand Up @@ -210,6 +211,10 @@ cl::opt<OutputLevelKind> OutputLevel(
cl::Optional,
cl::values(
clEnumValN(OutputLevelKind::AST, "dump-ast", "AST as text in JSON"),
clEnumValN(
OutputLevelKind::TranspiledAST,
"dump-transpiled-ast",
"Transformed AST as text after optional early transpilation"),
clEnumValN(
OutputLevelKind::TransformedAST,
"dump-transformed-ast",
Expand Down Expand Up @@ -758,6 +763,20 @@ ESTree::NodePtr parseJS(
}
}

if (cli::OutputLevel == OutputLevelKind::AST) {
hermes::dumpESTreeJSON(
llvh::outs(),
parsedAST,
cli::Pretty /* pretty */,
cli::IncludeEmptyASTNodes ? ESTreeDumpMode::DumpAll
: ESTreeDumpMode::HideEmpty,
context->getSourceErrorManager(),
cli::DumpSourceLocation,
cli::IncludeRawASTProp ? ESTreeRawProp::Include
: ESTreeRawProp::Exclude);
return parsedAST;
}

// Convert TS AST to Flow AST as an intermediate step until we have a
// separate TS type checker.
if (flowContext && context->getParseTS()) {
Expand All @@ -777,7 +796,7 @@ ESTree::NodePtr parseJS(
}
}

if (cli::OutputLevel == OutputLevelKind::AST) {
if (cli::OutputLevel == OutputLevelKind::TranspiledAST) {
hermes::dumpESTreeJSON(
llvh::outs(),
parsedAST,
Expand Down

0 comments on commit 40748fa

Please sign in to comment.