Skip to content

Commit 19f65ff

Browse files
authored
[eprh] Remove NoUnusedOptOutDirectives (facebook#34703)
This rule was a leftover from a while ago and doesn't actually lint anything useful. Specifically, you get a lint error if you try to opt out a component that isn't already bailing out. If there's a bailout the compiler already safely skips over it, so adding `'use no memo'` there is unnecessary. Fixes facebook#31407 --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34703). * __->__ facebook#34703 * facebook#34700
1 parent 26b177b commit 19f65ff

File tree

5 files changed

+14
-262
lines changed

5 files changed

+14
-262
lines changed

compiler/packages/eslint-plugin-react-compiler/__tests__/NoUnusedDirectivesRule-test.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -161,69 +161,21 @@ function makeRule(rule: LintRule): Rule.RuleModule {
161161
};
162162
}
163163

164-
export const NoUnusedDirectivesRule: Rule.RuleModule = {
165-
meta: {
166-
type: 'suggestion',
167-
docs: {
168-
recommended: true,
169-
},
170-
fixable: 'code',
171-
hasSuggestions: true,
172-
// validation is done at runtime with zod
173-
schema: [{type: 'object', additionalProperties: true}],
174-
},
175-
create(context: Rule.RuleContext): Rule.RuleListener {
176-
const results = getReactCompilerResult(context);
177-
178-
for (const directive of results.unusedOptOutDirectives) {
179-
context.report({
180-
message: `Unused '${directive.directive}' directive`,
181-
loc: directive.loc,
182-
suggest: [
183-
{
184-
desc: 'Remove the directive',
185-
fix(fixer): Rule.Fix {
186-
return fixer.removeRange(directive.range);
187-
},
188-
},
189-
],
190-
});
191-
}
192-
return {};
193-
},
194-
};
195-
196164
type RulesConfig = {
197165
[name: string]: {rule: Rule.RuleModule; severity: ErrorSeverity};
198166
};
199167

200-
export const allRules: RulesConfig = LintRules.reduce(
201-
(acc, rule) => {
202-
acc[rule.name] = {rule: makeRule(rule), severity: rule.severity};
203-
return acc;
204-
},
205-
{
206-
'no-unused-directives': {
207-
rule: NoUnusedDirectivesRule,
208-
severity: ErrorSeverity.Error,
209-
},
210-
} as RulesConfig,
211-
);
168+
export const allRules: RulesConfig = LintRules.reduce((acc, rule) => {
169+
acc[rule.name] = {rule: makeRule(rule), severity: rule.severity};
170+
return acc;
171+
}, {} as RulesConfig);
212172

213173
export const recommendedRules: RulesConfig = LintRules.filter(
214174
rule => rule.recommended,
215-
).reduce(
216-
(acc, rule) => {
217-
acc[rule.name] = {rule: makeRule(rule), severity: rule.severity};
218-
return acc;
219-
},
220-
{
221-
'no-unused-directives': {
222-
rule: NoUnusedDirectivesRule,
223-
severity: ErrorSeverity.Error,
224-
},
225-
} as RulesConfig,
226-
);
175+
).reduce((acc, rule) => {
176+
acc[rule.name] = {rule: makeRule(rule), severity: rule.severity};
177+
return acc;
178+
}, {} as RulesConfig);
227179

228180
export function mapErrorSeverityToESlint(
229181
severity: ErrorSeverity,

compiler/packages/eslint-plugin-react-compiler/src/shared/RunReactCompiler.ts

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

8-
import {transformFromAstSync, traverse} from '@babel/core';
8+
import {transformFromAstSync} from '@babel/core';
99
import {parse as babelParse} from '@babel/parser';
10-
import {Directive, File} from '@babel/types';
10+
import {File} from '@babel/types';
1111
// @ts-expect-error: no types available
1212
import PluginProposalPrivateMethods from '@babel/plugin-proposal-private-methods';
1313
import BabelPluginReactCompiler, {
1414
parsePluginOptions,
1515
validateEnvironmentConfig,
16-
OPT_OUT_DIRECTIVES,
1716
type PluginOptions,
1817
} from 'babel-plugin-react-compiler/src';
1918
import {Logger, LoggerEvent} from 'babel-plugin-react-compiler/src/Entrypoint';
2019
import type {SourceCode} from 'eslint';
21-
import {SourceLocation} from 'estree';
2220
// @ts-expect-error: no types available
2321
import * as HermesParser from 'hermes-parser';
2422
import {isDeepStrictEqual} from 'util';
@@ -45,17 +43,11 @@ const COMPILER_OPTIONS: PluginOptions = {
4543
}),
4644
};
4745

