Inline repo
This commit is contained in:
@ -8,20 +8,22 @@
|
||||
/* eslint-disable no-for-of-loops/no-for-of-loops */
|
||||
import type { Rule, Scope } from 'eslint';
|
||||
import type {
|
||||
FunctionDeclaration,
|
||||
CallExpression,
|
||||
Expression,
|
||||
Super,
|
||||
Node,
|
||||
ArrayExpression,
|
||||
ArrowFunctionExpression,
|
||||
FunctionExpression,
|
||||
SpreadElement,
|
||||
Identifier,
|
||||
VariableDeclarator,
|
||||
MemberExpression,
|
||||
CallExpression,
|
||||
ChainExpression,
|
||||
Pattern,
|
||||
Expression,
|
||||
FunctionDeclaration,
|
||||
FunctionExpression,
|
||||
Identifier,
|
||||
MemberExpression,
|
||||
Node,
|
||||
OptionalMemberExpression,
|
||||
Pattern,
|
||||
SpreadElement,
|
||||
Super,
|
||||
TSAsExpression,
|
||||
VariableDeclarator,
|
||||
} from 'estree';
|
||||
import type { FromSchema } from 'json-schema-to-ts';
|
||||
import { __EXPERIMENTAL__ } from './index';
|
||||
@ -109,7 +111,7 @@ const rule: Rule.RuleModule = {
|
||||
*/
|
||||
function visitFunctionWithDependencies(
|
||||
node: ArrowFunctionExpression | FunctionExpression | FunctionDeclaration,
|
||||
declaredDependenciesNode: SpreadElement | Expression,
|
||||
declaredDependenciesNode: SpreadElement | Expression | undefined,
|
||||
reactiveHook: Super | Expression,
|
||||
reactiveHookName: string,
|
||||
isEffect: boolean,
|
||||
@ -128,7 +130,7 @@ const rule: Rule.RuleModule = {
|
||||
' }\n' +
|
||||
' fetchData();\n' +
|
||||
`}, [someId]); // Or [] if effect doesn't need props or state\n\n` +
|
||||
'Learn more about data fetching with Hooks: https://reactjs.org/link/hooks-data-fetching',
|
||||
'Learn more about data fetching with Hooks: https://react.dev/link/hooks-data-fetching',
|
||||
});
|
||||
}
|
||||
|
||||
@ -194,7 +196,7 @@ const rule: Rule.RuleModule = {
|
||||
if (init == null) {
|
||||
return false;
|
||||
}
|
||||
while (init.type === 'TSAsExpression') {
|
||||
while (init.type === 'TSAsExpression' || init.type === 'AsExpression') {
|
||||
init = init.expression;
|
||||
}
|
||||
// Detect primitive constants
|
||||
@ -620,7 +622,11 @@ const rule: Rule.RuleModule = {
|
||||
|
||||
const declaredDependencies: DeclaredDependency[] = [];
|
||||
const externalDependencies = new Set<string>();
|
||||
if (declaredDependenciesNode.type !== 'ArrayExpression') {
|
||||
const isArrayExpression = declaredDependenciesNode.type === 'ArrayExpression';
|
||||
const isTSAsArrayExpression =
|
||||
declaredDependenciesNode.type === 'TSAsExpression' &&
|
||||
declaredDependenciesNode.expression.type === 'ArrayExpression';
|
||||
if (!isArrayExpression && !isTSAsArrayExpression) {
|
||||
// If the declared dependencies are not an array expression then we
|
||||
// can't verify that the user provided the correct dependencies. Tell
|
||||
// the user this in an error.
|
||||
@ -633,7 +639,10 @@ const rule: Rule.RuleModule = {
|
||||
'dependencies.',
|
||||
});
|
||||
} else {
|
||||
declaredDependenciesNode.elements.forEach(declaredDependencyNode => {
|
||||
const arrayExpression = isTSAsArrayExpression
|
||||
? ((declaredDependenciesNode as TSAsExpression).expression as ArrayExpression)
|
||||
: (declaredDependenciesNode as ArrayExpression);
|
||||
arrayExpression.elements.forEach(declaredDependencyNode => {
|
||||
// Skip elided elements.
|
||||
if (declaredDependencyNode === null) {
|
||||
return;
|
||||
@ -1158,7 +1167,11 @@ const rule: Rule.RuleModule = {
|
||||
const reactiveHook = node.callee as Identifier | MemberExpression;
|
||||
const reactiveHookName = (getNodeWithoutReactNamespace(reactiveHook) as Identifier)
|
||||
.name;
|
||||
const declaredDependenciesNode = node.arguments[callbackIndex + 1];
|
||||
const maybeNode = node.arguments[callbackIndex + 1];
|
||||
const declaredDependenciesNode =
|
||||
maybeNode && !(maybeNode.type === 'Identifier' && maybeNode.name === 'undefined')
|
||||
? maybeNode
|
||||
: undefined;
|
||||
const isEffect = /Effect($|[^a-z])/g.test(reactiveHookName);
|
||||
|
||||
// Check whether a callback is supplied. If there is no callback supplied
|
||||
@ -1203,6 +1216,15 @@ const rule: Rule.RuleModule = {
|
||||
isEffect,
|
||||
);
|
||||
return; // Handled
|
||||
case 'TSAsExpression':
|
||||
visitFunctionWithDependencies(
|
||||
callback.expression,
|
||||
declaredDependenciesNode,
|
||||
reactiveHook,
|
||||
reactiveHookName,
|
||||
isEffect,
|
||||
);
|
||||
return; // Handled
|
||||
case 'Identifier':
|
||||
if (!declaredDependenciesNode) {
|
||||
// No deps, no problems.
|
||||
@ -1545,7 +1567,7 @@ function getConstructionExpressionType(node: Node) {
|
||||
}
|
||||
return null;
|
||||
case 'TypeCastExpression':
|
||||
return getConstructionExpressionType(node.expression);
|
||||
case 'AsExpression':
|
||||
case 'TSAsExpression':
|
||||
return getConstructionExpressionType(node.expression);
|
||||
}
|
||||
@ -1749,7 +1771,7 @@ function analyzePropertyChain(
|
||||
}
|
||||
}
|
||||
|
||||
function getNodeWithoutReactNamespace(node: Identifier | MemberExpression) {
|
||||
function getNodeWithoutReactNamespace(node: Expression | Super) {
|
||||
if (
|
||||
node.type === 'MemberExpression' &&
|
||||
node.object.type === 'Identifier' &&
|
||||
|
@ -24,10 +24,7 @@ import { __EXPERIMENTAL__ } from './index';
|
||||
*/
|
||||
|
||||
function isHookName(s: string) {
|
||||
if (__EXPERIMENTAL__) {
|
||||
return s === 'use' || /^use[A-Z0-9]/.test(s);
|
||||
}
|
||||
return /^use[A-Z0-9]/.test(s);
|
||||
return s === 'use' || /^use[A-Z0-9]/.test(s);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -59,7 +56,7 @@ function isComponentName(node: Node) {
|
||||
return node.type === 'Identifier' && /^[A-Z]/.test(node.name);
|
||||
}
|
||||
|
||||
function isReactFunction(node: Expression | Super, functionName: string) {
|
||||
function isReactFunction(node: Node, functionName: string) {
|
||||
return (
|
||||
(node as Identifier).name === functionName ||
|
||||
(node.type === 'MemberExpression' &&
|
||||
@ -115,10 +112,7 @@ function isUseEffectEventIdentifier(node: Node) {
|
||||
}
|
||||
|
||||
function isUseIdentifier(node: Node) {
|
||||
if (__EXPERIMENTAL__) {
|
||||
return node.type === 'Identifier' && node.name === 'use';
|
||||
}
|
||||
return false;
|
||||
return isReactFunction(node, 'use');
|
||||
}
|
||||
|
||||
const rule: Rule.RuleModule = {
|
||||
|
@ -3,8 +3,8 @@
|
||||
"version": 1,
|
||||
"sources": {
|
||||
"main": {
|
||||
"repository": "git@github.com:facebook/react.git",
|
||||
"commit": "899cb95f52cc83ab5ca1eb1e268c909d3f0961e7",
|
||||
"repository": "https://github.com/facebook/react",
|
||||
"commit": "0e0b69321a6fcfe8a3eaae3b1016beb110437b38",
|
||||
"branch": "main"
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user