Skip to content

[eslint-plugin-react-hooks] updates for component syntax #33089

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

Merged
merged 1 commit into from
May 2, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function normalizeIndent(strings) {
// }
// ***************************************************

const tests = {
const allTests = {
valid: [
{
code: normalizeIndent`
Expand All @@ -44,6 +44,25 @@ const tests = {
}
`,
},
{
syntax: 'flow',
code: normalizeIndent`
// Component syntax
component Button() {
useHook();
return <div>Button!</div>;
}
`,
},
{
syntax: 'flow',
code: normalizeIndent`
// Hook syntax
hook useSampleHook() {
useHook();
}
`,
},
{
code: normalizeIndent`
// Valid because components can use hooks.
Expand Down Expand Up @@ -563,6 +582,28 @@ const tests = {
},
],
invalid: [
{
syntax: 'flow',
code: normalizeIndent`
component Button(cond: boolean) {
if (cond) {
useConditionalHook();
}
}
`,
errors: [conditionalError('useConditionalHook')],
},
{
syntax: 'flow',
code: normalizeIndent`
hook useTest(cond: boolean) {
if (cond) {
useConditionalHook();
}
}
`,
errors: [conditionalError('useConditionalHook')],
},
{
code: normalizeIndent`
// Invalid because it's dangerous and might not warn otherwise.
Expand Down Expand Up @@ -1287,8 +1328,8 @@ const tests = {
};

if (__EXPERIMENTAL__) {
tests.valid = [
...tests.valid,
allTests.valid = [
...allTests.valid,
{
code: normalizeIndent`
// Valid because functions created with useEffectEvent can be called in a useEffect.
Expand Down Expand Up @@ -1385,8 +1426,8 @@ if (__EXPERIMENTAL__) {
`,
},
];
tests.invalid = [
...tests.invalid,
allTests.invalid = [
...allTests.invalid,
{
code: normalizeIndent`
function MyComponent({ theme }) {
Expand Down Expand Up @@ -1536,7 +1577,7 @@ function asyncComponentHookError(fn) {
if (!process.env.CI) {
let only = [];
let skipped = [];
[...tests.valid, ...tests.invalid].forEach(t => {
[...allTests.valid, ...allTests.invalid].forEach(t => {
if (t.skip) {
delete t.skip;
skipped.push(t);
Expand All @@ -1555,10 +1596,23 @@ if (!process.env.CI) {
}
return true;
};
tests.valid = tests.valid.filter(predicate);
tests.invalid = tests.invalid.filter(predicate);
allTests.valid = allTests.valid.filter(predicate);
allTests.invalid = allTests.invalid.filter(predicate);
}

function filteredTests(predicate) {
return {
valid: allTests.valid.filter(predicate),
invalid: allTests.invalid.filter(predicate),
};
}

const flowTests = filteredTests(t => t.syntax == null || t.syntax === 'flow');
const tests = filteredTests(t => t.syntax !== 'flow');

allTests.valid.forEach(t => delete t.syntax);
allTests.invalid.forEach(t => delete t.syntax);

describe('rules-of-hooks/rules-of-hooks', () => {
const parserOptionsV7 = {
ecmaFeatures: {
Expand Down Expand Up @@ -1594,6 +1648,25 @@ describe('rules-of-hooks/rules-of-hooks', () => {
tests
);

new ESLintTesterV7({
parser: require.resolve('hermes-eslint'),
parserOptions: {
sourceType: 'module',
enableExperimentalComponentSyntax: true,
},
}).run('eslint: v7, parser: hermes-eslint', ReactHooksESLintRule, flowTests);

new ESLintTesterV9({
languageOptions: {
...languageOptionsV9,
parser: require('hermes-eslint'),
parserOptions: {
sourceType: 'module',
enableExperimentalComponentSyntax: true,
},
},
}).run('eslint: v9, parser: hermes-eslint', ReactHooksESLintRule, flowTests);

new ESLintTesterV7({
parser: require.resolve('@typescript-eslint/parser-v2'),
parserOptions: parserOptionsV7,
Expand Down
19 changes: 19 additions & 0 deletions packages/eslint-plugin-react-hooks/src/code-path-analysis/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright OpenJS Foundation and other contributors, <www.openjsf.org>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Code Path Analyzer

This code is a forked version of ESLints Code Path Analyzer which includes
support for Component Syntax.

Forked from: https://github.com/eslint/eslint/tree/main/lib/linter/code-path-analysis
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

function assert(cond) {
if (!cond) {
throw new Error('Assertion violated.');
}
}

module.exports = assert;
Loading
Loading