From e88b579cdfb0a7d6800861531fa9261c4ef76ba6 Mon Sep 17 00:00:00 2001 From: Daniel Frederico Lins Leite Date: Wed, 9 Nov 2022 10:18:59 -0300 Subject: [PATCH 1/7] support for object and array bindings when exporting --- .../noUnusedVariables/noUnusedVariables.js | 2 ++ .../noUnusedVariables/noUnusedVariables.js.snap | 3 ++- crates/rome_js_semantic/src/events.rs | 15 +++++++++++++-- crates/rome_js_semantic/src/semantic_model.rs | 7 +++++++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/rome_js_analyze/tests/specs/correctness/noUnusedVariables/noUnusedVariables.js b/crates/rome_js_analyze/tests/specs/correctness/noUnusedVariables/noUnusedVariables.js index d7927f94667..3464e556137 100644 --- a/crates/rome_js_analyze/tests/specs/correctness/noUnusedVariables/noUnusedVariables.js +++ b/crates/rome_js_analyze/tests/specs/correctness/noUnusedVariables/noUnusedVariables.js @@ -67,3 +67,5 @@ const a = { a; +export const { A } = z; +export const [ B ] = z; \ No newline at end of file diff --git a/crates/rome_js_analyze/tests/specs/correctness/noUnusedVariables/noUnusedVariables.js.snap b/crates/rome_js_analyze/tests/specs/correctness/noUnusedVariables/noUnusedVariables.js.snap index d70fd9f11cc..4117b1040ac 100644 --- a/crates/rome_js_analyze/tests/specs/correctness/noUnusedVariables/noUnusedVariables.js.snap +++ b/crates/rome_js_analyze/tests/specs/correctness/noUnusedVariables/noUnusedVariables.js.snap @@ -73,7 +73,8 @@ const a = { a; - +export const { A } = z; +export const [ B ] = z; ``` # Diagnostics diff --git a/crates/rome_js_semantic/src/events.rs b/crates/rome_js_semantic/src/events.rs index 0eb5f725a8d..2f2438c3804 100644 --- a/crates/rome_js_semantic/src/events.rs +++ b/crates/rome_js_semantic/src/events.rs @@ -371,6 +371,17 @@ impl SemanticEventExtractor { self.push_binding_into_scope(hoisted_scope_id, &name_token); self.export_class_expression(node, &parent); } + JS_OBJECT_BINDING_PATTERN_SHORTHAND_PROPERTY | JS_OBJECT_BINDING_PATTERN_PROPERTY => { + let declarator = parent.parent()? + .parent()? + .parent()?; + self.export_variable_declarator(node, &declarator); + } + JS_ARRAY_BINDING_PATTERN_ELEMENT_LIST => { + let declarator = parent.parent()? + .parent()?; + self.export_variable_declarator(node, &declarator); + } TS_TYPE_ALIAS_DECLARATION => { let hoisted_scope_id = self.scope_index_to_hoist_declarations(1); self.push_binding_into_scope(hoisted_scope_id, &name_token); @@ -542,7 +553,7 @@ impl SemanticEventExtractor { /// 1 - Match all references and declarations; /// 2 - Unmatched references are promoted to its parent scope or become [UnresolvedReference] events; /// 3 - All declarations of this scope are removed; - /// 4 - All shawed declarations are restored. + /// 4 - All shadowed declarations are restored. fn pop_scope(&mut self, range: TextRange) { debug_assert!(!self.scopes.is_empty()); @@ -820,7 +831,7 @@ impl SemanticEventExtractor { } } - // Check if a function is exported and raise the [Exported] event. + // Check if a variable is exported and raise the [Exported] event. fn export_variable_declarator( &mut self, binding: &JsSyntaxNode, diff --git a/crates/rome_js_semantic/src/semantic_model.rs b/crates/rome_js_semantic/src/semantic_model.rs index 935c5e28e2d..8ade4af01bd 100644 --- a/crates/rome_js_semantic/src/semantic_model.rs +++ b/crates/rome_js_semantic/src/semantic_model.rs @@ -1428,6 +1428,13 @@ mod test { assert_is_exported(true, "A", "enum A {}; module.exports = A"); assert_is_exported(true, "A", "enum A {}; exports = A"); assert_is_exported(true, "A", "enum A {}; exports.A = A"); + + // Object binding + assert_is_exported(true, "A", "export const { A } = a;"); + assert_is_exported(true, "b", "export const { A: b } = a;"); + + // Array binding + assert_is_exported(true, "A", "export const [A] = a;"); } #[test] From 45f5da5d698b665e8d1a2493a692e0990432ba3e Mon Sep 17 00:00:00 2001 From: Daniel Frederico Lins Leite Date: Wed, 9 Nov 2022 10:32:04 -0300 Subject: [PATCH 2/7] fmt and clippy issues --- crates/rome_js_semantic/src/events.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/crates/rome_js_semantic/src/events.rs b/crates/rome_js_semantic/src/events.rs index 2f2438c3804..4fa7a7cd8a4 100644 --- a/crates/rome_js_semantic/src/events.rs +++ b/crates/rome_js_semantic/src/events.rs @@ -372,14 +372,11 @@ impl SemanticEventExtractor { self.export_class_expression(node, &parent); } JS_OBJECT_BINDING_PATTERN_SHORTHAND_PROPERTY | JS_OBJECT_BINDING_PATTERN_PROPERTY => { - let declarator = parent.parent()? - .parent()? - .parent()?; + let declarator = parent.parent()?.parent()?.parent()?; self.export_variable_declarator(node, &declarator); } JS_ARRAY_BINDING_PATTERN_ELEMENT_LIST => { - let declarator = parent.parent()? - .parent()?; + let declarator = parent.parent()?.parent()?; self.export_variable_declarator(node, &declarator); } TS_TYPE_ALIAS_DECLARATION => { From dd957a7005ff09620a65a89d65c7effbb5ce7327 Mon Sep 17 00:00:00 2001 From: Daniel Frederico Lins Leite Date: Wed, 9 Nov 2022 14:00:34 -0300 Subject: [PATCH 3/7] support recursive navigation from binding to declarator --- crates/rome_js_semantic/src/events.rs | 20 +++++++++++++------ crates/rome_js_semantic/src/semantic_model.rs | 9 +++++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/crates/rome_js_semantic/src/events.rs b/crates/rome_js_semantic/src/events.rs index 4fa7a7cd8a4..0d5bf14d7fb 100644 --- a/crates/rome_js_semantic/src/events.rs +++ b/crates/rome_js_semantic/src/events.rs @@ -371,12 +371,20 @@ impl SemanticEventExtractor { self.push_binding_into_scope(hoisted_scope_id, &name_token); self.export_class_expression(node, &parent); } - JS_OBJECT_BINDING_PATTERN_SHORTHAND_PROPERTY | JS_OBJECT_BINDING_PATTERN_PROPERTY => { - let declarator = parent.parent()?.parent()?.parent()?; - self.export_variable_declarator(node, &declarator); - } - JS_ARRAY_BINDING_PATTERN_ELEMENT_LIST => { - let declarator = parent.parent()?.parent()?; + JS_OBJECT_BINDING_PATTERN_SHORTHAND_PROPERTY + | JS_OBJECT_BINDING_PATTERN_PROPERTY + | JS_ARRAY_BINDING_PATTERN_ELEMENT_LIST=> { + let declarator = parent.ancestors() + .skip_while(|x| { + matches!(x.kind(), + JS_OBJECT_BINDING_PATTERN_SHORTHAND_PROPERTY | + JS_OBJECT_BINDING_PATTERN_PROPERTY_LIST | + JS_OBJECT_BINDING_PATTERN_PROPERTY | + JS_OBJECT_BINDING_PATTERN | + JS_ARRAY_BINDING_PATTERN_ELEMENT_LIST | + JS_ARRAY_BINDING_PATTERN + ) + }).next()?; self.export_variable_declarator(node, &declarator); } TS_TYPE_ALIAS_DECLARATION => { diff --git a/crates/rome_js_semantic/src/semantic_model.rs b/crates/rome_js_semantic/src/semantic_model.rs index 8ade4af01bd..5e4242c4d88 100644 --- a/crates/rome_js_semantic/src/semantic_model.rs +++ b/crates/rome_js_semantic/src/semantic_model.rs @@ -1429,12 +1429,13 @@ mod test { assert_is_exported(true, "A", "enum A {}; exports = A"); assert_is_exported(true, "A", "enum A {}; exports.A = A"); - // Object binding + // Object and Array bindings assert_is_exported(true, "A", "export const { A } = a;"); assert_is_exported(true, "b", "export const { A: b } = a;"); - - // Array binding - assert_is_exported(true, "A", "export const [A] = a;"); + assert_is_exported(true, "A", "export const [ A ] = a;"); + assert_is_exported(true, "A", "export const [{ A }] = a;"); + assert_is_exported(true, "D", "export const [{ C: [ D ] }] = a;"); + assert_is_exported(true, "E", "export const [{ C: [{ E }] }] = a;"); } #[test] From d81e6276380e95fa97a895ee9900bf1010b08900 Mon Sep 17 00:00:00 2001 From: Daniel Frederico Lins Leite Date: Wed, 9 Nov 2022 14:23:42 -0300 Subject: [PATCH 4/7] checking binding is inside a declarator --- crates/rome_js_semantic/src/events.rs | 33 +- website/docs/src/lint/rules/index.md | 490 ------------------ .../lint/rules/useExhaustiveDependencies.md | 148 ------ 3 files changed, 18 insertions(+), 653 deletions(-) delete mode 100644 website/docs/src/lint/rules/index.md delete mode 100644 website/docs/src/lint/rules/useExhaustiveDependencies.md diff --git a/crates/rome_js_semantic/src/events.rs b/crates/rome_js_semantic/src/events.rs index 0d5bf14d7fb..b24c4dbac4a 100644 --- a/crates/rome_js_semantic/src/events.rs +++ b/crates/rome_js_semantic/src/events.rs @@ -371,21 +371,24 @@ impl SemanticEventExtractor { self.push_binding_into_scope(hoisted_scope_id, &name_token); self.export_class_expression(node, &parent); } - JS_OBJECT_BINDING_PATTERN_SHORTHAND_PROPERTY - | JS_OBJECT_BINDING_PATTERN_PROPERTY - | JS_ARRAY_BINDING_PATTERN_ELEMENT_LIST=> { - let declarator = parent.ancestors() - .skip_while(|x| { - matches!(x.kind(), - JS_OBJECT_BINDING_PATTERN_SHORTHAND_PROPERTY | - JS_OBJECT_BINDING_PATTERN_PROPERTY_LIST | - JS_OBJECT_BINDING_PATTERN_PROPERTY | - JS_OBJECT_BINDING_PATTERN | - JS_ARRAY_BINDING_PATTERN_ELEMENT_LIST | - JS_ARRAY_BINDING_PATTERN - ) - }).next()?; - self.export_variable_declarator(node, &declarator); + JS_OBJECT_BINDING_PATTERN_SHORTHAND_PROPERTY + | JS_OBJECT_BINDING_PATTERN_PROPERTY + | JS_ARRAY_BINDING_PATTERN_ELEMENT_LIST => { + let possible_declarator = parent.ancestors().find(|x| { + !matches!( + x.kind(), + JS_OBJECT_BINDING_PATTERN_SHORTHAND_PROPERTY + | JS_OBJECT_BINDING_PATTERN_PROPERTY_LIST + | JS_OBJECT_BINDING_PATTERN_PROPERTY + | JS_OBJECT_BINDING_PATTERN + | JS_ARRAY_BINDING_PATTERN_ELEMENT_LIST + | JS_ARRAY_BINDING_PATTERN + ) + })?; + + if let JS_VARIABLE_DECLARATOR = possible_declarator.kind() { + self.export_variable_declarator(node, &possible_declarator); + } } TS_TYPE_ALIAS_DECLARATION => { let hoisted_scope_id = self.scope_index_to_hoist_declarations(1); diff --git a/website/docs/src/lint/rules/index.md b/website/docs/src/lint/rules/index.md deleted file mode 100644 index 89b262267bc..00000000000 --- a/website/docs/src/lint/rules/index.md +++ /dev/null @@ -1,490 +0,0 @@ ---- -title: Lint Rules -main-class: rules ---- - -# Rules - - -## Accessibility - -Rules focused on preventing accessibility problems. -
-
-

- noAutofocus - recommended -

-Avoid the autoFocus attribute -
-
-

- noPositiveTabindex - recommended -

-Prevent the usage of positive integers on tabIndex property -
-
-

- useAltText - recommended -

-It asserts that alternative text to images or areas, help to rely on to screen readers to understand the purpose and the context of the image. -
-
-

- useAnchorContent - recommended -

-Enforce that anchor elements have content and that the content is accessible to screen readers. -
-
-

- useBlankTarget - recommended -

-Disallow target="_blank" attribute without rel="noreferrer" -
-
-

- useButtonType - recommended -

-Enforces the usage of the attribute type for the element button -
-
-

- useKeyWithClickEvents - recommended -

-Enforce to have the onClick mouse event with the onKeyUp, the onKeyDown, or the noKeyPress keyboard event. -
-
-

- useKeyWithMouseEvents - recommended -

-Enforce that onMouseOver/onMouseOut are accompanied by onFocus/onBlur for keyboard-only users. -It is important to take into account users with physical disabilities who cannot use a mouse, -who use assistive technology or screenreader. -
-
-

- useValidAnchor - recommended -

-Enforce that all anchors are valid, and they are navigable elements. -
- -
- -## Complexity - -Rules that focus on inspecting complex code that could be simplified. -
-
-

- noExtraBooleanCast - recommended -

-Disallow unnecessary boolean casts -
-
-

- useSimplifiedLogicExpression - recommended -

-Discard redundant terms from logical expressions. -
- -
- -## Correctness - -Rules that detect incorrect or useless code. -
-
-

- noArguments - recommended -

-Disallow the use of arguments -
-
-

- noArrayIndexKey - recommended -

-Discourage the usage of Array index in keys. -
-
-

- noAsyncPromiseExecutor - recommended -

-Disallows using an async function as a Promise executor. -
-
-

- noCatchAssign - recommended -

-Disallow reassigning exceptions in catch clauses -
-
-

- noChildrenProp - recommended -

-Prevent passing of children as props. -
-
-

- noCommentText - recommended -

-Prevent comments from being inserted as text nodes -
-
-

- noCompareNegZero - recommended -

-Disallow comparing against -0 -
-
-

- noDebugger - recommended -

-Disallow the use of debugger -
-
-

- noDelete - recommended -

-Disallow the use of the delete operator -
-
-

- noDoubleEquals - recommended -

-Require the use of === and !== -
-
-

- noDupeArgs - recommended -

-Disallow duplicate function arguments name. -
-
-

- noEmptyPattern - recommended -

-Disallows empty destructuring patterns. -
-
-

- noFunctionAssign - recommended -

-Disallow reassigning function declarations. -
-
-

- noImportAssign - recommended -

-Disallow assigning to imported bindings -
-
-

- noLabelVar - recommended -

-Disallow labels that share a name with a variable -
-
-

- noMultipleSpacesInRegularExpressionLiterals - recommended -

-Disallow unclear usage of multiple space characters in regular expression literals -
-
-

- noNewSymbol - recommended -

-Disallow new operators with the Symbol object -
-
-

- noRenderReturnValue - recommended -

-Prevent the usage of the return value of React.render. -
-
-

- noRestrictedGlobals -

-This rule allows you to specify global variable names that you don’t want to use in your application. -
-
-

- noShadowRestrictedNames - recommended -

-Disallow identifiers from shadowing restricted names. -
-
-

- noSparseArray - recommended -

-Disallow sparse arrays -
-
-

- noUndeclaredVariables -

-Prevents the usage of variables that haven't been declared inside the document -
-
-

- noUnnecessaryContinue - recommended -

-Avoid using unnecessary continue. -
-
-

- noUnreachable - recommended -

-Disallow unreachable code -
-
-

- noUnsafeNegation - recommended -

-Disallow using unsafe negation. -
-
-

- noUnusedVariables -

-Disallow unused variables. -
-
-

- noUselessFragments -

-Disallow unnecessary fragments -
-
-

- noVoidElementsWithChildren - recommended -

-This rules prevents void elements (AKA self-closing elements) from having children. -
-
-

- useSingleCaseStatement - recommended -

-Enforces case clauses have a single statement, emits a quick fix wrapping -the statements in a block -
-
-

- useValidTypeof - recommended -

-This rule verifies the result of typeof $expr unary expressions is being -compared to valid values, either string literals containing valid type -names or other typeof expressions -
-
-

- useWhile - recommended -

-Enforce the use of while loops instead of for loops when the -initializer and update expressions are not needed -
- -
- -## Security - -Rules that detect potential security flaws. -
-
-

- noDangerouslySetInnerHtml - recommended -

-Prevent the usage of dangerous JSX props -
-
-

- noDangerouslySetInnerHtmlWithChildren - recommended -

-Report when a DOM element or a component uses both children and dangerouslySetInnerHTML prop. -
- -
- -## Style - -Rules enforcing a consistent way of writing your code. -
-
-

- noImplicitBoolean - recommended -

-Disallow implicit true values on JSX boolean attributes -
-
-

- noNegationElse - recommended -

-Disallow negation in the condition of an if statement if it has an else clause -
-
-

- noShoutyConstants - recommended -

-Disallow the use of constants which its value is the upper-case version of its name. -
-
-

- noUnusedTemplateLiteral - recommended -

-Disallow template literals if interpolation and special-character handling are not needed -
-
-

- useBlockStatements - recommended -

-Requires following curly brace conventions. -JavaScript allows the omission of curly braces when a block contains only one statement. However, it is considered by many to be best practice to never omit curly braces around blocks, even when they are optional, because it can lead to bugs and reduces code clarity. -
-
-

- useFragmentSyntax -

-This rule enforces the use of <>...</> over <Fragment>...</Fragment>. -
-
-

- useOptionalChain - recommended -

-Enforce using concise optional chain instead of chained logical expressions. -
-
-

- useSelfClosingElements - recommended -

-Prevent extra closing tags for components without children -
-
-

- useShorthandArrayType - recommended -

-When expressing array types, this rule promotes the usage of T[] shorthand instead of Array<T>. -
-
-

- useSingleVarDeclarator - recommended -

-Disallow multiple variable declarations in the same variable statement -
-
-

- useTemplate - recommended -

-Template literals are preferred over string concatenation. -
- -
- -## Nursery - -New rules that are still under development. - -Nursery rules require explicit opt-in via configuration because they may still have bugs or performance problems. -Nursery rules get promoted to other groups once they become stable or may be removed. - -Rules that belong to this group are not subject to semantic version. -
-
-

- noBannedTypes -

-Disallow certain types. -
-
-

- noConstAssign -

-Prevents from having const variables being re-assigned. -
-
-

- noExplicitAny -

-Disallow the any type usage -
-
-

- noInvalidConstructorSuper -

-Prevents the incorrect use of super() inside classes. -It also checks whether a call super() is missing from classes that extends other constructors. -
-
-

- useCamelCase -

-Enforce camel case naming convention. -
-
-

- useExhaustiveDependencies -

-Enforce all dependencies are correctly specified. -
-
-

- useFlatMap -

-Promotes the use of .flatMap() when map().flat() are used together. -
-
-

- useValidForDirection -

-Enforce "for" loop update clause moving the counter in the right direction. -
- -
diff --git a/website/docs/src/lint/rules/useExhaustiveDependencies.md b/website/docs/src/lint/rules/useExhaustiveDependencies.md deleted file mode 100644 index 5739d0bcf30..00000000000 --- a/website/docs/src/lint/rules/useExhaustiveDependencies.md +++ /dev/null @@ -1,148 +0,0 @@ ---- -title: Lint Rule useExhaustiveDependencies ---- - -# useExhaustiveDependencies (since v10.0.0) - -Enforce all dependencies are correctly specified. - -## Examples - -### Invalid - -```jsx -let a = 1; -useEffect(() => { - console.log(a); -}) -``` - -{% raw %}
nursery/useExhaustiveDependencies.js:2:1 lint/nursery/useExhaustiveDependencies ━━━━━━━━━━━━━━━━━━━━
-
-   This hook do not specify all of its dependencies.
-  
-    1 │ let a = 1;
-  > 2 │ useEffect(() => {
-   ^^^^^^^^^
-    3 │     console.log(a);
-    4 │ })
-  
-   This dependency is not specified in the hook dependency list.
-  
-    1 │ let a = 1;
-    2 │ useEffect(() => {
-  > 3 │     console.log(a);
-                   ^
-    4 │ })
-    5 │ 
-  
-
{% endraw %} - -```jsx -let b = 1; -useEffect(() => { -}, [b]) -``` - -{% raw %}
nursery/useExhaustiveDependencies.js:2:1 lint/nursery/useExhaustiveDependencies ━━━━━━━━━━━━━━━━━━━━
-
-   This hook specifies more dependencies than necessary.
-  
-    1 │ let b = 1;
-  > 2 │ useEffect(() => {
-   ^^^^^^^^^
-    3 │ }, [b])
-    4 │ 
-  
-   This dependency can be removed from the list.
-  
-    1 │ let b = 1;
-    2 │ useEffect(() => {
-  > 3 │ }, [b])
-       ^
-    4 │ 
-  
-
{% endraw %} - -```jsx -const [name, setName] = useState(); -useEffect(() => { - console.log(name); - setName(""); -}, [name, setName]) -``` - -{% raw %}
nursery/useExhaustiveDependencies.js:2:1 lint/nursery/useExhaustiveDependencies ━━━━━━━━━━━━━━━━━━━━
-
-   This hook specifies more dependencies than necessary.
-  
-    1 │ const [name, setName] = useState();
-  > 2 │ useEffect(() => {
-   ^^^^^^^^^
-    3 │     console.log(name);
-    4 │     setName("");
-  
-   This dependency can be removed from the list.
-  
-    3 │     console.log(name);
-    4 │     setName("");
-  > 5 │ }, [name, setName])
-             ^^^^^^^
-    6 │ 
-  
-
{% endraw %} - -```jsx -let a = 1; -const b = a + 1; -useEffect(() => { - console.log(b); -}) -``` - -{% raw %}
nursery/useExhaustiveDependencies.js:3:1 lint/nursery/useExhaustiveDependencies ━━━━━━━━━━━━━━━━━━━━
-
-   This hook do not specify all of its dependencies.
-  
-    1 │ let a = 1;
-    2 │ const b = a + 1;
-  > 3 │ useEffect(() => {
-   ^^^^^^^^^
-    4 │     console.log(b);
-    5 │ })
-  
-   This dependency is not specified in the hook dependency list.
-  
-    2 │ const b = a + 1;
-    3 │ useEffect(() => {
-  > 4 │     console.log(b);
-                   ^
-    5 │ })
-    6 │ 
-  
-
{% endraw %} - -## Valid - -```jsx -let a = 1; -useEffect(() => { - console.log(a); -}, [a]); -``` - -```jsx -const a = 1; -useEffect(() => { - console.log(a); -}); -``` - -```jsx -const [name, setName] = useState(); -useEffect(() => { - console.log(name); - setName(""); -}, [name]) -``` - From 4654ffd847d53a6f9ceacd160ed03d50364c9639 Mon Sep 17 00:00:00 2001 From: Daniel Frederico Lins Leite Date: Wed, 9 Nov 2022 15:12:56 -0300 Subject: [PATCH 5/7] push bindings from obj and array bindings --- crates/rome_js_semantic/src/events.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/rome_js_semantic/src/events.rs b/crates/rome_js_semantic/src/events.rs index b24c4dbac4a..9af3629d468 100644 --- a/crates/rome_js_semantic/src/events.rs +++ b/crates/rome_js_semantic/src/events.rs @@ -374,6 +374,8 @@ impl SemanticEventExtractor { JS_OBJECT_BINDING_PATTERN_SHORTHAND_PROPERTY | JS_OBJECT_BINDING_PATTERN_PROPERTY | JS_ARRAY_BINDING_PATTERN_ELEMENT_LIST => { + self.push_binding_into_scope(None, &name_token); + let possible_declarator = parent.ancestors().find(|x| { !matches!( x.kind(), From d04e70916371e420e12362502f221fe17e808c66 Mon Sep 17 00:00:00 2001 From: Daniel Frederico Lins Leite Date: Wed, 9 Nov 2022 15:15:31 -0300 Subject: [PATCH 6/7] fmt and clippy issues --- crates/rome_js_semantic/src/events.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/rome_js_semantic/src/events.rs b/crates/rome_js_semantic/src/events.rs index 9af3629d468..eeba4f29cb1 100644 --- a/crates/rome_js_semantic/src/events.rs +++ b/crates/rome_js_semantic/src/events.rs @@ -375,7 +375,7 @@ impl SemanticEventExtractor { | JS_OBJECT_BINDING_PATTERN_PROPERTY | JS_ARRAY_BINDING_PATTERN_ELEMENT_LIST => { self.push_binding_into_scope(None, &name_token); - + let possible_declarator = parent.ancestors().find(|x| { !matches!( x.kind(), From b5e7690db208ed19f4fdb532dc1614e0e779ea30 Mon Sep 17 00:00:00 2001 From: Daniel Frederico Lins Leite Date: Wed, 9 Nov 2022 16:51:04 -0300 Subject: [PATCH 7/7] updating lintdoc --- website/docs/src/lint/rules/index.md | 490 ++++++++++++++++++ .../lint/rules/useExhaustiveDependencies.md | 148 ++++++ 2 files changed, 638 insertions(+) create mode 100644 website/docs/src/lint/rules/index.md create mode 100644 website/docs/src/lint/rules/useExhaustiveDependencies.md diff --git a/website/docs/src/lint/rules/index.md b/website/docs/src/lint/rules/index.md new file mode 100644 index 00000000000..89b262267bc --- /dev/null +++ b/website/docs/src/lint/rules/index.md @@ -0,0 +1,490 @@ +--- +title: Lint Rules +main-class: rules +--- + +# Rules + + +## Accessibility + +Rules focused on preventing accessibility problems. +
+
+

+ noAutofocus + recommended +

+Avoid the autoFocus attribute +
+
+

+ noPositiveTabindex + recommended +

+Prevent the usage of positive integers on tabIndex property +
+
+

+ useAltText + recommended +

+It asserts that alternative text to images or areas, help to rely on to screen readers to understand the purpose and the context of the image. +
+
+

+ useAnchorContent + recommended +

+Enforce that anchor elements have content and that the content is accessible to screen readers. +
+
+

+ useBlankTarget + recommended +

+Disallow target="_blank" attribute without rel="noreferrer" +
+
+

+ useButtonType + recommended +

+Enforces the usage of the attribute type for the element button +
+
+

+ useKeyWithClickEvents + recommended +

+Enforce to have the onClick mouse event with the onKeyUp, the onKeyDown, or the noKeyPress keyboard event. +
+
+

+ useKeyWithMouseEvents + recommended +

+Enforce that onMouseOver/onMouseOut are accompanied by onFocus/onBlur for keyboard-only users. +It is important to take into account users with physical disabilities who cannot use a mouse, +who use assistive technology or screenreader. +
+
+

+ useValidAnchor + recommended +

+Enforce that all anchors are valid, and they are navigable elements. +
+ +
+ +## Complexity + +Rules that focus on inspecting complex code that could be simplified. +
+
+

+ noExtraBooleanCast + recommended +

+Disallow unnecessary boolean casts +
+
+

+ useSimplifiedLogicExpression + recommended +

+Discard redundant terms from logical expressions. +
+ +
+ +## Correctness + +Rules that detect incorrect or useless code. +
+
+

+ noArguments + recommended +

+Disallow the use of arguments +
+
+

+ noArrayIndexKey + recommended +

+Discourage the usage of Array index in keys. +
+
+

+ noAsyncPromiseExecutor + recommended +

+Disallows using an async function as a Promise executor. +
+
+

+ noCatchAssign + recommended +

+Disallow reassigning exceptions in catch clauses +
+
+

+ noChildrenProp + recommended +

+Prevent passing of children as props. +
+
+

+ noCommentText + recommended +

+Prevent comments from being inserted as text nodes +
+
+

+ noCompareNegZero + recommended +

+Disallow comparing against -0 +
+
+

+ noDebugger + recommended +

+Disallow the use of debugger +
+
+

+ noDelete + recommended +

+Disallow the use of the delete operator +
+
+

+ noDoubleEquals + recommended +

+Require the use of === and !== +
+
+

+ noDupeArgs + recommended +

+Disallow duplicate function arguments name. +
+
+

+ noEmptyPattern + recommended +

+Disallows empty destructuring patterns. +
+
+

+ noFunctionAssign + recommended +

+Disallow reassigning function declarations. +
+
+

+ noImportAssign + recommended +

+Disallow assigning to imported bindings +
+
+

+ noLabelVar + recommended +

+Disallow labels that share a name with a variable +
+
+

+ noMultipleSpacesInRegularExpressionLiterals + recommended +

+Disallow unclear usage of multiple space characters in regular expression literals +
+
+

+ noNewSymbol + recommended +

+Disallow new operators with the Symbol object +
+
+

+ noRenderReturnValue + recommended +

+Prevent the usage of the return value of React.render. +
+
+

+ noRestrictedGlobals +

+This rule allows you to specify global variable names that you don’t want to use in your application. +
+
+

+ noShadowRestrictedNames + recommended +

+Disallow identifiers from shadowing restricted names. +
+
+

+ noSparseArray + recommended +

+Disallow sparse arrays +
+
+

+ noUndeclaredVariables +

+Prevents the usage of variables that haven't been declared inside the document +
+
+

+ noUnnecessaryContinue + recommended +

+Avoid using unnecessary continue. +
+
+

+ noUnreachable + recommended +

+Disallow unreachable code +
+
+

+ noUnsafeNegation + recommended +

+Disallow using unsafe negation. +
+
+

+ noUnusedVariables +

+Disallow unused variables. +
+
+

+ noUselessFragments +

+Disallow unnecessary fragments +
+
+

+ noVoidElementsWithChildren + recommended +

+This rules prevents void elements (AKA self-closing elements) from having children. +
+
+

+ useSingleCaseStatement + recommended +

+Enforces case clauses have a single statement, emits a quick fix wrapping +the statements in a block +
+
+

+ useValidTypeof + recommended +

+This rule verifies the result of typeof $expr unary expressions is being +compared to valid values, either string literals containing valid type +names or other typeof expressions +
+
+

+ useWhile + recommended +

+Enforce the use of while loops instead of for loops when the +initializer and update expressions are not needed +
+ +
+ +## Security + +Rules that detect potential security flaws. +
+
+

+ noDangerouslySetInnerHtml + recommended +

+Prevent the usage of dangerous JSX props +
+
+

+ noDangerouslySetInnerHtmlWithChildren + recommended +

+Report when a DOM element or a component uses both children and dangerouslySetInnerHTML prop. +
+ +
+ +## Style + +Rules enforcing a consistent way of writing your code. +
+
+

+ noImplicitBoolean + recommended +

+Disallow implicit true values on JSX boolean attributes +
+
+

+ noNegationElse + recommended +

+Disallow negation in the condition of an if statement if it has an else clause +
+
+

+ noShoutyConstants + recommended +

+Disallow the use of constants which its value is the upper-case version of its name. +
+
+

+ noUnusedTemplateLiteral + recommended +

+Disallow template literals if interpolation and special-character handling are not needed +
+
+

+ useBlockStatements + recommended +

+Requires following curly brace conventions. +JavaScript allows the omission of curly braces when a block contains only one statement. However, it is considered by many to be best practice to never omit curly braces around blocks, even when they are optional, because it can lead to bugs and reduces code clarity. +
+
+

+ useFragmentSyntax +

+This rule enforces the use of <>...</> over <Fragment>...</Fragment>. +
+
+

+ useOptionalChain + recommended +

+Enforce using concise optional chain instead of chained logical expressions. +
+
+

+ useSelfClosingElements + recommended +

+Prevent extra closing tags for components without children +
+
+

+ useShorthandArrayType + recommended +

+When expressing array types, this rule promotes the usage of T[] shorthand instead of Array<T>. +
+
+

+ useSingleVarDeclarator + recommended +

+Disallow multiple variable declarations in the same variable statement +
+
+

+ useTemplate + recommended +

+Template literals are preferred over string concatenation. +
+ +
+ +## Nursery + +New rules that are still under development. + +Nursery rules require explicit opt-in via configuration because they may still have bugs or performance problems. +Nursery rules get promoted to other groups once they become stable or may be removed. + +Rules that belong to this group are not subject to semantic version. +
+
+

+ noBannedTypes +

+Disallow certain types. +
+
+

+ noConstAssign +

+Prevents from having const variables being re-assigned. +
+
+

+ noExplicitAny +

+Disallow the any type usage +
+
+

+ noInvalidConstructorSuper +

+Prevents the incorrect use of super() inside classes. +It also checks whether a call super() is missing from classes that extends other constructors. +
+
+

+ useCamelCase +

+Enforce camel case naming convention. +
+
+

+ useExhaustiveDependencies +

+Enforce all dependencies are correctly specified. +
+
+

+ useFlatMap +

+Promotes the use of .flatMap() when map().flat() are used together. +
+
+

+ useValidForDirection +

+Enforce "for" loop update clause moving the counter in the right direction. +
+ +
diff --git a/website/docs/src/lint/rules/useExhaustiveDependencies.md b/website/docs/src/lint/rules/useExhaustiveDependencies.md new file mode 100644 index 00000000000..5739d0bcf30 --- /dev/null +++ b/website/docs/src/lint/rules/useExhaustiveDependencies.md @@ -0,0 +1,148 @@ +--- +title: Lint Rule useExhaustiveDependencies +--- + +# useExhaustiveDependencies (since v10.0.0) + +Enforce all dependencies are correctly specified. + +## Examples + +### Invalid + +```jsx +let a = 1; +useEffect(() => { + console.log(a); +}) +``` + +{% raw %}
nursery/useExhaustiveDependencies.js:2:1 lint/nursery/useExhaustiveDependencies ━━━━━━━━━━━━━━━━━━━━
+
+   This hook do not specify all of its dependencies.
+  
+    1 │ let a = 1;
+  > 2 │ useEffect(() => {
+   ^^^^^^^^^
+    3 │     console.log(a);
+    4 │ })
+  
+   This dependency is not specified in the hook dependency list.
+  
+    1 │ let a = 1;
+    2 │ useEffect(() => {
+  > 3 │     console.log(a);
+                   ^
+    4 │ })
+    5 │ 
+  
+
{% endraw %} + +```jsx +let b = 1; +useEffect(() => { +}, [b]) +``` + +{% raw %}
nursery/useExhaustiveDependencies.js:2:1 lint/nursery/useExhaustiveDependencies ━━━━━━━━━━━━━━━━━━━━
+
+   This hook specifies more dependencies than necessary.
+  
+    1 │ let b = 1;
+  > 2 │ useEffect(() => {
+   ^^^^^^^^^
+    3 │ }, [b])
+    4 │ 
+  
+   This dependency can be removed from the list.
+  
+    1 │ let b = 1;
+    2 │ useEffect(() => {
+  > 3 │ }, [b])
+       ^
+    4 │ 
+  
+
{% endraw %} + +```jsx +const [name, setName] = useState(); +useEffect(() => { + console.log(name); + setName(""); +}, [name, setName]) +``` + +{% raw %}
nursery/useExhaustiveDependencies.js:2:1 lint/nursery/useExhaustiveDependencies ━━━━━━━━━━━━━━━━━━━━
+
+   This hook specifies more dependencies than necessary.
+  
+    1 │ const [name, setName] = useState();
+  > 2 │ useEffect(() => {
+   ^^^^^^^^^
+    3 │     console.log(name);
+    4 │     setName("");
+  
+   This dependency can be removed from the list.
+  
+    3 │     console.log(name);
+    4 │     setName("");
+  > 5 │ }, [name, setName])
+             ^^^^^^^
+    6 │ 
+  
+
{% endraw %} + +```jsx +let a = 1; +const b = a + 1; +useEffect(() => { + console.log(b); +}) +``` + +{% raw %}
nursery/useExhaustiveDependencies.js:3:1 lint/nursery/useExhaustiveDependencies ━━━━━━━━━━━━━━━━━━━━
+
+   This hook do not specify all of its dependencies.
+  
+    1 │ let a = 1;
+    2 │ const b = a + 1;
+  > 3 │ useEffect(() => {
+   ^^^^^^^^^
+    4 │     console.log(b);
+    5 │ })
+  
+   This dependency is not specified in the hook dependency list.
+  
+    2 │ const b = a + 1;
+    3 │ useEffect(() => {
+  > 4 │     console.log(b);
+                   ^
+    5 │ })
+    6 │ 
+  
+
{% endraw %} + +## Valid + +```jsx +let a = 1; +useEffect(() => { + console.log(a); +}, [a]); +``` + +```jsx +const a = 1; +useEffect(() => { + console.log(a); +}); +``` + +```jsx +const [name, setName] = useState(); +useEffect(() => { + console.log(name); + setName(""); +}, [name]) +``` +