Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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 crates/flavor-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "flavor-cli"
version = "0.3.3"
version = "0.3.4"
edition = "2021"
license = "MIT"
description = "Personal check-only AST-backed code flavor lint CLI."
Expand Down
10 changes: 9 additions & 1 deletion crates/flavor-plugin-typescript/src/parser/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ impl<'a> Parser<'a> {
while !self.at(kind::END_OF_FILE) && !self.at_any(stops) {
let start = self.cursor;
match self.current() {
kind::LESS_THAN if self.jsx_enabled() && self.starts_jsx_open() => {
kind::LESS_THAN if self.jsx_enabled() && self.starts_jsx_expression_open() => {
self.parse_jsx_element();
}
kind::KEYWORD_AS | kind::KEYWORD_SATISFIES => {
self.parse_type_expression_tail(stops)
}
kind::KEYWORD_AWAIT => self.parse_await_expression(),
kind::KEYWORD_NEW => self.parse_new_expression(),
kind if is_expr_base(kind) && self.next_is(kind::OPEN_PAREN) => {
Expand Down Expand Up @@ -139,6 +142,11 @@ impl<'a> Parser<'a> {
self.builder.finish_node();
}

fn parse_type_expression_tail(&mut self, stops: &[Kind]) {
self.bump();
self.parse_type(stops);
}

fn parse_expression_operand(&mut self) {
match self.current() {
kind::KEYWORD_NEW => self.parse_new_expression(),
Expand Down
33 changes: 33 additions & 0 deletions crates/flavor-plugin-typescript/src/parser/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ impl<'a> Parser<'a> {
self.at(kind::LESS_THAN) && is_jsx_name_start(self.token_kind_at(1))
}

pub(super) fn starts_jsx_expression_open(&self) -> bool {
self.starts_jsx_open() && can_start_jsx_after(self.token_kind_at_back(1))
}

fn parse_jsx_opening(&mut self) -> Option<String> {
self.builder.start_node(kind::JSX_OPENING_ELEMENT);
self.bump();
Expand Down Expand Up @@ -209,3 +213,32 @@ fn is_jsx_name_part(kind: Kind) -> bool {
fn is_jsx_name_join(kind: Kind) -> bool {
matches!(kind, kind::DOT | kind::COLON | kind::MINUS)
}

fn can_start_jsx_after(kind: Kind) -> bool {
matches!(
kind,
kind::END_OF_FILE
| kind::OPEN_PAREN
| kind::OPEN_BRACKET
| kind::OPEN_BRACE
| kind::COMMA
| kind::SEMICOLON
| kind::COLON
| kind::QUESTION
| kind::EQUALS
| kind::ARROW
| kind::BANG
| kind::PLUS
| kind::MINUS
| kind::STAR
| kind::SLASH
| kind::PERCENT
| kind::AMPERSAND_AMPERSAND
| kind::PIPE_PIPE
| kind::QUESTION_QUESTION
| kind::KEYWORD_RETURN
| kind::KEYWORD_THROW
| kind::KEYWORD_CASE
| kind::KEYWORD_YIELD
)
}
26 changes: 24 additions & 2 deletions crates/flavor-plugin-typescript/tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,33 @@ mod tsx {

#[test]
fn comparison_stays_expression() {
let output = ts_output("const ok = left < right;");
let output = tsx_output(
"function inspectionLabelsOverlap(a: InspectionLabelBox, b: InspectionLabelBox) {
return a.left < b.right && a.right > b.left && a.top < b.bottom && a.bottom > b.top;
}",
);

assert!(has_node(&output, kind::BINARY_EXPRESSION));
assert!(has_node(&output, kind::MEMBER_EXPRESSION));
assert!(!has_node(&output, kind::JSX_ELEMENT));
assert!(output.diagnostics.is_empty());
assert!(output.diagnostics.is_empty(), "{:?}", output.diagnostics);
}

#[test]
fn satisfies_generics_stay_type() {
let output = tsx_output(
r#"type InspectDomain = "session" | "message";
type InspectDomainPanel = (props: InspectDomainPanelProps) => ReactNode;
const inspectDomainRegistry = {
message: MessageInspectDomain,
session: SessionInspectDomain,
} satisfies Record<InspectDomain, InspectDomainPanel>;"#,
);

assert!(has_node(&output, kind::BINARY_EXPRESSION));
assert!(has_node(&output, kind::TYPE_REFERENCE));
assert!(!has_node(&output, kind::JSX_ELEMENT));
assert!(output.diagnostics.is_empty(), "{:?}", output.diagnostics);
}
}

Expand Down