Skip to content

Commit a6858bb

Browse files
committed
refactor(eslint-plugin-react-hooks): tidy up type errors
1 parent b7fbd23 commit a6858bb

File tree

5 files changed

+21
-18
lines changed

5 files changed

+21
-18
lines changed

packages/eslint-plugin-react-hooks/__tests__/ReactCompilerRule-test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {ErrorSeverity} from 'babel-plugin-react-compiler/src';
8+
import {ErrorSeverity} from 'babel-plugin-react-compiler';
99
import {RuleTester as ESLintTester} from 'eslint';
10-
import ReactCompilerRule from '../src/rules/ReactCompilerRule';
10+
import ReactCompilerRule from '../src/rules/ReactCompiler';
1111

1212
/**
1313
* A string template tag that removes padding from the left side of multi-line strings
1414
* @param {Array} strings array of code strings (only one expected)
1515
*/
1616
function normalizeIndent(strings: TemplateStringsArray): string {
17-
const codeLines = strings[0].split('\n');
18-
const leftPadding = codeLines[1].match(/\s+/)![0];
17+
const codeLines = strings[0]?.split('\n') ?? [];
18+
const leftPadding = codeLines[1]?.match(/\s+/)![0] ?? '';
1919
return codeLines.map(line => line.slice(leftPadding.length)).join('\n');
2020
}
2121

packages/eslint-plugin-react-hooks/__tests__/ReactCompilerRuleTypescript-test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
*/
77

88
import {RuleTester} from 'eslint';
9-
import ReactCompilerRule from '../src/rules/ReactCompilerRule';
9+
import ReactCompilerRule from '../src/rules/ReactCompiler';
1010

1111
/**
1212
* A string template tag that removes padding from the left side of multi-line strings
1313
* @param {Array} strings array of code strings (only one expected)
1414
*/
1515
function normalizeIndent(strings: TemplateStringsArray): string {
16-
const codeLines = strings[0].split('\n');
17-
const leftPadding = codeLines[1].match(/\s+/)[0];
16+
const codeLines = strings[0]?.split('\n') ?? [];
17+
const leftPadding = codeLines[1]?.match(/\s+/)![0] ?? '';
1818
return codeLines.map(line => line.slice(leftPadding.length)).join('\n');
1919
}
2020

packages/eslint-plugin-react-hooks/src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
7-
import RulesOfHooks from './rules/RulesOfHooks';
8-
import ExhaustiveDeps from './rules/ExhaustiveDeps';
97
import type {ESLint, Linter, Rule} from 'eslint';
108

9+
import ExhaustiveDeps from './rules/ExhaustiveDeps';
10+
import ReactCompiler from './rules/ReactCompiler';
11+
import RulesOfHooks from './rules/RulesOfHooks';
12+
1113
// All rules
1214
const rules = {
13-
'rules-of-hooks': RulesOfHooks,
1415
'exhaustive-deps': ExhaustiveDeps,
16+
'react-compiler': ReactCompiler,
17+
'rules-of-hooks': RulesOfHooks,
1518
} satisfies Record<string, Rule.RuleModule>;
1619

1720
// Config rules

packages/eslint-plugin-react-hooks/src/rules/ReactCompiler.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import BabelPluginReactCompiler, {
1717
validateEnvironmentConfig,
1818
OPT_OUT_DIRECTIVES,
1919
type Logger,
20+
type LoggerEvent,
2021
type PluginOptions,
2122
} from 'babel-plugin-react-compiler';
2223
import type {Rule} from 'eslint';
@@ -180,10 +181,10 @@ const rule: Rule.RuleModule = {
180181
endLoc = {
181182
line: event.fnLoc.start.line,
182183
// Babel loc line numbers are 1-indexed
183-
column: sourceCode.text.split(
184-
/\r?\n|\r|\n/g,
185-
event.fnLoc.start.line,
186-
)[event.fnLoc.start.line - 1].length,
184+
column:
185+
sourceCode.text.split(/\r?\n|\r|\n/g)[
186+
event.fnLoc.start.line - 1
187+
]?.length ?? 0,
187188
};
188189
}
189190
const firstLineLoc = {
@@ -226,8 +227,8 @@ const rule: Rule.RuleModule = {
226227
options.environment = validateEnvironmentConfig(
227228
options.environment ?? {},
228229
);
229-
} catch (err) {
230-
options.logger?.logEvent('', err);
230+
} catch (err: unknown) {
231+
options.logger?.logEvent('', err as LoggerEvent);
231232
}
232233

233234
function hasFlowSuppression(

packages/eslint-plugin-react-hooks/tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@
1212
"babel-plugin-react-compiler": ["../../compiler/packages/babel-plugin-react-compiler/src/index.ts"]
1313
}
1414
},
15-
"exclude": ["node_modules"],
16-
"include": ["src/**/*.ts"]
15+
"include": ["src/**/*.ts", "__tests__/**/*.ts"]
1716
}

0 commit comments

Comments
 (0)