48-
export type UnusedOptOutDirective = {
49-
loc: SourceLocation;
50-
range: [number, number];
51-
directive: string;
52-
};
5346
export type RunCacheEntry = {
5447
sourceCode: string;
5548
filename: string;
5649
userOpts: PluginOptions;
5750
flowSuppressions: Array<{line: number; code: string}>;
58-
unusedOptOutDirectives: Array<UnusedOptOutDirective>;
5951
events: Array<LoggerEvent>;
6052
};
6153

@@ -87,25 +79,6 @@ function getFlowSuppressions(
8779
return results;
8880
}
8981

90-
function filterUnusedOptOutDirectives(
91-
directives: ReadonlyArray<Directive>,
92-
): Array<UnusedOptOutDirective> {
93-
const results: Array<UnusedOptOutDirective> = [];
94-
for (const directive of directives) {
95-
if (
96-
OPT_OUT_DIRECTIVES.has(directive.value.value) &&
97-
directive.loc != null
98-
) {
99-
results.push({
100-
loc: directive.loc,
101-
directive: directive.value.value,
102-
range: [directive.start!, directive.end!],
103-
});
104-
}
105-
}
106-
return results;
107-
}
108-
10982
function runReactCompilerImpl({
11083
sourceCode,
11184
filename,
@@ -125,7 +98,6 @@ function runReactCompilerImpl({
12598
filename,
12699
userOpts,
127100
flowSuppressions: [],
128-
unusedOptOutDirectives: [],
129101
events: [],
130102
};
131103
const userLogger: Logger | null = options.logger;
@@ -181,29 +153,6 @@ function runReactCompilerImpl({
181153
configFile: false,
182154
babelrc: false,
183155
});
184-
185-
if (results.events.filter(e => e.kind === 'CompileError').length === 0) {
186-
traverse(babelAST, {
187-
FunctionDeclaration(path) {
188-
path.node;
189-
results.unusedOptOutDirectives.push(
190-
...filterUnusedOptOutDirectives(path.node.body.directives),
191-
);
192-
},
193-
ArrowFunctionExpression(path) {
194-
if (path.node.body.type === 'BlockStatement') {
195-
results.unusedOptOutDirectives.push(
196-
...filterUnusedOptOutDirectives(path.node.body.directives),
197-
);
198-
}
199-
},
200-
FunctionExpression(path) {
201-
results.unusedOptOutDirectives.push(
202-
...filterUnusedOptOutDirectives(path.node.body.directives),
203-
);
204-
},
205-
});
206-
}
207156
} catch (err) {
208157
/* errors handled by injected logger */
209158
}

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

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -160,38 +160,6 @@ function makeRule(rule: LintRule): Rule.RuleModule {
160160
};
161161
}
162162

163-
export const NoUnusedDirectivesRule: Rule.RuleModule = {
164-
meta: {
165-
type: 'suggestion',
166-
docs: {
167-
recommended: true,
168-
},
169-
fixable: 'code',
170-
hasSuggestions: true,
171-
// validation is done at runtime with zod
172-
schema: [{type: 'object', additionalProperties: true}],
173-
},
174-
create(context: Rule.RuleContext): Rule.RuleListener {
175-
const results = getReactCompilerResult(context);
176-
177-
for (const directive of results.unusedOptOutDirectives) {
178-
context.report({
179-
message: `Unused '${directive.directive}' directive`,
180-
loc: directive.loc,
181-
suggest: [
182-
{
183-
desc: 'Remove the directive',
184-
fix(fixer): Rule.Fix {
185-
return fixer.removeRange(directive.range);
186-
},
187-
},
188-
],
189-
});
190-
}
191-
return {};
192-
},
193-
};
194-
195163
type RulesConfig = {
196164
[name: string]: {rule: Rule.RuleModule; severity: ErrorSeverity};
197165
};
@@ -201,12 +169,7 @@ export const allRules: RulesConfig = LintRules.reduce(
201169
acc[rule.name] = {rule: makeRule(rule), severity: rule.severity};
202170
return acc;
203171
},
204-
{
205-
'no-unused-directives': {
206-
rule: NoUnusedDirectivesRule,
207-
severity: ErrorSeverity.Error,
208-
},
209-
} as RulesConfig,
172+
{} as RulesConfig,
210173
);
211174

212175
export const recommendedRules: RulesConfig = LintRules.filter(
@@ -216,12 +179,7 @@ export const recommendedRules: RulesConfig = LintRules.filter(
216179
acc[rule.name] = {rule: makeRule(rule), severity: rule.severity};
217180
return acc;
218181
},
219-
{
220-
'no-unused-directives': {
221-
rule: NoUnusedDirectivesRule,
222-
severity: ErrorSeverity.Error,
223-
},
224-
} as RulesConfig,
182+
{} as RulesConfig,
225183
);
226184

227185
export function mapErrorSeverityToESlint(

0 commit comments

Comments
 (0)