1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

fix(lsp): formatting should error on certain additional swc diagnostics (#12491)

This commit is contained in:
David Sherret 2021-10-21 10:18:18 -04:00 committed by GitHub
parent 2997021615
commit 43cd0459b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 269 additions and 231 deletions

View file

@ -35,7 +35,7 @@
"tools/wpt/manifest.json"
],
"plugins": [
"https://plugins.dprint.dev/typescript-0.57.4.wasm",
"https://plugins.dprint.dev/typescript-0.58.1.wasm",
"https://plugins.dprint.dev/json-0.13.0.wasm",
"https://plugins.dprint.dev/markdown-0.10.0.wasm",
"https://plugins.dprint.dev/toml-0.5.2.wasm"

121
Cargo.lock generated
View file

@ -716,9 +716,9 @@ dependencies = [
[[package]]
name = "deno_ast"
version = "0.3.0"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d177a5fbe88bec8d8b86882c77b7ba39ba03be99c5d80d3c9fc529191f3c0933"
checksum = "e23902664e99dc1e0f7186ddd26dbf31fc2ab934ef86593700d43052f5ad43bf"
dependencies = [
"data-url",
"dprint-swc-ecma-ast-view",
@ -802,9 +802,9 @@ dependencies = [
[[package]]
name = "deno_doc"
version = "0.16.0"
version = "0.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73fa23e7482a6bce5a6dfbcca145e247aa282ef002db0f9f060d0a812ac18ff3"
checksum = "96b60f0447a17ed4fc413bd7abad29e7d31e76f13422813f2e7978b0c4bdca7a"
dependencies = [
"cfg-if 1.0.0",
"deno_ast",
@ -847,9 +847,9 @@ dependencies = [
[[package]]
name = "deno_graph"
version = "0.7.0"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9adc8acfce4c311ebb4cd08a70d3149cf2cd157d468bd46bb5c556db0754eec"
checksum = "d3d7197f490abd2a1e7d1edc3a1c4db1f4e294d7067dbbf65c4714932c7c3c70"
dependencies = [
"anyhow",
"cfg-if 1.0.0",
@ -857,6 +857,7 @@ dependencies = [
"deno_ast",
"futures",
"lazy_static",
"parking_lot_core",
"regex",
"ring",
"serde",
@ -882,9 +883,9 @@ dependencies = [
[[package]]
name = "deno_lint"
version = "0.17.0"
version = "0.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "286985c2af9d4fcf162e2d5e08b196f68ab2f093ee6eaa28618842c47126c11d"
checksum = "dce18a8ca950791a5aa75f6dbd64d3a49a9cb7339877c557ca2df7eb9dba0c5e"
dependencies = [
"anyhow",
"deno_ast",
@ -1151,24 +1152,22 @@ dependencies = [
[[package]]
name = "dprint-plugin-typescript"
version = "0.57.4"
version = "0.58.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2482bd23ef65c93a4f3ad0f5413d2f3ac6f357caded8bb562c37008f0a69fb5d"
checksum = "a5749ee5f8d448912fdab331a1535ac29364fc0f94e5c540532a6fa60178ba06"
dependencies = [
"deno_ast",
"dprint-core",
"dprint-swc-ecma-ast-view",
"parking_lot_core",
"rustc-hash",
"serde",
"swc_common",
"swc_ecmascript",
]
[[package]]
name = "dprint-swc-ecma-ast-view"
version = "0.39.0"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3530a71ad541e1182d19a1fa4da83c8d134e38f6fffd4cd1035440444dd08d15"
checksum = "bac2108f47408837b37dfa645cca2b6c03a8f6c993bc7cd3d17506c2b4dd3326"
dependencies = [
"bumpalo",
"num-bigint",
@ -3496,12 +3495,13 @@ checksum = "d44a3643b4ff9caf57abcee9c2c621d6c03d9135e0d8b589bd9afb5992cb176a"
[[package]]
name = "string_cache"
version = "0.8.1"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ddb1139b5353f96e429e1a5e19fbaf663bddedaa06d1dbd49f82e352601209a"
checksum = "923f0f39b6267d37d23ce71ae7235602134b250ace715dd2c90421998ddac0c6"
dependencies = [
"lazy_static",
"new_debug_unreachable",
"parking_lot",
"phf_shared",
"precomputed-hash",
"serde",
@ -3552,9 +3552,9 @@ checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601"
[[package]]
name = "swc_atoms"
version = "0.2.7"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "837a3ef86c2817228e733b6f173c821fd76f9eb21a0bc9001a826be48b00b4e7"
checksum = "9f5229fe227ff0060e13baa386d6e368797700eab909523f730008d191ee53ae"
dependencies = [
"string_cache",
"string_cache_codegen",
@ -3562,9 +3562,9 @@ dependencies = [
[[package]]
name = "swc_bundler"
version = "0.68.1"
version = "0.75.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e39f8571be48d4397f9186d449ca9a435da3314dfe8db428f2485b8489ccbb53"
checksum = "a15b9dfe3beb671044c8237ee921a007255ad6a92c12c119cf29fb7bb7c807ad"
dependencies = [
"ahash 0.7.4",
"anyhow",
@ -3592,9 +3592,9 @@ dependencies = [
[[package]]
name = "swc_common"
version = "0.13.4"
version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "259212df9a949aa6ec440b64244475bcda531609a85489a22eb27908dd8d1b0d"
checksum = "de8be830f71f62908dae13fd9db66522e77dbf9188bd07d0b86d15f48557b219"
dependencies = [
"ahash 0.7.4",
"ast_node",
@ -3618,9 +3618,9 @@ dependencies = [
[[package]]
name = "swc_ecma_ast"
version = "0.54.3"
version = "0.55.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eae24a6603262822de50069ff72394b0d208267e5bd5298678ba6a79f670c40f"
checksum = "e40d99e5376086f6a057202b3889f276c3f5cbcafeead8f536ed088ad0bf36b3"
dependencies = [
"is-macro",
"num-bigint",
@ -3632,9 +3632,9 @@ dependencies = [
[[package]]
name = "swc_ecma_codegen"
version = "0.74.4"
version = "0.77.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2f84ab8cdd0395372a9b94d2938ee787d18e4d6805b5e11d26756c8410ef78d"
checksum = "9679c138f4cfe98c86e0947bdc089c4402b372db064f6aca2636a86c93898052"
dependencies = [
"bitflags",
"memchr",
@ -3646,6 +3646,7 @@ dependencies = [
"swc_ecma_ast",
"swc_ecma_codegen_macros",
"swc_ecma_parser",
"tracing",
]
[[package]]
@ -3663,9 +3664,9 @@ dependencies = [
[[package]]
name = "swc_ecma_dep_graph"
version = "0.42.0"
version = "0.44.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e27fde9ec4d2b24663836ac0bff26909db0505abad3b873d30808f7659d977a"
checksum = "235e8d4a5f0136b50a0339e015f77a42a4bbae66c3a82ae399491d4d5caed3d2"
dependencies = [
"swc_atoms",
"swc_common",
@ -3675,9 +3676,9 @@ dependencies = [
[[package]]
name = "swc_ecma_loader"
version = "0.21.0"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a1c79e93e0182da35ab5be1fcb6e8060b5af41bf4e9c1760cad3520dbc287ac"
checksum = "1a9507f40d574997316948f94423c3c93dcb03bf593bd0a5197b51c34ed09558"
dependencies = [
"ahash 0.7.4",
"anyhow",
@ -3690,9 +3691,9 @@ dependencies = [
[[package]]
name = "swc_ecma_parser"
version = "0.73.11"
version = "0.75.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8d7c0925ea8ad9ca8554ed668ab8cf98e057064a79a819893f71a008cd65e36"
checksum = "dbf91ac2559e8cb4d5e66ca5b7b71f0bcf9a16289cc0eb31211314bffb1c4f4b"
dependencies = [
"either",
"enum_kind",
@ -3705,14 +3706,15 @@ dependencies = [
"swc_ecma_ast",
"swc_ecma_visit",
"tracing",
"typed-arena",
"unicode-xid 0.2.2",
]
[[package]]
name = "swc_ecma_transforms"
version = "0.81.0"
version = "0.88.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9170807286d86c713320a5122c299a5cd62c81ac62e572e2259aa1a93d80c171"
checksum = "c38ddf75f012a84fe05ccdbceaf3a57c8657a989ad376ad5a5fd0ec7cf197cf9"
dependencies = [
"swc_atoms",
"swc_common",
@ -3729,9 +3731,9 @@ dependencies = [
[[package]]
name = "swc_ecma_transforms_base"
version = "0.37.1"
version = "0.40.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5ab2108361a8bb8664951ae0f533fab36c609aa849143b3358e29ed33060bbe"
checksum = "d1ce842ee29a2e96647950dba48dddd757ad7e5b392b1902191a16c3e8be22ae"
dependencies = [
"once_cell",
"phf",
@ -3747,9 +3749,9 @@ dependencies = [
[[package]]
name = "swc_ecma_transforms_classes"
version = "0.23.0"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cafd0ad50aedf503cd640c0a3b5a9e9c0a8a279c1772c523f78ee707f30d3423"
checksum = "86440b9078c3496db893afb298d20a59baf2fc46caa3298d16fdf3c88f27a250"
dependencies = [
"swc_atoms",
"swc_common",
@ -3761,9 +3763,9 @@ dependencies = [
[[package]]
name = "swc_ecma_transforms_macros"
version = "0.2.1"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7680ada61fa22c2164c3f32864efba31566710b503c30631ccc3b6f0fa800bc"
checksum = "063ad8426598df1aad8cdb9e9994a54cecb07fe902190c467bf195f5f553ed8d"
dependencies = [
"pmutil",
"proc-macro2 1.0.29",
@ -3774,9 +3776,9 @@ dependencies = [
[[package]]
name = "swc_ecma_transforms_optimization"
version = "0.51.0"
version = "0.58.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc478afa98cd8272d75a994cddd598f903cc8c9d0fce6555516dcad0ed2a0c61"
checksum = "f8774d32f481b47dec0b0e30765a71d02a1c63919b4ca52f925afbf0dd5b81e6"
dependencies = [
"ahash 0.7.4",
"dashmap",
@ -3789,6 +3791,7 @@ dependencies = [
"swc_ecma_ast",
"swc_ecma_parser",
"swc_ecma_transforms_base",
"swc_ecma_transforms_macros",
"swc_ecma_utils",
"swc_ecma_visit",
"tracing",
@ -3796,9 +3799,9 @@ dependencies = [
[[package]]
name = "swc_ecma_transforms_proposal"
version = "0.46.0"
version = "0.51.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb257ed97c73338e9776a0c1404bc55570d14e69d0b57f1030105630dd36a5a"
checksum = "d4f7cfc191cb5b199bf9aedafd583785ea21662e312ad49d6b5a73dc3c4efe6d"
dependencies = [
"either",
"serde",
@ -3816,9 +3819,9 @@ dependencies = [
[[package]]
name = "swc_ecma_transforms_react"
version = "0.48.1"
version = "0.53.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c30996f62170b3cad11ac85dc058c59655d66a6de134d3182e99324c903da58"
checksum = "0c7a21856bade56164a0da969aacd3ec90c27bed56e82480c92721fca18d1fe8"
dependencies = [
"ahash 0.7.4",
"base64 0.13.0",
@ -3834,15 +3837,16 @@ dependencies = [
"swc_ecma_ast",
"swc_ecma_parser",
"swc_ecma_transforms_base",
"swc_ecma_transforms_macros",
"swc_ecma_utils",
"swc_ecma_visit",
]
[[package]]
name = "swc_ecma_transforms_typescript"
version = "0.48.0"
version = "0.54.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b8ba9d4bc21426d3b21e92c6007932f421dd4698d4e301352ff7e969c72295"
checksum = "bfbca21d37a9ec2e5de9f92d6dd5ff2d749d741bc0fac832d38ccbcf4bde4f28"
dependencies = [
"serde",
"swc_atoms",
@ -3850,15 +3854,16 @@ dependencies = [
"swc_ecma_ast",
"swc_ecma_parser",
"swc_ecma_transforms_base",
"swc_ecma_transforms_react",
"swc_ecma_utils",
"swc_ecma_visit",
]
[[package]]
name = "swc_ecma_utils"
version = "0.47.1"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3e427f476cd95fa625678d2df2c8a56ad94ce4182034f9489f9249f3f1c7c9f"
checksum = "fc932d46dabd2250f4bb690cf5eb14a672c6c054caee1a1a9ff3ecf77b472606"
dependencies = [
"once_cell",
"scoped-tls",
@ -3871,9 +3876,9 @@ dependencies = [
[[package]]
name = "swc_ecma_visit"
version = "0.40.0"
version = "0.41.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c24a7600061813d7df3248d93ff27cacc1a81f9eeec47701866f3adc9ae2930"
checksum = "c242ca4236cf826f9d575f27235a049e7e5629b66f130fdc1f333fa23e6a2ff4"
dependencies = [
"num-bigint",
"swc_atoms",
@ -3884,9 +3889,9 @@ dependencies = [
[[package]]
name = "swc_ecmascript"
version = "0.73.0"
version = "0.80.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8147c047015f4289b3202a5362a6ed8c384a585eee05c98f040299a0b303f22e"
checksum = "cc83776796ba1c4602e268ff0a71a325fbaf8b65d312b8fe975ee94865300501"
dependencies = [
"swc_ecma_ast",
"swc_ecma_codegen",
@ -4372,6 +4377,12 @@ dependencies = [
"unchecked-index",
]
[[package]]
name = "typed-arena"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0685c84d5d54d1c26f7d3eb96cd41550adb97baed141a761cf335d3d33bcd0ae"
[[package]]
name = "typenum"
version = "1.14.0"

View file

@ -39,11 +39,11 @@ winapi = "0.3.9"
winres = "0.1.11"
[dependencies]
deno_ast = { version = "0.3.0", features = ["bundler", "codegen", "dep_graph", "module_specifier", "proposal", "react", "sourcemap", "transforms", "typescript", "view", "visit"] }
deno_ast = { version = "0.4.1", features = ["bundler", "codegen", "dep_graph", "module_specifier", "proposal", "react", "sourcemap", "transforms", "typescript", "view", "visit"] }
deno_core = { version = "0.104.0", path = "../core" }
deno_doc = "0.16.0"
deno_graph = "0.7.0"
deno_lint = { version = "0.17.0", features = ["docs"] }
deno_doc = "0.17.1"
deno_graph = "0.8.2"
deno_lint = { version = "0.18.1", features = ["docs"] }
deno_runtime = { version = "0.30.0", path = "../runtime" }
deno_tls = { version = "0.9.0", path = "../ext/tls" }
@ -54,7 +54,7 @@ data-url = "0.1.0"
dissimilar = "1.0.2"
dprint-plugin-json = "0.13.0"
dprint-plugin-markdown = "0.10.0"
dprint-plugin-typescript = "0.57.4"
dprint-plugin-typescript = "0.58.1"
encoding_rs = "0.8.28"
env_logger = "0.8.4"
fancy-regex = "0.7.1"

View file

@ -24,7 +24,7 @@ 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;
use deno_ast::swc::transforms::resolver::ts_resolver;
use deno_ast::swc::transforms::resolver_with_mark;
use deno_ast::swc::transforms::typescript;
use deno_ast::swc::visit::FoldWith;
use deno_ast::Diagnostic;
@ -182,6 +182,8 @@ fn strip_config_from_emit_options(
options: &EmitOptions,
) -> typescript::strip::Config {
typescript::strip::Config {
pragma: Some(options.jsx_factory.clone()),
pragma_frag: Some(options.jsx_fragment_factory.clone()),
import_not_used_as_values: match options.imports_not_used_as_values {
ImportsNotUsedAsValues::Remove => {
typescript::strip::ImportsNotUsedAsValues::Remove
@ -219,41 +221,13 @@ pub fn transpile(
let globals = Globals::new();
deno_ast::swc::common::GLOBALS.set(&globals, || {
let top_level_mark = Mark::fresh(Mark::root());
let jsx_pass = chain!(
ts_resolver(top_level_mark),
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,
..Default::default()
},
top_level_mark,
),
let module = fold_program(
program,
options,
source_map.clone(),
&comments,
top_level_mark,
);
let mut passes = chain!(
Optional::new(jsx_pass, options.transform_jsx),
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(),
typescript::strip::strip_with_config(strip_config_from_emit_options(
options
)),
fixer(Some(&comments)),
hygiene(),
);
let program = helpers::HELPERS.set(&helpers::Helpers::new(false), || {
program.fold_with(&mut passes)
});
let mut src_map_buf = vec![];
let mut buf = vec![];
@ -271,7 +245,7 @@ pub fn transpile(
cm: source_map.clone(),
wr: writer,
};
program.emit_with(&mut emitter)?;
module.emit_with(&mut emitter)?;
}
let mut src = String::from_utf8(buf)?;
let mut map: Option<String> = None;
@ -299,8 +273,7 @@ pub fn transpile_module(
specifier: &ModuleSpecifier,
source: &str,
media_type: MediaType,
emit_options: &EmitOptions,
globals: &Globals,
options: &EmitOptions,
cm: Rc<SourceMap>,
) -> Result<(Rc<deno_ast::swc::common::SourceFile>, Module), AnyError> {
let source = strip_bom(source);
@ -314,52 +287,85 @@ pub fn transpile_module(
let module = parser.parse_module().map_err(|err| {
let location = cm.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,
},
specifier: specifier.to_string(),
message: err.into_kind().msg().to_string(),
kind: err.into_kind(),
}
})?;
deno_ast::swc::common::GLOBALS.set(globals, || {
let top_level_mark = Mark::fresh(Mark::root());
let jsx_pass = chain!(
ts_resolver(top_level_mark),
react::react(
cm,
Some(&comments),
react::Options {
pragma: emit_options.jsx_factory.clone(),
pragma_frag: emit_options.jsx_fragment_factory.clone(),
// this will use `Object.assign()` instead of the `_extends` helper
// when spreading props.
use_builtins: true,
..Default::default()
},
top_level_mark,
),
);
let mut passes = chain!(
Optional::new(jsx_pass, emit_options.transform_jsx),
proposals::decorators::decorators(proposals::decorators::Config {
legacy: true,
emit_metadata: emit_options.emit_metadata
}),
helpers::inject_helpers(),
let top_level_mark = Mark::fresh(Mark::root());
let program = fold_program(
Program::Module(module),
options,
cm,
&comments,
top_level_mark,
);
let module = match program {
Program::Module(module) => module,
_ => unreachable!(),
};
Ok((source_file, module))
}
fn fold_program(
program: Program,
options: &EmitOptions,
source_map: Rc<SourceMap>,
comments: &SingleThreadedComments,
top_level_mark: Mark,
) -> Program {
let jsx_pass = chain!(
resolver_with_mark(top_level_mark),
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,
..Default::default()
},
top_level_mark,
),
);
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(),
Optional::new(
typescript::strip::strip_with_config(strip_config_from_emit_options(
emit_options
options
)),
fixer(Some(&comments)),
hygiene(),
);
!options.transform_jsx
),
Optional::new(
typescript::strip::strip_with_jsx(
source_map,
strip_config_from_emit_options(options),
comments,
top_level_mark
),
options.transform_jsx
),
Optional::new(jsx_pass, options.transform_jsx),
fixer(Some(comments)),
hygiene(),
);
let module = helpers::HELPERS.set(&helpers::Helpers::new(false), || {
module.fold_with(&mut passes)
});
Ok((source_file, module))
helpers::HELPERS.set(&helpers::Helpers::new(false), || {
program.fold_with(&mut passes)
})
}
@ -434,6 +440,37 @@ mod tests {
assert!(code.contains("React.createElement(\"div\", null"));
}
#[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);
}
#[test]
fn test_transpile_decorators() {
let specifier = resolve_url_or_path("https://deno.land/x/mod.ts")

View file

@ -439,7 +439,6 @@ pub(crate) struct BundleOptions {
struct BundleLoader<'a> {
cm: Rc<swc::common::SourceMap>,
emit_options: &'a ast::EmitOptions,
globals: &'a deno_ast::swc::common::Globals,
graph: &'a ModuleGraph,
}
@ -456,7 +455,6 @@ impl swc::bundler::Load for BundleLoader<'_> {
&m.source,
m.media_type,
self.emit_options,
self.globals,
self.cm.clone(),
)?;
Ok(swc::bundler::ModuleData {
@ -518,81 +516,82 @@ pub(crate) fn bundle(
graph: &ModuleGraph,
options: BundleOptions,
) -> Result<(String, Option<String>), AnyError> {
let emit_options: ast::EmitOptions = options.ts_config.into();
let cm = Rc::new(swc::common::SourceMap::new(
swc::common::FilePathMapping::empty(),
));
let globals = swc::common::Globals::new();
let loader = BundleLoader {
graph,
emit_options: &emit_options,
globals: &globals,
cm: cm.clone(),
};
let resolver = BundleResolver(graph);
let config = swc::bundler::Config {
module: options.bundle_type.into(),
..Default::default()
};
// This hook will rewrite the `import.meta` when bundling to give a consistent
// behavior between bundled and unbundled code.
let hook = Box::new(ast::BundleHook);
let bundler = swc::bundler::Bundler::new(
&globals,
cm.clone(),
loader,
resolver,
config,
hook,
);
let mut entries = HashMap::new();
entries.insert(
"bundle".to_string(),
swc::common::FileName::Url(graph.roots[0].clone()),
);
let output = bundler
.bundle(entries)
.context("Unable to output during bundling.")?;
let mut buf = Vec::new();
let mut srcmap = Vec::new();
{
let cfg = swc::codegen::Config { minify: false };
let wr = Box::new(swc::codegen::text_writer::JsWriter::new(
cm.clone(),
"\n",
&mut buf,
Some(&mut srcmap),
));
let mut emitter = swc::codegen::Emitter {
cfg,
cm: cm.clone(),
comments: None,
wr,
};
emitter
.emit_module(&output[0].module)
.context("Unable to emit during bundling.")?;
}
let mut code =
String::from_utf8(buf).context("Emitted code is an invalid string.")?;
let mut maybe_map: Option<String> = None;
{
let mut buf = Vec::new();
cm.build_source_map_from(&mut srcmap, None)
.to_writer(&mut buf)?;
if emit_options.inline_source_map {
let encoded_map = format!(
"//# sourceMappingURL=data:application/json;base64,{}\n",
base64::encode(buf)
);
code.push_str(&encoded_map);
} else if emit_options.source_map {
maybe_map = Some(String::from_utf8(buf)?);
}
}
deno_ast::swc::common::GLOBALS.set(&globals, || {
let emit_options: ast::EmitOptions = options.ts_config.into();
Ok((code, maybe_map))
let cm = Rc::new(swc::common::SourceMap::new(
swc::common::FilePathMapping::empty(),
));
let loader = BundleLoader {
graph,
emit_options: &emit_options,
cm: cm.clone(),
};
let resolver = BundleResolver(graph);
let config = swc::bundler::Config {
module: options.bundle_type.into(),
..Default::default()
};
// This hook will rewrite the `import.meta` when bundling to give a consistent
// behavior between bundled and unbundled code.
let hook = Box::new(ast::BundleHook);
let mut bundler = swc::bundler::Bundler::new(
&globals,
cm.clone(),
loader,
resolver,
config,
hook,
);
let mut entries = HashMap::new();
entries.insert(
"bundle".to_string(),
swc::common::FileName::Url(graph.roots[0].clone()),
);
let output = bundler
.bundle(entries)
.context("Unable to output during bundling.")?;
let mut buf = Vec::new();
let mut srcmap = Vec::new();
{
let cfg = swc::codegen::Config { minify: false };
let wr = Box::new(swc::codegen::text_writer::JsWriter::new(
cm.clone(),
"\n",
&mut buf,
Some(&mut srcmap),
));
let mut emitter = swc::codegen::Emitter {
cfg,
cm: cm.clone(),
comments: None,
wr,
};
emitter
.emit_module(&output[0].module)
.context("Unable to emit during bundling.")?;
}
let mut code =
String::from_utf8(buf).context("Emitted code is an invalid string.")?;
let mut maybe_map: Option<String> = None;
{
let mut buf = Vec::new();
cm.build_source_map_from(&mut srcmap, None)
.to_writer(&mut buf)?;
if emit_options.inline_source_map {
let encoded_map = format!(
"//# sourceMappingURL=data:application/json;base64,{}\n",
base64::encode(buf)
);
code.push_str(&encoded_map);
} else if emit_options.source_map {
maybe_map = Some(String::from_utf8(buf)?);
}
}
Ok((code, maybe_map))
})
}
pub(crate) struct EmitOptions {
@ -735,7 +734,7 @@ impl fmt::Display for GraphError {
ResolutionError::InvalidDowngrade(_, _)
| ResolutionError::InvalidLocalImport(_, _)
) {
write!(f, "{}", err.to_string_with_span())
write!(f, "{}", err.to_string_with_range())
} else {
self.0.fmt(f)
}

View file

@ -62,7 +62,7 @@ use crate::file_fetcher::get_source_from_data_url;
use crate::fs_util;
use crate::logger;
use crate::tools::fmt::format_file;
use crate::tools::fmt::format_parsed_module;
use crate::tools::fmt::format_parsed_source;
pub const REGISTRIES_PATH: &str = "registries";
const SOURCES_PATH: &str = "deps";
@ -1086,7 +1086,7 @@ impl Inner {
let text_edits = tokio::task::spawn_blocking(move || {
let format_result = match source.module() {
Some(Ok(parsed_module)) => {
Ok(format_parsed_module(parsed_module, fmt_options))
format_parsed_source(parsed_module, fmt_options)
}
Some(Err(err)) => Err(err.to_string()),
None => {

View file

@ -60,10 +60,10 @@ struct GraphData {
// resolution map so that those errors can be surfaced at the appropriate time
resolution_map:
HashMap<ModuleSpecifier, HashMap<String, deno_graph::Resolved>>,
// in some cases we want to provide the span where the resolution error
// in some cases we want to provide the range where the resolution error
// occurred but need to surface it on load, but on load we don't know who the
// referrer and span was, so we need to cache those
resolved_map: HashMap<ModuleSpecifier, deno_graph::Span>,
resolved_map: HashMap<ModuleSpecifier, deno_graph::Range>,
// deno_graph detects all sorts of issues at build time (prepare_module_load)
// but if they are errors at that stage, the don't cause the correct behaviors
// so we cache the error and then surface it when appropriate (e.g. load)
@ -469,7 +469,7 @@ impl ProcState {
Some(Err(err)) => {
return Err(custom_error(
"TypeError",
format!("{}\n", err.to_string_with_span()),
format!("{}\n", err.to_string_with_range()),
))
}
_ => (),

View file

@ -220,24 +220,15 @@ pub fn format_file(
}
}
pub fn format_parsed_module(
pub fn format_parsed_source(
parsed_source: &ParsedSource,
fmt_options: FmtOptionsConfig,
) -> 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(),
},
) -> Result<String, String> {
dprint_plugin_typescript::format_parsed_source(
parsed_source,
&get_resolved_typescript_config(&fmt_options),
)
.map_err(|e| e.to_string())
}
async fn check_source_files(

View file

@ -540,7 +540,7 @@ impl ReplSession {
Some(diagnostic) => Ok(EvaluationOutput::Error(format!(
"{}: {} at {}:{}",
colors::red("parse error"),
diagnostic.message,
diagnostic.message(),
diagnostic.display_position.line_number,
diagnostic.display_position.column_number,
))),