Skip to content

Commit

Permalink
fix: handle override keyword for auto accessors (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jan 8, 2024
1 parent 46f10fd commit ece0cff
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 58 deletions.
53 changes: 24 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
8 changes: 4 additions & 4 deletions dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.74.0"
channel = "1.75.0"
components = ["clippy"]
13 changes: 9 additions & 4 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 ");
}
Expand All @@ -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(),
Expand Down
32 changes: 19 additions & 13 deletions src/utils/char_iterator.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
use std::iter::Peekable;
use std::str::Chars;

pub trait IteratorCharExt: Iterator<Item = char> {
fn peek(&mut self) -> Option<&char>;
pub struct IteratorCharExt<'a>(Peekable<Chars<'a>>);

fn check_text(&mut self, text: &str) -> bool {
impl<'a> IteratorCharExt<'a> {
pub fn new(chars: Peekable<Chars<'a>>) -> Self {
Self(chars)
}

pub fn next(&mut self) -> Option<char> {
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(),
Expand All @@ -14,19 +26,19 @@ pub trait IteratorCharExt: Iterator<Item = char> {
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<P: Fn(char) -> bool>(&mut self, predicate: P) {
pub fn skip_while<P: Fn(char) -> bool>(&mut self, predicate: P) {
while let Some(ch) = self.peek() {
if !predicate(*ch) {
break;
Expand All @@ -35,9 +47,3 @@ pub trait IteratorCharExt: Iterator<Item = char> {
}
}
}

impl<'a> IteratorCharExt for Peekable<Chars<'a>> {
fn peek(&mut self) -> Option<&char> {
self.peek()
}
}
8 changes: 3 additions & 5 deletions src/utils/file_text_has_ignore_comment.rs
Original file line number Diff line number Diff line change
@@ -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("#!") {
Expand Down Expand Up @@ -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<Chars>, 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;
Expand All @@ -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<Chars>, 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;
Expand Down
1 change: 0 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit ece0cff

Please sign in to comment.