From ece0cff2797daa5d3cf5d565d2dd85b7f6e63ab6 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 8 Jan 2024 14:31:43 -0500 Subject: [PATCH] fix: handle override keyword for auto accessors (#597) --- Cargo.lock | 53 +++++++++---------- Cargo.toml | 2 +- dprint.json | 8 +-- rust-toolchain.toml | 2 +- src/generation/generate.rs | 13 +++-- src/utils/char_iterator.rs | 32 ++++++----- src/utils/file_text_has_ignore_comment.rs | 8 ++- src/utils/mod.rs | 1 - .../class/auto_accessor/AutoAccessor_All.txt | 2 + 9 files changed, 63 insertions(+), 58 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8c22cc18..e7d85a09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,11 +44,10 @@ checksum = "8c6f84b74db2535ebae81eede2f39b947dcbf01d093ae5f791e5dd414a1bf289" [[package]] name = "ast_node" -version = "0.9.5" +version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c09c69dffe06d222d072c878c3afe86eee2179806f20503faec97250268b4c24" +checksum = "c3e3e06ec6ac7d893a0db7127d91063ad7d9da8988f8a1a256f03729e6eec026" dependencies = [ - "pmutil", "proc-macro2", "quote", "swc_macros_common", @@ -138,9 +137,9 @@ dependencies = [ [[package]] name = "deno_ast" -version = "0.31.6" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da7b09db895527a94de1305455338926cd2a7003231ba589b7b7b57e8da344f2" +checksum = "0d19e5fe5dfe61c87726fdf404babc2369adfec28d5a83adee7ef06f6ce0ec68" dependencies = [ "deno_media_type", "dprint-swc-ext", @@ -263,11 +262,10 @@ dependencies = [ [[package]] name = "from_variant" -version = "0.1.6" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03ec5dc38ee19078d84a692b1c41181ff9f94331c76cee66ff0208c770b5e54f" +checksum = "3a0b11eeb173ce52f84ebd943d42e58813a2ebb78a6a3ff0a243b71c5199cd7b" dependencies = [ - "pmutil", "proc-macro2", "swc_macros_common", "syn", @@ -669,11 +667,10 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "string_enum" -version = "0.4.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fa4d4f81d7c05b9161f8de839975d3326328b8ba2831164b465524cc2f55252" +checksum = "1b650ea2087d32854a0f20b837fc56ec987a1cb4f758c9757e1171ee9812da63" dependencies = [ - "pmutil", "proc-macro2", "quote", "swc_macros_common", @@ -682,9 +679,9 @@ dependencies = [ [[package]] name = "swc_atoms" -version = "0.6.4" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8a9e1b6d97f27b6abe5571f8fe3bdbd2fa987299fc2126450c7cde6214896ef" +checksum = "7d538eaaa6f085161d088a04cf0a3a5a52c5a7f2b3bd9b83f73f058b0ed357c0" dependencies = [ "hstr", "once_cell", @@ -694,9 +691,9 @@ dependencies = [ [[package]] name = "swc_common" -version = "0.33.9" +version = "0.33.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ccb656cd57c93614e4e8b33a60e75ca095383565c1a8d2bbe6a1103942831e0" +checksum = "9b3ae36feceded27f0178dc9dabb49399830847ffb7f866af01798844de8f973" dependencies = [ "ast_node", "better_scoped_tls", @@ -719,9 +716,9 @@ dependencies = [ [[package]] name = "swc_ecma_ast" -version = "0.110.10" +version = "0.110.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c3d416121da2d56bcbd1b1623725a68890af4552fef0c6d1e4bfa92776ccd6a" +checksum = "79401a45da704f4fb2552c5bf86ee2198e8636b121cb81f8036848a300edd53b" dependencies = [ "bitflags", "is-macro", @@ -737,9 +734,9 @@ dependencies = [ [[package]] name = "swc_ecma_parser" -version = "0.141.26" +version = "0.141.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9590deff1b29aafbff8901b9d38d00211393f6b17b5cab878562db89a8966d88" +checksum = "c4d17401dd95048a6a62b777d533c0999dabdd531ef9d667e22f8ae2a2a0d294" dependencies = [ "either", "new_debug_unreachable", @@ -759,11 +756,10 @@ dependencies = [ [[package]] name = "swc_eq_ignore_macros" -version = "0.1.2" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05a95d367e228d52484c53336991fdcf47b6b553ef835d9159db4ba40efb0ee8" +checksum = "695a1d8b461033d32429b5befbf0ad4d7a2c4d6ba9cd5ba4e0645c615839e8e4" dependencies = [ - "pmutil", "proc-macro2", "quote", "syn", @@ -771,11 +767,10 @@ dependencies = [ [[package]] name = "swc_macros_common" -version = "0.3.8" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a273205ccb09b51fabe88c49f3b34c5a4631c4c00a16ae20e03111d6a42e832" +checksum = "50176cfc1cbc8bb22f41c6fe9d1ec53fbe057001219b5954961b8ad0f336fce9" dependencies = [ - "pmutil", "proc-macro2", "quote", "syn", @@ -783,9 +778,9 @@ dependencies = [ [[package]] name = "swc_visit" -version = "0.5.7" +version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e87c337fbb2d191bf371173dea6a957f01899adb8f189c6c31b122a6cfc98fc3" +checksum = "b27078d8571abe23aa52ef608dd1df89096a37d867cf691cbb4f4c392322b7c9" dependencies = [ "either", "swc_visit_macros", @@ -793,9 +788,9 @@ dependencies = [ [[package]] name = "swc_visit_macros" -version = "0.5.8" +version = "0.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f322730fb82f3930a450ac24de8c98523af7d34ab8cb2f46bcb405839891a99" +checksum = "fa8bb05975506741555ea4d10c3a3bdb0e2357cd58e1a4a4332b8ebb4b44c34d" dependencies = [ "Inflector", "pmutil", diff --git a/Cargo.toml b/Cargo.toml index ac89c093..0313b363 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ tracing = ["dprint-core/tracing"] [dependencies] anyhow = "1.0.64" -deno_ast = { version = "0.31.6", features = ["view"] } +deno_ast = { version = "1.0.0", features = ["view"] } dprint-core = { version = "0.63.3", features = ["formatting"] } rustc-hash = "1.1.0" serde = { version = "1.0.144", features = ["derive"] } diff --git a/dprint.json b/dprint.json index a45a41fc..3efc062c 100644 --- a/dprint.json +++ b/dprint.json @@ -12,10 +12,10 @@ "**/target" ], "plugins": [ - "https://plugins.dprint.dev/typescript-0.86.1.wasm", - "https://plugins.dprint.dev/json-0.17.4.wasm", - "https://plugins.dprint.dev/markdown-0.15.3.wasm", + "https://plugins.dprint.dev/typescript-0.88.7.wasm", + "https://plugins.dprint.dev/json-0.19.1.wasm", + "https://plugins.dprint.dev/markdown-0.16.3.wasm", "https://plugins.dprint.dev/toml-0.5.4.wasm", - "https://plugins.dprint.dev/exec-0.4.3.json@42343548b8022c99b1d750be6b894fe6b6c7ee25f72ae9f9082226dd2e515072" + "https://plugins.dprint.dev/exec-0.4.4.json@c207bf9b9a4ee1f0ecb75c594f774924baf62e8e53a2ce9d873816a408cecbf7" ] } diff --git a/rust-toolchain.toml b/rust-toolchain.toml index a3558fd8..4739bf10 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] -channel = "1.74.0" +channel = "1.75.0" components = ["clippy"] diff --git a/src/generation/generate.rs b/src/generation/generate.rs index 220b815d..2554f84b 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -472,7 +472,12 @@ fn gen_auto_accessor<'a>(node: &AutoAccessor<'a>, context: &mut Context<'a>) -> accessibility: node.accessibility(), is_abstract: false, is_optional: false, - is_override: false, + // todo: https://github.com/swc-project/swc/issues/8344 + is_override: node + .tokens_fast(context.program) + .iter() + .take_while(|t| t.start() < node.key.start()) + .any(|t| t.span.text_fast(context.program) == "override"), readonly: false, // todo: https://github.com/swc-project/swc/issues/8344 definite: node.type_ann.is_some() && node.key.next_token_fast(context.program).is_some_and(|t| t.token == Token::Bang), @@ -635,9 +640,6 @@ fn gen_class_prop_common<'a, 'b>(node: GenClassPropCommon<'a, 'b>, context: &mut if node.is_static { items.push_str("static "); } - if node.is_auto_accessor { - items.push_str("accessor "); - } if node.is_abstract { items.push_str("abstract "); } @@ -647,6 +649,9 @@ fn gen_class_prop_common<'a, 'b>(node: GenClassPropCommon<'a, 'b>, context: &mut if node.readonly { items.push_str("readonly "); } + if node.is_auto_accessor { + items.push_str("accessor "); + } items.extend(if node.computed { let inner_key_node = match node.key { Node::ComputedPropName(prop) => prop.expr.as_node(), diff --git a/src/utils/char_iterator.rs b/src/utils/char_iterator.rs index 4cc3fdbe..660e00e6 100644 --- a/src/utils/char_iterator.rs +++ b/src/utils/char_iterator.rs @@ -1,10 +1,22 @@ use std::iter::Peekable; use std::str::Chars; -pub trait IteratorCharExt: Iterator { - fn peek(&mut self) -> Option<&char>; +pub struct IteratorCharExt<'a>(Peekable>); - fn check_text(&mut self, text: &str) -> bool { +impl<'a> IteratorCharExt<'a> { + pub fn new(chars: Peekable>) -> Self { + Self(chars) + } + + pub fn next(&mut self) -> Option { + self.0.next() + } + + pub fn peek(&mut self) -> Option<&char> { + self.0.peek() + } + + pub fn check_text(&mut self, text: &str) -> bool { for text_ch in text.chars() { match self.peek() { Some(ch) if *ch == text_ch => self.next(), @@ -14,19 +26,19 @@ pub trait IteratorCharExt: Iterator { true } - fn skip_whitespace(&mut self) { + pub fn skip_whitespace(&mut self) { self.skip_while(char::is_whitespace); } - fn skip_spaces(&mut self) { + pub fn skip_spaces(&mut self) { self.skip_while(|c| c == ' '); } - fn skip_all_until_new_line(&mut self) { + pub fn skip_all_until_new_line(&mut self) { self.skip_while(|c| c != '\n'); } - fn skip_while bool>(&mut self, predicate: P) { + pub fn skip_while bool>(&mut self, predicate: P) { while let Some(ch) = self.peek() { if !predicate(*ch) { break; @@ -35,9 +47,3 @@ pub trait IteratorCharExt: Iterator { } } } - -impl<'a> IteratorCharExt for Peekable> { - fn peek(&mut self) -> Option<&char> { - self.peek() - } -} diff --git a/src/utils/file_text_has_ignore_comment.rs b/src/utils/file_text_has_ignore_comment.rs index f2423365..e680f288 100644 --- a/src/utils/file_text_has_ignore_comment.rs +++ b/src/utils/file_text_has_ignore_comment.rs @@ -1,10 +1,8 @@ use crate::utils::char_iterator::IteratorCharExt; use std::iter::Iterator; -use std::iter::Peekable; -use std::str::Chars; pub fn file_text_has_ignore_comment(file_text: &str, ignore_text: &str) -> bool { - let mut chars = file_text.chars().peekable(); + let mut chars = IteratorCharExt::new(file_text.chars().peekable()); // skip over the shebang if file_text.starts_with("#!") { @@ -35,7 +33,7 @@ pub fn file_text_has_ignore_comment(file_text: &str, ignore_text: &str) -> bool } return false; - fn check_single_line_comment(chars: &mut Peekable, ignore_text: &str) -> bool { + fn check_single_line_comment(chars: &mut IteratorCharExt, ignore_text: &str) -> bool { chars.skip_spaces(); // only spaces, not whitespace if chars.check_text(ignore_text) { return true; @@ -44,7 +42,7 @@ pub fn file_text_has_ignore_comment(file_text: &str, ignore_text: &str) -> bool false } - fn check_multi_line_comment(chars: &mut Peekable, ignore_text: &str) -> bool { + fn check_multi_line_comment(chars: &mut IteratorCharExt, ignore_text: &str) -> bool { chars.skip_whitespace(); if chars.check_text(ignore_text) { return true; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 8354ee78..a471637e 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -5,7 +5,6 @@ mod stack; mod string_utils; mod vec_map; -pub use char_iterator::*; pub use file_text_has_ignore_comment::*; pub use is_prefix_semi_colon_insertion_char::*; pub use stack::*; diff --git a/tests/specs/declarations/class/auto_accessor/AutoAccessor_All.txt b/tests/specs/declarations/class/auto_accessor/AutoAccessor_All.txt index fbe51329..1bdda02a 100644 --- a/tests/specs/declarations/class/auto_accessor/AutoAccessor_All.txt +++ b/tests/specs/declarations/class/auto_accessor/AutoAccessor_All.txt @@ -7,6 +7,7 @@ class Person { private accessor test3 : string; accessor #test = 2; accessor here!: string; + override accessor test4: string; constructor(name: string) { this.name = name; @@ -21,6 +22,7 @@ class Person { private accessor test3: string; accessor #test = 2; accessor here!: string; + override accessor test4: string; constructor(name: string) { this.name = name;