Skip to content

Commit

Permalink
feature(rome_js_parser): JSX support in .js files (rome#2674)
Browse files Browse the repository at this point in the history
Enable JSX by default for all javascript files (not typescript)
  • Loading branch information
Nicholas Yang authored and IWANABETHATGUY committed Jun 15, 2022
1 parent 042e65d commit 2b08299
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 220 deletions.
2 changes: 1 addition & 1 deletion crates/rome_analyze/tests/specs/flipBinExp.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const a = b ** c;

const a = b << c;
const a = b >> c;
const a = b <<< c;
const a = b >>> c;
const a = b & c;
const a = b | c;
const a = b ^ c;
10 changes: 4 additions & 6 deletions crates/rome_analyze/tests/specs/flipBinExp.js.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
source: crates/rome_analyze/tests/spec_tests.rs
assertion_line: 92
expression: flipBinExp.js

---
# Input
```js
Expand Down Expand Up @@ -32,7 +30,7 @@ const a = b ** c;
const a = b << c;
const a = b >> c;
const a = b <<< c;
const a = b >>> c;
const a = b & c;
const a = b | c;
const a = b ^ c;
Expand Down Expand Up @@ -170,7 +168,7 @@ const a = b ^ c;
| @@ -25,6 +25,6 @@
24 24 | const a = b << c;
25 25 | const a = b >> c;
26 26 | const a = b <<< c;
26 26 | const a = b >>> c;
27 | - const a = b & c;
27 | + const a = c & b;
28 28 | const a = b | c;
Expand All @@ -181,7 +179,7 @@ const a = b ^ c;
```
| @@ -26,5 +26,5 @@
25 25 | const a = b >> c;
26 26 | const a = b <<< c;
26 26 | const a = b >>> c;
27 27 | const a = b & c;
28 | - const a = b | c;
28 | + const a = c | b;
Expand All @@ -191,7 +189,7 @@ const a = b ^ c;
```
| @@ -27,4 +27,4 @@
26 26 | const a = b <<< c;
26 26 | const a = b >>> c;
27 27 | const a = b & c;
28 28 | const a = b | c;
29 | - const a = b ^ c;
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_js_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl<'src> Lexer<'src> {
ReLexContext::BinaryOperator => self.re_lex_binary_operator(),
ReLexContext::TypeArgumentLessThan => self.re_lex_type_argument_less_than(),
ReLexContext::JsxIdentifier => self.re_lex_jsx_identifier(old_position),
ReLexContext::JsxChild => self.lex_jsx_child_token(),
ReLexContext::JsxChild if !self.is_eof() => self.lex_jsx_child_token(),
_ => self.current(),
};

Expand Down
5 changes: 3 additions & 2 deletions crates/rome_js_parser/src/syntax/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ use super::typescript::parse_ts_type_arguments;
// <div />

// test_err jsx_or_type_assertion
// // SCRIPT
// function f() {
// let a = <div>a</div>; // JSX
// let b = <string>b; //type assertion
// let b = <string>b; // type assertion
// let c = <string>b<a>d; // type assertion
// let d = <div>a</div>/; // ambigous: JSX or "type assertion a less than regex /div>/". Probably JSX.
// let d = <div>a</div>/; // ambiguous: JSX or "type assertion a less than regex /div>/". Probably JSX.
// let d = <string>a</string>/;
// }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// SCRIPT
function f() {
let a = <div>a</div>; // JSX
let b = <string>b; //type assertion
let b = <string>b; // type assertion
let c = <string>b<a>d; // type assertion
let d = <div>a</div>/; // ambigous: JSX or "type assertion a less than regex /div>/". Probably JSX.
let d = <div>a</div>/; // ambiguous: JSX or "type assertion a less than regex /div>/". Probably JSX.
let d = <string>a</string>/;
}
413 changes: 207 additions & 206 deletions crates/rome_js_parser/test_data/inline/err/jsx_or_type_assertion.rast

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions crates/rome_js_syntax/src/source_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,8 @@ fn compute_source_type_from_path_or_extension(
SourceType::d_ts().with_module_kind(ModuleKind::Script)
} else {
match extension {
"js" | "mjs" => SourceType::js_module(),
"cjs" => SourceType::js_module().with_module_kind(ModuleKind::Script),
"jsx" => SourceType::jsx(),
"js" | "mjs" | "jsx" => SourceType::jsx(),
"ts" | "mts" => SourceType::ts(),
"cts" => SourceType::ts().with_module_kind(ModuleKind::Script),
"tsx" => SourceType::tsx(),
Expand Down

0 comments on commit 2b08299

Please sign in to comment.