0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-09 21:57:40 -04:00

refactor: upgrade to deno_ast 0.15 (#14680)

This commit is contained in:
David Sherret 2022-05-20 16:40:55 -04:00 committed by Kitson Kelly
parent ff37b255c6
commit ca44586a18
No known key found for this signature in database
GPG key ID: 2D87CFF11B51960A
34 changed files with 365 additions and 314 deletions

143
Cargo.lock generated
View file

@ -811,18 +811,16 @@ dependencies = [
[[package]] [[package]]
name = "deno_ast" name = "deno_ast"
version = "0.14.1" version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0691d08faa4f1fd9898ecfbe82c32623f89969e92e7b97b5119a18399609d25" checksum = "a2989afff97ba7da10f186e9a45e946b4ef943b9d4babd2ee7b4b24cc9906b69"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"base64 0.13.0", "base64 0.13.0",
"data-url", "data-url",
"dprint-swc-ecma-ast-view", "dprint-swc-ext",
"serde", "serde",
"swc_atoms",
"swc_bundler", "swc_bundler",
"swc_common",
"swc_ecmascript", "swc_ecmascript",
"text_lines", "text_lines",
"url", "url",
@ -908,9 +906,9 @@ dependencies = [
[[package]] [[package]]
name = "deno_doc" name = "deno_doc"
version = "0.34.0" version = "0.35.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0add4cb8ed6a0698aca6964995083b292300b79ab9451675147ba8d144566935" checksum = "cb498c0dcc46a195f565b19eeef5593a6cdbd387ae1ef278ebf7eca2be55128b"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"deno_ast", "deno_ast",
@ -954,9 +952,9 @@ dependencies = [
[[package]] [[package]]
name = "deno_graph" name = "deno_graph"
version = "0.26.0" version = "0.27.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2cb46950445fbcf6fa0fdf74c6ebbaf938540d000dfcc556685ec397ec265fc" checksum = "99110bbe6cc90fc653a6bd51cf56d8545cd210f3da913aab63d7e362ab9dbaa4"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"cfg-if", "cfg-if",
@ -969,6 +967,7 @@ dependencies = [
"ring", "ring",
"serde", "serde",
"serde_json", "serde_json",
"sourcemap",
"termcolor", "termcolor",
"url", "url",
] ]
@ -999,9 +998,9 @@ dependencies = [
[[package]] [[package]]
name = "deno_lint" name = "deno_lint"
version = "0.29.0" version = "0.30.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9272866b24121c43a715c9afe70106ca92e97bc6d9175bc1576c254bac8ec94" checksum = "a550a14b0b175707c116ab26dd97d96424f238a8c562a719971376d3d75529be"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"deno_ast", "deno_ast",
@ -1301,9 +1300,9 @@ dependencies = [
[[package]] [[package]]
name = "dprint-plugin-typescript" name = "dprint-plugin-typescript"
version = "0.68.2" version = "0.68.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7210d773012a869d45bfde328cb8a0a088aa9f4fe66d4a07a509f78363741f1" checksum = "d20d1a1d9d423e890a9b2fd687abf68572d509ac40d9af6175793a36fc4af9b1"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"deno_ast", "deno_ast",
@ -1313,10 +1312,10 @@ dependencies = [
] ]
[[package]] [[package]]
name = "dprint-swc-ecma-ast-view" name = "dprint-swc-ext"
version = "0.55.0" version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43f9e087f831642347a4b0370824cedf4649b7bff5065c25d53cd73094c014ca" checksum = "3df529037ff02f1c43ae8c6cce54d9ad85546ff89cb5c1988f56130c16e16a48"
dependencies = [ dependencies = [
"bumpalo", "bumpalo",
"num-bigint", "num-bigint",
@ -1463,9 +1462,9 @@ dependencies = [
[[package]] [[package]]
name = "eszip" name = "eszip"
version = "0.19.0" version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34efd9e689611033cf3783634d6196d010d0cf772ee200229ad5c03e99c49f39" checksum = "9c2a7e3d40eb2f779d30d68f5b3605584d79533d5b314ece9cdbb9c14358cb80"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"base64 0.13.0", "base64 0.13.0",
@ -3864,9 +3863,9 @@ dependencies = [
[[package]] [[package]]
name = "swc_bundler" name = "swc_bundler"
version = "0.133.0" version = "0.147.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "052dafe1f3a9144331ee15f0a3f2c5fe0bb535e19f0bc1ada374b2d0256c314c" checksum = "21cb7497dc98ccc6b677b6c0be2890c5ee827e4763f5151ef6d60a82e9b93bb8"
dependencies = [ dependencies = [
"ahash", "ahash",
"anyhow", "anyhow",
@ -3896,9 +3895,9 @@ dependencies = [
[[package]] [[package]]
name = "swc_common" name = "swc_common"
version = "0.17.25" version = "0.18.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "766ad22c1cb8586c038ccba7371a4903a6074b53ee4ba8980a52f502413f120e" checksum = "269446a3916d32071ca4c1adb39761fd067bc352ba40e40fca11de881ece8007"
dependencies = [ dependencies = [
"ahash", "ahash",
"ast_node", "ast_node",
@ -3922,13 +3921,40 @@ dependencies = [
] ]
[[package]] [[package]]
name = "swc_ecma_ast" name = "swc_config"
version = "0.75.0" version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72961898fbe56591997e667a1ec6a268383582810351c279a15ec710b6177d33" checksum = "b8bb05ef56c14b95dd7e62e95960153af811b9a447287f1f6ca59f1337fb83d4"
dependencies = [
"anyhow",
"indexmap",
"serde",
"serde_json",
"swc_config_macro",
]
[[package]]
name = "swc_config_macro"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb64bc03d90fd5c90d6ab917bb2b1d7fbd31957df39e31ea24a3f554b4372251"
dependencies = [
"pmutil",
"proc-macro2 1.0.38",
"quote 1.0.18",
"swc_macros_common",
"syn 1.0.93",
]
[[package]]
name = "swc_ecma_ast"
version = "0.78.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21f40169fe465e9a93cda5fe397c3afcb69be5ba2f76c4ab22137af6effaebcc"
dependencies = [ dependencies = [
"is-macro", "is-macro",
"num-bigint", "num-bigint",
"scoped-tls",
"serde", "serde",
"string_enum", "string_enum",
"swc_atoms", "swc_atoms",
@ -3938,9 +3964,9 @@ dependencies = [
[[package]] [[package]]
name = "swc_ecma_codegen" name = "swc_ecma_codegen"
version = "0.103.0" version = "0.108.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99ca430d8ea2c8791d1341c4035431c90b87330e39479b4a6dabb4fded124e30" checksum = "012de8d3583c4cce8ce08891dc6317136730f87ba53937d5f0eb3b86bca31048"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"memchr", "memchr",
@ -3970,9 +3996,9 @@ dependencies = [
[[package]] [[package]]
name = "swc_ecma_dep_graph" name = "swc_ecma_dep_graph"
version = "0.72.0" version = "0.76.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4c65494e803ddcb30d06aebb0e4a6b5a441da1fed91a9f630ebbaeee9ab0e66" checksum = "553628795fd79a45f3e23b1a732684d887356f9177128cd8c3e90c3631075116"
dependencies = [ dependencies = [
"swc_atoms", "swc_atoms",
"swc_common", "swc_common",
@ -3982,9 +4008,9 @@ dependencies = [
[[package]] [[package]]
name = "swc_ecma_loader" name = "swc_ecma_loader"
version = "0.29.1" version = "0.30.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e719f646201c51964a2c7b2a3dd79fadb563fc6a72454a7bc093d18c4aad44b0" checksum = "0f7baaa5b99cdf49e830caf54b837891c5c38275ac94c31d555859be95f6479c"
dependencies = [ dependencies = [
"ahash", "ahash",
"anyhow", "anyhow",
@ -3996,9 +4022,9 @@ dependencies = [
[[package]] [[package]]
name = "swc_ecma_parser" name = "swc_ecma_parser"
version = "0.100.2" version = "0.104.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "890d967031e3e7330cd7892f27d826b7b4f37c7caa19db85c78a0862e1fe3974" checksum = "efb97dc6efc95313dedc5158055cc811da77395ef7b54be61948b5ad097a3671"
dependencies = [ dependencies = [
"either", "either",
"enum_kind", "enum_kind",
@ -4016,9 +4042,9 @@ dependencies = [
[[package]] [[package]]
name = "swc_ecma_transforms" name = "swc_ecma_transforms"
version = "0.142.0" version = "0.154.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f20e5e2d8ab843fa0454e049f73f6d99c444a8c0e2320f77028361ab75e2d18e" checksum = "2bce21d9e8ff785aaf9b4ac11375d9f5767630fcaf882f72e6af0516224085a6"
dependencies = [ dependencies = [
"swc_atoms", "swc_atoms",
"swc_common", "swc_common",
@ -4033,13 +4059,14 @@ dependencies = [
[[package]] [[package]]
name = "swc_ecma_transforms_base" name = "swc_ecma_transforms_base"
version = "0.75.0" version = "0.85.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "404c6ea7ca61ceb2ce1f4ed448d1436a38c31b8c572850f04541c0229c966bbf" checksum = "8262876d5387887776f23c4894fbddff26e5f184edadf2375f3dc19fca2b42a4"
dependencies = [ dependencies = [
"better_scoped_tls", "better_scoped_tls",
"once_cell", "once_cell",
"phf", "phf",
"rustc-hash",
"serde", "serde",
"smallvec", "smallvec",
"swc_atoms", "swc_atoms",
@ -4053,9 +4080,9 @@ dependencies = [
[[package]] [[package]]
name = "swc_ecma_transforms_classes" name = "swc_ecma_transforms_classes"
version = "0.63.0" version = "0.73.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "503f2f6bd0f9e6363a93406753bf64675163423774256a267c85a5d9b5b44b08" checksum = "e74a27c29def9db5ff03db4d3ab3d37701fb6d100951162223b71132908451eb"
dependencies = [ dependencies = [
"swc_atoms", "swc_atoms",
"swc_common", "swc_common",
@ -4080,14 +4107,15 @@ dependencies = [
[[package]] [[package]]
name = "swc_ecma_transforms_optimization" name = "swc_ecma_transforms_optimization"
version = "0.112.1" version = "0.124.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af43d7d92e0bb8ba60d64ce8a7edcab7738f7e858b8e42814fca5c133ba17c19" checksum = "c995fb0565ace6368253af588cb848a92f0347dd74aef39e64af3c56466206d5"
dependencies = [ dependencies = [
"ahash", "ahash",
"dashmap", "dashmap",
"indexmap", "indexmap",
"once_cell", "once_cell",
"rustc-hash",
"serde_json", "serde_json",
"swc_atoms", "swc_atoms",
"swc_common", "swc_common",
@ -4102,9 +4130,9 @@ dependencies = [
[[package]] [[package]]
name = "swc_ecma_transforms_proposal" name = "swc_ecma_transforms_proposal"
version = "0.97.0" version = "0.107.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93d08411e517736b0167f3c9784fe9b98cc09308ae12e6072abd2bb2c2236da2" checksum = "47fc0f3b336764f89adf1899830321c3f5a7e845ede3ad5949eeb7468aa260ab"
dependencies = [ dependencies = [
"either", "either",
"serde", "serde",
@ -4121,9 +4149,9 @@ dependencies = [
[[package]] [[package]]
name = "swc_ecma_transforms_react" name = "swc_ecma_transforms_react"
version = "0.104.0" version = "0.114.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43cda44270dfcc95d61582981baddaf53d96c5233ea7384e81cd6e462816c58e" checksum = "2fbfcd197ebeb0547b59dee39a164633bcf4fb0edbae886f8046e471e6a10502"
dependencies = [ dependencies = [
"ahash", "ahash",
"base64 0.13.0", "base64 0.13.0",
@ -4136,6 +4164,7 @@ dependencies = [
"string_enum", "string_enum",
"swc_atoms", "swc_atoms",
"swc_common", "swc_common",
"swc_config",
"swc_ecma_ast", "swc_ecma_ast",
"swc_ecma_parser", "swc_ecma_parser",
"swc_ecma_transforms_base", "swc_ecma_transforms_base",
@ -4146,9 +4175,9 @@ dependencies = [
[[package]] [[package]]
name = "swc_ecma_transforms_typescript" name = "swc_ecma_transforms_typescript"
version = "0.107.0" version = "0.117.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a09397169ed7ce0751a82cb71655f3a4a1fb00d8863aabd5cca9b46eff3dd5f2" checksum = "fa8f32954c5a7c6bdead39c8a8a1580127a1759f33ef8b87d00f754882e6090a"
dependencies = [ dependencies = [
"serde", "serde",
"swc_atoms", "swc_atoms",
@ -4162,9 +4191,9 @@ dependencies = [
[[package]] [[package]]
name = "swc_ecma_utils" name = "swc_ecma_utils"
version = "0.79.1" version = "0.85.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44ee8d60b9977f58214af7102dc30855a6754e742afe6d6e26e5bf13883c7b91" checksum = "dff9d469b284a48317a695a81346a9609d04ce3a31da4493aac508e0d48a4257"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"once_cell", "once_cell",
@ -4177,9 +4206,9 @@ dependencies = [
[[package]] [[package]]
name = "swc_ecma_visit" name = "swc_ecma_visit"
version = "0.61.0" version = "0.64.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5ea00a52ba2b971955c62275696d5c59f3cf0cd06db74a66dec378ec9843c78" checksum = "f2d3783a0dd1e301ae2945ab1241405f913427f9512ec62756d3d2072f7c21bb"
dependencies = [ dependencies = [
"num-bigint", "num-bigint",
"swc_atoms", "swc_atoms",
@ -4191,9 +4220,9 @@ dependencies = [
[[package]] [[package]]
name = "swc_ecmascript" name = "swc_ecmascript"
version = "0.143.0" version = "0.157.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebda93aa6422956c184a9eb5fdb0f0f0ff433169fa15e55ef445e5ad0b5e0abe" checksum = "bd35679e1dc392f776b691b125692d90a7bebd5d23ec96699cfe37d8ae8633b1"
dependencies = [ dependencies = [
"swc_ecma_ast", "swc_ecma_ast",
"swc_ecma_codegen", "swc_ecma_codegen",
@ -4218,9 +4247,9 @@ dependencies = [
[[package]] [[package]]
name = "swc_fast_graph" name = "swc_fast_graph"
version = "0.5.0" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9860ef8ffc31eedf45bc39a60a2500838a331e3e687bc005fe69088f6a966460" checksum = "dccdc7e1f2d987c1e2fc7dfb36ef86666f04e5fad4fe88d3a1d05e4f01181d95"
dependencies = [ dependencies = [
"ahash", "ahash",
"indexmap", "indexmap",
@ -4230,9 +4259,9 @@ dependencies = [
[[package]] [[package]]
name = "swc_graph_analyzer" name = "swc_graph_analyzer"
version = "0.6.0" version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67696e05cdf3efc1daded3b4803639da25fd9254ca6bae16539058197a411de8" checksum = "c279894062688a31a6de1c95e00eb7cfcaa2a471334f6b741f083b86096f2a84"
dependencies = [ dependencies = [
"ahash", "ahash",
"auto_impl", "auto_impl",

View file

@ -45,11 +45,11 @@ winapi = "=0.3.9"
winres = "=0.1.12" winres = "=0.1.12"
[dependencies] [dependencies]
deno_ast = { version = "0.14.0", features = ["bundler", "cjs", "codegen", "dep_graph", "module_specifier", "proposal", "react", "sourcemap", "transforms", "transpiling", "typescript", "view", "visit"] } deno_ast = { version = "0.15.0", features = ["bundler", "cjs", "codegen", "dep_graph", "module_specifier", "proposal", "react", "sourcemap", "transforms", "transpiling", "typescript", "view", "visit"] }
deno_core = { version = "0.135.0", path = "../core" } deno_core = { version = "0.135.0", path = "../core" }
deno_doc = "0.34.0" deno_doc = "0.35.0"
deno_graph = "0.26.0" deno_graph = "0.27.0"
deno_lint = { version = "0.29.0", features = ["docs"] } deno_lint = { version = "0.30.0", features = ["docs"] }
deno_runtime = { version = "0.61.0", path = "../runtime" } deno_runtime = { version = "0.61.0", path = "../runtime" }
deno_task_shell = "0.3.0" deno_task_shell = "0.3.0"
@ -64,10 +64,10 @@ data-url = "=0.1.1"
dissimilar = "=1.0.3" dissimilar = "=1.0.3"
dprint-plugin-json = "=0.15.2" dprint-plugin-json = "=0.15.2"
dprint-plugin-markdown = "=0.13.2" dprint-plugin-markdown = "=0.13.2"
dprint-plugin-typescript = "=0.68.2" dprint-plugin-typescript = "=0.68.3"
encoding_rs = "=0.8.31" encoding_rs = "=0.8.31"
env_logger = "=0.9.0" env_logger = "=0.9.0"
eszip = "=0.19.0" eszip = "=0.20.0"
fancy-regex = "=0.9.0" fancy-regex = "=0.9.0"
http = "=0.2.6" http = "=0.2.6"
import_map = "=0.9.0" import_map = "=0.9.0"

View file

@ -11,7 +11,6 @@ use deno_core::url::Url;
use deno_core::JsRuntime; use deno_core::JsRuntime;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::sync::Arc;
pub use esm_resolver::check_if_should_use_esm_loader; pub use esm_resolver::check_if_should_use_esm_loader;
pub use esm_resolver::NodeEsmResolver; pub use esm_resolver::NodeEsmResolver;
@ -172,7 +171,7 @@ pub async fn translate_cjs_to_esm(
) -> Result<String, AnyError> { ) -> Result<String, AnyError> {
let parsed_source = deno_ast::parse_script(deno_ast::ParseParams { let parsed_source = deno_ast::parse_script(deno_ast::ParseParams {
specifier: specifier.to_string(), specifier: specifier.to_string(),
source: deno_ast::SourceTextInfo::new(Arc::new(code)), text_info: deno_ast::SourceTextInfo::new(code.into()),
media_type, media_type,
capture_tokens: true, capture_tokens: true,
scope_analysis: false, scope_analysis: false,
@ -203,7 +202,7 @@ pub async fn translate_cjs_to_esm(
{ {
let parsed_source = deno_ast::parse_script(deno_ast::ParseParams { let parsed_source = deno_ast::parse_script(deno_ast::ParseParams {
specifier: reexport_specifier.to_string(), specifier: reexport_specifier.to_string(),
source: deno_ast::SourceTextInfo::new(reexport_file.source), text_info: deno_ast::SourceTextInfo::new(reexport_file.source),
media_type: reexport_file.media_type, media_type: reexport_file.media_type,
capture_tokens: true, capture_tokens: true,
scope_analysis: false, scope_analysis: false,

View file

@ -34,6 +34,7 @@ use deno_ast::swc::parser::lexer::Lexer;
use deno_ast::swc::parser::StringInput; use deno_ast::swc::parser::StringInput;
use deno_ast::Diagnostic; use deno_ast::Diagnostic;
use deno_ast::LineAndColumnDisplay; use deno_ast::LineAndColumnDisplay;
use deno_ast::SourceRangedForSpanned;
use deno_core::anyhow::anyhow; use deno_core::anyhow::anyhow;
use deno_core::anyhow::Context; use deno_core::anyhow::Context;
use deno_core::error::AnyError; use deno_core::error::AnyError;
@ -529,7 +530,7 @@ impl swc::bundler::Load for BundleLoader<'_> {
if let Some(m) = self.graph.get(specifier) { if let Some(m) = self.graph.get(specifier) {
let (fm, module) = transpile_module( let (fm, module) = transpile_module(
specifier, specifier,
m.maybe_source.as_ref().map(|s| s.as_str()).unwrap_or(""), m.maybe_source.as_ref().map(|s| s as &str).unwrap_or(""),
m.media_type, m.media_type,
self.emit_options, self.emit_options,
self.cm.clone(), self.cm.clone(),
@ -616,7 +617,7 @@ fn swc_err_to_diagnostic(
let location = source_map.lookup_char_pos(err.span().lo); let location = source_map.lookup_char_pos(err.span().lo);
Diagnostic { Diagnostic {
specifier: specifier.to_string(), specifier: specifier.to_string(),
span: err.span(), range: err.range(),
display_position: LineAndColumnDisplay { display_position: LineAndColumnDisplay {
line_number: location.line, line_number: location.line,
column_number: location.col_display + 1, column_number: location.col_display + 1,
@ -669,7 +670,6 @@ pub fn bundle(
let emit_options: deno_ast::EmitOptions = options.ts_config.into(); let emit_options: deno_ast::EmitOptions = options.ts_config.into();
let source_map_config = deno_ast::SourceMapConfig { let source_map_config = deno_ast::SourceMapConfig {
inline_sources: emit_options.inline_sources, inline_sources: emit_options.inline_sources,
maybe_base: None,
}; };
let cm = Rc::new(swc::common::SourceMap::new( let cm = Rc::new(swc::common::SourceMap::new(
@ -707,7 +707,11 @@ pub fn bundle(
let mut buf = Vec::new(); let mut buf = Vec::new();
let mut srcmap = Vec::new(); let mut srcmap = Vec::new();
{ {
let cfg = swc::codegen::Config { minify: false }; let cfg = swc::codegen::Config {
minify: false,
ascii_only: false,
target: deno_ast::ES_VERSION,
};
let mut wr = Box::new(swc::codegen::text_writer::JsWriter::new( let mut wr = Box::new(swc::codegen::text_writer::JsWriter::new(
cm.clone(), cm.clone(),
"\n", "\n",

View file

@ -59,7 +59,7 @@ pub struct File {
/// The resolved media type for the file. /// The resolved media type for the file.
pub media_type: MediaType, pub media_type: MediaType,
/// The source of the file as a string. /// The source of the file as a string.
pub source: Arc<String>, pub source: Arc<str>,
/// The _final_ specifier for the file. The requested specifier and the final /// The _final_ specifier for the file. The requested specifier and the final
/// specifier maybe different for remote files that have been redirected. /// specifier maybe different for remote files that have been redirected.
pub specifier: ModuleSpecifier, pub specifier: ModuleSpecifier,
@ -161,7 +161,7 @@ fn fetch_local(specifier: &ModuleSpecifier) -> Result<File, AnyError> {
local, local,
maybe_types: None, maybe_types: None,
media_type, media_type,
source: Arc::new(source), source: source.into(),
specifier: specifier.clone(), specifier: specifier.clone(),
maybe_headers: None, maybe_headers: None,
}) })
@ -383,7 +383,7 @@ impl FileFetcher {
local, local,
maybe_types, maybe_types,
media_type, media_type,
source: Arc::new(source), source: source.into(),
specifier: specifier.clone(), specifier: specifier.clone(),
maybe_headers: Some(headers.clone()), maybe_headers: Some(headers.clone()),
}) })
@ -469,7 +469,7 @@ impl FileFetcher {
local, local,
maybe_types: None, maybe_types: None,
media_type, media_type,
source: Arc::new(source), source: source.into(),
specifier: specifier.clone(), specifier: specifier.clone(),
maybe_headers: Some(headers), maybe_headers: Some(headers),
}) })
@ -533,7 +533,7 @@ impl FileFetcher {
local, local,
maybe_types: None, maybe_types: None,
media_type, media_type,
source: Arc::new(source), source: source.into(),
specifier: specifier.clone(), specifier: specifier.clone(),
maybe_headers: Some(headers), maybe_headers: Some(headers),
}) })
@ -815,7 +815,7 @@ mod tests {
let url_str = format!("http://127.0.0.1:4545/encoding/{}", fixture); let url_str = format!("http://127.0.0.1:4545/encoding/{}", fixture);
let specifier = resolve_url(&url_str).unwrap(); let specifier = resolve_url(&url_str).unwrap();
let (file, headers) = test_fetch_remote(&specifier).await; let (file, headers) = test_fetch_remote(&specifier).await;
assert_eq!(file.source.as_str(), expected); assert_eq!(&*file.source, expected);
assert_eq!(file.media_type, MediaType::TypeScript); assert_eq!(file.media_type, MediaType::TypeScript);
assert_eq!( assert_eq!(
headers.get("content-type").unwrap(), headers.get("content-type").unwrap(),
@ -827,7 +827,7 @@ mod tests {
let p = test_util::testdata_path().join(format!("encoding/{}.ts", charset)); let p = test_util::testdata_path().join(format!("encoding/{}.ts", charset));
let specifier = resolve_url_or_path(p.to_str().unwrap()).unwrap(); let specifier = resolve_url_or_path(p.to_str().unwrap()).unwrap();
let (file, _) = test_fetch(&specifier).await; let (file, _) = test_fetch(&specifier).await;
assert_eq!(file.source.as_str(), expected); assert_eq!(&*file.source, expected);
} }
#[test] #[test]
@ -1037,7 +1037,7 @@ mod tests {
local, local,
maybe_types: None, maybe_types: None,
media_type: MediaType::TypeScript, media_type: MediaType::TypeScript,
source: Arc::new("some source code".to_string()), source: "some source code".into(),
specifier: specifier.clone(), specifier: specifier.clone(),
maybe_headers: None, maybe_headers: None,
}; };
@ -1067,7 +1067,7 @@ mod tests {
let maybe_file = file_fetcher.get_source(&specifier); let maybe_file = file_fetcher.get_source(&specifier);
assert!(maybe_file.is_some()); assert!(maybe_file.is_some());
let file = maybe_file.unwrap(); let file = maybe_file.unwrap();
assert_eq!(file.source.as_str(), "export const redirect = 1;\n"); assert_eq!(&*file.source, "export const redirect = 1;\n");
assert_eq!( assert_eq!(
file.specifier, file.specifier,
resolve_url("http://localhost:4545/subdir/redirects/redirect1.js") resolve_url("http://localhost:4545/subdir/redirects/redirect1.js")
@ -1094,7 +1094,7 @@ mod tests {
assert!(result.is_ok()); assert!(result.is_ok());
let file = result.unwrap(); let file = result.unwrap();
assert_eq!( assert_eq!(
file.source.as_str(), &*file.source,
"export const a = \"a\";\n\nexport enum A {\n A,\n B,\n C,\n}\n" "export const a = \"a\";\n\nexport enum A {\n A,\n B,\n C,\n}\n"
); );
assert_eq!(file.media_type, MediaType::TypeScript); assert_eq!(file.media_type, MediaType::TypeScript);
@ -1126,7 +1126,7 @@ mod tests {
assert!(result.is_ok()); assert!(result.is_ok());
let file = result.unwrap(); let file = result.unwrap();
assert_eq!( assert_eq!(
file.source.as_str(), &*file.source,
"export const a = \"a\";\n\nexport enum A {\n A,\n B,\n C,\n}\n" "export const a = \"a\";\n\nexport enum A {\n A,\n B,\n C,\n}\n"
); );
assert_eq!(file.media_type, MediaType::TypeScript); assert_eq!(file.media_type, MediaType::TypeScript);
@ -1149,7 +1149,7 @@ mod tests {
assert!(result.is_ok()); assert!(result.is_ok());
let file = result.unwrap(); let file = result.unwrap();
assert_eq!( assert_eq!(
file.source.as_str(), &*file.source,
"export { printHello } from \"./print_hello.ts\";\n" "export { printHello } from \"./print_hello.ts\";\n"
); );
assert_eq!(file.media_type, MediaType::TypeScript); assert_eq!(file.media_type, MediaType::TypeScript);
@ -1172,7 +1172,7 @@ mod tests {
assert!(result.is_ok()); assert!(result.is_ok());
let file = result.unwrap(); let file = result.unwrap();
assert_eq!( assert_eq!(
file.source.as_str(), &*file.source,
"export { printHello } from \"./print_hello.ts\";\n" "export { printHello } from \"./print_hello.ts\";\n"
); );
// This validates that when using the cached value, because we modified // This validates that when using the cached value, because we modified
@ -1193,7 +1193,7 @@ mod tests {
assert!(result.is_ok()); assert!(result.is_ok());
let file = result.unwrap(); let file = result.unwrap();
assert_eq!( assert_eq!(
file.source.as_str(), &*file.source,
"export { printHello } from \"./print_hello.ts\";\n" "export { printHello } from \"./print_hello.ts\";\n"
); );
assert_eq!(file.media_type, MediaType::Json); assert_eq!(file.media_type, MediaType::Json);
@ -1216,7 +1216,7 @@ mod tests {
assert!(result.is_ok()); assert!(result.is_ok());
let file = result.unwrap(); let file = result.unwrap();
assert_eq!( assert_eq!(
file.source.as_str(), &*file.source,
"export { printHello } from \"./print_hello.ts\";\n" "export { printHello } from \"./print_hello.ts\";\n"
); );
assert_eq!(file.media_type, MediaType::TypeScript); assert_eq!(file.media_type, MediaType::TypeScript);
@ -1602,7 +1602,7 @@ mod tests {
.await; .await;
assert!(result.is_ok()); assert!(result.is_ok());
let file = result.unwrap(); let file = result.unwrap();
assert_eq!(file.source.as_str(), r#"console.log("hello deno");"#); assert_eq!(&*file.source, r#"console.log("hello deno");"#);
fs::write(fixture_path, r#"console.log("goodbye deno");"#).unwrap(); fs::write(fixture_path, r#"console.log("goodbye deno");"#).unwrap();
let result = file_fetcher let result = file_fetcher
@ -1610,7 +1610,7 @@ mod tests {
.await; .await;
assert!(result.is_ok()); assert!(result.is_ok());
let file = result.unwrap(); let file = result.unwrap();
assert_eq!(file.source.as_str(), r#"console.log("goodbye deno");"#); assert_eq!(&*file.source, r#"console.log("goodbye deno");"#);
} }
#[tokio::test] #[tokio::test]
@ -1626,7 +1626,7 @@ mod tests {
.await; .await;
assert!(result.is_ok()); assert!(result.is_ok());
let file = result.unwrap(); let file = result.unwrap();
let first = file.source.as_str(); let first = file.source;
let (file_fetcher, _) = let (file_fetcher, _) =
setup(CacheSetting::RespectHeaders, Some(temp_dir.clone())); setup(CacheSetting::RespectHeaders, Some(temp_dir.clone()));
@ -1635,7 +1635,7 @@ mod tests {
.await; .await;
assert!(result.is_ok()); assert!(result.is_ok());
let file = result.unwrap(); let file = result.unwrap();
let second = file.source.as_str(); let second = file.source;
assert_ne!(first, second); assert_ne!(first, second);
} }
@ -1653,7 +1653,7 @@ mod tests {
.await; .await;
assert!(result.is_ok()); assert!(result.is_ok());
let file = result.unwrap(); let file = result.unwrap();
let first = file.source.as_str(); let first = file.source;
let (file_fetcher, _) = let (file_fetcher, _) =
setup(CacheSetting::RespectHeaders, Some(temp_dir.clone())); setup(CacheSetting::RespectHeaders, Some(temp_dir.clone()));
@ -1662,7 +1662,7 @@ mod tests {
.await; .await;
assert!(result.is_ok()); assert!(result.is_ok());
let file = result.unwrap(); let file = result.unwrap();
let second = file.source.as_str(); let second = file.source;
assert_eq!(first, second); assert_eq!(first, second);
} }

View file

@ -36,7 +36,7 @@ pub fn contains_specifier(
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
pub enum ModuleEntry { pub enum ModuleEntry {
Module { Module {
code: Arc<String>, code: Arc<str>,
dependencies: BTreeMap<String, Dependency>, dependencies: BTreeMap<String, Dependency>,
media_type: MediaType, media_type: MediaType,
/// Whether or not this is a JS/JSX module with a `@ts-check` directive. /// Whether or not this is a JS/JSX module with a `@ts-check` directive.

View file

@ -9,6 +9,8 @@ use crate::config_file::LintConfig;
use crate::tools::lint::create_linter; use crate::tools::lint::create_linter;
use crate::tools::lint::get_configured_rules; use crate::tools::lint::get_configured_rules;
use deno_ast::SourceRange;
use deno_ast::SourceRangedForSpanned;
use deno_ast::SourceTextInfo; use deno_ast::SourceTextInfo;
use deno_core::anyhow::anyhow; use deno_core::anyhow::anyhow;
use deno_core::error::custom_error; use deno_core::error::custom_error;
@ -468,8 +470,8 @@ impl CodeActionCollection {
// Get the end position of the comment. // Get the end position of the comment.
let line = maybe_parsed_source let line = maybe_parsed_source
.unwrap() .unwrap()
.source() .text_info()
.line_and_column_index(ignore_comment.span.hi()); .line_and_column_index(ignore_comment.end());
let position = lsp::Position { let position = lsp::Position {
line: line.line_index as u32, line: line.line_index as u32,
character: line.column_index as u32, character: line.column_index as u32,
@ -719,6 +721,24 @@ fn prepend_whitespace(content: String, line_content: Option<String>) -> String {
} }
} }
pub fn source_range_to_lsp_range(
range: &SourceRange,
source_text_info: &SourceTextInfo,
) -> lsp::Range {
let start = source_text_info.line_and_column_index(range.start);
let end = source_text_info.line_and_column_index(range.end);
lsp::Range {
start: lsp::Position {
line: start.line_index as u32,
character: start.column_index as u32,
},
end: lsp::Position {
line: end.line_index as u32,
character: end.column_index as u32,
},
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -787,12 +807,12 @@ mod tests {
start: deno_lint::diagnostic::Position { start: deno_lint::diagnostic::Position {
line_index: 0, line_index: 0,
column_index: 2, column_index: 2,
byte_pos: 23, byte_index: 23,
}, },
end: deno_lint::diagnostic::Position { end: deno_lint::diagnostic::Position {
line_index: 1, line_index: 1,
column_index: 0, column_index: 0,
byte_pos: 33, byte_index: 33,
}, },
}; };
let actual = as_lsp_range(&fixture); let actual = as_lsp_range(&fixture);

View file

@ -1,5 +1,6 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use super::analysis::source_range_to_lsp_range;
use super::config::Config; use super::config::Config;
use super::config::WorkspaceSettings; use super::config::WorkspaceSettings;
use super::language_server; use super::language_server;
@ -8,10 +9,11 @@ use super::tsc;
use super::tsc::NavigationTree; use super::tsc::NavigationTree;
use deno_ast::swc::ast; use deno_ast::swc::ast;
use deno_ast::swc::common::Span;
use deno_ast::swc::visit::Visit; use deno_ast::swc::visit::Visit;
use deno_ast::swc::visit::VisitWith; use deno_ast::swc::visit::VisitWith;
use deno_ast::ParsedSource; use deno_ast::ParsedSource;
use deno_ast::SourceRange;
use deno_ast::SourceRangedForSpanned;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::resolve_url; use deno_core::resolve_url;
use deno_core::serde::Deserialize; use deno_core::serde::Deserialize;
@ -48,21 +50,6 @@ pub struct CodeLensData {
pub specifier: ModuleSpecifier, pub specifier: ModuleSpecifier,
} }
fn span_to_range(span: &Span, parsed_source: &ParsedSource) -> lsp::Range {
let start = parsed_source.source().line_and_column_index(span.lo);
let end = parsed_source.source().line_and_column_index(span.hi);
lsp::Range {
start: lsp::Position {
line: start.line_index as u32,
character: start.column_index as u32,
},
end: lsp::Position {
line: end.line_index as u32,
character: end.column_index as u32,
},
}
}
struct DenoTestCollector { struct DenoTestCollector {
code_lenses: Vec<lsp::CodeLens>, code_lenses: Vec<lsp::CodeLens>,
parsed_source: ParsedSource, parsed_source: ParsedSource,
@ -80,8 +67,9 @@ impl DenoTestCollector {
} }
} }
fn add_code_lenses<N: AsRef<str>>(&mut self, name: N, span: &Span) { fn add_code_lenses<N: AsRef<str>>(&mut self, name: N, range: &SourceRange) {
let range = span_to_range(span, &self.parsed_source); let range =
source_range_to_lsp_range(range, self.parsed_source.text_info());
self.add_code_lens(&name, range, "\u{fe0e} Run Test", false); self.add_code_lens(&name, range, "\u{fe0e} Run Test", false);
self.add_code_lens(&name, range, "Debug", true); self.add_code_lens(&name, range, "Debug", true);
} }
@ -111,7 +99,7 @@ impl DenoTestCollector {
}); });
} }
fn check_call_expr(&mut self, node: &ast::CallExpr, span: &Span) { fn check_call_expr(&mut self, node: &ast::CallExpr, range: &SourceRange) {
if let Some(expr) = node.args.get(0).map(|es| es.expr.as_ref()) { if let Some(expr) = node.args.get(0).map(|es| es.expr.as_ref()) {
match expr { match expr {
ast::Expr::Object(obj_lit) => { ast::Expr::Object(obj_lit) => {
@ -126,7 +114,7 @@ impl DenoTestCollector {
key_value_prop.value.as_ref() key_value_prop.value.as_ref()
{ {
let name = lit_str.value.to_string(); let name = lit_str.value.to_string();
self.add_code_lenses(name, span); self.add_code_lenses(name, range);
} }
} }
} }
@ -137,12 +125,12 @@ impl DenoTestCollector {
ast::Expr::Fn(fn_expr) => { ast::Expr::Fn(fn_expr) => {
if let Some(ast::Ident { sym, .. }) = fn_expr.ident.as_ref() { if let Some(ast::Ident { sym, .. }) = fn_expr.ident.as_ref() {
let name = sym.to_string(); let name = sym.to_string();
self.add_code_lenses(name, span); self.add_code_lenses(name, range);
} }
} }
ast::Expr::Lit(ast::Lit::Str(lit_str)) => { ast::Expr::Lit(ast::Lit::Str(lit_str)) => {
let name = lit_str.value.to_string(); let name = lit_str.value.to_string();
self.add_code_lenses(name, span); self.add_code_lenses(name, range);
} }
_ => (), _ => (),
} }
@ -161,7 +149,7 @@ impl Visit for DenoTestCollector {
match callee_expr.as_ref() { match callee_expr.as_ref() {
ast::Expr::Ident(ident) => { ast::Expr::Ident(ident) => {
if self.test_vars.contains(&ident.sym.to_string()) { if self.test_vars.contains(&ident.sym.to_string()) {
self.check_call_expr(node, &ident.span); self.check_call_expr(node, &ident.range());
} }
} }
ast::Expr::Member(member_expr) => { ast::Expr::Member(member_expr) => {
@ -169,7 +157,7 @@ impl Visit for DenoTestCollector {
if ns_prop_ident.sym.to_string() == "test" { if ns_prop_ident.sym.to_string() == "test" {
if let ast::Expr::Ident(ident) = member_expr.obj.as_ref() { if let ast::Expr::Ident(ident) = member_expr.obj.as_ref() {
if ident.sym.to_string() == "Deno" { if ident.sym.to_string() == "Deno" {
self.check_call_expr(node, &ns_prop_ident.span); self.check_call_expr(node, &ns_prop_ident.range());
} }
} }
} }
@ -557,8 +545,7 @@ mod tests {
#[test] #[test]
fn test_deno_test_collector() { fn test_deno_test_collector() {
let specifier = resolve_url("https://deno.land/x/mod.ts").unwrap(); let specifier = resolve_url("https://deno.land/x/mod.ts").unwrap();
let source = Arc::new( let source = r#"
r#"
Deno.test({ Deno.test({
name: "test a", name: "test a",
fn() {} fn() {}
@ -567,12 +554,10 @@ mod tests {
Deno.test(function useFnName() {}); Deno.test(function useFnName() {});
Deno.test("test b", function anotherTest() {}); Deno.test("test b", function anotherTest() {});
"# "#;
.to_string(),
);
let parsed_module = deno_ast::parse_module(deno_ast::ParseParams { let parsed_module = deno_ast::parse_module(deno_ast::ParseParams {
specifier: specifier.to_string(), specifier: specifier.to_string(),
source: SourceTextInfo::new(source), text_info: SourceTextInfo::new(source.into()),
media_type: MediaType::TypeScript, media_type: MediaType::TypeScript,
capture_tokens: true, capture_tokens: true,
scope_analysis: true, scope_analysis: true,

View file

@ -97,13 +97,15 @@ fn to_narrow_lsp_range(
text_info: &SourceTextInfo, text_info: &SourceTextInfo,
range: &deno_graph::Range, range: &deno_graph::Range,
) -> lsp::Range { ) -> lsp::Range {
let end_byte_index = text_info.byte_index(LineAndColumnIndex { let end_byte_index = text_info
.loc_to_source_pos(LineAndColumnIndex {
line_index: range.end.line, line_index: range.end.line,
column_index: range.end.character, column_index: range.end.character,
}); })
.as_byte_index(text_info.range().start);
let text_bytes = text_info.text_str().as_bytes(); let text_bytes = text_info.text_str().as_bytes();
let has_trailing_quote = let has_trailing_quote =
matches!(text_bytes[end_byte_index.0 as usize - 1], b'"' | b'\''); matches!(text_bytes[end_byte_index - 1], b'"' | b'\'');
lsp::Range { lsp::Range {
start: lsp::Position { start: lsp::Position {
line: range.start.line as u32, line: range.start.line as u32,
@ -577,7 +579,6 @@ mod tests {
use deno_graph::Range; use deno_graph::Range;
use std::collections::HashMap; use std::collections::HashMap;
use std::path::Path; use std::path::Path;
use std::sync::Arc;
use test_util::TempDir; use test_util::TempDir;
fn mock_documents( fn mock_documents(
@ -593,7 +594,7 @@ mod tests {
specifier.clone(), specifier.clone(),
*version, *version,
language_id.clone(), language_id.clone(),
Arc::new(source.to_string()), (*source).into(),
); );
} }
let http_cache = HttpCache::new(location); let http_cache = HttpCache::new(location);

View file

@ -898,7 +898,7 @@ mod tests {
specifier.clone(), specifier.clone(),
*version, *version,
language_id.clone(), language_id.clone(),
Arc::new(source.to_string()), (*source).into(),
); );
} }
StateSnapshot { StateSnapshot {

View file

@ -81,12 +81,12 @@ impl deno_graph::SourceParser for SourceParser {
fn parse_module( fn parse_module(
&self, &self,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
source: Arc<String>, source: Arc<str>,
media_type: MediaType, media_type: MediaType,
) -> Result<deno_ast::ParsedSource, deno_ast::Diagnostic> { ) -> Result<deno_ast::ParsedSource, deno_ast::Diagnostic> {
deno_ast::parse_module(deno_ast::ParseParams { deno_ast::parse_module(deno_ast::ParseParams {
specifier: specifier.to_string(), specifier: specifier.to_string(),
source: SourceTextInfo::new(source), text_info: SourceTextInfo::new(source),
media_type, media_type,
capture_tokens: true, capture_tokens: true,
scope_analysis: true, scope_analysis: true,
@ -179,7 +179,7 @@ impl AssetOrDocument {
} }
} }
pub fn text(&self) -> Arc<String> { pub fn text(&self) -> Arc<str> {
match self { match self {
AssetOrDocument::Asset(a) => a.text(), AssetOrDocument::Asset(a) => a.text(),
AssetOrDocument::Document(d) => d.0.text_info.text(), AssetOrDocument::Document(d) => d.0.text_info.text(),
@ -254,7 +254,7 @@ impl Document {
specifier: ModuleSpecifier, specifier: ModuleSpecifier,
fs_version: String, fs_version: String,
maybe_headers: Option<&HashMap<String, String>>, maybe_headers: Option<&HashMap<String, String>>,
content: Arc<String>, content: Arc<str>,
maybe_resolver: Option<&dyn deno_graph::source::Resolver>, maybe_resolver: Option<&dyn deno_graph::source::Resolver>,
) -> Self { ) -> Self {
let parser = SourceParser::default(); let parser = SourceParser::default();
@ -293,7 +293,7 @@ impl Document {
specifier: ModuleSpecifier, specifier: ModuleSpecifier,
version: i32, version: i32,
language_id: LanguageId, language_id: LanguageId,
content: Arc<String>, content: Arc<str>,
maybe_resolver: Option<&dyn deno_graph::source::Resolver>, maybe_resolver: Option<&dyn deno_graph::source::Resolver>,
) -> Self { ) -> Self {
let maybe_headers = language_id.as_headers(); let maybe_headers = language_id.as_headers();
@ -352,7 +352,7 @@ impl Document {
index_valid = IndexValid::UpTo(0); index_valid = IndexValid::UpTo(0);
} }
} }
let content = Arc::new(content); let content: Arc<str> = content.into();
let maybe_module = if self let maybe_module = if self
.0 .0
.maybe_language_id .maybe_language_id
@ -413,7 +413,7 @@ impl Document {
&self.0.specifier &self.0.specifier
} }
pub fn content(&self) -> Arc<String> { pub fn content(&self) -> Arc<str> {
self.0.text_info.text() self.0.text_info.text()
} }
@ -661,12 +661,12 @@ impl FileSystemDocuments {
let doc = if specifier.scheme() == "file" { let doc = if specifier.scheme() == "file" {
let maybe_charset = let maybe_charset =
Some(text_encoding::detect_charset(&bytes).to_string()); Some(text_encoding::detect_charset(&bytes).to_string());
let content = Arc::new(get_source_from_bytes(bytes, maybe_charset).ok()?); let content = get_source_from_bytes(bytes, maybe_charset).ok()?;
Document::new( Document::new(
specifier.clone(), specifier.clone(),
fs_version, fs_version,
None, None,
content, content.into(),
maybe_resolver, maybe_resolver,
) )
} else { } else {
@ -677,12 +677,12 @@ impl FileSystemDocuments {
specifier_metadata.headers.get("content-type").cloned(); specifier_metadata.headers.get("content-type").cloned();
let maybe_headers = Some(&specifier_metadata.headers); let maybe_headers = Some(&specifier_metadata.headers);
let (_, maybe_charset) = map_content_type(specifier, maybe_content_type); let (_, maybe_charset) = map_content_type(specifier, maybe_content_type);
let content = Arc::new(get_source_from_bytes(bytes, maybe_charset).ok()?); let content = get_source_from_bytes(bytes, maybe_charset).ok()?;
Document::new( Document::new(
specifier.clone(), specifier.clone(),
fs_version, fs_version,
maybe_headers, maybe_headers,
content, content.into(),
maybe_resolver, maybe_resolver,
) )
}; };
@ -752,7 +752,7 @@ impl Documents {
specifier: ModuleSpecifier, specifier: ModuleSpecifier,
version: i32, version: i32,
language_id: LanguageId, language_id: LanguageId,
content: Arc<String>, content: Arc<str>,
) -> Document { ) -> Document {
let maybe_resolver = self.get_maybe_resolver(); let maybe_resolver = self.get_maybe_resolver();
let document = Document::open( let document = Document::open(
@ -1154,14 +1154,15 @@ mod tests {
let temp_dir = TempDir::new(); let temp_dir = TempDir::new();
let (mut documents, _) = setup(&temp_dir); let (mut documents, _) = setup(&temp_dir);
let specifier = ModuleSpecifier::parse("file:///a.ts").unwrap(); let specifier = ModuleSpecifier::parse("file:///a.ts").unwrap();
let content = Arc::new( let content = r#"import * as b from "./b.ts";
r#"import * as b from "./b.ts";
console.log(b); console.log(b);
"# "#;
.to_string(), let document = documents.open(
specifier,
1,
"javascript".parse().unwrap(),
content.into(),
); );
let document =
documents.open(specifier, 1, "javascript".parse().unwrap(), content);
assert!(document.is_open()); assert!(document.is_open());
assert!(document.is_diagnosable()); assert!(document.is_diagnosable());
} }
@ -1171,17 +1172,14 @@ console.log(b);
let temp_dir = TempDir::new(); let temp_dir = TempDir::new();
let (mut documents, _) = setup(&temp_dir); let (mut documents, _) = setup(&temp_dir);
let specifier = ModuleSpecifier::parse("file:///a.ts").unwrap(); let specifier = ModuleSpecifier::parse("file:///a.ts").unwrap();
let content = Arc::new( let content = r#"import * as b from "./b.ts";
r#"import * as b from "./b.ts";
console.log(b); console.log(b);
"# "#;
.to_string(),
);
documents.open( documents.open(
specifier.clone(), specifier.clone(),
1, 1,
"javascript".parse().unwrap(), "javascript".parse().unwrap(),
content, content.into(),
); );
documents documents
.change( .change(
@ -1204,7 +1202,7 @@ console.log(b);
) )
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
documents.get(&specifier).unwrap().content().as_str(), &*documents.get(&specifier).unwrap().content(),
r#"import * as b from "./b.ts"; r#"import * as b from "./b.ts";
console.log(b, "hello deno"); console.log(b, "hello deno");
"# "#
@ -1227,7 +1225,7 @@ console.log(b, "hello deno");
file_specifier.clone(), file_specifier.clone(),
1, 1,
LanguageId::TypeScript, LanguageId::TypeScript,
Default::default(), "".into(),
); );
// make a clone of the document store and close the document in that one // make a clone of the document store and close the document in that one

View file

@ -853,12 +853,11 @@ impl Inner {
params.text_document.language_id, params.text_document.uri params.text_document.language_id, params.text_document.uri
); );
} }
let content = Arc::new(params.text_document.text);
let document = self.documents.open( let document = self.documents.open(
specifier.clone(), specifier.clone(),
params.text_document.version, params.text_document.version,
params.text_document.language_id.parse().unwrap(), params.text_document.language_id.parse().unwrap(),
content, params.text_document.text.into(),
); );
self.performance.measure(mark); self.performance.measure(mark);
@ -1106,13 +1105,13 @@ impl Inner {
Some(Err(err)) => Err(anyhow!("{}", err)), Some(Err(err)) => Err(anyhow!("{}", err)),
None => { None => {
// it's not a js/ts file, so attempt to format its contents // it's not a js/ts file, so attempt to format its contents
format_file(&file_path, document.content().as_str(), &fmt_options) format_file(&file_path, &document.content(), &fmt_options)
} }
}; };
match format_result { match format_result {
Ok(Some(new_text)) => Some(text::get_edits( Ok(Some(new_text)) => Some(text::get_edits(
document.content().as_str(), &document.content(),
&new_text, &new_text,
document.line_index().as_ref(), document.line_index().as_ref(),
)), )),
@ -1931,7 +1930,7 @@ impl Inner {
.map(|span| { .map(|span| {
span.to_folding_range( span.to_folding_range(
asset_or_doc.line_index(), asset_or_doc.line_index(),
asset_or_doc.text().as_str().as_bytes(), asset_or_doc.text().as_bytes(),
self.config.client_capabilities.line_folding_only, self.config.client_capabilities.line_folding_only,
) )
}) })

View file

@ -2,8 +2,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use deno_ast::swc::common::BytePos;
use deno_ast::swc::common::Span;
use deno_ast::LineAndColumnIndex; use deno_ast::LineAndColumnIndex;
use deno_ast::ModuleSpecifier; use deno_ast::ModuleSpecifier;
use deno_ast::SourceTextInfo; use deno_ast::SourceTextInfo;
@ -42,7 +40,7 @@ use super::config::WorkspaceSettings;
#[derive(Debug)] #[derive(Debug)]
pub struct ReplCompletionItem { pub struct ReplCompletionItem {
pub new_text: String, pub new_text: String,
pub span: Span, pub range: std::ops::Range<usize>,
} }
pub struct ReplLanguageServer { pub struct ReplLanguageServer {
@ -113,12 +111,12 @@ impl ReplLanguageServer {
position: usize, position: usize,
) -> Vec<ReplCompletionItem> { ) -> Vec<ReplCompletionItem> {
self.did_change(line_text).await; self.did_change(line_text).await;
let before_line_len = BytePos(self.document_text.len() as u32);
let position = before_line_len + BytePos(position as u32);
let text_info = deno_ast::SourceTextInfo::from_string(format!( let text_info = deno_ast::SourceTextInfo::from_string(format!(
"{}{}", "{}{}",
self.document_text, self.pending_text self.document_text, self.pending_text
)); ));
let before_line_len = self.document_text.len();
let position = text_info.range().start + before_line_len + position;
let line_and_column = text_info.line_and_column_index(position); let line_and_column = text_info.line_and_column_index(position);
let response = self let response = self
.language_server .language_server
@ -158,24 +156,20 @@ impl ReplLanguageServer {
item.text_edit.and_then(|edit| match edit { item.text_edit.and_then(|edit| match edit {
CompletionTextEdit::Edit(edit) => Some(ReplCompletionItem { CompletionTextEdit::Edit(edit) => Some(ReplCompletionItem {
new_text: edit.new_text, new_text: edit.new_text,
span: lsp_range_to_span(&text_info, &edit.range), range: lsp_range_to_std_range(&text_info, &edit.range),
}), }),
CompletionTextEdit::InsertAndReplace(_) => None, CompletionTextEdit::InsertAndReplace(_) => None,
}) })
}) })
.filter(|item| { .filter(|item| {
// filter the results to only exact matches // filter the results to only exact matches
let text = &text_info.text_str() let text = &text_info.text_str()[item.range.clone()];
[item.span.lo.0 as usize..item.span.hi.0 as usize];
item.new_text.starts_with(text) item.new_text.starts_with(text)
}) })
.map(|mut item| { .map(|mut item| {
// convert back to a line position // convert back to a line position
item.span = Span::new( item.range.start -= before_line_len;
item.span.lo - before_line_len, item.range.end -= before_line_len;
item.span.hi - before_line_len,
Default::default(),
);
item item
}) })
.collect() .collect()
@ -251,18 +245,24 @@ impl ReplLanguageServer {
} }
} }
fn lsp_range_to_span(text_info: &SourceTextInfo, range: &Range) -> Span { fn lsp_range_to_std_range(
Span::new( text_info: &SourceTextInfo,
text_info.byte_index(LineAndColumnIndex { range: &Range,
) -> std::ops::Range<usize> {
let start_index = text_info
.loc_to_source_pos(LineAndColumnIndex {
line_index: range.start.line as usize, line_index: range.start.line as usize,
column_index: range.start.character as usize, column_index: range.start.character as usize,
}), })
text_info.byte_index(LineAndColumnIndex { .as_byte_index(text_info.range().start);
let end_index = text_info
.loc_to_source_pos(LineAndColumnIndex {
line_index: range.end.line as usize, line_index: range.end.line as usize,
column_index: range.end.character as usize, column_index: range.end.character as usize,
}), })
Default::default(), .as_byte_index(text_info.range().start);
)
start_index..end_index
} }
fn get_cwd_uri() -> Result<ModuleSpecifier, AnyError> { fn get_cwd_uri() -> Result<ModuleSpecifier, AnyError> {

View file

@ -3,9 +3,10 @@
use super::definitions::TestDefinition; use super::definitions::TestDefinition;
use deno_ast::swc::ast; use deno_ast::swc::ast;
use deno_ast::swc::common::Span;
use deno_ast::swc::visit::Visit; use deno_ast::swc::visit::Visit;
use deno_ast::swc::visit::VisitWith; use deno_ast::swc::visit::VisitWith;
use deno_ast::SourceRange;
use deno_ast::SourceRangedForSpanned;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
use std::collections::HashSet; use std::collections::HashSet;
@ -254,12 +255,12 @@ impl TestStepCollector {
fn add_step<N: AsRef<str>>( fn add_step<N: AsRef<str>>(
&mut self, &mut self,
name: N, name: N,
span: &Span, range: SourceRange,
steps: Option<Vec<TestDefinition>>, steps: Option<Vec<TestDefinition>>,
) { ) {
let step = TestDefinition::new_step( let step = TestDefinition::new_step(
name.as_ref().to_string(), name.as_ref().to_string(),
*span, range,
self.parent.clone(), self.parent.clone(),
self.level, self.level,
steps, steps,
@ -267,11 +268,11 @@ impl TestStepCollector {
self.steps.push(step); self.steps.push(step);
} }
fn check_call_expr(&mut self, node: &ast::CallExpr, span: &Span) { fn check_call_expr(&mut self, node: &ast::CallExpr, range: SourceRange) {
if let Some((name, steps)) = if let Some((name, steps)) =
check_call_expr(&self.parent, node, self.level + 1) check_call_expr(&self.parent, node, self.level + 1)
{ {
self.add_step(name, span, steps); self.add_step(name, range, steps);
} }
} }
@ -288,7 +289,7 @@ impl Visit for TestStepCollector {
// Identify calls to identified variables // Identify calls to identified variables
ast::Expr::Ident(ident) => { ast::Expr::Ident(ident) => {
if self.vars.contains(&ident.sym.to_string()) { if self.vars.contains(&ident.sym.to_string()) {
self.check_call_expr(node, &ident.span); self.check_call_expr(node, ident.range());
} }
} }
// Identify calls to `test.step()` // Identify calls to `test.step()`
@ -298,7 +299,7 @@ impl Visit for TestStepCollector {
if ns_prop_ident.sym.eq("step") { if ns_prop_ident.sym.eq("step") {
if let ast::Expr::Ident(ident) = member_expr.obj.as_ref() { if let ast::Expr::Ident(ident) = member_expr.obj.as_ref() {
if ident.sym == *test_context { if ident.sym == *test_context {
self.check_call_expr(node, &ns_prop_ident.span); self.check_call_expr(node, ns_prop_ident.range());
} }
} }
} }
@ -386,23 +387,23 @@ impl TestCollector {
fn add_definition<N: AsRef<str>>( fn add_definition<N: AsRef<str>>(
&mut self, &mut self,
name: N, name: N,
span: &Span, range: SourceRange,
steps: Option<Vec<TestDefinition>>, steps: Option<Vec<TestDefinition>>,
) { ) {
let definition = TestDefinition::new( let definition = TestDefinition::new(
&self.specifier, &self.specifier,
name.as_ref().to_string(), name.as_ref().to_string(),
*span, range,
steps, steps,
); );
self.definitions.push(definition); self.definitions.push(definition);
} }
fn check_call_expr(&mut self, node: &ast::CallExpr, span: &Span) { fn check_call_expr(&mut self, node: &ast::CallExpr, range: SourceRange) {
if let Some((name, steps)) = if let Some((name, steps)) =
check_call_expr(self.specifier.as_str(), node, 1) check_call_expr(self.specifier.as_str(), node, 1)
{ {
self.add_definition(name, span, steps); self.add_definition(name, range, steps);
} }
} }
@ -418,7 +419,7 @@ impl Visit for TestCollector {
match callee_expr.as_ref() { match callee_expr.as_ref() {
ast::Expr::Ident(ident) => { ast::Expr::Ident(ident) => {
if self.vars.contains(&ident.sym.to_string()) { if self.vars.contains(&ident.sym.to_string()) {
self.check_call_expr(node, &ident.span); self.check_call_expr(node, ident.range());
} }
} }
ast::Expr::Member(member_expr) => { ast::Expr::Member(member_expr) => {
@ -426,7 +427,7 @@ impl Visit for TestCollector {
if ns_prop_ident.sym.to_string() == "test" { if ns_prop_ident.sym.to_string() == "test" {
if let ast::Expr::Ident(ident) = member_expr.obj.as_ref() { if let ast::Expr::Ident(ident) = member_expr.obj.as_ref() {
if ident.sym.to_string() == "Deno" { if ident.sym.to_string() == "Deno" {
self.check_call_expr(node, &ns_prop_ident.span); self.check_call_expr(node, ns_prop_ident.range());
} }
} }
} }
@ -494,24 +495,20 @@ impl Visit for TestCollector {
#[cfg(test)] #[cfg(test)]
pub mod tests { pub mod tests {
use super::*; use super::*;
use deno_ast::swc::common::BytePos; use deno_ast::StartSourcePos;
use deno_ast::swc::common::SyntaxContext;
use deno_core::resolve_url; use deno_core::resolve_url;
use std::sync::Arc;
pub fn new_span(lo: u32, hi: u32, ctxt: u32) -> Span { pub fn new_range(start: usize, end: usize) -> SourceRange {
Span { SourceRange::new(
lo: BytePos(lo), StartSourcePos::START_SOURCE_POS + start,
hi: BytePos(hi), StartSourcePos::START_SOURCE_POS + end,
ctxt: SyntaxContext::from_u32(ctxt), )
}
} }
#[test] #[test]
fn test_test_collector() { fn test_test_collector() {
let specifier = resolve_url("file:///a/example.ts").unwrap(); let specifier = resolve_url("file:///a/example.ts").unwrap();
let source = Arc::new( let source = r#"
r#"
Deno.test({ Deno.test({
name: "test a", name: "test a",
async fn(t) { async fn(t) {
@ -535,13 +532,11 @@ pub mod tests {
const t = Deno.test; const t = Deno.test;
t("test d", () => {}); t("test d", () => {});
"# "#;
.to_string(),
);
let parsed_module = deno_ast::parse_module(deno_ast::ParseParams { let parsed_module = deno_ast::parse_module(deno_ast::ParseParams {
specifier: specifier.to_string(), specifier: specifier.to_string(),
source: deno_ast::SourceTextInfo::new(source), text_info: deno_ast::SourceTextInfo::new(source.into()),
media_type: deno_ast::MediaType::TypeScript, media_type: deno_ast::MediaType::TypeScript,
capture_tokens: true, capture_tokens: true,
scope_analysis: true, scope_analysis: true,
@ -557,19 +552,19 @@ pub mod tests {
id: "cf31850c831233526df427cdfd25b6b84b2af0d6ce5f8ee1d22c465234b46348".to_string(), id: "cf31850c831233526df427cdfd25b6b84b2af0d6ce5f8ee1d22c465234b46348".to_string(),
level: 0, level: 0,
name: "test a".to_string(), name: "test a".to_string(),
span: new_span(12, 16, 0), range: new_range(12, 16),
steps: Some(vec![ steps: Some(vec![
TestDefinition { TestDefinition {
id: "4c7333a1e47721631224408c467f32751fe34b876cab5ec1f6ac71980ff15ad3".to_string(), id: "4c7333a1e47721631224408c467f32751fe34b876cab5ec1f6ac71980ff15ad3".to_string(),
level: 1, level: 1,
name: "a step".to_string(), name: "a step".to_string(),
span: new_span(83, 87, 0), range: new_range(83, 87),
steps: Some(vec![ steps: Some(vec![
TestDefinition { TestDefinition {
id: "abf356f59139b77574089615f896a6f501c010985d95b8a93abeb0069ccb2201".to_string(), id: "abf356f59139b77574089615f896a6f501c010985d95b8a93abeb0069ccb2201".to_string(),
level: 2, level: 2,
name: "sub step".to_string(), name: "sub step".to_string(),
span: new_span(132, 136, 3), range: new_range(132, 136),
steps: None, steps: None,
} }
]) ])
@ -580,13 +575,13 @@ pub mod tests {
id: "86b4c821900e38fc89f24bceb0e45193608ab3f9d2a6019c7b6a5aceff5d7df2".to_string(), id: "86b4c821900e38fc89f24bceb0e45193608ab3f9d2a6019c7b6a5aceff5d7df2".to_string(),
level: 0, level: 0,
name: "useFnName".to_string(), name: "useFnName".to_string(),
span: new_span(254, 258, 0), range: new_range(254, 258),
steps: Some(vec![ steps: Some(vec![
TestDefinition { TestDefinition {
id: "67a390d0084ae5fb88f3510c470a72a553581f1d0d5ba5fa89aee7a754f3953a".to_string(), id: "67a390d0084ae5fb88f3510c470a72a553581f1d0d5ba5fa89aee7a754f3953a".to_string(),
level: 1, level: 1,
name: "step c".to_string(), name: "step c".to_string(),
span: new_span(313, 314, 4), range: new_range(313, 314),
steps: None, steps: None,
} }
]) ])
@ -595,21 +590,21 @@ pub mod tests {
id: "580eda89d7f5e619774c20e13b7d07a8e77c39cba101d60565144d48faa837cb".to_string(), id: "580eda89d7f5e619774c20e13b7d07a8e77c39cba101d60565144d48faa837cb".to_string(),
level: 0, level: 0,
name: "test b".to_string(), name: "test b".to_string(),
span: new_span(358, 362, 0), range: new_range(358, 362),
steps: None, steps: None,
}, },
TestDefinition { TestDefinition {
id: "0b7c6bf3cd617018d33a1bf982a08fe088c5bb54fcd5eb9e802e7c137ec1af94".to_string(), id: "0b7c6bf3cd617018d33a1bf982a08fe088c5bb54fcd5eb9e802e7c137ec1af94".to_string(),
level: 0, level: 0,
name: "test c".to_string(), name: "test c".to_string(),
span: new_span(420, 424, 1), range: new_range(420, 424),
steps: None, steps: None,
}, },
TestDefinition { TestDefinition {
id: "69d9fe87f64f5b66cb8b631d4fd2064e8224b8715a049be54276c42189ff8f9f".to_string(), id: "69d9fe87f64f5b66cb8b631d4fd2064e8224b8715a049be54276c42189ff8f9f".to_string(),
level: 0, level: 0,
name: "test d".to_string(), name: "test d".to_string(),
span: new_span(480, 481, 1), range: new_range(480, 481),
steps: None, steps: None,
} }
] ]

View file

@ -3,38 +3,21 @@
use super::lsp_custom; use super::lsp_custom;
use crate::checksum; use crate::checksum;
use crate::lsp::analysis::source_range_to_lsp_range;
use crate::lsp::client::TestingNotification; use crate::lsp::client::TestingNotification;
use deno_ast::swc::common::Span; use deno_ast::SourceRange;
use deno_ast::SourceTextInfo; use deno_ast::SourceTextInfo;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
use std::collections::HashMap; use std::collections::HashMap;
use tower_lsp::lsp_types as lsp; use tower_lsp::lsp_types as lsp;
fn span_to_range(
span: &Span,
source_text_info: &SourceTextInfo,
) -> Option<lsp::Range> {
let start = source_text_info.line_and_column_index(span.lo);
let end = source_text_info.line_and_column_index(span.hi);
Some(lsp::Range {
start: lsp::Position {
line: start.line_index as u32,
character: start.column_index as u32,
},
end: lsp::Position {
line: end.line_index as u32,
character: end.column_index as u32,
},
})
}
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct TestDefinition { pub struct TestDefinition {
pub id: String, pub id: String,
pub level: usize, pub level: usize,
pub name: String, pub name: String,
pub span: Span, pub range: SourceRange,
pub steps: Option<Vec<TestDefinition>>, pub steps: Option<Vec<TestDefinition>>,
} }
@ -42,7 +25,7 @@ impl TestDefinition {
pub fn new( pub fn new(
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
name: String, name: String,
span: Span, range: SourceRange,
steps: Option<Vec<TestDefinition>>, steps: Option<Vec<TestDefinition>>,
) -> Self { ) -> Self {
let id = checksum::gen(&[specifier.as_str().as_bytes(), name.as_bytes()]); let id = checksum::gen(&[specifier.as_str().as_bytes(), name.as_bytes()]);
@ -50,14 +33,14 @@ impl TestDefinition {
id, id,
level: 0, level: 0,
name, name,
span, range,
steps, steps,
} }
} }
pub fn new_step( pub fn new_step(
name: String, name: String,
span: Span, range: SourceRange,
parent: String, parent: String,
level: usize, level: usize,
steps: Option<Vec<TestDefinition>>, steps: Option<Vec<TestDefinition>>,
@ -71,7 +54,7 @@ impl TestDefinition {
id, id,
level, level,
name, name,
span, range,
steps, steps,
} }
} }
@ -89,7 +72,7 @@ impl TestDefinition {
.map(|step| step.as_test_data(source_text_info)) .map(|step| step.as_test_data(source_text_info))
.collect() .collect()
}), }),
range: span_to_range(&self.span, source_text_info), range: Some(source_range_to_lsp_range(&self.range, source_text_info)),
} }
} }

View file

@ -917,7 +917,7 @@ impl test::TestReporter for LspTestReporter {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::lsp::testing::collectors::tests::new_span; use crate::lsp::testing::collectors::tests::new_range;
#[test] #[test]
fn test_as_queue_and_filters() { fn test_as_queue_and_filters() {
@ -949,7 +949,7 @@ mod tests {
.to_string(), .to_string(),
level: 0, level: 0,
name: "test a".to_string(), name: "test a".to_string(),
span: new_span(420, 424, 1), range: new_range(420, 424),
steps: None, steps: None,
}; };
let test_def_b = TestDefinition { let test_def_b = TestDefinition {
@ -957,7 +957,7 @@ mod tests {
.to_string(), .to_string(),
level: 0, level: 0,
name: "test b".to_string(), name: "test b".to_string(),
span: new_span(480, 481, 1), range: new_range(480, 481),
steps: None, steps: None,
}; };
let test_definitions = TestDefinitions { let test_definitions = TestDefinitions {

View file

@ -117,7 +117,7 @@ impl TestServer {
test_definitions.as_notification( test_definitions.as_notification(
specifier, specifier,
mru.as_ref(), mru.as_ref(),
parsed_source.source(), parsed_source.text_info(),
), ),
); );
} }

View file

@ -154,7 +154,7 @@ impl TsServer {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct AssetDocumentInner { struct AssetDocumentInner {
specifier: ModuleSpecifier, specifier: ModuleSpecifier,
text: Arc<String>, text: Arc<str>,
line_index: Arc<LineIndex>, line_index: Arc<LineIndex>,
maybe_navigation_tree: Option<Arc<NavigationTree>>, maybe_navigation_tree: Option<Arc<NavigationTree>>,
} }
@ -169,7 +169,7 @@ impl AssetDocument {
let text = text.as_ref(); let text = text.as_ref();
Self(Arc::new(AssetDocumentInner { Self(Arc::new(AssetDocumentInner {
specifier, specifier,
text: Arc::new(text.to_string()), text: text.into(),
line_index: Arc::new(LineIndex::new(text)), line_index: Arc::new(LineIndex::new(text)),
maybe_navigation_tree: None, maybe_navigation_tree: None,
})) }))
@ -189,7 +189,7 @@ impl AssetDocument {
})) }))
} }
pub fn text(&self) -> Arc<String> { pub fn text(&self) -> Arc<str> {
self.0.text.clone() self.0.text.clone()
} }
@ -3114,7 +3114,7 @@ mod tests {
specifier.clone(), specifier.clone(),
*version, *version,
language_id.clone(), language_id.clone(),
Arc::new(source.to_string()), (*source).into(),
); );
} }
StateSnapshot { StateSnapshot {

View file

@ -636,7 +636,7 @@ async fn eval_command(
local: main_module.clone().to_file_path().unwrap(), local: main_module.clone().to_file_path().unwrap(),
maybe_types: None, maybe_types: None,
media_type: MediaType::Unknown, media_type: MediaType::Unknown,
source: Arc::new(String::from_utf8(source_code)?), source: String::from_utf8(source_code)?.into(),
specifier: main_module.clone(), specifier: main_module.clone(),
maybe_headers: None, maybe_headers: None,
}; };
@ -974,7 +974,7 @@ async fn run_from_stdin(flags: Flags) -> Result<i32, AnyError> {
local: main_module.clone().to_file_path().unwrap(), local: main_module.clone().to_file_path().unwrap(),
maybe_types: None, maybe_types: None,
media_type: MediaType::TypeScript, media_type: MediaType::TypeScript,
source: Arc::new(String::from_utf8(source)?), source: String::from_utf8(source)?.into(),
specifier: main_module.clone(), specifier: main_module.clone(),
maybe_headers: None, maybe_headers: None,
}; };

View file

@ -573,7 +573,7 @@ impl ProcState {
{ {
source.to_owned() source.to_owned()
} else { } else {
code.as_ref().clone() code.to_string()
} }
} }
MediaType::Dts => "".to_string(), MediaType::Dts => "".to_string(),

View file

@ -363,7 +363,7 @@ fn typescript_decorators() {
Some(vec![("NO_COLOR".to_owned(), "1".to_owned())]), Some(vec![("NO_COLOR".to_owned(), "1".to_owned())]),
false, false,
); );
assert!(out.ends_with("undefined\nundefined\n2\n")); assert!(out.ends_with("undefined\n[Function: Test]\n2\n"));
assert!(err.is_empty()); assert!(err.is_empty());
} }

View file

@ -1,5 +1,49 @@
[WILDCARD] [WILDCARD]
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { // deno-fmt-ignore-file
[WILDCARD] // deno-lint-ignore-file
new SomeClass[WILDCARD].test(); // This code was bundled using `deno bundle` and it's not recommended to edit it manually
var __decorate = this && this.__decorate || function(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
function a() {
console.log("a(): evaluated");
return (_target, _propertyKey, _descriptor)=>{
console.log("a(): called");
};
}
class B {
method() {
console.log("method");
}
}
__decorate([
a()
], B.prototype, "method", null);
var __decorate1 = this && this.__decorate || function(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
function Decorator() {
return function(target, propertyKey, descriptor) {
const originalFn = descriptor.value;
descriptor.value = async function(...args) {
return await originalFn.apply(this, args);
};
return descriptor;
};
}
class SomeClass {
async test() {}
}
__decorate1([
Decorator()
], SomeClass.prototype, "test", null);
new SomeClass().test();
new B().method();
[WILDCARD] [WILDCARD]

View file

@ -175,12 +175,12 @@ fn generate_coverage_report(
.map(|source_map| SourceMap::from_slice(source_map).unwrap()); .map(|source_map| SourceMap::from_slice(source_map).unwrap());
let text_lines = TextLines::new(script_source); let text_lines = TextLines::new(script_source);
let comment_spans = deno_ast::lex(script_source, MediaType::JavaScript) let comment_ranges = deno_ast::lex(script_source, MediaType::JavaScript)
.into_iter() .into_iter()
.filter(|item| { .filter(|item| {
matches!(item.inner, deno_ast::TokenOrComment::Comment { .. }) matches!(item.inner, deno_ast::TokenOrComment::Comment { .. })
}) })
.map(|item| item.span) .map(|item| item.range)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let url = Url::parse(&script_coverage.url).unwrap(); let url = Url::parse(&script_coverage.url).unwrap();
@ -267,9 +267,8 @@ fn generate_coverage_report(
for line_index in 0..text_lines.lines_count() { for line_index in 0..text_lines.lines_count() {
let line_start_offset = text_lines.line_start(line_index); let line_start_offset = text_lines.line_start(line_index);
let line_end_offset = text_lines.line_end(line_index); let line_end_offset = text_lines.line_end(line_index);
let ignore = comment_spans.iter().any(|span| { let ignore = comment_ranges.iter().any(|range| {
(span.lo.0 as usize) <= line_start_offset range.start <= line_start_offset && range.end >= line_end_offset
&& (span.hi.0 as usize) >= line_end_offset
}) || script_source[line_start_offset..line_end_offset] }) || script_source[line_start_offset..line_end_offset]
.trim() .trim()
.is_empty(); .is_empty();
@ -664,7 +663,7 @@ pub async fn cover_files(
| MediaType::Unknown | MediaType::Unknown
| MediaType::Cjs | MediaType::Cjs
| MediaType::Mjs | MediaType::Mjs
| MediaType::Json => file.source.as_ref().clone(), | MediaType::Json => file.source.as_ref().to_string(),
MediaType::Dts | MediaType::Dmts | MediaType::Dcts => "".to_string(), MediaType::Dts | MediaType::Dmts | MediaType::Dcts => "".to_string(),
MediaType::TypeScript MediaType::TypeScript
| MediaType::Jsx | MediaType::Jsx

View file

@ -122,7 +122,7 @@ pub async fn print_docs(
doc_parser.parse_source( doc_parser.parse_source(
&source_file_specifier, &source_file_specifier,
MediaType::Dts, MediaType::Dts,
Arc::new(get_types(ps.flags.unstable)), get_types(ps.flags.unstable).into(),
) )
} else { } else {
let module_specifier = resolve_url_or_path(&source_file)?; let module_specifier = resolve_url_or_path(&source_file)?;
@ -134,7 +134,7 @@ pub async fn print_docs(
local: PathBuf::from("./$deno$doc.ts"), local: PathBuf::from("./$deno$doc.ts"),
maybe_types: None, maybe_types: None,
media_type: MediaType::TypeScript, media_type: MediaType::TypeScript,
source: Arc::new(format!("export * from \"{}\";", module_specifier)), source: format!("export * from \"{}\";", module_specifier).into(),
specifier: root_specifier.clone(), specifier: root_specifier.clone(),
maybe_headers: None, maybe_headers: None,
}; };

View file

@ -179,7 +179,7 @@ impl Completer for EditorHelper {
if !lsp_completions.is_empty() { if !lsp_completions.is_empty() {
// assumes all lsp completions have the same start position // assumes all lsp completions have the same start position
return Ok(( return Ok((
lsp_completions[0].span.lo.0 as usize, lsp_completions[0].range.start,
lsp_completions.into_iter().map(|c| c.new_text).collect(), lsp_completions.into_iter().map(|c| c.new_text).collect(),
)); ));
} }
@ -302,43 +302,40 @@ impl Highlighter for EditorHelper {
// Adding color adds more bytes to the string, // Adding color adds more bytes to the string,
// so an offset is needed to stop spans falling out of sync. // so an offset is needed to stop spans falling out of sync.
let offset = out_line.len() - line.len(); let offset = out_line.len() - line.len();
let span = std::ops::Range { let range = item.range;
start: item.span.lo.0 as usize,
end: item.span.hi.0 as usize,
};
out_line.replace_range( out_line.replace_range(
span.start + offset..span.end + offset, range.start + offset..range.end + offset,
&match item.inner { &match item.inner {
deno_ast::TokenOrComment::Token(token) => match token { deno_ast::TokenOrComment::Token(token) => match token {
Token::Str { .. } | Token::Template { .. } | Token::BackQuote => { Token::Str { .. } | Token::Template { .. } | Token::BackQuote => {
colors::green(&line[span]).to_string() colors::green(&line[range]).to_string()
} }
Token::Regex(_, _) => colors::red(&line[span]).to_string(), Token::Regex(_, _) => colors::red(&line[range]).to_string(),
Token::Num { .. } | Token::BigInt { .. } => { Token::Num { .. } | Token::BigInt { .. } => {
colors::yellow(&line[span]).to_string() colors::yellow(&line[range]).to_string()
} }
Token::Word(word) => match word { Token::Word(word) => match word {
Word::True | Word::False | Word::Null => { Word::True | Word::False | Word::Null => {
colors::yellow(&line[span]).to_string() colors::yellow(&line[range]).to_string()
} }
Word::Keyword(_) => colors::cyan(&line[span]).to_string(), Word::Keyword(_) => colors::cyan(&line[range]).to_string(),
Word::Ident(ident) => { Word::Ident(ident) => {
if ident == *"undefined" { if ident == *"undefined" {
colors::gray(&line[span]).to_string() colors::gray(&line[range]).to_string()
} else if ident == *"Infinity" || ident == *"NaN" { } else if ident == *"Infinity" || ident == *"NaN" {
colors::yellow(&line[span]).to_string() colors::yellow(&line[range]).to_string()
} else if ident == *"async" || ident == *"of" { } else if ident == *"async" || ident == *"of" {
colors::cyan(&line[span]).to_string() colors::cyan(&line[range]).to_string()
} else { } else {
line[span].to_string() line[range].to_string()
} }
} }
}, },
_ => line[span].to_string(), _ => line[range].to_string(),
}, },
deno_ast::TokenOrComment::Comment { .. } => { deno_ast::TokenOrComment::Comment { .. } => {
colors::gray(&line[span]).to_string() colors::gray(&line[range]).to_string()
} }
}, },
); );

View file

@ -70,7 +70,7 @@ async fn read_eval_file(
.fetch(&specifier, &mut Permissions::allow_all()) .fetch(&specifier, &mut Permissions::allow_all())
.await?; .await?;
Ok((*file.source).clone()) Ok((*file.source).to_string())
} }
pub async fn run( pub async fn run(

View file

@ -332,7 +332,7 @@ impl ReplSession {
) -> Result<TsEvaluateResponse, AnyError> { ) -> Result<TsEvaluateResponse, AnyError> {
let parsed_module = deno_ast::parse_module(deno_ast::ParseParams { let parsed_module = deno_ast::parse_module(deno_ast::ParseParams {
specifier: "repl.ts".to_string(), specifier: "repl.ts".to_string(),
source: deno_ast::SourceTextInfo::from_string(expression.to_string()), text_info: deno_ast::SourceTextInfo::from_string(expression.to_string()),
media_type: deno_ast::MediaType::TypeScript, media_type: deno_ast::MediaType::TypeScript,
capture_tokens: false, capture_tokens: false,
maybe_syntax: None, maybe_syntax: None,

View file

@ -29,6 +29,7 @@ use crate::tools::coverage::CoverageCollector;
use deno_ast::swc::common::comments::CommentKind; use deno_ast::swc::common::comments::CommentKind;
use deno_ast::MediaType; use deno_ast::MediaType;
use deno_ast::SourceRangedForSpanned;
use deno_core::error::generic_error; use deno_core::error::generic_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::error::JsError; use deno_core::error::JsError;
@ -825,7 +826,7 @@ fn extract_files_from_regex_blocks(
local: file_specifier.to_file_path().unwrap(), local: file_specifier.to_file_path().unwrap(),
maybe_types: None, maybe_types: None,
media_type: file_media_type, media_type: file_media_type,
source: Arc::new(file_source), source: file_source.into(),
specifier: file_specifier, specifier: file_specifier,
maybe_headers: None, maybe_headers: None,
}) })
@ -837,12 +838,12 @@ fn extract_files_from_regex_blocks(
fn extract_files_from_source_comments( fn extract_files_from_source_comments(
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
source: Arc<String>, source: Arc<str>,
media_type: MediaType, media_type: MediaType,
) -> Result<Vec<File>, AnyError> { ) -> Result<Vec<File>, AnyError> {
let parsed_source = deno_ast::parse_module(deno_ast::ParseParams { let parsed_source = deno_ast::parse_module(deno_ast::ParseParams {
specifier: specifier.as_str().to_string(), specifier: specifier.as_str().to_string(),
source: deno_ast::SourceTextInfo::new(source), text_info: deno_ast::SourceTextInfo::new(source),
media_type, media_type,
capture_tokens: false, capture_tokens: false,
maybe_syntax: None, maybe_syntax: None,
@ -866,7 +867,7 @@ fn extract_files_from_source_comments(
specifier, specifier,
&comment.text, &comment.text,
media_type, media_type,
parsed_source.source().line_index(comment.span.lo), parsed_source.text_info().line_index(comment.start()),
&blocks_regex, &blocks_regex,
&lines_regex, &lines_regex,
) )

View file

@ -106,7 +106,7 @@ mod test {
maybe_syntax: None, maybe_syntax: None,
media_type: MediaType::TypeScript, media_type: MediaType::TypeScript,
scope_analysis: false, scope_analysis: false,
source: SourceTextInfo::from_string(text.to_string()), text_info: SourceTextInfo::from_string(text.to_string()),
}) })
.unwrap() .unwrap()
} }

View file

@ -120,7 +120,7 @@ fn visit_modules(
) { ) {
for module in modules { for module in modules {
let text_info = match &module.maybe_parsed_source { let text_info = match &module.maybe_parsed_source {
Some(source) => source.source(), Some(source) => source.text_info(),
None => continue, None => continue,
}; };
let source_text = match &module.maybe_source { let source_text = match &module.maybe_source {
@ -272,10 +272,8 @@ fn byte_range(
fn byte_index(text_info: &SourceTextInfo, pos: &Position) -> usize { fn byte_index(text_info: &SourceTextInfo, pos: &Position) -> usize {
// todo(https://github.com/denoland/deno_graph/issues/79): use byte indexes all the way down // todo(https://github.com/denoland/deno_graph/issues/79): use byte indexes all the way down
text_info text_info.loc_to_source_pos(LineAndColumnIndex {
.byte_index(LineAndColumnIndex {
line_index: pos.line, line_index: pos.line,
column_index: pos.character, column_index: pos.character,
}) }) - text_info.range().start
.0 as usize
} }

View file

@ -5,7 +5,6 @@ use std::collections::HashMap;
use std::collections::HashSet; use std::collections::HashSet;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc;
use deno_ast::ModuleSpecifier; use deno_ast::ModuleSpecifier;
use deno_core::anyhow::anyhow; use deno_core::anyhow::anyhow;
@ -97,7 +96,7 @@ impl Loader for TestLoader {
let result = self.files.get(specifier).map(|result| match result { let result = self.files.get(specifier).map(|result| match result {
Ok(result) => Ok(LoadResponse::Module { Ok(result) => Ok(LoadResponse::Module {
specifier: specifier.clone(), specifier: specifier.clone(),
content: Arc::new(result.0.clone()), content: result.0.clone().into(),
maybe_headers: result.1.clone(), maybe_headers: result.1.clone(),
}), }),
Err(err) => Err(err), Err(err) => Err(err),

View file

@ -121,7 +121,7 @@ pub fn get_asset(asset: &str) -> Option<&'static str> {
} }
fn get_maybe_hash( fn get_maybe_hash(
maybe_source: Option<&String>, maybe_source: Option<&str>,
hash_data: &[Vec<u8>], hash_data: &[Vec<u8>],
) -> Option<String> { ) -> Option<String> {
if let Some(source) = maybe_source { if let Some(source) = maybe_source {
@ -449,18 +449,19 @@ fn op_load(state: &mut OpState, args: Value) -> Result<Value, AnyError> {
.context("Error converting a string module specifier for \"op_load\".")?; .context("Error converting a string module specifier for \"op_load\".")?;
let mut hash: Option<String> = None; let mut hash: Option<String> = None;
let mut media_type = MediaType::Unknown; let mut media_type = MediaType::Unknown;
let graph_data = state.graph_data.read();
let data = if &v.specifier == "deno:///.tsbuildinfo" { let data = if &v.specifier == "deno:///.tsbuildinfo" {
state.maybe_tsbuildinfo.clone() state.maybe_tsbuildinfo.as_deref()
// in certain situations we return a "blank" module to tsc and we need to // in certain situations we return a "blank" module to tsc and we need to
// handle the request for that module here. // handle the request for that module here.
} else if &v.specifier == "deno:///missing_dependency.d.ts" { } else if &v.specifier == "deno:///missing_dependency.d.ts" {
hash = Some("1".to_string()); hash = Some("1".to_string());
media_type = MediaType::Dts; media_type = MediaType::Dts;
Some("declare const __: any;\nexport = __;\n".to_string()) Some("declare const __: any;\nexport = __;\n")
} else if v.specifier.starts_with("asset:///") { } else if v.specifier.starts_with("asset:///") {
let name = v.specifier.replace("asset:///", ""); let name = v.specifier.replace("asset:///", "");
let maybe_source = get_asset(&name).map(String::from); let maybe_source = get_asset(&name);
hash = get_maybe_hash(maybe_source.as_ref(), &state.hash_data); hash = get_maybe_hash(maybe_source, &state.hash_data);
media_type = MediaType::from(&v.specifier); media_type = MediaType::from(&v.specifier);
maybe_source maybe_source
} else { } else {
@ -473,7 +474,6 @@ fn op_load(state: &mut OpState, args: Value) -> Result<Value, AnyError> {
} else { } else {
specifier specifier
}; };
let graph_data = state.graph_data.read();
let maybe_source = if let Some(ModuleEntry::Module { let maybe_source = if let Some(ModuleEntry::Module {
code, code,
media_type: mt, media_type: mt,
@ -482,12 +482,12 @@ fn op_load(state: &mut OpState, args: Value) -> Result<Value, AnyError> {
graph_data.get(&graph_data.follow_redirect(&specifier)) graph_data.get(&graph_data.follow_redirect(&specifier))
{ {
media_type = *mt; media_type = *mt;
Some(code.as_ref().clone()) Some(code as &str)
} else { } else {
media_type = MediaType::Unknown; media_type = MediaType::Unknown;
None None
}; };
hash = get_maybe_hash(maybe_source.as_ref(), &state.hash_data); hash = get_maybe_hash(maybe_source, &state.hash_data);
maybe_source maybe_source
}; };
@ -752,7 +752,7 @@ mod tests {
Some(deno_graph::source::LoadResponse::Module { Some(deno_graph::source::LoadResponse::Module {
specifier: specifier.clone(), specifier: specifier.clone(),
maybe_headers: None, maybe_headers: None,
content: Arc::new(c), content: c.into(),
}) })
}) })
.map_err(|err| err.into()); .map_err(|err| err.into());

View file

@ -35,5 +35,5 @@ path = "examples/http_bench_json_ops.rs"
# These dependencies are only used for the 'http_bench_*_ops' examples. # These dependencies are only used for the 'http_bench_*_ops' examples.
[dev-dependencies] [dev-dependencies]
deno_ast = { version = "0.14.0", features = ["transpiling"] } deno_ast = { version = "0.15.0", features = ["transpiling"] }
tokio = { version = "1.17", features = ["full"] } tokio = { version = "1.17", features = ["full"] }

View file

@ -69,7 +69,7 @@ impl ModuleLoader for TypescriptModuleLoader {
let code = if should_transpile { let code = if should_transpile {
let parsed = deno_ast::parse_module(ParseParams { let parsed = deno_ast::parse_module(ParseParams {
specifier: module_specifier.to_string(), specifier: module_specifier.to_string(),
source: SourceTextInfo::from_string(code), text_info: SourceTextInfo::from_string(code),
media_type, media_type,
capture_tokens: false, capture_tokens: false,
scope_analysis: false, scope_analysis: false,