1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 16:19:12 -05: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 GitHub
parent e7c894e8f5
commit 1fcecb6789
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 365 additions and 314 deletions

143
Cargo.lock generated
View file

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

View file

@ -45,11 +45,11 @@ winapi = "=0.3.9"
winres = "=0.1.12"
[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_doc = "0.34.0"
deno_graph = "0.26.0"
deno_lint = { version = "0.29.0", features = ["docs"] }
deno_doc = "0.35.0"
deno_graph = "0.27.0"
deno_lint = { version = "0.30.0", features = ["docs"] }
deno_runtime = { version = "0.61.0", path = "../runtime" }
deno_task_shell = "0.3.0"
@ -64,10 +64,10 @@ data-url = "=0.1.1"
dissimilar = "=1.0.3"
dprint-plugin-json = "=0.15.2"
dprint-plugin-markdown = "=0.13.2"
dprint-plugin-typescript = "=0.68.2"
dprint-plugin-typescript = "=0.68.3"
encoding_rs = "=0.8.31"
env_logger = "=0.9.0"
eszip = "=0.19.0"
eszip = "=0.20.0"
fancy-regex = "=0.9.0"
http = "=0.2.6"
import_map = "=0.9.0"

View file

@ -11,7 +11,6 @@ use deno_core::url::Url;
use deno_core::JsRuntime;
use deno_core::ModuleSpecifier;
use once_cell::sync::Lazy;
use std::sync::Arc;
pub use esm_resolver::check_if_should_use_esm_loader;
pub use esm_resolver::NodeEsmResolver;
@ -172,7 +171,7 @@ pub async fn translate_cjs_to_esm(
) -> Result<String, AnyError> {
let parsed_source = deno_ast::parse_script(deno_ast::ParseParams {
specifier: specifier.to_string(),
source: deno_ast::SourceTextInfo::new(Arc::new(code)),
text_info: deno_ast::SourceTextInfo::new(code.into()),
media_type,
capture_tokens: true,
scope_analysis: false,
@ -203,7 +202,7 @@ pub async fn translate_cjs_to_esm(
{
let parsed_source = deno_ast::parse_script(deno_ast::ParseParams {
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,
capture_tokens: true,
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::Diagnostic;
use deno_ast::LineAndColumnDisplay;
use deno_ast::SourceRangedForSpanned;
use deno_core::anyhow::anyhow;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
@ -529,7 +530,7 @@ impl swc::bundler::Load for BundleLoader<'_> {
if let Some(m) = self.graph.get(specifier) {
let (fm, module) = transpile_module(
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,
self.emit_options,
self.cm.clone(),
@ -616,7 +617,7 @@ fn swc_err_to_diagnostic(
let location = source_map.lookup_char_pos(err.span().lo);
Diagnostic {
specifier: specifier.to_string(),
span: err.span(),
range: err.range(),
display_position: LineAndColumnDisplay {
line_number: location.line,
column_number: location.col_display + 1,
@ -669,7 +670,6 @@ pub fn bundle(
let emit_options: deno_ast::EmitOptions = options.ts_config.into();
let source_map_config = deno_ast::SourceMapConfig {
inline_sources: emit_options.inline_sources,
maybe_base: None,
};
let cm = Rc::new(swc::common::SourceMap::new(
@ -707,7 +707,11 @@ pub fn bundle(
let mut buf = 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(
cm.clone(),
"\n",

View file

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

View file

@ -36,7 +36,7 @@ pub fn contains_specifier(
#[allow(clippy::large_enum_variant)]
pub enum ModuleEntry {
Module {
code: Arc<String>,
code: Arc<str>,
dependencies: BTreeMap<String, Dependency>,
media_type: MediaType,
/// 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::get_configured_rules;
use deno_ast::SourceRange;
use deno_ast::SourceRangedForSpanned;
use deno_ast::SourceTextInfo;
use deno_core::anyhow::anyhow;
use deno_core::error::custom_error;
@ -468,8 +470,8 @@ impl CodeActionCollection {
// Get the end position of the comment.
let line = maybe_parsed_source
.unwrap()
.source()
.line_and_column_index(ignore_comment.span.hi());
.text_info()
.line_and_column_index(ignore_comment.end());
let position = lsp::Position {
line: line.line_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)]
mod tests {
use super::*;
@ -787,12 +807,12 @@ mod tests {
start: deno_lint::diagnostic::Position {
line_index: 0,
column_index: 2,
byte_pos: 23,
byte_index: 23,
},
end: deno_lint::diagnostic::Position {
line_index: 1,
column_index: 0,
byte_pos: 33,
byte_index: 33,
},
};
let actual = as_lsp_range(&fixture);

View file

@ -1,5 +1,6 @@
// 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::WorkspaceSettings;
use super::language_server;
@ -8,10 +9,11 @@ use super::tsc;
use super::tsc::NavigationTree;
use deno_ast::swc::ast;
use deno_ast::swc::common::Span;
use deno_ast::swc::visit::Visit;
use deno_ast::swc::visit::VisitWith;
use deno_ast::ParsedSource;
use deno_ast::SourceRange;
use deno_ast::SourceRangedForSpanned;
use deno_core::error::AnyError;
use deno_core::resolve_url;
use deno_core::serde::Deserialize;
@ -48,21 +50,6 @@ pub struct CodeLensData {
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 {
code_lenses: Vec<lsp::CodeLens>,
parsed_source: ParsedSource,
@ -80,8 +67,9 @@ impl DenoTestCollector {
}
}
fn add_code_lenses<N: AsRef<str>>(&mut self, name: N, span: &Span) {
let range = span_to_range(span, &self.parsed_source);
fn add_code_lenses<N: AsRef<str>>(&mut self, name: N, range: &SourceRange) {
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, "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()) {
match expr {
ast::Expr::Object(obj_lit) => {
@ -126,7 +114,7 @@ impl DenoTestCollector {
key_value_prop.value.as_ref()
{
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) => {
if let Some(ast::Ident { sym, .. }) = fn_expr.ident.as_ref() {
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)) => {
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() {
ast::Expr::Ident(ident) => {
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) => {
@ -169,7 +157,7 @@ impl Visit for DenoTestCollector {
if ns_prop_ident.sym.to_string() == "test" {
if let ast::Expr::Ident(ident) = member_expr.obj.as_ref() {
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]
fn test_deno_test_collector() {
let specifier = resolve_url("https://deno.land/x/mod.ts").unwrap();
let source = Arc::new(
r#"
let source = r#"
Deno.test({
name: "test a",
fn() {}
@ -567,12 +554,10 @@ mod tests {
Deno.test(function useFnName() {});
Deno.test("test b", function anotherTest() {});
"#
.to_string(),
);
"#;
let parsed_module = deno_ast::parse_module(deno_ast::ParseParams {
specifier: specifier.to_string(),
source: SourceTextInfo::new(source),
text_info: SourceTextInfo::new(source.into()),
media_type: MediaType::TypeScript,
capture_tokens: true,
scope_analysis: true,

View file

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

View file

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

View file

@ -81,12 +81,12 @@ impl deno_graph::SourceParser for SourceParser {
fn parse_module(
&self,
specifier: &ModuleSpecifier,
source: Arc<String>,
source: Arc<str>,
media_type: MediaType,
) -> Result<deno_ast::ParsedSource, deno_ast::Diagnostic> {
deno_ast::parse_module(deno_ast::ParseParams {
specifier: specifier.to_string(),
source: SourceTextInfo::new(source),
text_info: SourceTextInfo::new(source),
media_type,
capture_tokens: true,
scope_analysis: true,
@ -179,7 +179,7 @@ impl AssetOrDocument {
}
}
pub fn text(&self) -> Arc<String> {
pub fn text(&self) -> Arc<str> {
match self {
AssetOrDocument::Asset(a) => a.text(),
AssetOrDocument::Document(d) => d.0.text_info.text(),
@ -254,7 +254,7 @@ impl Document {
specifier: ModuleSpecifier,
fs_version: String,
maybe_headers: Option<&HashMap<String, String>>,
content: Arc<String>,
content: Arc<str>,
maybe_resolver: Option<&dyn deno_graph::source::Resolver>,
) -> Self {
let parser = SourceParser::default();
@ -293,7 +293,7 @@ impl Document {
specifier: ModuleSpecifier,
version: i32,
language_id: LanguageId,
content: Arc<String>,
content: Arc<str>,
maybe_resolver: Option<&dyn deno_graph::source::Resolver>,
) -> Self {
let maybe_headers = language_id.as_headers();
@ -352,7 +352,7 @@ impl Document {
index_valid = IndexValid::UpTo(0);
}
}
let content = Arc::new(content);
let content: Arc<str> = content.into();
let maybe_module = if self
.0
.maybe_language_id
@ -413,7 +413,7 @@ impl Document {
&self.0.specifier
}
pub fn content(&self) -> Arc<String> {
pub fn content(&self) -> Arc<str> {
self.0.text_info.text()
}
@ -661,12 +661,12 @@ impl FileSystemDocuments {
let doc = if specifier.scheme() == "file" {
let maybe_charset =
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(
specifier.clone(),
fs_version,
None,
content,
content.into(),
maybe_resolver,
)
} else {
@ -677,12 +677,12 @@ impl FileSystemDocuments {
specifier_metadata.headers.get("content-type").cloned();
let maybe_headers = Some(&specifier_metadata.headers);
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(
specifier.clone(),
fs_version,
maybe_headers,
content,
content.into(),
maybe_resolver,
)
};
@ -752,7 +752,7 @@ impl Documents {
specifier: ModuleSpecifier,
version: i32,
language_id: LanguageId,
content: Arc<String>,
content: Arc<str>,
) -> Document {
let maybe_resolver = self.get_maybe_resolver();
let document = Document::open(
@ -1154,14 +1154,15 @@ mod tests {
let temp_dir = TempDir::new();
let (mut documents, _) = setup(&temp_dir);
let specifier = ModuleSpecifier::parse("file:///a.ts").unwrap();
let content = Arc::new(
r#"import * as b from "./b.ts";
let content = r#"import * as b from "./b.ts";
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_diagnosable());
}
@ -1171,17 +1172,14 @@ console.log(b);
let temp_dir = TempDir::new();
let (mut documents, _) = setup(&temp_dir);
let specifier = ModuleSpecifier::parse("file:///a.ts").unwrap();
let content = Arc::new(
r#"import * as b from "./b.ts";
let content = r#"import * as b from "./b.ts";
console.log(b);
"#
.to_string(),
);
"#;
documents.open(
specifier.clone(),
1,
"javascript".parse().unwrap(),
content,
content.into(),
);
documents
.change(
@ -1204,7 +1202,7 @@ console.log(b);
)
.unwrap();
assert_eq!(
documents.get(&specifier).unwrap().content().as_str(),
&*documents.get(&specifier).unwrap().content(),
r#"import * as b from "./b.ts";
console.log(b, "hello deno");
"#
@ -1227,7 +1225,7 @@ console.log(b, "hello deno");
file_specifier.clone(),
1,
LanguageId::TypeScript,
Default::default(),
"".into(),
);
// 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
);
}
let content = Arc::new(params.text_document.text);
let document = self.documents.open(
specifier.clone(),
params.text_document.version,
params.text_document.language_id.parse().unwrap(),
content,
params.text_document.text.into(),
);
self.performance.measure(mark);
@ -1106,13 +1105,13 @@ impl Inner {
Some(Err(err)) => Err(anyhow!("{}", err)),
None => {
// 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 {
Ok(Some(new_text)) => Some(text::get_edits(
document.content().as_str(),
&document.content(),
&new_text,
document.line_index().as_ref(),
)),
@ -1931,7 +1930,7 @@ impl Inner {
.map(|span| {
span.to_folding_range(
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,
)
})

View file

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

View file

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

View file

@ -3,38 +3,21 @@
use super::lsp_custom;
use crate::checksum;
use crate::lsp::analysis::source_range_to_lsp_range;
use crate::lsp::client::TestingNotification;
use deno_ast::swc::common::Span;
use deno_ast::SourceRange;
use deno_ast::SourceTextInfo;
use deno_core::ModuleSpecifier;
use std::collections::HashMap;
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)]
pub struct TestDefinition {
pub id: String,
pub level: usize,
pub name: String,
pub span: Span,
pub range: SourceRange,
pub steps: Option<Vec<TestDefinition>>,
}
@ -42,7 +25,7 @@ impl TestDefinition {
pub fn new(
specifier: &ModuleSpecifier,
name: String,
span: Span,
range: SourceRange,
steps: Option<Vec<TestDefinition>>,
) -> Self {
let id = checksum::gen(&[specifier.as_str().as_bytes(), name.as_bytes()]);
@ -50,14 +33,14 @@ impl TestDefinition {
id,
level: 0,
name,
span,
range,
steps,
}
}
pub fn new_step(
name: String,
span: Span,
range: SourceRange,
parent: String,
level: usize,
steps: Option<Vec<TestDefinition>>,
@ -71,7 +54,7 @@ impl TestDefinition {
id,
level,
name,
span,
range,
steps,
}
}
@ -89,7 +72,7 @@ impl TestDefinition {
.map(|step| step.as_test_data(source_text_info))
.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)]
mod tests {
use super::*;
use crate::lsp::testing::collectors::tests::new_span;
use crate::lsp::testing::collectors::tests::new_range;
#[test]
fn test_as_queue_and_filters() {
@ -949,7 +949,7 @@ mod tests {
.to_string(),
level: 0,
name: "test a".to_string(),
span: new_span(420, 424, 1),
range: new_range(420, 424),
steps: None,
};
let test_def_b = TestDefinition {
@ -957,7 +957,7 @@ mod tests {
.to_string(),
level: 0,
name: "test b".to_string(),
span: new_span(480, 481, 1),
range: new_range(480, 481),
steps: None,
};
let test_definitions = TestDefinitions {

View file

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

View file

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

View file

@ -636,7 +636,7 @@ async fn eval_command(
local: main_module.clone().to_file_path().unwrap(),
maybe_types: None,
media_type: MediaType::Unknown,
source: Arc::new(String::from_utf8(source_code)?),
source: String::from_utf8(source_code)?.into(),
specifier: main_module.clone(),
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(),
maybe_types: None,
media_type: MediaType::TypeScript,
source: Arc::new(String::from_utf8(source)?),
source: String::from_utf8(source)?.into(),
specifier: main_module.clone(),
maybe_headers: None,
};

View file

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

View file

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

View file

@ -1,5 +1,49 @@
[WILDCARD]
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
[WILDCARD]
new SomeClass[WILDCARD].test();
// deno-fmt-ignore-file
// deno-lint-ignore-file
// 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]

View file

@ -175,12 +175,12 @@ fn generate_coverage_report(
.map(|source_map| SourceMap::from_slice(source_map).unwrap());
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()
.filter(|item| {
matches!(item.inner, deno_ast::TokenOrComment::Comment { .. })
})
.map(|item| item.span)
.map(|item| item.range)
.collect::<Vec<_>>();
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() {
let line_start_offset = text_lines.line_start(line_index);
let line_end_offset = text_lines.line_end(line_index);
let ignore = comment_spans.iter().any(|span| {
(span.lo.0 as usize) <= line_start_offset
&& (span.hi.0 as usize) >= line_end_offset
let ignore = comment_ranges.iter().any(|range| {
range.start <= line_start_offset && range.end >= line_end_offset
}) || script_source[line_start_offset..line_end_offset]
.trim()
.is_empty();
@ -664,7 +663,7 @@ pub async fn cover_files(
| MediaType::Unknown
| MediaType::Cjs
| 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::TypeScript
| MediaType::Jsx

View file

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

View file

@ -179,7 +179,7 @@ impl Completer for EditorHelper {
if !lsp_completions.is_empty() {
// assumes all lsp completions have the same start position
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(),
));
}
@ -302,43 +302,40 @@ impl Highlighter for EditorHelper {
// Adding color adds more bytes to the string,
// so an offset is needed to stop spans falling out of sync.
let offset = out_line.len() - line.len();
let span = std::ops::Range {
start: item.span.lo.0 as usize,
end: item.span.hi.0 as usize,
};
let range = item.range;
out_line.replace_range(
span.start + offset..span.end + offset,
range.start + offset..range.end + offset,
&match item.inner {
deno_ast::TokenOrComment::Token(token) => match token {
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 { .. } => {
colors::yellow(&line[span]).to_string()
colors::yellow(&line[range]).to_string()
}
Token::Word(word) => match word {
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) => {
if ident == *"undefined" {
colors::gray(&line[span]).to_string()
colors::gray(&line[range]).to_string()
} else if ident == *"Infinity" || ident == *"NaN" {
colors::yellow(&line[span]).to_string()
colors::yellow(&line[range]).to_string()
} else if ident == *"async" || ident == *"of" {
colors::cyan(&line[span]).to_string()
colors::cyan(&line[range]).to_string()
} else {
line[span].to_string()
line[range].to_string()
}
}
},
_ => line[span].to_string(),
_ => line[range].to_string(),
},
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())
.await?;
Ok((*file.source).clone())
Ok((*file.source).to_string())
}
pub async fn run(

View file

@ -332,7 +332,7 @@ impl ReplSession {
) -> Result<TsEvaluateResponse, AnyError> {
let parsed_module = deno_ast::parse_module(deno_ast::ParseParams {
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,
capture_tokens: false,
maybe_syntax: None,

View file

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

View file

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

View file

@ -120,7 +120,7 @@ fn visit_modules(
) {
for module in modules {
let text_info = match &module.maybe_parsed_source {
Some(source) => source.source(),
Some(source) => source.text_info(),
None => continue,
};
let source_text = match &module.maybe_source {
@ -272,10 +272,8 @@ fn byte_range(
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
text_info
.byte_index(LineAndColumnIndex {
line_index: pos.line,
column_index: pos.character,
})
.0 as usize
text_info.loc_to_source_pos(LineAndColumnIndex {
line_index: pos.line,
column_index: pos.character,
}) - text_info.range().start
}

View file

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

View file

@ -121,7 +121,7 @@ pub fn get_asset(asset: &str) -> Option<&'static str> {
}
fn get_maybe_hash(
maybe_source: Option<&String>,
maybe_source: Option<&str>,
hash_data: &[Vec<u8>],
) -> Option<String> {
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\".")?;
let mut hash: Option<String> = None;
let mut media_type = MediaType::Unknown;
let graph_data = state.graph_data.read();
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
// handle the request for that module here.
} else if &v.specifier == "deno:///missing_dependency.d.ts" {
hash = Some("1".to_string());
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:///") {
let name = v.specifier.replace("asset:///", "");
let maybe_source = get_asset(&name).map(String::from);
hash = get_maybe_hash(maybe_source.as_ref(), &state.hash_data);
let maybe_source = get_asset(&name);
hash = get_maybe_hash(maybe_source, &state.hash_data);
media_type = MediaType::from(&v.specifier);
maybe_source
} else {
@ -473,7 +474,6 @@ fn op_load(state: &mut OpState, args: Value) -> Result<Value, AnyError> {
} else {
specifier
};
let graph_data = state.graph_data.read();
let maybe_source = if let Some(ModuleEntry::Module {
code,
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))
{
media_type = *mt;
Some(code.as_ref().clone())
Some(code as &str)
} else {
media_type = MediaType::Unknown;
None
};
hash = get_maybe_hash(maybe_source.as_ref(), &state.hash_data);
hash = get_maybe_hash(maybe_source, &state.hash_data);
maybe_source
};
@ -752,7 +752,7 @@ mod tests {
Some(deno_graph::source::LoadResponse::Module {
specifier: specifier.clone(),
maybe_headers: None,
content: Arc::new(c),
content: c.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.
[dev-dependencies]
deno_ast = { version = "0.14.0", features = ["transpiling"] }
deno_ast = { version = "0.15.0", features = ["transpiling"] }
tokio = { version = "1.17", features = ["full"] }

View file

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