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

feat: Add "deno ast" subcommand #6867

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1e82d10
feat: add AST command flags and parser
littledivy Jul 24, 2020
cfc71f6
feat: implement swc_util ast parser
littledivy Jul 24, 2020
4584b2c
chores: add debug log end
littledivy Jul 24, 2020
67b025e
chores: fmt
littledivy Jul 24, 2020
943b0cb
fix: remove other args
littledivy Jul 24, 2020
cfa7433
fix: flags and suggestions
littledivy Jul 24, 2020
e6fe05f
chores: fmt
littledivy Jul 25, 2020
e43ea5a
Merge branch 'master' into feat/deno-ast-parser
littledivy Jul 25, 2020
03874a9
fix
littledivy Jul 25, 2020
db58565
fix: tests
littledivy Jul 25, 2020
95017a5
feat: pretty print ast
littledivy Jul 25, 2020
560e876
fix: LR assertion
littledivy Jul 25, 2020
584e900
tests(ast): add integration test
littledivy Jul 26, 2020
59c7cd1
tests: add expected output to tests
littledivy Jul 26, 2020
5a7d8dc
feat: integration tests
littledivy Jul 26, 2020
6da64c6
fix: output is not matching
littledivy Jul 26, 2020
9f485ba
fix: eslint with new test example
littledivy Jul 26, 2020
c1bca03
chores: fmt
littledivy Jul 26, 2020
e272049
fix: forgot flags
littledivy Jul 26, 2020
4e601eb
fix: out
littledivy Jul 26, 2020
8e17324
fix: newline issue
littledivy Jul 26, 2020
25ac535
Adjust help text
ry Jul 26, 2020
bc371b3
add unstable flag
ry Jul 26, 2020
0bcc5c2
unstable flag comes before source file
ry Jul 26, 2020
0172247
Merge remote-tracking branch 'origin/master' into feat/deno-ast-parser
littledivy Sep 17, 2020
3c209cb
merge
littledivy Sep 17, 2020
85219ab
chores: merge api changes
littledivy Sep 17, 2020
cab2116
use new ast parser
littledivy Sep 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ macro_rules! svec {

#[derive(Clone, Debug, PartialEq)]
pub enum DenoSubcommand {
Ast {
source_file: String,
},
Bundle {
source_file: String,
out_file: Option<PathBuf>,
Expand Down Expand Up @@ -273,6 +276,8 @@ pub fn flags_from_vec_safe(args: Vec<String>) -> clap::Result<Flags> {
doc_parse(&mut flags, m);
} else if let Some(m) = matches.subcommand_matches("lint") {
lint_parse(&mut flags, m);
} else if let Some(m) = matches.subcommand_matches("ast") {
ast_parse(&mut flags, m);
} else {
repl_parse(&mut flags, &matches);
}
Expand Down Expand Up @@ -314,6 +319,7 @@ If the flag is set, restrict these messages to errors.",
)
.global(true),
)
.subcommand(ast_subcommand())
.subcommand(bundle_subcommand())
.subcommand(cache_subcommand())
.subcommand(completions_subcommand())
Expand All @@ -337,6 +343,13 @@ fn types_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
flags.subcommand = DenoSubcommand::Types;
}

fn ast_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
unstable_arg_parse(flags, matches);

let source_file = matches.value_of("source_file").unwrap().to_string();
flags.subcommand = DenoSubcommand::Ast { source_file };
}

fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
let files = match matches.values_of("files") {
Some(f) => f.map(String::from).collect(),
Expand Down Expand Up @@ -755,6 +768,21 @@ If no output file is given, the output is written to standard output:
)
}

fn ast_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("ast")
.arg(
Arg::with_name("source_file")
.takes_value(true)
.required(true),
)
.arg(unstable_arg())
.about("Print the AST of a particular source file.")
.long_about(
"Prints the AST of a single JavaScript/TypeScript file.
deno ast https://deno.land/std/examples/colors.ts",
)
}

fn completions_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("completions")
.setting(AppSettings::DisableHelpSubcommand)
Expand Down Expand Up @@ -3027,7 +3055,25 @@ mod tests {
}
);
}

#[test]
fn ast() {
let r = flags_from_vec_safe(svec![
"deno",
"ast",
"--unstable",
"https://deno.land/std/http/file_server.ts"
]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Ast {
source_file: "https://deno.land/std/http/file_server.ts".to_string(),
},
unstable: true,
..Flags::default()
}
);
}
#[test]
fn inspect_default_host() {
let r = flags_from_vec_safe(svec!["deno", "run", "--inspect", "foo.js"]);
Expand Down
36 changes: 36 additions & 0 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ use crate::global_state::GlobalState;
use crate::msg::MediaType;
use crate::op_error::OpError;
use crate::permissions::Permissions;
use crate::swc_util::AstParser;
use crate::tsc::TargetLib;
use crate::worker::MainWorker;
use deno_core::v8_set_flags;
Expand Down Expand Up @@ -436,6 +437,38 @@ async fn eval_command(
Ok(())
}

async fn ast_command(flags: Flags, source_file: String) -> Result<(), ErrBox> {
let module_specifier = ModuleSpecifier::resolve_url_or_path(&source_file)?;

debug!(">>>>> ast START");

if !flags.unstable {
exit_unstable("ast");
}

let global_state = GlobalState::new(flags)?;

let out = global_state
.file_fetcher
.fetch_source_file(&module_specifier, None, Permissions::allow_all())
.await?;
let src = std::str::from_utf8(&out.source_code)?;
let parser = AstParser::new();
let ast = parser.parse_module::<_, Result<swc_ecma_ast::Module, ErrBox>>(
&module_specifier.to_string(),
out.media_type,
src,
|parse_result| {
let module = parse_result.unwrap();
Ok(module)
},
)?;

debug!(">>>>> ast END");
write_to_stdout_ignore_sigpipe(serde_json::to_string_pretty(&ast)?.as_bytes())
.map_err(ErrBox::from)
}

async fn bundle_command(
flags: Flags,
source_file: String,
Expand Down Expand Up @@ -701,6 +734,9 @@ pub fn main() {
log::set_max_level(log_level.to_level_filter());

let fut = match flags.clone().subcommand {
DenoSubcommand::Ast { source_file } => {
ast_command(flags, source_file).boxed_local()
}
DenoSubcommand::Bundle {
source_file,
out_file,
Expand Down