Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parser): better control on JSX parsing #4718

Merged
merged 3 commits into from
Dec 12, 2024
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
11 changes: 11 additions & 0 deletions .changeset/add_new_option_javascriptjsxeverywhere.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
cli: minor
---

# Add new option `javascript.parser.jsxEverywhere`

This new option allows to control whether Biome should expect JSX syntax in `.js`/`.ts` files.

When `jsxEverywhere` is set to `false`, having JSX syntax like `<div></div>` inside `.js`/`.ts` files will result in a **parsing error**.

This option defaults to `true`.
25 changes: 18 additions & 7 deletions crates/biome_configuration/src/javascript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct JavascriptConfiguration {
pub assists: JavascriptAssists,

/// Parsing options
#[partial(type, bpaf(external(partial_javascript_parser), optional))]
#[partial(type, bpaf(pure(Default::default()), optional))]
pub parser: JavascriptParser,

/// A list of global bindings that should be ignored by the analyzers
Expand All @@ -39,32 +39,43 @@ pub struct JavascriptConfiguration {
#[partial(bpaf(pure(Default::default()), hide))]
pub jsx_runtime: JsxRuntime,

#[partial(type, bpaf(external(partial_javascript_organize_imports), optional))]
#[partial(type, bpaf(pure(Default::default()), optional))]
pub organize_imports: JavascriptOrganizeImports,
}

#[derive(Clone, Debug, Default, Deserialize, Eq, Partial, PartialEq, Serialize)]
#[partial(derive(Bpaf, Clone, Deserializable, Eq, Merge, PartialEq))]
#[partial(derive(Clone, Deserializable, Eq, Merge, PartialEq))]
#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))]
#[partial(serde(default, deny_unknown_fields))]
pub struct JavascriptOrganizeImports {}

/// Options that changes how the JavaScript parser behaves
#[derive(Clone, Debug, Default, Deserialize, Eq, Partial, PartialEq, Serialize)]
#[partial(derive(Bpaf, Clone, Deserializable, Eq, Merge, PartialEq))]
#[derive(Clone, Debug, Deserialize, Eq, Partial, PartialEq, Serialize)]
#[partial(derive(Clone, Deserializable, Eq, Merge, PartialEq))]
#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))]
#[partial(serde(rename_all = "camelCase", default, deny_unknown_fields))]
pub struct JavascriptParser {
/// It enables the experimental and unsafe parsing of parameter decorators
///
/// These decorators belong to an old proposal, and they are subject to change.
#[partial(bpaf(hide))]
pub unsafe_parameter_decorators_enabled: bool,

/// Enables parsing of Grit metavariables.
/// Defaults to `false`.
#[partial(bpaf(hide))]
pub grit_metavariables: bool,

/// When enabled, files like `.js`/`.ts` can contain JSX syntax. Defaults to `true`.
pub jsx_everywhere: bool,
}

impl Default for JavascriptParser {
fn default() -> Self {
Self {
jsx_everywhere: true,
grit_metavariables: false,
unsafe_parameter_decorators_enabled: false,
}
}
}

/// Indicates the type of runtime or transformation used for interpreting JSX.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ declare_lint_rule! {
///
/// ### Valid
///
/// ```js
/// ```jsx
/// <>
/// <a href="#" aria-expanded />
/// <img alt="foobar" aria-hidden />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,27 @@ declare_lint_rule! {
///
/// ### Valid
///
/// ```js
/// ```jsx
/// <div />
///```
///
/// ```js
/// ```jsx
/// <div>child</div>
///```
///
/// ```js
/// ```jsx
/// <Component />
///```
///
/// ```js
/// ```jsx
/// <Component>child</Component>
///```
///
/// ```js
/// ```jsx
/// <Foo.bar />
///```
///
/// ```js
/// ```jsx
/// <Foo.bar>child</Foo.bar>
///```
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: preferObjectSpread.grit
snapshot_kind: text
---
# Input
```jsx
```js
Object.assign({}, foo);

Object.assign({}, {foo: 'bar'});
Expand Down
1 change: 0 additions & 1 deletion crates/biome_js_analyze/tests/spec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ pub(crate) fn analyze_and_snap(
let parsed = parse(input_code, source_type, parser_options.clone());
let root = parsed.tree();

//
let options = create_analyzer_options(input_file, &mut diagnostics);

let (_, errors) = biome_js_analyze::analyze(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: reactCreateElementInvalid.js
snapshot_kind: text
---
# Input
```jsx
```js
React.createElement("div", { tabIndex: '1' })
React.createElement("div", { tabIndex: 1 })
React.createElement("div", { tabIndex: +1 })
Expand Down Expand Up @@ -89,5 +90,3 @@ reactCreateElementInvalid.js:3:40 lint/a11y/noPositiveTabindex FIXABLE ━━


```


Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: reactCreateElementValid.js
snapshot_kind: text
---
# Input
```jsx
```js
React.createElement("div", { tabIndex: '0' })
React.createElement("div", { tabIndex: '-1' })
React.createElement("div", { tabIndex: dynamic })
Expand All @@ -23,5 +24,3 @@ React.createElement("div", props)
React.createElement("div", {})

```


Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalid.js
snapshot_kind: text
---
# Input
```jsx
```js
<div aria-activedescendant={someID} />;

```

# Diagnostics
```
invalid.js:1:1 lint/a11y/useAriaActivedescendantWithTabindex FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Enforce elements with aria-activedescendant are tabbable.

> 1 │ <div aria-activedescendant={someID} />;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

i aria-activedescendant is used to manage focus within a composite widget.
The element with the attribute aria-activedescendant retains the active document focus.

i Add the tabIndex attribute to the element with a value greater than or equal to -1.

i Unsafe fix: Add the tabIndex attribute.

1 │ <div·aria-activedescendant={someID}··tabIndex="0"·/>;
│ ++++++++++++++

```


Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: valid.js
snapshot_kind: text
---
# Input
```jsx
```js
<CustomComponent />;
<CustomComponent aria-activedescendant={someID} />;
<CustomComponent aria-activedescendant={someID} tabIndex={0} />;
Expand All @@ -21,5 +22,3 @@ expression: valid.js
<input aria-activedescendant={someID} tabIndex={-1} />;

```


Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: inObject.js
snapshot_kind: text
---
# Input
```jsx
```js
// invalid
React.createElement('button');
React.createElement('button', {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: withBindingInvalid.js
snapshot_kind: text
---
# Input
```jsx
```js
import React, { createElement } from "react";

React.createElement('button');
Expand Down Expand Up @@ -93,5 +94,3 @@ withBindingInvalid.js:13:13 lint/a11y/useButtonType ━━━━━━━━━


```


Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: withBindingValid.js
snapshot_kind: text
---
# Input
```jsx
```js
import React, { createElement } from "notReact";

React.createElement('button');
Expand All @@ -15,5 +16,3 @@ createElement('button', {
"type": "bar"
});
```


Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: withDefaultNamespaceInvalid.js
snapshot_kind: text
---
# Input
```jsx
```js
import * as DefaultNamespace from "react";

DefaultNamespace.createElement('button');
Expand Down Expand Up @@ -71,5 +72,3 @@ withDefaultNamespaceInvalid.js:8:13 lint/a11y/useButtonType ━━━━━━


```


Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: withRenamedImportInvalid.js
snapshot_kind: text
---
# Input
```jsx
```js
import AwesomeReact, { createElement as awesomeCreateElement } from "react";

AwesomeReact.createElement('button');
Expand Down Expand Up @@ -93,5 +94,3 @@ withRenamedImportInvalid.js:13:13 lint/a11y/useButtonType ━━━━━━━


```


Original file line number Diff line number Diff line change
@@ -1,50 +1,13 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalid.js
snapshot_kind: text
---
# Input
```jsx
```js
<div>
<div role="button" />
<div role="tab" />
</div>;

```

# Diagnostics
```
invalid.js:2:2 lint/a11y/useFocusableInteractive ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! The HTML element with the interactive role "button" is not focusable.

1 │ <div>
> 2 │ <div role="button" />
│ ^^^^^^^^^^^^^^^^^^^^^
3 │ <div role="tab" />
4 │ </div>;

i A non-interactive HTML element that is not focusable may not be reachable for users that rely on keyboard navigation, even with an added role like "button".

i Add a tabIndex attribute to make this element focusable.


```

```
invalid.js:3:2 lint/a11y/useFocusableInteractive ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! The HTML element with the interactive role "tab" is not focusable.

1 │ <div>
2 │ <div role="button" />
> 3 │ <div role="tab" />
│ ^^^^^^^^^^^^^^^^^^
4 │ </div>;
5 │

i A non-interactive HTML element that is not focusable may not be reachable for users that rely on keyboard navigation, even with an added role like "tab".

i Add a tabIndex attribute to make this element focusable.


```
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: valid.js
snapshot_kind: text
---
# Input
```jsx
```js
<div>
<div role="button" tabIndex={0} />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: booleanOperators.js
snapshot_kind: text
---
# Input
```jsx
```js
function booleanOperators() {
if (a // +1 for `if`
&& b && c // +1
Expand All @@ -30,5 +31,3 @@ booleanOperators.js:1:10 lint/complexity/noExcessiveCognitiveComplexity ━━


```


Loading
Loading