diff options
Diffstat (limited to 'cli/tools')
-rw-r--r-- | cli/tools/coverage.rs | 17 | ||||
-rw-r--r-- | cli/tools/doc.rs | 17 | ||||
-rw-r--r-- | cli/tools/fmt.rs | 96 | ||||
-rw-r--r-- | cli/tools/lint.rs | 11 | ||||
-rw-r--r-- | cli/tools/repl.rs | 51 | ||||
-rw-r--r-- | cli/tools/test.rs | 42 |
6 files changed, 123 insertions, 111 deletions
diff --git a/cli/tools/coverage.rs b/cli/tools/coverage.rs index 62f5f5d2e..92ade77fd 100644 --- a/cli/tools/coverage.rs +++ b/cli/tools/coverage.rs @@ -1,14 +1,13 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use crate::ast; -use crate::ast::TokenOrComment; use crate::colors; use crate::flags::Flags; use crate::fs_util::collect_files; -use crate::media_type::MediaType; use crate::module_graph::TypeLib; use crate::program_state::ProgramState; use crate::source_maps::SourceMapGetter; +use deno_ast::swc::common::Span; +use deno_ast::MediaType; use deno_core::error::AnyError; use deno_core::serde_json; use deno_core::url::Url; @@ -23,7 +22,7 @@ use std::fs::File; use std::io::BufWriter; use std::io::Write; use std::path::PathBuf; -use swc_common::Span; +use std::sync::Arc; use uuid::Uuid; // TODO(caspervonb) all of these structs can and should be made private, possibly moved to @@ -190,7 +189,7 @@ pub trait CoverageReporter { script_coverage: &ScriptCoverage, script_source: &str, maybe_source_map: Option<Vec<u8>>, - maybe_original_source: Option<String>, + maybe_original_source: Option<Arc<String>>, ); fn done(&mut self); @@ -210,7 +209,7 @@ impl CoverageReporter for LcovCoverageReporter { script_coverage: &ScriptCoverage, script_source: &str, maybe_source_map: Option<Vec<u8>>, - _maybe_original_source: Option<String>, + _maybe_original_source: Option<Arc<String>>, ) { // TODO(caspervonb) cleanup and reduce duplication between reporters, pre-compute line coverage // elsewhere. @@ -426,14 +425,14 @@ impl CoverageReporter for PrettyCoverageReporter { script_coverage: &ScriptCoverage, script_source: &str, maybe_source_map: Option<Vec<u8>>, - maybe_original_source: Option<String>, + maybe_original_source: Option<Arc<String>>, ) { let maybe_source_map = maybe_source_map .map(|source_map| SourceMap::from_slice(&source_map).unwrap()); let mut ignored_spans: Vec<Span> = Vec::new(); - for item in ast::lex(script_source, &MediaType::JavaScript) { - if let TokenOrComment::Token(_) = item.inner { + for item in deno_ast::lex(script_source, MediaType::JavaScript) { + if let deno_ast::TokenOrComment::Token(_) = item.inner { continue; } diff --git a/cli/tools/doc.rs b/cli/tools/doc.rs index 339e046c3..cde601f1a 100644 --- a/cli/tools/doc.rs +++ b/cli/tools/doc.rs @@ -1,15 +1,14 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use crate::ast; use crate::colors; use crate::file_fetcher::File; use crate::flags::Flags; use crate::get_types; use crate::import_map::ImportMap; -use crate::media_type::MediaType; use crate::program_state::ProgramState; use crate::write_json_to_stdout; use crate::write_to_stdout_ignore_sigpipe; +use deno_ast::MediaType; use deno_core::error::AnyError; use deno_core::futures::future; use deno_core::futures::future::FutureExt; @@ -81,7 +80,7 @@ impl Loader for DocLoader { .map(|file| { Some(LoadResponse { specifier: specifier.clone(), - content: Arc::new(file.source), + content: file.source.clone(), maybe_headers: file.maybe_headers, }) }); @@ -100,6 +99,7 @@ pub async fn print_docs( ) -> Result<(), AnyError> { let program_state = ProgramState::build(flags.clone()).await?; let source_file = source_file.unwrap_or_else(|| "--builtin".to_string()); + let source_parser = deno_graph::DefaultSourceParser::new(); let parse_result = if source_file == "--builtin" { let mut loader = StubDocLoader; @@ -113,12 +113,11 @@ pub async fn print_docs( None, ) .await; - let doc_parser = doc::DocParser::new(graph, private); - let syntax = ast::get_syntax(&MediaType::Dts); + let doc_parser = doc::DocParser::new(graph, private, &source_parser); doc_parser.parse_source( &source_file_specifier, - syntax, - get_types(flags.unstable).as_str(), + MediaType::Dts, + Arc::new(get_types(flags.unstable)), ) } else { let module_specifier = resolve_url_or_path(&source_file)?; @@ -130,7 +129,7 @@ pub async fn print_docs( local: PathBuf::from("./$deno$doc.ts"), maybe_types: None, media_type: MediaType::TypeScript, - source: format!("export * from \"{}\";", module_specifier), + source: Arc::new(format!("export * from \"{}\";", module_specifier)), specifier: root_specifier.clone(), maybe_headers: None, }; @@ -152,7 +151,7 @@ pub async fn print_docs( None, ) .await; - let doc_parser = doc::DocParser::new(graph, private); + let doc_parser = doc::DocParser::new(graph, private, &source_parser); doc_parser.parse_with_reexports(&root_specifier) }; diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index 59f9ddc8b..b3dfc86e8 100644 --- a/cli/tools/fmt.rs +++ b/cli/tools/fmt.rs @@ -13,6 +13,7 @@ use crate::file_watcher; use crate::file_watcher::ResolutionResult; use crate::fs_util::{collect_files, get_extension, is_supported_ext_fmt}; use crate::text_encoding; +use deno_ast::ParsedSource; use deno_core::error::generic_error; use deno_core::error::AnyError; use deno_core::futures; @@ -62,16 +63,13 @@ pub async fn format( } } }; - let operation = |paths: Vec<PathBuf>| { - let config = get_typescript_config(); - async move { - if check { - check_source_files(config, paths).await?; - } else { - format_source_files(config, paths).await?; - } - Ok(()) + let operation = |paths: Vec<PathBuf>| async move { + if check { + check_source_files(paths).await?; + } else { + format_source_files(paths).await?; } + Ok(()) }; if watch { @@ -91,14 +89,10 @@ pub async fn format( /// Formats markdown (using <https://github.com/dprint/dprint-plugin-markdown>) and its code blocks /// (ts/tsx, js/jsx). -fn format_markdown( - file_text: &str, - ts_config: dprint_plugin_typescript::configuration::Configuration, -) -> Result<String, String> { - let md_config = get_markdown_config(); +fn format_markdown(file_text: &str) -> Result<String, String> { dprint_plugin_markdown::format_text( file_text, - &md_config, + &MARKDOWN_CONFIG, move |tag, text, line_width| { let tag = tag.to_lowercase(); if matches!( @@ -121,13 +115,13 @@ fn format_markdown( }; if matches!(extension, "json" | "jsonc") { - let mut json_config = get_json_config(); + let mut json_config = JSON_CONFIG.clone(); json_config.line_width = line_width; dprint_plugin_json::format_text(text, &json_config) } else { let fake_filename = PathBuf::from(format!("deno_fmt_stdin.{}", extension)); - let mut codeblock_config = ts_config.clone(); + let mut codeblock_config = TYPESCRIPT_CONFIG.clone(); codeblock_config.line_width = line_width; dprint_plugin_typescript::format_text( &fake_filename, @@ -147,8 +141,7 @@ fn format_markdown( /// of configuration builder of <https://github.com/dprint/dprint-plugin-json>. /// See <https://git.io/Jt4ht> for configuration. fn format_json(file_text: &str) -> Result<String, String> { - let json_config = get_json_config(); - dprint_plugin_json::format_text(file_text, &json_config) + dprint_plugin_json::format_text(file_text, &JSON_CONFIG) .map_err(|e| e.to_string()) } @@ -156,23 +149,40 @@ fn format_json(file_text: &str) -> Result<String, String> { pub fn format_file( file_path: &Path, file_text: &str, - config: dprint_plugin_typescript::configuration::Configuration, ) -> Result<String, String> { let ext = get_extension(file_path).unwrap_or_else(String::new); if ext == "md" { - format_markdown(file_text, config) + format_markdown(file_text) } else if matches!(ext.as_str(), "json" | "jsonc") { format_json(file_text) } else { - dprint_plugin_typescript::format_text(file_path, file_text, &config) - .map_err(|e| e.to_string()) + dprint_plugin_typescript::format_text( + file_path, + file_text, + &TYPESCRIPT_CONFIG, + ) + .map_err(|e| e.to_string()) } } -async fn check_source_files( - config: dprint_plugin_typescript::configuration::Configuration, - paths: Vec<PathBuf>, -) -> Result<(), AnyError> { +pub fn format_parsed_module(parsed_source: &ParsedSource) -> String { + dprint_plugin_typescript::format_parsed_file( + &dprint_plugin_typescript::SourceFileInfo { + is_jsx: matches!( + parsed_source.media_type(), + deno_ast::MediaType::Jsx | deno_ast::MediaType::Tsx + ), + info: parsed_source.source(), + leading_comments: parsed_source.comments().leading_map(), + trailing_comments: parsed_source.comments().trailing_map(), + module: parsed_source.module(), + tokens: parsed_source.tokens(), + }, + &TYPESCRIPT_CONFIG, + ) +} + +async fn check_source_files(paths: Vec<PathBuf>) -> Result<(), AnyError> { let not_formatted_files_count = Arc::new(AtomicUsize::new(0)); let checked_files_count = Arc::new(AtomicUsize::new(0)); @@ -186,7 +196,7 @@ async fn check_source_files( checked_files_count.fetch_add(1, Ordering::Relaxed); let file_text = read_file_contents(&file_path)?.text; - match format_file(&file_path, &file_text, config) { + match format_file(&file_path, &file_text) { Ok(formatted_text) => { if formatted_text != file_text { not_formatted_files_count.fetch_add(1, Ordering::Relaxed); @@ -225,10 +235,7 @@ async fn check_source_files( } } -async fn format_source_files( - config: dprint_plugin_typescript::configuration::Configuration, - paths: Vec<PathBuf>, -) -> Result<(), AnyError> { +async fn format_source_files(paths: Vec<PathBuf>) -> Result<(), AnyError> { let formatted_files_count = Arc::new(AtomicUsize::new(0)); let checked_files_count = Arc::new(AtomicUsize::new(0)); let output_lock = Arc::new(Mutex::new(0)); // prevent threads outputting at the same time @@ -240,7 +247,7 @@ async fn format_source_files( checked_files_count.fetch_add(1, Ordering::Relaxed); let file_contents = read_file_contents(&file_path)?; - match format_file(&file_path, &file_contents.text, config) { + match format_file(&file_path, &file_contents.text) { Ok(formatted_text) => { if formatted_text != file_contents.text { write_file_contents( @@ -291,10 +298,9 @@ pub fn format_stdin(check: bool, ext: String) -> Result<(), AnyError> { if stdin().read_to_string(&mut source).is_err() { return Err(generic_error("Failed to read from stdin")); } - let config = get_typescript_config(); let file_path = PathBuf::from(format!("_stdin.{}", ext)); - match format_file(&file_path, &source, config) { + match format_file(&file_path, &source) { Ok(formatted_text) => { if check { if formatted_text != source { @@ -319,24 +325,18 @@ fn files_str(len: usize) -> &'static str { } } -pub fn get_typescript_config( -) -> dprint_plugin_typescript::configuration::Configuration { - dprint_plugin_typescript::configuration::ConfigurationBuilder::new() +lazy_static::lazy_static! { + static ref TYPESCRIPT_CONFIG: dprint_plugin_typescript::configuration::Configuration = dprint_plugin_typescript::configuration::ConfigurationBuilder::new() .deno() - .build() -} + .build(); -fn get_markdown_config() -> dprint_plugin_markdown::configuration::Configuration -{ - dprint_plugin_markdown::configuration::ConfigurationBuilder::new() + static ref MARKDOWN_CONFIG: dprint_plugin_markdown::configuration::Configuration = dprint_plugin_markdown::configuration::ConfigurationBuilder::new() .deno() - .build() -} + .build(); -fn get_json_config() -> dprint_plugin_json::configuration::Configuration { - dprint_plugin_json::configuration::ConfigurationBuilder::new() + static ref JSON_CONFIG: dprint_plugin_json::configuration::Configuration = dprint_plugin_json::configuration::ConfigurationBuilder::new() .deno() - .build() + .build(); } struct FileContents { diff --git a/cli/tools/lint.rs b/cli/tools/lint.rs index 8a912a45c..3895d0a50 100644 --- a/cli/tools/lint.rs +++ b/cli/tools/lint.rs @@ -1,18 +1,18 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -//! This module provides file formatting utilities using +//! This module provides file linting utilities using //! [`deno_lint`](https://github.com/denoland/deno_lint). //! //! At the moment it is only consumed using CLI but in //! the future it can be easily extended to provide //! the same functions as ops available in JS runtime. -use crate::ast; use crate::colors; use crate::config_file::LintConfig; use crate::fmt_errors; use crate::fs_util::{collect_files, is_supported_ext}; -use crate::media_type::MediaType; use crate::tools::fmt::run_parallelized; +use deno_ast::swc::parser::Syntax; +use deno_ast::MediaType; use deno_core::error::{anyhow, generic_error, AnyError, JsStackFrame}; use deno_core::serde_json; use deno_lint::diagnostic::LintDiagnostic; @@ -28,7 +28,6 @@ use std::io::{stdin, Read}; use std::path::PathBuf; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; -use swc_ecmascript::parser::Syntax; pub enum LintReporterKind { Pretty, @@ -210,7 +209,7 @@ fn lint_file( let file_name = file_path.to_string_lossy().to_string(); let source_code = fs::read_to_string(&file_path)?; let media_type = MediaType::from(&file_path); - let syntax = ast::get_syntax(&media_type); + let syntax = deno_ast::get_syntax(media_type); // Obtaining rules from config is infallible at this point. let lint_rules = get_configured_rules( @@ -254,7 +253,7 @@ fn lint_stdin( rules_include, rules_exclude, )?; - let syntax = ast::get_syntax(&MediaType::TypeScript); + let syntax = deno_ast::get_syntax(MediaType::TypeScript); let linter = create_linter(syntax, lint_rules); let mut has_error = false; let pseudo_file_name = "_stdin.ts"; diff --git a/cli/tools/repl.rs b/cli/tools/repl.rs index e313d1735..4b3080c63 100644 --- a/cli/tools/repl.rs +++ b/cli/tools/repl.rs @@ -1,12 +1,11 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use crate::ast; -use crate::ast::Diagnostic; +use crate::ast::transpile; use crate::ast::ImportsNotUsedAsValues; -use crate::ast::TokenOrComment; use crate::colors; -use crate::media_type::MediaType; use crate::program_state::ProgramState; +use deno_ast::swc::parser::error::SyntaxError; +use deno_ast::swc::parser::token::{Token, Word}; use deno_core::error::AnyError; use deno_core::futures::FutureExt; use deno_core::parking_lot::Mutex; @@ -29,8 +28,6 @@ use std::borrow::Cow; use std::cell::RefCell; use std::path::PathBuf; use std::sync::Arc; -use swc_ecmascript::parser::error::SyntaxError; -use swc_ecmascript::parser::token::{Token, Word}; use tokio::sync::mpsc::channel; use tokio::sync::mpsc::unbounded_channel; use tokio::sync::mpsc::Receiver; @@ -231,8 +228,8 @@ impl Validator for EditorHelper { let mut stack: Vec<Token> = Vec::new(); let mut in_template = false; - for item in ast::lex(ctx.input(), &MediaType::TypeScript) { - if let TokenOrComment::Token(token) = item.inner { + for item in deno_ast::lex(ctx.input(), deno_ast::MediaType::TypeScript) { + if let deno_ast::TokenOrComment::Token(token) = item.inner { match token { Token::BackQuote => in_template = !in_template, Token::LParen @@ -306,16 +303,19 @@ impl Highlighter for EditorHelper { fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> { let mut out_line = String::from(line); - for item in ast::lex(line, &MediaType::TypeScript) { + for item in deno_ast::lex(line, deno_ast::MediaType::TypeScript) { // Adding color adds more bytes to the string, // so an offset is needed to stop spans falling out of sync. let offset = out_line.len() - line.len(); - let span = item.span_as_range(); + let span = std::ops::Range { + start: item.span.lo.0 as usize, + end: item.span.hi.0 as usize, + }; out_line.replace_range( span.start + offset..span.end + offset, &match item.inner { - TokenOrComment::Token(token) => match token { + deno_ast::TokenOrComment::Token(token) => match token { Token::Str { .. } | Token::Template { .. } | Token::BackQuote => { colors::green(&line[span]).to_string() } @@ -342,7 +342,7 @@ impl Highlighter for EditorHelper { }, _ => line[span].to_string(), }, - TokenOrComment::Comment { .. } => { + deno_ast::TokenOrComment::Comment { .. } => { colors::gray(&line[span]).to_string() } }, @@ -536,13 +536,13 @@ impl ReplSession { } Err(err) => { // handle a parsing diagnostic - match err.downcast_ref::<Diagnostic>() { + match err.downcast_ref::<deno_ast::Diagnostic>() { Some(diagnostic) => Ok(EvaluationOutput::Error(format!( "{}: {} at {}:{}", colors::red("parse error"), diagnostic.message, - diagnostic.location.line, - diagnostic.location.col + diagnostic.display_position.line_number, + diagnostic.display_position.column_number, ))), None => Err(err), } @@ -649,11 +649,17 @@ impl ReplSession { &mut self, expression: &str, ) -> Result<Value, AnyError> { - let parsed_module = - crate::ast::parse("repl.ts", expression, &crate::MediaType::TypeScript)?; - - let transpiled_src = parsed_module - .transpile(&crate::ast::EmitOptions { + let parsed_module = deno_ast::parse_module(deno_ast::ParseParams { + specifier: "repl.ts".to_string(), + source: deno_ast::SourceTextInfo::from_string(expression.to_string()), + media_type: deno_ast::MediaType::TypeScript, + capture_tokens: false, + maybe_syntax: None, + })?; + + let transpiled_src = transpile( + &parsed_module, + &crate::ast::EmitOptions { emit_metadata: false, source_map: false, inline_source_map: false, @@ -663,8 +669,9 @@ impl ReplSession { jsx_factory: "React.createElement".into(), jsx_fragment_factory: "React.Fragment".into(), repl_imports: true, - })? - .0; + }, + )? + .0; self .evaluate_expression(&format!( diff --git a/cli/tools/test.rs b/cli/tools/test.rs index 859e23934..dc1cd643f 100644 --- a/cli/tools/test.rs +++ b/cli/tools/test.rs @@ -1,6 +1,5 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use crate::ast; use crate::ast::Location; use crate::colors; use crate::create_main_worker; @@ -12,7 +11,6 @@ use crate::fs_util::collect_specifiers; use crate::fs_util::is_supported_test_ext; use crate::fs_util::is_supported_test_path; use crate::located_script_name; -use crate::media_type::MediaType; use crate::module_graph; use crate::module_graph::GraphBuilder; use crate::module_graph::Module; @@ -22,6 +20,8 @@ use crate::program_state::ProgramState; use crate::tokio_util; use crate::tools::coverage::CoverageCollector; use crate::FetchHandler; +use deno_ast::swc::common::comments::CommentKind; +use deno_ast::MediaType; use deno_core::error::generic_error; use deno_core::error::AnyError; use deno_core::futures::future; @@ -47,7 +47,6 @@ use std::sync::mpsc::Sender; use std::sync::Arc; use std::time::Duration; use std::time::Instant; -use swc_common::comments::CommentKind; use uuid::Uuid; /// The test mode is used to determine how a specifier is to be tested. @@ -269,7 +268,7 @@ async fn test_specifier( local: test_specifier.to_file_path().unwrap(), maybe_types: None, media_type: MediaType::JavaScript, - source: test_source.clone(), + source: Arc::new(test_source), specifier: test_specifier.clone(), maybe_headers: None, }; @@ -344,7 +343,7 @@ async fn test_specifier( fn extract_files_from_regex_blocks( location: &Location, source: &str, - media_type: &MediaType, + media_type: MediaType, blocks_regex: &Regex, lines_regex: &Regex, ) -> Result<Vec<File>, AnyError> { @@ -365,11 +364,11 @@ fn extract_files_from_regex_blocks( Some(&"jsx") => MediaType::Jsx, Some(&"ts") => MediaType::TypeScript, Some(&"tsx") => MediaType::Tsx, - Some(&"") => *media_type, + Some(&"") => media_type, _ => MediaType::Unknown, } } else { - *media_type + media_type }; if file_media_type == MediaType::Unknown { @@ -408,7 +407,7 @@ fn extract_files_from_regex_blocks( local: file_specifier.to_file_path().unwrap(), maybe_types: None, media_type: file_media_type, - source: file_source, + source: Arc::new(file_source), specifier: file_specifier, maybe_headers: None, }) @@ -420,11 +419,20 @@ fn extract_files_from_regex_blocks( fn extract_files_from_source_comments( specifier: &ModuleSpecifier, - source: &str, - media_type: &MediaType, + source: Arc<String>, + media_type: MediaType, ) -> Result<Vec<File>, AnyError> { - let parsed_module = ast::parse(specifier.as_str(), source, media_type)?; - let comments = parsed_module.get_comments(); + let parsed_source = deno_ast::parse_module(deno_ast::ParseParams { + specifier: specifier.as_str().to_string(), + source: deno_ast::SourceTextInfo::new( + deno_ast::swc::common::BytePos(0), + source, + ), + media_type, + capture_tokens: false, + maybe_syntax: None, + })?; + let comments = parsed_source.comments().get_vec(); let blocks_regex = Regex::new(r"```([^\n]*)\n([\S\s]*?)```")?; let lines_regex = Regex::new(r"(?:\* ?)(?:\# ?)?(.*)")?; @@ -438,7 +446,7 @@ fn extract_files_from_source_comments( true }) .flat_map(|comment| { - let location = parsed_module.get_location(comment.span.lo); + let location = Location::from_pos(&parsed_source, comment.span.lo); extract_files_from_regex_blocks( &location, @@ -457,7 +465,7 @@ fn extract_files_from_source_comments( fn extract_files_from_fenced_blocks( specifier: &ModuleSpecifier, source: &str, - media_type: &MediaType, + media_type: MediaType, ) -> Result<Vec<File>, AnyError> { let location = Location { specifier: specifier.to_string(), @@ -493,13 +501,13 @@ async fn fetch_inline_files( extract_files_from_fenced_blocks( &file.specifier, &file.source, - &file.media_type, + file.media_type, ) } else { extract_files_from_source_comments( &file.specifier, - &file.source, - &file.media_type, + file.source.clone(), + file.media_type, ) }; |