2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-14 08:27:44 -04:00
|
|
|
|
2021-05-10 12:16:39 -04:00
|
|
|
use crate::config_file;
|
2021-08-16 03:28:29 -04:00
|
|
|
use crate::text_encoding::strip_bom;
|
2020-09-24 18:31:17 -04:00
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::get_syntax;
|
|
|
|
use deno_ast::swc::ast::Module;
|
|
|
|
use deno_ast::swc::ast::Program;
|
|
|
|
use deno_ast::swc::codegen::text_writer::JsWriter;
|
|
|
|
use deno_ast::swc::codegen::Node;
|
|
|
|
use deno_ast::swc::common::chain;
|
|
|
|
use deno_ast::swc::common::comments::SingleThreadedComments;
|
2021-11-15 18:04:34 -05:00
|
|
|
use deno_ast::swc::common::errors::Diagnostic as SwcDiagnostic;
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::swc::common::BytePos;
|
|
|
|
use deno_ast::swc::common::FileName;
|
|
|
|
use deno_ast::swc::common::Globals;
|
2021-10-12 09:58:04 -04:00
|
|
|
use deno_ast::swc::common::Mark;
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::swc::common::SourceMap;
|
|
|
|
use deno_ast::swc::common::Spanned;
|
2021-12-08 19:12:14 -05:00
|
|
|
use deno_ast::swc::parser::error::Error as SwcError;
|
|
|
|
use deno_ast::swc::parser::error::SyntaxError;
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::swc::parser::lexer::Lexer;
|
|
|
|
use deno_ast::swc::parser::StringInput;
|
|
|
|
use deno_ast::swc::transforms::fixer;
|
|
|
|
use deno_ast::swc::transforms::helpers;
|
|
|
|
use deno_ast::swc::transforms::hygiene;
|
|
|
|
use deno_ast::swc::transforms::pass::Optional;
|
|
|
|
use deno_ast::swc::transforms::proposals;
|
|
|
|
use deno_ast::swc::transforms::react;
|
2021-10-21 10:18:18 -04:00
|
|
|
use deno_ast::swc::transforms::resolver_with_mark;
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::swc::transforms::typescript;
|
|
|
|
use deno_ast::swc::visit::FoldWith;
|
|
|
|
use deno_ast::Diagnostic;
|
|
|
|
use deno_ast::LineAndColumnDisplay;
|
|
|
|
use deno_ast::MediaType;
|
|
|
|
use deno_ast::ParsedSource;
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::anyhow::anyhow;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2021-02-17 13:47:18 -05:00
|
|
|
use deno_core::resolve_url_or_path;
|
2020-10-19 23:10:42 -04:00
|
|
|
use deno_core::serde_json;
|
2020-09-14 08:27:44 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
2021-11-15 18:04:34 -05:00
|
|
|
use std::cell::RefCell;
|
2021-12-08 19:12:14 -05:00
|
|
|
use std::fmt;
|
2020-09-14 08:27:44 -04:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2021-08-06 10:36:16 -04:00
|
|
|
mod bundle_hook;
|
2021-06-24 09:00:46 -04:00
|
|
|
mod transforms;
|
|
|
|
|
2021-08-06 10:36:16 -04:00
|
|
|
pub use bundle_hook::BundleHook;
|
2020-09-14 08:27:44 -04:00
|
|
|
|
2020-09-14 17:59:49 -04:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-09-14 08:27:44 -04:00
|
|
|
pub struct Location {
|
2021-08-06 10:36:16 -04:00
|
|
|
pub specifier: String,
|
2020-09-14 08:27:44 -04:00
|
|
|
pub line: usize,
|
|
|
|
pub col: usize,
|
|
|
|
}
|
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
impl Location {
|
|
|
|
pub fn from_pos(parsed_source: &ParsedSource, pos: BytePos) -> Self {
|
|
|
|
Location::from_line_and_column(
|
|
|
|
parsed_source.specifier().to_string(),
|
|
|
|
parsed_source.source().line_and_column_index(pos),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_line_and_column(
|
|
|
|
specifier: String,
|
|
|
|
line_and_column: deno_ast::LineAndColumnIndex,
|
|
|
|
) -> Self {
|
|
|
|
Location {
|
|
|
|
specifier,
|
|
|
|
line: line_and_column.line_index + 1,
|
|
|
|
col: line_and_column.column_index,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<deno_ast::swc::common::Loc> for Location {
|
|
|
|
fn from(swc_loc: deno_ast::swc::common::Loc) -> Self {
|
|
|
|
use deno_ast::swc::common::FileName::*;
|
2020-09-14 08:27:44 -04:00
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
let filename = match &swc_loc.file.name {
|
2020-09-14 08:27:44 -04:00
|
|
|
Real(path_buf) => path_buf.to_string_lossy().to_string(),
|
|
|
|
Custom(str_) => str_.to_string(),
|
2021-09-08 00:05:34 -04:00
|
|
|
Url(url) => url.to_string(),
|
2020-09-14 08:27:44 -04:00
|
|
|
_ => panic!("invalid filename"),
|
|
|
|
};
|
|
|
|
|
|
|
|
Location {
|
2021-08-06 10:36:16 -04:00
|
|
|
specifier: filename,
|
2021-03-25 14:17:37 -04:00
|
|
|
line: swc_loc.line,
|
2021-09-07 10:39:32 -04:00
|
|
|
col: swc_loc.col.0,
|
2020-09-14 08:27:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<Location> for ModuleSpecifier {
|
|
|
|
fn from(loc: Location) -> Self {
|
2021-08-06 10:36:16 -04:00
|
|
|
resolve_url_or_path(&loc.specifier).unwrap()
|
2020-10-22 20:50:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for Location {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
2021-08-06 10:36:16 -04:00
|
|
|
write!(f, "{}:{}:{}", self.specifier, self.line, self.col)
|
2020-10-22 20:50:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 19:12:14 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Diagnostics(pub Vec<Diagnostic>);
|
|
|
|
|
|
|
|
impl std::error::Error for Diagnostics {}
|
|
|
|
|
|
|
|
impl fmt::Display for Diagnostics {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
for (i, diagnostic) in self.0.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
write!(f, "\n\n")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "{}", diagnostic)?
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 15:40:11 -05:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum ImportsNotUsedAsValues {
|
|
|
|
Remove,
|
|
|
|
Preserve,
|
|
|
|
Error,
|
|
|
|
}
|
|
|
|
|
2020-09-14 08:27:44 -04:00
|
|
|
/// Options which can be adjusted when transpiling a module.
|
|
|
|
#[derive(Debug, Clone)]
|
2020-10-19 23:10:42 -04:00
|
|
|
pub struct EmitOptions {
|
2020-09-14 08:27:44 -04:00
|
|
|
/// When emitting a legacy decorator, also emit experimental decorator meta
|
|
|
|
/// data. Defaults to `false`.
|
|
|
|
pub emit_metadata: bool,
|
2021-03-07 15:40:11 -05:00
|
|
|
/// What to do with import statements that only import types i.e. whether to
|
|
|
|
/// remove them (`Remove`), keep them as side-effect imports (`Preserve`)
|
|
|
|
/// or error (`Error`). Defaults to `Remove`.
|
|
|
|
pub imports_not_used_as_values: ImportsNotUsedAsValues,
|
2020-09-14 08:27:44 -04:00
|
|
|
/// Should the source map be inlined in the emitted code file, or provided
|
|
|
|
/// as a separate file. Defaults to `true`.
|
|
|
|
pub inline_source_map: bool,
|
2021-09-08 00:05:34 -04:00
|
|
|
/// Should the sources be inlined in the source map. Defaults to `true`.
|
|
|
|
pub inline_sources: bool,
|
2021-11-08 20:26:39 -05:00
|
|
|
/// Should a corresponding .map file be created for the output. This should be
|
|
|
|
/// false if inline_source_map is true. Defaults to `false`.
|
2021-05-19 00:18:01 -04:00
|
|
|
pub source_map: bool,
|
2021-11-08 20:26:39 -05:00
|
|
|
/// `true` if the program should use an implicit JSX import source/the "new"
|
|
|
|
/// JSX transforms.
|
|
|
|
pub jsx_automatic: bool,
|
|
|
|
/// If JSX is automatic, if it is in development mode, meaning that it should
|
|
|
|
/// import `jsx-dev-runtime` and transform JSX using `jsxDEV` import from the
|
|
|
|
/// JSX import source as well as provide additional debug information to the
|
|
|
|
/// JSX factory.
|
|
|
|
pub jsx_development: bool,
|
2020-09-14 08:27:44 -04:00
|
|
|
/// When transforming JSX, what value should be used for the JSX factory.
|
|
|
|
/// Defaults to `React.createElement`.
|
|
|
|
pub jsx_factory: String,
|
|
|
|
/// When transforming JSX, what value should be used for the JSX fragment
|
|
|
|
/// factory. Defaults to `React.Fragment`.
|
|
|
|
pub jsx_fragment_factory: String,
|
2021-11-08 20:26:39 -05:00
|
|
|
/// The string module specifier to implicitly import JSX factories from when
|
|
|
|
/// transpiling JSX.
|
|
|
|
pub jsx_import_source: Option<String>,
|
2020-09-14 08:27:44 -04:00
|
|
|
/// Should JSX be transformed or preserved. Defaults to `true`.
|
|
|
|
pub transform_jsx: bool,
|
2021-06-24 09:00:46 -04:00
|
|
|
/// Should import declarations be transformed to variable declarations.
|
|
|
|
/// This should only be set to true for the REPL. Defaults to `false`.
|
|
|
|
pub repl_imports: bool,
|
2020-09-14 08:27:44 -04:00
|
|
|
}
|
|
|
|
|
2020-10-19 23:10:42 -04:00
|
|
|
impl Default for EmitOptions {
|
2020-09-14 08:27:44 -04:00
|
|
|
fn default() -> Self {
|
2020-10-19 23:10:42 -04:00
|
|
|
EmitOptions {
|
2020-09-14 08:27:44 -04:00
|
|
|
emit_metadata: false,
|
2021-03-07 15:40:11 -05:00
|
|
|
imports_not_used_as_values: ImportsNotUsedAsValues::Remove,
|
2020-09-14 08:27:44 -04:00
|
|
|
inline_source_map: true,
|
2021-09-08 00:05:34 -04:00
|
|
|
inline_sources: true,
|
2021-05-19 00:18:01 -04:00
|
|
|
source_map: false,
|
2021-11-08 20:26:39 -05:00
|
|
|
jsx_automatic: false,
|
|
|
|
jsx_development: false,
|
2020-09-14 08:27:44 -04:00
|
|
|
jsx_factory: "React.createElement".into(),
|
|
|
|
jsx_fragment_factory: "React.Fragment".into(),
|
2021-11-08 20:26:39 -05:00
|
|
|
jsx_import_source: None,
|
2020-09-14 08:27:44 -04:00
|
|
|
transform_jsx: true,
|
2021-06-24 09:00:46 -04:00
|
|
|
repl_imports: false,
|
2020-09-14 08:27:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 12:16:39 -04:00
|
|
|
impl From<config_file::TsConfig> for EmitOptions {
|
|
|
|
fn from(config: config_file::TsConfig) -> Self {
|
|
|
|
let options: config_file::EmitConfigOptions =
|
2020-10-19 23:10:42 -04:00
|
|
|
serde_json::from_value(config.0).unwrap();
|
2021-03-07 15:40:11 -05:00
|
|
|
let imports_not_used_as_values =
|
|
|
|
match options.imports_not_used_as_values.as_str() {
|
|
|
|
"preserve" => ImportsNotUsedAsValues::Preserve,
|
|
|
|
"error" => ImportsNotUsedAsValues::Error,
|
|
|
|
_ => ImportsNotUsedAsValues::Remove,
|
|
|
|
};
|
2021-11-08 20:26:39 -05:00
|
|
|
let (transform_jsx, jsx_automatic, jsx_development) =
|
|
|
|
match options.jsx.as_str() {
|
|
|
|
"react" => (true, false, false),
|
|
|
|
"react-jsx" => (true, true, false),
|
|
|
|
"react-jsxdev" => (true, true, true),
|
|
|
|
_ => (false, false, false),
|
|
|
|
};
|
2020-10-19 23:10:42 -04:00
|
|
|
EmitOptions {
|
|
|
|
emit_metadata: options.emit_decorator_metadata,
|
2021-03-07 15:40:11 -05:00
|
|
|
imports_not_used_as_values,
|
2020-10-26 09:03:03 -04:00
|
|
|
inline_source_map: options.inline_source_map,
|
2021-09-08 00:05:34 -04:00
|
|
|
inline_sources: options.inline_sources,
|
2021-05-19 00:18:01 -04:00
|
|
|
source_map: options.source_map,
|
2021-11-08 20:26:39 -05:00
|
|
|
jsx_automatic,
|
|
|
|
jsx_development,
|
2020-10-19 23:10:42 -04:00
|
|
|
jsx_factory: options.jsx_factory,
|
|
|
|
jsx_fragment_factory: options.jsx_fragment_factory,
|
2021-11-08 20:26:39 -05:00
|
|
|
jsx_import_source: options.jsx_import_source,
|
|
|
|
transform_jsx,
|
2021-06-24 09:00:46 -04:00
|
|
|
repl_imports: false,
|
2020-10-19 23:10:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 15:40:11 -05:00
|
|
|
fn strip_config_from_emit_options(
|
|
|
|
options: &EmitOptions,
|
|
|
|
) -> typescript::strip::Config {
|
2021-04-09 17:35:29 -04:00
|
|
|
typescript::strip::Config {
|
2021-10-21 10:18:18 -04:00
|
|
|
pragma: Some(options.jsx_factory.clone()),
|
|
|
|
pragma_frag: Some(options.jsx_fragment_factory.clone()),
|
2021-04-09 17:35:29 -04:00
|
|
|
import_not_used_as_values: match options.imports_not_used_as_values {
|
|
|
|
ImportsNotUsedAsValues::Remove => {
|
|
|
|
typescript::strip::ImportsNotUsedAsValues::Remove
|
|
|
|
}
|
|
|
|
ImportsNotUsedAsValues::Preserve => {
|
|
|
|
typescript::strip::ImportsNotUsedAsValues::Preserve
|
|
|
|
}
|
|
|
|
// `Error` only affects the type-checking stage. Fall back to `Remove` here.
|
|
|
|
ImportsNotUsedAsValues::Error => {
|
|
|
|
typescript::strip::ImportsNotUsedAsValues::Remove
|
|
|
|
}
|
|
|
|
},
|
2021-04-10 17:56:40 -04:00
|
|
|
use_define_for_class_fields: true,
|
2021-06-06 12:42:12 -04:00
|
|
|
// TODO(bartlomieju): this could be changed to `false` to provide `export {}`
|
|
|
|
// in Typescript files without manual changes
|
|
|
|
no_empty_export: true,
|
2021-04-09 17:35:29 -04:00
|
|
|
}
|
2021-03-07 15:40:11 -05:00
|
|
|
}
|
|
|
|
|
2021-10-27 02:18:53 -04:00
|
|
|
/// Implements a configuration trait for source maps that reflects the logic
|
|
|
|
/// to embed sources in the source map or not.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct SourceMapConfig {
|
|
|
|
pub inline_sources: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_ast::swc::common::source_map::SourceMapGenConfig for SourceMapConfig {
|
|
|
|
fn file_name_to_source(&self, f: &FileName) -> String {
|
|
|
|
f.to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inline_sources_content(&self, f: &FileName) -> bool {
|
|
|
|
match f {
|
|
|
|
FileName::Real(..) | FileName::Custom(..) => false,
|
|
|
|
FileName::Url(..) => self.inline_sources,
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
/// Transform a TypeScript file into a JavaScript file, based on the supplied
|
|
|
|
/// options.
|
2020-12-01 15:20:18 -05:00
|
|
|
///
|
2021-09-07 10:39:32 -04:00
|
|
|
/// The result is a tuple of the code and optional source map as strings.
|
|
|
|
pub fn transpile(
|
|
|
|
parsed_source: &ParsedSource,
|
|
|
|
options: &EmitOptions,
|
|
|
|
) -> Result<(String, Option<String>), AnyError> {
|
2021-12-08 19:12:14 -05:00
|
|
|
ensure_no_fatal_diagnostics(parsed_source.diagnostics().iter())?;
|
2021-09-07 10:39:32 -04:00
|
|
|
let program: Program = (*parsed_source.program()).clone();
|
|
|
|
let source_map = Rc::new(SourceMap::default());
|
2021-10-27 02:18:53 -04:00
|
|
|
let source_map_config = SourceMapConfig {
|
|
|
|
inline_sources: options.inline_sources,
|
|
|
|
};
|
2021-09-08 00:05:34 -04:00
|
|
|
let specifier = resolve_url_or_path(parsed_source.specifier())?;
|
|
|
|
let file_name = FileName::Url(specifier);
|
2021-09-07 10:39:32 -04:00
|
|
|
source_map
|
|
|
|
.new_source_file(file_name, parsed_source.source().text().to_string());
|
|
|
|
let comments = parsed_source.comments().as_single_threaded(); // needs to be mutable
|
2021-10-12 09:58:04 -04:00
|
|
|
let globals = Globals::new();
|
|
|
|
deno_ast::swc::common::GLOBALS.set(&globals, || {
|
|
|
|
let top_level_mark = Mark::fresh(Mark::root());
|
2021-10-21 10:18:18 -04:00
|
|
|
let module = fold_program(
|
|
|
|
program,
|
|
|
|
options,
|
|
|
|
source_map.clone(),
|
|
|
|
&comments,
|
|
|
|
top_level_mark,
|
2021-11-15 18:04:34 -05:00
|
|
|
)?;
|
2021-10-12 09:58:04 -04:00
|
|
|
|
|
|
|
let mut src_map_buf = vec![];
|
|
|
|
let mut buf = vec![];
|
|
|
|
{
|
|
|
|
let writer = Box::new(JsWriter::new(
|
|
|
|
source_map.clone(),
|
|
|
|
"\n",
|
|
|
|
&mut buf,
|
|
|
|
Some(&mut src_map_buf),
|
|
|
|
));
|
|
|
|
let config = deno_ast::swc::codegen::Config { minify: false };
|
|
|
|
let mut emitter = deno_ast::swc::codegen::Emitter {
|
|
|
|
cfg: config,
|
|
|
|
comments: Some(&comments),
|
|
|
|
cm: source_map.clone(),
|
|
|
|
wr: writer,
|
|
|
|
};
|
2021-10-21 10:18:18 -04:00
|
|
|
module.emit_with(&mut emitter)?;
|
2021-09-07 10:39:32 -04:00
|
|
|
}
|
2021-10-12 09:58:04 -04:00
|
|
|
let mut src = String::from_utf8(buf)?;
|
|
|
|
let mut map: Option<String> = None;
|
|
|
|
{
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
source_map
|
2021-10-27 02:18:53 -04:00
|
|
|
.build_source_map_with_config(&mut src_map_buf, None, source_map_config)
|
2021-10-12 09:58:04 -04:00
|
|
|
.to_writer(&mut buf)?;
|
|
|
|
|
|
|
|
if options.inline_source_map {
|
|
|
|
src.push_str("//# sourceMappingURL=data:application/json;base64,");
|
|
|
|
let encoded_map = base64::encode(buf);
|
|
|
|
src.push_str(&encoded_map);
|
|
|
|
} else {
|
|
|
|
map = Some(String::from_utf8(buf)?);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok((src, map))
|
|
|
|
})
|
2020-11-27 14:14:54 -05:00
|
|
|
}
|
|
|
|
|
2020-10-19 23:10:42 -04:00
|
|
|
/// A low level function which transpiles a source module into an swc
|
|
|
|
/// SourceFile.
|
|
|
|
pub fn transpile_module(
|
2021-09-08 00:05:34 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2021-08-06 10:36:16 -04:00
|
|
|
source: &str,
|
2021-09-07 10:39:32 -04:00
|
|
|
media_type: MediaType,
|
2021-10-21 10:18:18 -04:00
|
|
|
options: &EmitOptions,
|
2020-10-19 23:10:42 -04:00
|
|
|
cm: Rc<SourceMap>,
|
2021-09-07 10:39:32 -04:00
|
|
|
) -> Result<(Rc<deno_ast::swc::common::SourceFile>, Module), AnyError> {
|
2021-08-16 03:28:29 -04:00
|
|
|
let source = strip_bom(source);
|
2021-12-23 17:38:20 -05:00
|
|
|
let source = if media_type == MediaType::Json {
|
|
|
|
format!(
|
|
|
|
"export default JSON.parse(`{}`);",
|
|
|
|
source.replace("${", "\\${").replace('`', "\\`")
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
source.to_string()
|
|
|
|
};
|
2021-09-08 00:05:34 -04:00
|
|
|
let source_file =
|
2021-12-23 17:38:20 -05:00
|
|
|
cm.new_source_file(FileName::Url(specifier.clone()), source);
|
2021-08-06 10:36:16 -04:00
|
|
|
let input = StringInput::from(&*source_file);
|
2021-09-07 10:39:32 -04:00
|
|
|
let comments = SingleThreadedComments::default();
|
2021-12-23 17:38:20 -05:00
|
|
|
let syntax = if media_type == MediaType::Json {
|
|
|
|
get_syntax(MediaType::JavaScript)
|
|
|
|
} else {
|
|
|
|
get_syntax(media_type)
|
|
|
|
};
|
2021-12-08 19:12:14 -05:00
|
|
|
let lexer = Lexer::new(syntax, deno_ast::ES_VERSION, input, Some(&comments));
|
2021-09-07 10:39:32 -04:00
|
|
|
let mut parser = deno_ast::swc::parser::Parser::new_from(lexer);
|
2021-12-08 19:12:14 -05:00
|
|
|
let module = parser
|
|
|
|
.parse_module()
|
|
|
|
.map_err(|e| swc_err_to_diagnostic(&cm, specifier, e))?;
|
|
|
|
let diagnostics = parser
|
|
|
|
.take_errors()
|
|
|
|
.into_iter()
|
|
|
|
.map(|e| swc_err_to_diagnostic(&cm, specifier, e))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
ensure_no_fatal_diagnostics(diagnostics.iter())?;
|
2020-10-19 23:10:42 -04:00
|
|
|
|
2021-10-21 10:18:18 -04:00
|
|
|
let top_level_mark = Mark::fresh(Mark::root());
|
|
|
|
let program = fold_program(
|
|
|
|
Program::Module(module),
|
|
|
|
options,
|
|
|
|
cm,
|
|
|
|
&comments,
|
|
|
|
top_level_mark,
|
2021-11-15 18:04:34 -05:00
|
|
|
)?;
|
2021-10-21 10:18:18 -04:00
|
|
|
let module = match program {
|
|
|
|
Program::Module(module) => module,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((source_file, module))
|
|
|
|
}
|
|
|
|
|
2021-11-15 18:04:34 -05:00
|
|
|
#[derive(Default, Clone)]
|
|
|
|
struct DiagnosticCollector {
|
|
|
|
diagnostics_cell: Rc<RefCell<Vec<SwcDiagnostic>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DiagnosticCollector {
|
|
|
|
pub fn into_handler(self) -> deno_ast::swc::common::errors::Handler {
|
|
|
|
deno_ast::swc::common::errors::Handler::with_emitter(
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
Box::new(self),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_ast::swc::common::errors::Emitter for DiagnosticCollector {
|
|
|
|
fn emit(
|
|
|
|
&mut self,
|
|
|
|
db: &deno_ast::swc::common::errors::DiagnosticBuilder<'_>,
|
|
|
|
) {
|
|
|
|
use std::ops::Deref;
|
|
|
|
self.diagnostics_cell.borrow_mut().push(db.deref().clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 10:18:18 -04:00
|
|
|
fn fold_program(
|
|
|
|
program: Program,
|
|
|
|
options: &EmitOptions,
|
|
|
|
source_map: Rc<SourceMap>,
|
|
|
|
comments: &SingleThreadedComments,
|
|
|
|
top_level_mark: Mark,
|
2021-11-15 18:04:34 -05:00
|
|
|
) -> Result<Program, AnyError> {
|
2021-12-08 19:12:14 -05:00
|
|
|
let jsx_pass = react::react(
|
|
|
|
source_map.clone(),
|
|
|
|
Some(comments),
|
|
|
|
react::Options {
|
|
|
|
pragma: options.jsx_factory.clone(),
|
|
|
|
pragma_frag: options.jsx_fragment_factory.clone(),
|
|
|
|
// this will use `Object.assign()` instead of the `_extends` helper
|
|
|
|
// when spreading props.
|
|
|
|
use_builtins: true,
|
|
|
|
runtime: if options.jsx_automatic {
|
|
|
|
Some(react::Runtime::Automatic)
|
|
|
|
} else {
|
|
|
|
None
|
2021-10-21 10:18:18 -04:00
|
|
|
},
|
2021-12-08 19:12:14 -05:00
|
|
|
development: options.jsx_development,
|
|
|
|
import_source: options.jsx_import_source.clone().unwrap_or_default(),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
top_level_mark,
|
2021-10-21 10:18:18 -04:00
|
|
|
);
|
|
|
|
let mut passes = chain!(
|
|
|
|
Optional::new(transforms::DownlevelImportsFolder, options.repl_imports),
|
|
|
|
Optional::new(transforms::StripExportsFolder, options.repl_imports),
|
|
|
|
proposals::decorators::decorators(proposals::decorators::Config {
|
|
|
|
legacy: true,
|
|
|
|
emit_metadata: options.emit_metadata
|
|
|
|
}),
|
|
|
|
helpers::inject_helpers(),
|
2021-12-08 19:12:14 -05:00
|
|
|
resolver_with_mark(top_level_mark),
|
2021-10-21 10:18:18 -04:00
|
|
|
Optional::new(
|
2021-12-08 19:12:14 -05:00
|
|
|
typescript::strip::strip_with_config(
|
|
|
|
strip_config_from_emit_options(options),
|
|
|
|
top_level_mark
|
|
|
|
),
|
2021-10-21 10:18:18 -04:00
|
|
|
!options.transform_jsx
|
|
|
|
),
|
|
|
|
Optional::new(
|
|
|
|
typescript::strip::strip_with_jsx(
|
2021-11-15 18:04:34 -05:00
|
|
|
source_map.clone(),
|
2021-10-21 10:18:18 -04:00
|
|
|
strip_config_from_emit_options(options),
|
|
|
|
comments,
|
|
|
|
top_level_mark
|
|
|
|
),
|
|
|
|
options.transform_jsx
|
|
|
|
),
|
|
|
|
Optional::new(jsx_pass, options.transform_jsx),
|
|
|
|
fixer(Some(comments)),
|
|
|
|
hygiene(),
|
|
|
|
);
|
2020-10-19 23:10:42 -04:00
|
|
|
|
2021-11-15 18:04:34 -05:00
|
|
|
let emitter = DiagnosticCollector::default();
|
|
|
|
let diagnostics_cell = emitter.diagnostics_cell.clone();
|
|
|
|
let handler = emitter.into_handler();
|
|
|
|
let result = deno_ast::swc::utils::HANDLER.set(&handler, || {
|
|
|
|
helpers::HELPERS.set(&helpers::Helpers::new(false), || {
|
|
|
|
program.fold_with(&mut passes)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let diagnostics = diagnostics_cell.borrow();
|
2021-12-08 19:12:14 -05:00
|
|
|
ensure_no_fatal_swc_diagnostics(&source_map, diagnostics.iter())?;
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ensure_no_fatal_swc_diagnostics<'a>(
|
|
|
|
source_map: &SourceMap,
|
|
|
|
diagnostics: impl Iterator<Item = &'a SwcDiagnostic>,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let fatal_diagnostics = diagnostics
|
|
|
|
.filter(|d| is_fatal_swc_diagnostic(d))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
if !fatal_diagnostics.is_empty() {
|
2021-11-15 18:04:34 -05:00
|
|
|
Err(anyhow!(
|
|
|
|
"{}",
|
2021-12-08 19:12:14 -05:00
|
|
|
fatal_diagnostics
|
|
|
|
.iter()
|
|
|
|
.map(|d| format_swc_diagnostic(source_map, d))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\n\n")
|
2021-11-15 18:04:34 -05:00
|
|
|
))
|
|
|
|
} else {
|
2021-12-08 19:12:14 -05:00
|
|
|
Ok(())
|
2021-11-15 18:04:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 19:12:14 -05:00
|
|
|
fn is_fatal_swc_diagnostic(diagnostic: &SwcDiagnostic) -> bool {
|
2021-11-15 18:04:34 -05:00
|
|
|
use deno_ast::swc::common::errors::Level;
|
|
|
|
match diagnostic.level {
|
|
|
|
Level::Bug
|
|
|
|
| Level::Cancelled
|
|
|
|
| Level::FailureNote
|
|
|
|
| Level::Fatal
|
|
|
|
| Level::PhaseFatal
|
|
|
|
| Level::Error => true,
|
|
|
|
Level::Help | Level::Note | Level::Warning => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn format_swc_diagnostic(
|
|
|
|
source_map: &SourceMap,
|
|
|
|
diagnostic: &SwcDiagnostic,
|
|
|
|
) -> String {
|
|
|
|
if let Some(span) = &diagnostic.span.primary_span() {
|
|
|
|
let file_name = source_map.span_to_filename(*span);
|
|
|
|
let loc = source_map.lookup_char_pos(span.lo);
|
|
|
|
format!(
|
|
|
|
"{} at {}:{}:{}",
|
|
|
|
diagnostic.message(),
|
|
|
|
file_name.to_string(),
|
|
|
|
loc.line,
|
|
|
|
loc.col_display + 1,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
diagnostic.message()
|
|
|
|
}
|
2020-10-19 23:10:42 -04:00
|
|
|
}
|
|
|
|
|
2021-12-08 19:12:14 -05:00
|
|
|
fn swc_err_to_diagnostic(
|
|
|
|
source_map: &SourceMap,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
err: SwcError,
|
|
|
|
) -> Diagnostic {
|
|
|
|
let location = source_map.lookup_char_pos(err.span().lo);
|
|
|
|
Diagnostic {
|
|
|
|
specifier: specifier.to_string(),
|
|
|
|
span: err.span(),
|
|
|
|
display_position: LineAndColumnDisplay {
|
|
|
|
line_number: location.line,
|
|
|
|
column_number: location.col_display + 1,
|
|
|
|
},
|
|
|
|
kind: err.into_kind(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ensure_no_fatal_diagnostics<'a>(
|
|
|
|
diagnostics: impl Iterator<Item = &'a Diagnostic>,
|
|
|
|
) -> Result<(), Diagnostics> {
|
|
|
|
let fatal_diagnostics = diagnostics
|
|
|
|
.filter(|d| is_fatal_syntax_error(&d.kind))
|
|
|
|
.map(ToOwned::to_owned)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
if !fatal_diagnostics.is_empty() {
|
|
|
|
Err(Diagnostics(fatal_diagnostics))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_fatal_syntax_error(error_kind: &SyntaxError) -> bool {
|
|
|
|
matches!(
|
|
|
|
error_kind,
|
|
|
|
// expected identifier
|
|
|
|
SyntaxError::TS1003 |
|
|
|
|
// expected semi-colon
|
|
|
|
SyntaxError::TS1005 |
|
|
|
|
// expected expression
|
|
|
|
SyntaxError::TS1109 |
|
|
|
|
// unterminated string literal
|
|
|
|
SyntaxError::UnterminatedStrLit
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-14 08:27:44 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::parse_module;
|
|
|
|
use deno_ast::ParseParams;
|
|
|
|
use deno_ast::SourceTextInfo;
|
2020-09-14 08:27:44 -04:00
|
|
|
|
2021-12-08 19:12:14 -05:00
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
|
2020-09-14 08:27:44 -04:00
|
|
|
#[test]
|
|
|
|
fn test_transpile() {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url_or_path("https://deno.land/x/mod.ts")
|
|
|
|
.expect("could not resolve specifier");
|
2020-09-14 08:27:44 -04:00
|
|
|
let source = r#"
|
2021-12-08 19:12:14 -05:00
|
|
|
enum D {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
}
|
2020-09-14 08:27:44 -04:00
|
|
|
|
2021-12-08 19:12:14 -05:00
|
|
|
namespace N {
|
|
|
|
export enum D {
|
|
|
|
A = "value"
|
|
|
|
}
|
|
|
|
export const Value = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class A {
|
|
|
|
private b: string;
|
|
|
|
protected c: number = 1;
|
|
|
|
e: "foo";
|
|
|
|
constructor (public d = D.A) {
|
|
|
|
const e = "foo" as const;
|
|
|
|
this.e = e;
|
|
|
|
console.log(N.Value);
|
|
|
|
}
|
|
|
|
}
|
2020-09-14 08:27:44 -04:00
|
|
|
"#;
|
2021-12-08 19:12:14 -05:00
|
|
|
let module = deno_ast::parse_module(ParseParams {
|
2021-09-07 10:39:32 -04:00
|
|
|
specifier: specifier.as_str().to_string(),
|
|
|
|
source: SourceTextInfo::from_string(source.to_string()),
|
|
|
|
media_type: deno_ast::MediaType::TypeScript,
|
|
|
|
capture_tokens: false,
|
|
|
|
maybe_syntax: None,
|
2021-10-12 09:58:04 -04:00
|
|
|
scope_analysis: false,
|
2021-09-07 10:39:32 -04:00
|
|
|
})
|
2021-12-08 19:12:14 -05:00
|
|
|
.unwrap();
|
2021-09-07 10:39:32 -04:00
|
|
|
let (code, maybe_map) = transpile(&module, &EmitOptions::default())
|
2020-09-14 08:27:44 -04:00
|
|
|
.expect("could not strip types");
|
2021-12-08 19:12:14 -05:00
|
|
|
let expected_text = r#"var D;
|
|
|
|
(function(D) {
|
|
|
|
D[D["A"] = 0] = "A";
|
|
|
|
D[D["B"] = 1] = "B";
|
|
|
|
})(D || (D = {
|
|
|
|
}));
|
|
|
|
var N;
|
|
|
|
(function(N1) {
|
|
|
|
let D;
|
|
|
|
(function(D) {
|
|
|
|
D["A"] = "value";
|
|
|
|
})(D = N1.D || (N1.D = {
|
|
|
|
}));
|
|
|
|
N1.Value = 5;
|
|
|
|
})(N || (N = {
|
|
|
|
}));
|
|
|
|
export class A {
|
|
|
|
d;
|
|
|
|
b;
|
|
|
|
c = 1;
|
|
|
|
e;
|
|
|
|
constructor(d = D.A){
|
|
|
|
this.d = d;
|
|
|
|
const e = "foo";
|
|
|
|
this.e = e;
|
|
|
|
console.log(N.Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
assert_eq!(&code[..expected_text.len()], expected_text);
|
2020-10-07 07:43:44 -04:00
|
|
|
assert!(
|
|
|
|
code.contains("\n//# sourceMappingURL=data:application/json;base64,")
|
|
|
|
);
|
2020-09-14 08:27:44 -04:00
|
|
|
assert!(maybe_map.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transpile_tsx() {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url_or_path("https://deno.land/x/mod.ts")
|
|
|
|
.expect("could not resolve specifier");
|
2020-09-14 08:27:44 -04:00
|
|
|
let source = r#"
|
|
|
|
export class A {
|
|
|
|
render() {
|
|
|
|
return <div><span></span></div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#;
|
2021-09-07 10:39:32 -04:00
|
|
|
let module = parse_module(ParseParams {
|
|
|
|
specifier: specifier.as_str().to_string(),
|
|
|
|
source: SourceTextInfo::from_string(source.to_string()),
|
|
|
|
media_type: deno_ast::MediaType::Tsx,
|
|
|
|
capture_tokens: false,
|
|
|
|
maybe_syntax: None,
|
2021-10-12 09:58:04 -04:00
|
|
|
scope_analysis: true, // ensure scope analysis doesn't conflict with a second resolver pass
|
2021-09-07 10:39:32 -04:00
|
|
|
})
|
|
|
|
.expect("could not parse module");
|
|
|
|
let (code, _) = transpile(&module, &EmitOptions::default())
|
2020-09-14 08:27:44 -04:00
|
|
|
.expect("could not strip types");
|
2020-10-07 07:43:44 -04:00
|
|
|
assert!(code.contains("React.createElement(\"div\", null"));
|
2020-09-14 08:27:44 -04:00
|
|
|
}
|
|
|
|
|
2021-10-21 10:18:18 -04:00
|
|
|
#[test]
|
|
|
|
fn test_transpile_jsx_pragma() {
|
|
|
|
let specifier = resolve_url_or_path("https://deno.land/x/mod.ts")
|
|
|
|
.expect("could not resolve specifier");
|
|
|
|
let source = r#"
|
|
|
|
/** @jsx h */
|
|
|
|
/** @jsxFrag Fragment */
|
|
|
|
import { h, Fragment } from "https://deno.land/x/mod.ts";
|
|
|
|
|
|
|
|
function App() {
|
|
|
|
return (
|
|
|
|
<div><></></div>
|
|
|
|
);
|
|
|
|
}"#;
|
|
|
|
let module = parse_module(ParseParams {
|
|
|
|
specifier: specifier.as_str().to_string(),
|
|
|
|
source: SourceTextInfo::from_string(source.to_string()),
|
|
|
|
media_type: deno_ast::MediaType::Jsx,
|
|
|
|
capture_tokens: false,
|
|
|
|
maybe_syntax: None,
|
|
|
|
scope_analysis: true,
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
let (code, _) = transpile(&module, &EmitOptions::default()).unwrap();
|
|
|
|
let expected = r#"/** @jsx h */ /** @jsxFrag Fragment */ import { h, Fragment } from "https://deno.land/x/mod.ts";
|
|
|
|
function App() {
|
|
|
|
return(/*#__PURE__*/ h("div", null, /*#__PURE__*/ h(Fragment, null)));
|
|
|
|
}"#;
|
|
|
|
assert_eq!(&code[..expected.len()], expected);
|
|
|
|
}
|
|
|
|
|
2021-11-08 20:26:39 -05:00
|
|
|
#[test]
|
|
|
|
fn test_transpile_jsx_import_source_pragma() {
|
|
|
|
let specifier = resolve_url_or_path("https://deno.land/x/mod.tsx")
|
|
|
|
.expect("could not resolve specifier");
|
|
|
|
let source = r#"
|
|
|
|
/** @jsxImportSource jsx_lib */
|
|
|
|
|
|
|
|
function App() {
|
|
|
|
return (
|
|
|
|
<div><></></div>
|
|
|
|
);
|
|
|
|
}"#;
|
|
|
|
let module = parse_module(ParseParams {
|
|
|
|
specifier: specifier.as_str().to_string(),
|
|
|
|
source: SourceTextInfo::from_string(source.to_string()),
|
|
|
|
media_type: deno_ast::MediaType::Jsx,
|
|
|
|
capture_tokens: false,
|
|
|
|
maybe_syntax: None,
|
|
|
|
scope_analysis: true,
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
let (code, _) = transpile(&module, &EmitOptions::default()).unwrap();
|
|
|
|
let expected = r#"import { jsx as _jsx, Fragment as _Fragment } from "jsx_lib/jsx-runtime";
|
|
|
|
/** @jsxImportSource jsx_lib */ function App() {
|
|
|
|
return(/*#__PURE__*/ _jsx("div", {
|
|
|
|
children: /*#__PURE__*/ _jsx(_Fragment, {
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
"#;
|
|
|
|
assert_eq!(&code[..expected.len()], expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transpile_jsx_import_source_no_pragma() {
|
|
|
|
let specifier = resolve_url_or_path("https://deno.land/x/mod.tsx")
|
|
|
|
.expect("could not resolve specifier");
|
|
|
|
let source = r#"
|
|
|
|
function App() {
|
|
|
|
return (
|
|
|
|
<div><></></div>
|
|
|
|
);
|
|
|
|
}"#;
|
|
|
|
let module = parse_module(ParseParams {
|
|
|
|
specifier: specifier.as_str().to_string(),
|
|
|
|
source: SourceTextInfo::from_string(source.to_string()),
|
|
|
|
media_type: deno_ast::MediaType::Jsx,
|
|
|
|
capture_tokens: false,
|
|
|
|
maybe_syntax: None,
|
|
|
|
scope_analysis: true,
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
let emit_options = EmitOptions {
|
|
|
|
jsx_automatic: true,
|
|
|
|
jsx_import_source: Some("jsx_lib".to_string()),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let (code, _) = transpile(&module, &emit_options).unwrap();
|
|
|
|
let expected = r#"import { jsx as _jsx, Fragment as _Fragment } from "jsx_lib/jsx-runtime";
|
|
|
|
function App() {
|
|
|
|
return(/*#__PURE__*/ _jsx("div", {
|
|
|
|
children: /*#__PURE__*/ _jsx(_Fragment, {
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
assert_eq!(&code[..expected.len()], expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@kitsonk) https://github.com/swc-project/swc/issues/2656
|
|
|
|
// #[test]
|
|
|
|
// fn test_transpile_jsx_import_source_no_pragma_dev() {
|
|
|
|
// let specifier = resolve_url_or_path("https://deno.land/x/mod.tsx")
|
|
|
|
// .expect("could not resolve specifier");
|
|
|
|
// let source = r#"
|
|
|
|
// function App() {
|
|
|
|
// return (
|
|
|
|
// <div><></></div>
|
|
|
|
// );
|
|
|
|
// }"#;
|
|
|
|
// let module = parse_module(ParseParams {
|
|
|
|
// specifier: specifier.as_str().to_string(),
|
|
|
|
// source: SourceTextInfo::from_string(source.to_string()),
|
|
|
|
// media_type: deno_ast::MediaType::Jsx,
|
|
|
|
// capture_tokens: false,
|
|
|
|
// maybe_syntax: None,
|
|
|
|
// scope_analysis: true,
|
|
|
|
// })
|
|
|
|
// .unwrap();
|
|
|
|
// let emit_options = EmitOptions {
|
|
|
|
// jsx_automatic: true,
|
|
|
|
// jsx_import_source: Some("jsx_lib".to_string()),
|
|
|
|
// jsx_development: true,
|
|
|
|
// ..Default::default()
|
|
|
|
// };
|
|
|
|
// let (code, _) = transpile(&module, &emit_options).unwrap();
|
|
|
|
// let expected = r#"import { jsx as _jsx, Fragment as _Fragment } from "jsx_lib/jsx-dev-runtime";
|
|
|
|
// function App() {
|
|
|
|
// return(/*#__PURE__*/ _jsx("div", {
|
|
|
|
// children: /*#__PURE__*/ _jsx(_Fragment, {
|
|
|
|
// })
|
|
|
|
// }));
|
|
|
|
// }
|
|
|
|
// "#;
|
|
|
|
// assert_eq!(&code[..expected.len()], expected);
|
|
|
|
// }
|
|
|
|
|
2020-09-14 08:27:44 -04:00
|
|
|
#[test]
|
|
|
|
fn test_transpile_decorators() {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url_or_path("https://deno.land/x/mod.ts")
|
|
|
|
.expect("could not resolve specifier");
|
2020-09-14 08:27:44 -04:00
|
|
|
let source = r#"
|
|
|
|
function enumerable(value: boolean) {
|
|
|
|
return function (
|
|
|
|
_target: any,
|
|
|
|
_propertyKey: string,
|
|
|
|
descriptor: PropertyDescriptor,
|
|
|
|
) {
|
|
|
|
descriptor.enumerable = value;
|
|
|
|
};
|
|
|
|
}
|
2021-02-17 13:47:18 -05:00
|
|
|
|
2020-09-14 08:27:44 -04:00
|
|
|
export class A {
|
|
|
|
@enumerable(false)
|
|
|
|
a() {
|
|
|
|
Test.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#;
|
2021-09-07 10:39:32 -04:00
|
|
|
let module = parse_module(ParseParams {
|
|
|
|
specifier: specifier.as_str().to_string(),
|
|
|
|
source: SourceTextInfo::from_string(source.to_string()),
|
|
|
|
media_type: deno_ast::MediaType::TypeScript,
|
|
|
|
capture_tokens: false,
|
|
|
|
maybe_syntax: None,
|
2021-10-12 09:58:04 -04:00
|
|
|
scope_analysis: false,
|
2021-09-07 10:39:32 -04:00
|
|
|
})
|
|
|
|
.expect("could not parse module");
|
|
|
|
let (code, _) = transpile(&module, &EmitOptions::default())
|
2020-09-14 08:27:44 -04:00
|
|
|
.expect("could not strip types");
|
2020-10-07 07:43:44 -04:00
|
|
|
assert!(code.contains("_applyDecoratedDescriptor("));
|
2020-09-14 08:27:44 -04:00
|
|
|
}
|
2021-10-12 21:27:22 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn transpile_handle_code_nested_in_ts_nodes_with_jsx_pass() {
|
|
|
|
// from issue 12409
|
|
|
|
let specifier = resolve_url_or_path("https://deno.land/x/mod.ts").unwrap();
|
|
|
|
let source = r#"
|
|
|
|
export function g() {
|
|
|
|
let algorithm: any
|
|
|
|
algorithm = {}
|
|
|
|
|
|
|
|
return <Promise>(
|
|
|
|
test(algorithm, false, keyUsages)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
let module = parse_module(ParseParams {
|
|
|
|
specifier: specifier.as_str().to_string(),
|
|
|
|
source: SourceTextInfo::from_string(source.to_string()),
|
|
|
|
media_type: deno_ast::MediaType::TypeScript,
|
|
|
|
capture_tokens: false,
|
|
|
|
maybe_syntax: None,
|
|
|
|
scope_analysis: false,
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
let emit_options = EmitOptions {
|
|
|
|
transform_jsx: true,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let (code, _) = transpile(&module, &emit_options).unwrap();
|
|
|
|
let expected = r#"export function g() {
|
|
|
|
let algorithm;
|
|
|
|
algorithm = {
|
|
|
|
};
|
|
|
|
return test(algorithm, false, keyUsages);
|
|
|
|
}"#;
|
|
|
|
assert_eq!(&code[..expected.len()], expected);
|
|
|
|
}
|
2021-11-15 18:04:34 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn diagnostic_jsx_spread_instead_of_panic() {
|
|
|
|
let specifier = resolve_url_or_path("https://deno.land/x/mod.ts").unwrap();
|
|
|
|
let source = r#"const A = () => {
|
|
|
|
return <div>{...[]}</div>;
|
|
|
|
};"#;
|
|
|
|
let parsed_source = parse_module(ParseParams {
|
|
|
|
specifier: specifier.as_str().to_string(),
|
|
|
|
source: SourceTextInfo::from_string(source.to_string()),
|
|
|
|
media_type: deno_ast::MediaType::Tsx,
|
|
|
|
capture_tokens: false,
|
|
|
|
maybe_syntax: None,
|
|
|
|
scope_analysis: false,
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
let err = transpile(&parsed_source, &Default::default())
|
|
|
|
.err()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(err.to_string(), "Spread children are not supported in React. at https://deno.land/x/mod.ts:2:15");
|
|
|
|
}
|
2020-09-14 08:27:44 -04:00
|
|
|
}
|