@eslint-react/jsx 0.9.3-beta.1 → 0.9.3-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +32 -27
- package/dist/index.d.mts +13 -7
- package/dist/index.d.ts +13 -7
- package/dist/index.js +32 -27
- package/dist/index.mjs +32 -27
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -48,13 +48,13 @@ function getFragmentFromContext(context) {
|
|
|
48
48
|
const getPragmaFromContext = memo((context)=>{
|
|
49
49
|
// eslint-disable-next-line prefer-destructuring
|
|
50
50
|
const settings = context.settings;
|
|
51
|
-
const sourceCode = context
|
|
51
|
+
const { sourceCode } = context;
|
|
52
52
|
const pragmaNode = sourceCode.getAllComments().find((node)=>RE_JSX_ANNOTATION_REGEX.test(node.value));
|
|
53
53
|
return tools.F.pipe(tools.O.orElse(tools.O.fromNullable(settings.react?.pragma), ()=>tools.F.pipe(tools.O.fromNullable(pragmaNode), tools.O.map(({ value })=>RE_JSX_ANNOTATION_REGEX.exec(value)), tools.O.flatMapNullable((matches)=>matches?.[1]?.split(".")[0]))), tools.O.flatMap(tools.O.liftPredicate((x)=>RE_JS_IDENTIFIER_REGEX.test(x))), tools.O.getOrElse(tools.F.constant("React")));
|
|
54
54
|
});
|
|
55
55
|
|
|
56
|
-
function isInitializedFromPragma(variableName, context, pragma = getPragmaFromContext(context)) {
|
|
57
|
-
const variables = ast.getVariablesUpToGlobal(
|
|
56
|
+
function isInitializedFromPragma(variableName, context, initialScope, pragma = getPragmaFromContext(context)) {
|
|
57
|
+
const variables = ast.getVariablesUpToGlobal(initialScope);
|
|
58
58
|
const maybeVariable = ast.findVariableByName(variableName)(variables);
|
|
59
59
|
const maybeLatestDef = tools.O.flatMapNullable(maybeVariable, (variable)=>variable.defs.at(-1));
|
|
60
60
|
if (tools.O.isNone(maybeLatestDef)) {
|
|
@@ -135,6 +135,7 @@ function isPropertyOfPragma(name, context, pragma = getPragmaFromContext(context
|
|
|
135
135
|
* @returns A predicate that checks if the given node is a call expression to the given function or method
|
|
136
136
|
*/ function isCallFromPragma(name) {
|
|
137
137
|
return (node, context)=>{
|
|
138
|
+
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
138
139
|
if (node.type !== ast.NodeType.CallExpression || !("callee" in node)) {
|
|
139
140
|
return false;
|
|
140
141
|
}
|
|
@@ -142,7 +143,7 @@ function isPropertyOfPragma(name, context, pragma = getPragmaFromContext(context
|
|
|
142
143
|
return isPropertyOfPragma(name, context)(node.callee);
|
|
143
144
|
}
|
|
144
145
|
if ("name" in node.callee && node.callee.name === name) {
|
|
145
|
-
return isInitializedFromPragma(name, context);
|
|
146
|
+
return isInitializedFromPragma(name, context, initialScope);
|
|
146
147
|
}
|
|
147
148
|
return false;
|
|
148
149
|
};
|
|
@@ -454,7 +455,8 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
454
455
|
if (ast.isJSXTagNameExpression(node)) {
|
|
455
456
|
return true;
|
|
456
457
|
}
|
|
457
|
-
const
|
|
458
|
+
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
459
|
+
const maybeVariable = ast.findVariableByNameUpToGlobal(name, initialScope);
|
|
458
460
|
return tools.F.pipe(maybeVariable, tools.O.flatMap(ast.getVariableInit(0)), tools.O.exists((n)=>isJSXValue(n, context, hint)));
|
|
459
461
|
}).otherwise(tools.F.constFalse);
|
|
460
462
|
}
|
|
@@ -480,8 +482,8 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
480
482
|
*/ function getPropName(node) {
|
|
481
483
|
return tools.M.match(node.name).when(ast.is(ast.NodeType.JSXIdentifier), (n)=>n.name).when(ast.is(ast.NodeType.JSXNamespacedName), (n)=>`${n.namespace.name}:${n.name.name}`).exhaustive();
|
|
482
484
|
}
|
|
483
|
-
function getProp(props, propName, context) {
|
|
484
|
-
return findPropInAttributes(props, context)(propName);
|
|
485
|
+
function getProp(props, propName, context, initialScope) {
|
|
486
|
+
return findPropInAttributes(props, context, initialScope)(propName);
|
|
485
487
|
}
|
|
486
488
|
/**
|
|
487
489
|
* Gets and resolves the static value of a JSX attribute
|
|
@@ -489,30 +491,30 @@ function getProp(props, propName, context) {
|
|
|
489
491
|
* @param context The rule context
|
|
490
492
|
* @returns The static value of the given JSX attribute
|
|
491
493
|
*/ function getPropValue(attribute, context) {
|
|
492
|
-
const
|
|
494
|
+
const initialScope = context.sourceCode.getScope?.(attribute) ?? context.getScope();
|
|
493
495
|
if (attribute.type === ast.NodeType.JSXAttribute && "value" in attribute) {
|
|
494
496
|
const { value } = attribute;
|
|
495
497
|
if (value === null) {
|
|
496
498
|
return tools.O.none();
|
|
497
499
|
}
|
|
498
500
|
if (value.type === ast.NodeType.Literal) {
|
|
499
|
-
return tools.O.some(ast.getStaticValue(value,
|
|
501
|
+
return tools.O.some(ast.getStaticValue(value, initialScope));
|
|
500
502
|
}
|
|
501
503
|
if (value.type === ast.NodeType.JSXExpressionContainer) {
|
|
502
|
-
return tools.O.some(ast.getStaticValue(value.expression,
|
|
504
|
+
return tools.O.some(ast.getStaticValue(value.expression, initialScope));
|
|
503
505
|
}
|
|
504
506
|
return tools.O.none();
|
|
505
507
|
}
|
|
506
508
|
const { argument } = attribute;
|
|
507
|
-
return tools.O.some(ast.getStaticValue(argument,
|
|
509
|
+
return tools.O.some(ast.getStaticValue(argument, initialScope));
|
|
508
510
|
}
|
|
509
511
|
/**
|
|
510
512
|
* @param properties The properties to search in
|
|
511
513
|
* @param context The rule context
|
|
514
|
+
* @param initialScope
|
|
512
515
|
* @param seenProps The properties that have already been seen
|
|
513
516
|
* @returns A function that searches for a property in the given properties
|
|
514
|
-
*/ function findPropInProperties(properties, context, seenProps = []) {
|
|
515
|
-
const startScope = context.getScope();
|
|
517
|
+
*/ function findPropInProperties(properties, context, initialScope, seenProps = []) {
|
|
516
518
|
/**
|
|
517
519
|
* Search for a property in the given properties
|
|
518
520
|
* @param propName The name of the property to search for
|
|
@@ -524,7 +526,7 @@ function getProp(props, propName, context) {
|
|
|
524
526
|
}).when(ast.is(ast.NodeType.SpreadElement), (prop)=>{
|
|
525
527
|
return tools.M.match(prop.argument).when(ast.is(ast.NodeType.Identifier), (argument)=>{
|
|
526
528
|
const { name } = argument;
|
|
527
|
-
const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name,
|
|
529
|
+
const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name, initialScope), ast.getVariableInit(0));
|
|
528
530
|
if (tools.O.isNone(maybeInit)) {
|
|
529
531
|
return false;
|
|
530
532
|
}
|
|
@@ -535,12 +537,12 @@ function getProp(props, propName, context) {
|
|
|
535
537
|
if (seenProps.includes(name)) {
|
|
536
538
|
return false;
|
|
537
539
|
}
|
|
538
|
-
return tools.O.isSome(findPropInProperties(init.properties, context, [
|
|
540
|
+
return tools.O.isSome(findPropInProperties(init.properties, context, initialScope, [
|
|
539
541
|
...seenProps,
|
|
540
542
|
name
|
|
541
543
|
])(propName));
|
|
542
544
|
}).when(ast.is(ast.NodeType.ObjectExpression), (argument)=>{
|
|
543
|
-
return tools.O.isSome(findPropInProperties(argument.properties, context, seenProps)(propName));
|
|
545
|
+
return tools.O.isSome(findPropInProperties(argument.properties, context, initialScope, seenProps)(propName));
|
|
544
546
|
}).when(ast.is(ast.NodeType.MemberExpression), ()=>{
|
|
545
547
|
// Not implemented
|
|
546
548
|
}).when(ast.is(ast.NodeType.CallExpression), ()=>{
|
|
@@ -556,9 +558,9 @@ function getProp(props, propName, context) {
|
|
|
556
558
|
/**
|
|
557
559
|
* @param attributes The attributes to search in
|
|
558
560
|
* @param context The rule context
|
|
561
|
+
* @param initialScope
|
|
559
562
|
* @returns A function that searches for a property in the given attributes
|
|
560
|
-
*/ function findPropInAttributes(attributes, context) {
|
|
561
|
-
const startScope = context.getScope();
|
|
563
|
+
*/ function findPropInAttributes(attributes, context, initialScope) {
|
|
562
564
|
/**
|
|
563
565
|
* Search for a property in the given attributes
|
|
564
566
|
* @param propName The name of the property to search for
|
|
@@ -570,7 +572,7 @@ function getProp(props, propName, context) {
|
|
|
570
572
|
type: ast.NodeType.Identifier
|
|
571
573
|
}, (argument)=>{
|
|
572
574
|
const { name } = argument;
|
|
573
|
-
const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name,
|
|
575
|
+
const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name, initialScope), ast.getVariableInit(0));
|
|
574
576
|
if (tools.O.isNone(maybeInit)) {
|
|
575
577
|
return false;
|
|
576
578
|
}
|
|
@@ -578,9 +580,9 @@ function getProp(props, propName, context) {
|
|
|
578
580
|
if (!("properties" in init)) {
|
|
579
581
|
return false;
|
|
580
582
|
}
|
|
581
|
-
return tools.O.isSome(findPropInProperties(init.properties, context)(propName));
|
|
583
|
+
return tools.O.isSome(findPropInProperties(init.properties, context, initialScope)(propName));
|
|
582
584
|
}).when(ast.is(ast.NodeType.ObjectExpression), (argument)=>{
|
|
583
|
-
return tools.O.isSome(findPropInProperties(argument.properties, context)(propName));
|
|
585
|
+
return tools.O.isSome(findPropInProperties(argument.properties, context, initialScope)(propName));
|
|
584
586
|
}).when(ast.is(ast.NodeType.MemberExpression), ()=>{
|
|
585
587
|
// Not implemented
|
|
586
588
|
return false;
|
|
@@ -598,27 +600,30 @@ function getProp(props, propName, context) {
|
|
|
598
600
|
* @param attributes The attributes to search in
|
|
599
601
|
* @param propName The prop name to search for
|
|
600
602
|
* @param context The rule context
|
|
603
|
+
* @param initialScope
|
|
601
604
|
* @returns `true` if the given prop name is present in the given properties
|
|
602
|
-
*/ function hasProp(attributes, propName, context) {
|
|
603
|
-
return tools.O.isSome(findPropInAttributes(attributes, context)(propName));
|
|
605
|
+
*/ function hasProp(attributes, propName, context, initialScope) {
|
|
606
|
+
return tools.O.isSome(findPropInAttributes(attributes, context, initialScope)(propName));
|
|
604
607
|
}
|
|
605
608
|
/**
|
|
606
609
|
* Check if any of the given prop names are present in the given attributes
|
|
607
610
|
* @param attributes The attributes to search in
|
|
608
611
|
* @param propNames The prop names to search for
|
|
609
612
|
* @param context The rule context
|
|
613
|
+
* @param initialScope
|
|
610
614
|
* @returns `true` if any of the given prop names are present in the given attributes
|
|
611
|
-
*/ function hasAnyProp(attributes, propNames, context) {
|
|
612
|
-
return propNames.some((propName)=>hasProp(attributes, propName, context));
|
|
615
|
+
*/ function hasAnyProp(attributes, propNames, context, initialScope) {
|
|
616
|
+
return propNames.some((propName)=>hasProp(attributes, propName, context, initialScope));
|
|
613
617
|
}
|
|
614
618
|
/**
|
|
615
619
|
* Check if all of the given prop names are present in the given attributes
|
|
616
620
|
* @param attributes The attributes to search in
|
|
617
621
|
* @param propNames The prop names to search for
|
|
618
622
|
* @param context The rule context
|
|
623
|
+
* @param initialScope
|
|
619
624
|
* @returns `true` if all of the given prop names are present in the given attributes
|
|
620
|
-
*/ function hasEveryProp(attributes, propNames, context) {
|
|
621
|
-
return propNames.every((propName)=>hasProp(attributes, propName, context));
|
|
625
|
+
*/ function hasEveryProp(attributes, propNames, context, initialScope) {
|
|
626
|
+
return propNames.every((propName)=>hasProp(attributes, propName, context, initialScope));
|
|
622
627
|
}
|
|
623
628
|
|
|
624
629
|
/**
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { TSESTree } from '@typescript-eslint/types';
|
|
2
2
|
import { RuleContext } from '@eslint-react/shared';
|
|
3
|
+
import { Scope } from '@typescript-eslint/scope-manager';
|
|
3
4
|
import { TSESTreeFunction } from '@eslint-react/ast';
|
|
4
5
|
import { O } from '@eslint-react/tools';
|
|
5
6
|
import { TSESTree as TSESTree$1 } from '@typescript-eslint/utils';
|
|
@@ -51,7 +52,7 @@ declare function isJSXElementOfBuiltinComponent(node: TSESTree.Node): node is TS
|
|
|
51
52
|
declare function getFragmentFromContext<T extends RuleContext>(context: T): string;
|
|
52
53
|
declare const getPragmaFromContext: <T extends RuleContext>(context: T) => string;
|
|
53
54
|
|
|
54
|
-
declare function isInitializedFromPragma(variableName: string, context: RuleContext, pragma?: string): boolean;
|
|
55
|
+
declare function isInitializedFromPragma(variableName: string, context: RuleContext, initialScope: Scope, pragma?: string): boolean;
|
|
55
56
|
declare function isPropertyOfPragma(name: string, context: RuleContext, pragma?: string): (node: TSESTree.Node) => boolean;
|
|
56
57
|
type CallFromPragmaPredicate = (node: TSESTree.Node, context: RuleContext) => node is TSESTree.CallExpression;
|
|
57
58
|
/**
|
|
@@ -119,7 +120,7 @@ declare function isFunctionReturningJSXValue(node: TSESTreeFunction, context: Ru
|
|
|
119
120
|
* @returns string
|
|
120
121
|
*/
|
|
121
122
|
declare function getPropName(node: TSESTree$1.JSXAttribute): string;
|
|
122
|
-
declare function getProp(props: (TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute)[], propName: string, context: RuleContext): O.Option<TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute>;
|
|
123
|
+
declare function getProp(props: (TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute)[], propName: string, context: RuleContext, initialScope: Scope): O.Option<TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute>;
|
|
123
124
|
/**
|
|
124
125
|
* Gets and resolves the static value of a JSX attribute
|
|
125
126
|
* @param attribute The JSX attribute to get the value of
|
|
@@ -134,41 +135,46 @@ declare function getPropValue(attribute: TSESTree$1.JSXAttribute | TSESTree$1.JS
|
|
|
134
135
|
/**
|
|
135
136
|
* @param properties The properties to search in
|
|
136
137
|
* @param context The rule context
|
|
138
|
+
* @param initialScope
|
|
137
139
|
* @param seenProps The properties that have already been seen
|
|
138
140
|
* @returns A function that searches for a property in the given properties
|
|
139
141
|
*/
|
|
140
|
-
declare function findPropInProperties(properties: (TSESTree$1.Property | TSESTree$1.RestElement | TSESTree$1.SpreadElement)[] | TSESTree$1.ObjectLiteralElement[], context: RuleContext, seenProps?: string[]): (propName: string) => O.Option<(typeof properties)[number]>;
|
|
142
|
+
declare function findPropInProperties(properties: (TSESTree$1.Property | TSESTree$1.RestElement | TSESTree$1.SpreadElement)[] | TSESTree$1.ObjectLiteralElement[], context: RuleContext, initialScope: Scope, seenProps?: string[]): (propName: string) => O.Option<(typeof properties)[number]>;
|
|
141
143
|
/**
|
|
142
144
|
* @param attributes The attributes to search in
|
|
143
145
|
* @param context The rule context
|
|
146
|
+
* @param initialScope
|
|
144
147
|
* @returns A function that searches for a property in the given attributes
|
|
145
148
|
*/
|
|
146
|
-
declare function findPropInAttributes(attributes: (TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute)[], context: RuleContext): (propName: string) => O.Option<NonNullable<TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute | undefined>>;
|
|
149
|
+
declare function findPropInAttributes(attributes: (TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute)[], context: RuleContext, initialScope: Scope): (propName: string) => O.Option<NonNullable<TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute | undefined>>;
|
|
147
150
|
|
|
148
151
|
/**
|
|
149
152
|
* Check if the given prop name is present in the given attributes
|
|
150
153
|
* @param attributes The attributes to search in
|
|
151
154
|
* @param propName The prop name to search for
|
|
152
155
|
* @param context The rule context
|
|
156
|
+
* @param initialScope
|
|
153
157
|
* @returns `true` if the given prop name is present in the given properties
|
|
154
158
|
*/
|
|
155
|
-
declare function hasProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propName: string, context: RuleContext): boolean;
|
|
159
|
+
declare function hasProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propName: string, context: RuleContext, initialScope: Scope): boolean;
|
|
156
160
|
/**
|
|
157
161
|
* Check if any of the given prop names are present in the given attributes
|
|
158
162
|
* @param attributes The attributes to search in
|
|
159
163
|
* @param propNames The prop names to search for
|
|
160
164
|
* @param context The rule context
|
|
165
|
+
* @param initialScope
|
|
161
166
|
* @returns `true` if any of the given prop names are present in the given attributes
|
|
162
167
|
*/
|
|
163
|
-
declare function hasAnyProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propNames: string[], context: RuleContext): boolean;
|
|
168
|
+
declare function hasAnyProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propNames: string[], context: RuleContext, initialScope: Scope): boolean;
|
|
164
169
|
/**
|
|
165
170
|
* Check if all of the given prop names are present in the given attributes
|
|
166
171
|
* @param attributes The attributes to search in
|
|
167
172
|
* @param propNames The prop names to search for
|
|
168
173
|
* @param context The rule context
|
|
174
|
+
* @param initialScope
|
|
169
175
|
* @returns `true` if all of the given prop names are present in the given attributes
|
|
170
176
|
*/
|
|
171
|
-
declare function hasEveryProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propNames: string[], context: RuleContext): boolean;
|
|
177
|
+
declare function hasEveryProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propNames: string[], context: RuleContext, initialScope: Scope): boolean;
|
|
172
178
|
|
|
173
179
|
/**
|
|
174
180
|
* Checks if the node is inside a prop's value
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { TSESTree } from '@typescript-eslint/types';
|
|
2
2
|
import { RuleContext } from '@eslint-react/shared';
|
|
3
|
+
import { Scope } from '@typescript-eslint/scope-manager';
|
|
3
4
|
import { TSESTreeFunction } from '@eslint-react/ast';
|
|
4
5
|
import { O } from '@eslint-react/tools';
|
|
5
6
|
import { TSESTree as TSESTree$1 } from '@typescript-eslint/utils';
|
|
@@ -51,7 +52,7 @@ declare function isJSXElementOfBuiltinComponent(node: TSESTree.Node): node is TS
|
|
|
51
52
|
declare function getFragmentFromContext<T extends RuleContext>(context: T): string;
|
|
52
53
|
declare const getPragmaFromContext: <T extends RuleContext>(context: T) => string;
|
|
53
54
|
|
|
54
|
-
declare function isInitializedFromPragma(variableName: string, context: RuleContext, pragma?: string): boolean;
|
|
55
|
+
declare function isInitializedFromPragma(variableName: string, context: RuleContext, initialScope: Scope, pragma?: string): boolean;
|
|
55
56
|
declare function isPropertyOfPragma(name: string, context: RuleContext, pragma?: string): (node: TSESTree.Node) => boolean;
|
|
56
57
|
type CallFromPragmaPredicate = (node: TSESTree.Node, context: RuleContext) => node is TSESTree.CallExpression;
|
|
57
58
|
/**
|
|
@@ -119,7 +120,7 @@ declare function isFunctionReturningJSXValue(node: TSESTreeFunction, context: Ru
|
|
|
119
120
|
* @returns string
|
|
120
121
|
*/
|
|
121
122
|
declare function getPropName(node: TSESTree$1.JSXAttribute): string;
|
|
122
|
-
declare function getProp(props: (TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute)[], propName: string, context: RuleContext): O.Option<TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute>;
|
|
123
|
+
declare function getProp(props: (TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute)[], propName: string, context: RuleContext, initialScope: Scope): O.Option<TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute>;
|
|
123
124
|
/**
|
|
124
125
|
* Gets and resolves the static value of a JSX attribute
|
|
125
126
|
* @param attribute The JSX attribute to get the value of
|
|
@@ -134,41 +135,46 @@ declare function getPropValue(attribute: TSESTree$1.JSXAttribute | TSESTree$1.JS
|
|
|
134
135
|
/**
|
|
135
136
|
* @param properties The properties to search in
|
|
136
137
|
* @param context The rule context
|
|
138
|
+
* @param initialScope
|
|
137
139
|
* @param seenProps The properties that have already been seen
|
|
138
140
|
* @returns A function that searches for a property in the given properties
|
|
139
141
|
*/
|
|
140
|
-
declare function findPropInProperties(properties: (TSESTree$1.Property | TSESTree$1.RestElement | TSESTree$1.SpreadElement)[] | TSESTree$1.ObjectLiteralElement[], context: RuleContext, seenProps?: string[]): (propName: string) => O.Option<(typeof properties)[number]>;
|
|
142
|
+
declare function findPropInProperties(properties: (TSESTree$1.Property | TSESTree$1.RestElement | TSESTree$1.SpreadElement)[] | TSESTree$1.ObjectLiteralElement[], context: RuleContext, initialScope: Scope, seenProps?: string[]): (propName: string) => O.Option<(typeof properties)[number]>;
|
|
141
143
|
/**
|
|
142
144
|
* @param attributes The attributes to search in
|
|
143
145
|
* @param context The rule context
|
|
146
|
+
* @param initialScope
|
|
144
147
|
* @returns A function that searches for a property in the given attributes
|
|
145
148
|
*/
|
|
146
|
-
declare function findPropInAttributes(attributes: (TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute)[], context: RuleContext): (propName: string) => O.Option<NonNullable<TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute | undefined>>;
|
|
149
|
+
declare function findPropInAttributes(attributes: (TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute)[], context: RuleContext, initialScope: Scope): (propName: string) => O.Option<NonNullable<TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute | undefined>>;
|
|
147
150
|
|
|
148
151
|
/**
|
|
149
152
|
* Check if the given prop name is present in the given attributes
|
|
150
153
|
* @param attributes The attributes to search in
|
|
151
154
|
* @param propName The prop name to search for
|
|
152
155
|
* @param context The rule context
|
|
156
|
+
* @param initialScope
|
|
153
157
|
* @returns `true` if the given prop name is present in the given properties
|
|
154
158
|
*/
|
|
155
|
-
declare function hasProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propName: string, context: RuleContext): boolean;
|
|
159
|
+
declare function hasProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propName: string, context: RuleContext, initialScope: Scope): boolean;
|
|
156
160
|
/**
|
|
157
161
|
* Check if any of the given prop names are present in the given attributes
|
|
158
162
|
* @param attributes The attributes to search in
|
|
159
163
|
* @param propNames The prop names to search for
|
|
160
164
|
* @param context The rule context
|
|
165
|
+
* @param initialScope
|
|
161
166
|
* @returns `true` if any of the given prop names are present in the given attributes
|
|
162
167
|
*/
|
|
163
|
-
declare function hasAnyProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propNames: string[], context: RuleContext): boolean;
|
|
168
|
+
declare function hasAnyProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propNames: string[], context: RuleContext, initialScope: Scope): boolean;
|
|
164
169
|
/**
|
|
165
170
|
* Check if all of the given prop names are present in the given attributes
|
|
166
171
|
* @param attributes The attributes to search in
|
|
167
172
|
* @param propNames The prop names to search for
|
|
168
173
|
* @param context The rule context
|
|
174
|
+
* @param initialScope
|
|
169
175
|
* @returns `true` if all of the given prop names are present in the given attributes
|
|
170
176
|
*/
|
|
171
|
-
declare function hasEveryProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propNames: string[], context: RuleContext): boolean;
|
|
177
|
+
declare function hasEveryProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propNames: string[], context: RuleContext, initialScope: Scope): boolean;
|
|
172
178
|
|
|
173
179
|
/**
|
|
174
180
|
* Checks if the node is inside a prop's value
|
package/dist/index.js
CHANGED
|
@@ -48,13 +48,13 @@ function getFragmentFromContext(context) {
|
|
|
48
48
|
const getPragmaFromContext = memo((context)=>{
|
|
49
49
|
// eslint-disable-next-line prefer-destructuring
|
|
50
50
|
const settings = context.settings;
|
|
51
|
-
const sourceCode = context
|
|
51
|
+
const { sourceCode } = context;
|
|
52
52
|
const pragmaNode = sourceCode.getAllComments().find((node)=>RE_JSX_ANNOTATION_REGEX.test(node.value));
|
|
53
53
|
return tools.F.pipe(tools.O.orElse(tools.O.fromNullable(settings.react?.pragma), ()=>tools.F.pipe(tools.O.fromNullable(pragmaNode), tools.O.map(({ value })=>RE_JSX_ANNOTATION_REGEX.exec(value)), tools.O.flatMapNullable((matches)=>matches?.[1]?.split(".")[0]))), tools.O.flatMap(tools.O.liftPredicate((x)=>RE_JS_IDENTIFIER_REGEX.test(x))), tools.O.getOrElse(tools.F.constant("React")));
|
|
54
54
|
});
|
|
55
55
|
|
|
56
|
-
function isInitializedFromPragma(variableName, context, pragma = getPragmaFromContext(context)) {
|
|
57
|
-
const variables = ast.getVariablesUpToGlobal(
|
|
56
|
+
function isInitializedFromPragma(variableName, context, initialScope, pragma = getPragmaFromContext(context)) {
|
|
57
|
+
const variables = ast.getVariablesUpToGlobal(initialScope);
|
|
58
58
|
const maybeVariable = ast.findVariableByName(variableName)(variables);
|
|
59
59
|
const maybeLatestDef = tools.O.flatMapNullable(maybeVariable, (variable)=>variable.defs.at(-1));
|
|
60
60
|
if (tools.O.isNone(maybeLatestDef)) {
|
|
@@ -135,6 +135,7 @@ function isPropertyOfPragma(name, context, pragma = getPragmaFromContext(context
|
|
|
135
135
|
* @returns A predicate that checks if the given node is a call expression to the given function or method
|
|
136
136
|
*/ function isCallFromPragma(name) {
|
|
137
137
|
return (node, context)=>{
|
|
138
|
+
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
138
139
|
if (node.type !== ast.NodeType.CallExpression || !("callee" in node)) {
|
|
139
140
|
return false;
|
|
140
141
|
}
|
|
@@ -142,7 +143,7 @@ function isPropertyOfPragma(name, context, pragma = getPragmaFromContext(context
|
|
|
142
143
|
return isPropertyOfPragma(name, context)(node.callee);
|
|
143
144
|
}
|
|
144
145
|
if ("name" in node.callee && node.callee.name === name) {
|
|
145
|
-
return isInitializedFromPragma(name, context);
|
|
146
|
+
return isInitializedFromPragma(name, context, initialScope);
|
|
146
147
|
}
|
|
147
148
|
return false;
|
|
148
149
|
};
|
|
@@ -454,7 +455,8 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
454
455
|
if (ast.isJSXTagNameExpression(node)) {
|
|
455
456
|
return true;
|
|
456
457
|
}
|
|
457
|
-
const
|
|
458
|
+
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
459
|
+
const maybeVariable = ast.findVariableByNameUpToGlobal(name, initialScope);
|
|
458
460
|
return tools.F.pipe(maybeVariable, tools.O.flatMap(ast.getVariableInit(0)), tools.O.exists((n)=>isJSXValue(n, context, hint)));
|
|
459
461
|
}).otherwise(tools.F.constFalse);
|
|
460
462
|
}
|
|
@@ -480,8 +482,8 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
480
482
|
*/ function getPropName(node) {
|
|
481
483
|
return tools.M.match(node.name).when(ast.is(ast.NodeType.JSXIdentifier), (n)=>n.name).when(ast.is(ast.NodeType.JSXNamespacedName), (n)=>`${n.namespace.name}:${n.name.name}`).exhaustive();
|
|
482
484
|
}
|
|
483
|
-
function getProp(props, propName, context) {
|
|
484
|
-
return findPropInAttributes(props, context)(propName);
|
|
485
|
+
function getProp(props, propName, context, initialScope) {
|
|
486
|
+
return findPropInAttributes(props, context, initialScope)(propName);
|
|
485
487
|
}
|
|
486
488
|
/**
|
|
487
489
|
* Gets and resolves the static value of a JSX attribute
|
|
@@ -489,30 +491,30 @@ function getProp(props, propName, context) {
|
|
|
489
491
|
* @param context The rule context
|
|
490
492
|
* @returns The static value of the given JSX attribute
|
|
491
493
|
*/ function getPropValue(attribute, context) {
|
|
492
|
-
const
|
|
494
|
+
const initialScope = context.sourceCode.getScope?.(attribute) ?? context.getScope();
|
|
493
495
|
if (attribute.type === ast.NodeType.JSXAttribute && "value" in attribute) {
|
|
494
496
|
const { value } = attribute;
|
|
495
497
|
if (value === null) {
|
|
496
498
|
return tools.O.none();
|
|
497
499
|
}
|
|
498
500
|
if (value.type === ast.NodeType.Literal) {
|
|
499
|
-
return tools.O.some(ast.getStaticValue(value,
|
|
501
|
+
return tools.O.some(ast.getStaticValue(value, initialScope));
|
|
500
502
|
}
|
|
501
503
|
if (value.type === ast.NodeType.JSXExpressionContainer) {
|
|
502
|
-
return tools.O.some(ast.getStaticValue(value.expression,
|
|
504
|
+
return tools.O.some(ast.getStaticValue(value.expression, initialScope));
|
|
503
505
|
}
|
|
504
506
|
return tools.O.none();
|
|
505
507
|
}
|
|
506
508
|
const { argument } = attribute;
|
|
507
|
-
return tools.O.some(ast.getStaticValue(argument,
|
|
509
|
+
return tools.O.some(ast.getStaticValue(argument, initialScope));
|
|
508
510
|
}
|
|
509
511
|
/**
|
|
510
512
|
* @param properties The properties to search in
|
|
511
513
|
* @param context The rule context
|
|
514
|
+
* @param initialScope
|
|
512
515
|
* @param seenProps The properties that have already been seen
|
|
513
516
|
* @returns A function that searches for a property in the given properties
|
|
514
|
-
*/ function findPropInProperties(properties, context, seenProps = []) {
|
|
515
|
-
const startScope = context.getScope();
|
|
517
|
+
*/ function findPropInProperties(properties, context, initialScope, seenProps = []) {
|
|
516
518
|
/**
|
|
517
519
|
* Search for a property in the given properties
|
|
518
520
|
* @param propName The name of the property to search for
|
|
@@ -524,7 +526,7 @@ function getProp(props, propName, context) {
|
|
|
524
526
|
}).when(ast.is(ast.NodeType.SpreadElement), (prop)=>{
|
|
525
527
|
return tools.M.match(prop.argument).when(ast.is(ast.NodeType.Identifier), (argument)=>{
|
|
526
528
|
const { name } = argument;
|
|
527
|
-
const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name,
|
|
529
|
+
const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name, initialScope), ast.getVariableInit(0));
|
|
528
530
|
if (tools.O.isNone(maybeInit)) {
|
|
529
531
|
return false;
|
|
530
532
|
}
|
|
@@ -535,12 +537,12 @@ function getProp(props, propName, context) {
|
|
|
535
537
|
if (seenProps.includes(name)) {
|
|
536
538
|
return false;
|
|
537
539
|
}
|
|
538
|
-
return tools.O.isSome(findPropInProperties(init.properties, context, [
|
|
540
|
+
return tools.O.isSome(findPropInProperties(init.properties, context, initialScope, [
|
|
539
541
|
...seenProps,
|
|
540
542
|
name
|
|
541
543
|
])(propName));
|
|
542
544
|
}).when(ast.is(ast.NodeType.ObjectExpression), (argument)=>{
|
|
543
|
-
return tools.O.isSome(findPropInProperties(argument.properties, context, seenProps)(propName));
|
|
545
|
+
return tools.O.isSome(findPropInProperties(argument.properties, context, initialScope, seenProps)(propName));
|
|
544
546
|
}).when(ast.is(ast.NodeType.MemberExpression), ()=>{
|
|
545
547
|
// Not implemented
|
|
546
548
|
}).when(ast.is(ast.NodeType.CallExpression), ()=>{
|
|
@@ -556,9 +558,9 @@ function getProp(props, propName, context) {
|
|
|
556
558
|
/**
|
|
557
559
|
* @param attributes The attributes to search in
|
|
558
560
|
* @param context The rule context
|
|
561
|
+
* @param initialScope
|
|
559
562
|
* @returns A function that searches for a property in the given attributes
|
|
560
|
-
*/ function findPropInAttributes(attributes, context) {
|
|
561
|
-
const startScope = context.getScope();
|
|
563
|
+
*/ function findPropInAttributes(attributes, context, initialScope) {
|
|
562
564
|
/**
|
|
563
565
|
* Search for a property in the given attributes
|
|
564
566
|
* @param propName The name of the property to search for
|
|
@@ -570,7 +572,7 @@ function getProp(props, propName, context) {
|
|
|
570
572
|
type: ast.NodeType.Identifier
|
|
571
573
|
}, (argument)=>{
|
|
572
574
|
const { name } = argument;
|
|
573
|
-
const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name,
|
|
575
|
+
const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name, initialScope), ast.getVariableInit(0));
|
|
574
576
|
if (tools.O.isNone(maybeInit)) {
|
|
575
577
|
return false;
|
|
576
578
|
}
|
|
@@ -578,9 +580,9 @@ function getProp(props, propName, context) {
|
|
|
578
580
|
if (!("properties" in init)) {
|
|
579
581
|
return false;
|
|
580
582
|
}
|
|
581
|
-
return tools.O.isSome(findPropInProperties(init.properties, context)(propName));
|
|
583
|
+
return tools.O.isSome(findPropInProperties(init.properties, context, initialScope)(propName));
|
|
582
584
|
}).when(ast.is(ast.NodeType.ObjectExpression), (argument)=>{
|
|
583
|
-
return tools.O.isSome(findPropInProperties(argument.properties, context)(propName));
|
|
585
|
+
return tools.O.isSome(findPropInProperties(argument.properties, context, initialScope)(propName));
|
|
584
586
|
}).when(ast.is(ast.NodeType.MemberExpression), ()=>{
|
|
585
587
|
// Not implemented
|
|
586
588
|
return false;
|
|
@@ -598,27 +600,30 @@ function getProp(props, propName, context) {
|
|
|
598
600
|
* @param attributes The attributes to search in
|
|
599
601
|
* @param propName The prop name to search for
|
|
600
602
|
* @param context The rule context
|
|
603
|
+
* @param initialScope
|
|
601
604
|
* @returns `true` if the given prop name is present in the given properties
|
|
602
|
-
*/ function hasProp(attributes, propName, context) {
|
|
603
|
-
return tools.O.isSome(findPropInAttributes(attributes, context)(propName));
|
|
605
|
+
*/ function hasProp(attributes, propName, context, initialScope) {
|
|
606
|
+
return tools.O.isSome(findPropInAttributes(attributes, context, initialScope)(propName));
|
|
604
607
|
}
|
|
605
608
|
/**
|
|
606
609
|
* Check if any of the given prop names are present in the given attributes
|
|
607
610
|
* @param attributes The attributes to search in
|
|
608
611
|
* @param propNames The prop names to search for
|
|
609
612
|
* @param context The rule context
|
|
613
|
+
* @param initialScope
|
|
610
614
|
* @returns `true` if any of the given prop names are present in the given attributes
|
|
611
|
-
*/ function hasAnyProp(attributes, propNames, context) {
|
|
612
|
-
return propNames.some((propName)=>hasProp(attributes, propName, context));
|
|
615
|
+
*/ function hasAnyProp(attributes, propNames, context, initialScope) {
|
|
616
|
+
return propNames.some((propName)=>hasProp(attributes, propName, context, initialScope));
|
|
613
617
|
}
|
|
614
618
|
/**
|
|
615
619
|
* Check if all of the given prop names are present in the given attributes
|
|
616
620
|
* @param attributes The attributes to search in
|
|
617
621
|
* @param propNames The prop names to search for
|
|
618
622
|
* @param context The rule context
|
|
623
|
+
* @param initialScope
|
|
619
624
|
* @returns `true` if all of the given prop names are present in the given attributes
|
|
620
|
-
*/ function hasEveryProp(attributes, propNames, context) {
|
|
621
|
-
return propNames.every((propName)=>hasProp(attributes, propName, context));
|
|
625
|
+
*/ function hasEveryProp(attributes, propNames, context, initialScope) {
|
|
626
|
+
return propNames.every((propName)=>hasProp(attributes, propName, context, initialScope));
|
|
622
627
|
}
|
|
623
628
|
|
|
624
629
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -46,13 +46,13 @@ function getFragmentFromContext(context) {
|
|
|
46
46
|
const getPragmaFromContext = memo((context)=>{
|
|
47
47
|
// eslint-disable-next-line prefer-destructuring
|
|
48
48
|
const settings = context.settings;
|
|
49
|
-
const sourceCode = context
|
|
49
|
+
const { sourceCode } = context;
|
|
50
50
|
const pragmaNode = sourceCode.getAllComments().find((node)=>RE_JSX_ANNOTATION_REGEX.test(node.value));
|
|
51
51
|
return F.pipe(O.orElse(O.fromNullable(settings.react?.pragma), ()=>F.pipe(O.fromNullable(pragmaNode), O.map(({ value })=>RE_JSX_ANNOTATION_REGEX.exec(value)), O.flatMapNullable((matches)=>matches?.[1]?.split(".")[0]))), O.flatMap(O.liftPredicate((x)=>RE_JS_IDENTIFIER_REGEX.test(x))), O.getOrElse(F.constant("React")));
|
|
52
52
|
});
|
|
53
53
|
|
|
54
|
-
function isInitializedFromPragma(variableName, context, pragma = getPragmaFromContext(context)) {
|
|
55
|
-
const variables = getVariablesUpToGlobal(
|
|
54
|
+
function isInitializedFromPragma(variableName, context, initialScope, pragma = getPragmaFromContext(context)) {
|
|
55
|
+
const variables = getVariablesUpToGlobal(initialScope);
|
|
56
56
|
const maybeVariable = findVariableByName(variableName)(variables);
|
|
57
57
|
const maybeLatestDef = O.flatMapNullable(maybeVariable, (variable)=>variable.defs.at(-1));
|
|
58
58
|
if (O.isNone(maybeLatestDef)) {
|
|
@@ -133,6 +133,7 @@ function isPropertyOfPragma(name, context, pragma = getPragmaFromContext(context
|
|
|
133
133
|
* @returns A predicate that checks if the given node is a call expression to the given function or method
|
|
134
134
|
*/ function isCallFromPragma(name) {
|
|
135
135
|
return (node, context)=>{
|
|
136
|
+
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
136
137
|
if (node.type !== NodeType.CallExpression || !("callee" in node)) {
|
|
137
138
|
return false;
|
|
138
139
|
}
|
|
@@ -140,7 +141,7 @@ function isPropertyOfPragma(name, context, pragma = getPragmaFromContext(context
|
|
|
140
141
|
return isPropertyOfPragma(name, context)(node.callee);
|
|
141
142
|
}
|
|
142
143
|
if ("name" in node.callee && node.callee.name === name) {
|
|
143
|
-
return isInitializedFromPragma(name, context);
|
|
144
|
+
return isInitializedFromPragma(name, context, initialScope);
|
|
144
145
|
}
|
|
145
146
|
return false;
|
|
146
147
|
};
|
|
@@ -452,7 +453,8 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
452
453
|
if (isJSXTagNameExpression(node)) {
|
|
453
454
|
return true;
|
|
454
455
|
}
|
|
455
|
-
const
|
|
456
|
+
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
457
|
+
const maybeVariable = findVariableByNameUpToGlobal(name, initialScope);
|
|
456
458
|
return F.pipe(maybeVariable, O.flatMap(getVariableInit(0)), O.exists((n)=>isJSXValue(n, context, hint)));
|
|
457
459
|
}).otherwise(F.constFalse);
|
|
458
460
|
}
|
|
@@ -478,8 +480,8 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
478
480
|
*/ function getPropName(node) {
|
|
479
481
|
return M.match(node.name).when(is(NodeType.JSXIdentifier), (n)=>n.name).when(is(NodeType.JSXNamespacedName), (n)=>`${n.namespace.name}:${n.name.name}`).exhaustive();
|
|
480
482
|
}
|
|
481
|
-
function getProp(props, propName, context) {
|
|
482
|
-
return findPropInAttributes(props, context)(propName);
|
|
483
|
+
function getProp(props, propName, context, initialScope) {
|
|
484
|
+
return findPropInAttributes(props, context, initialScope)(propName);
|
|
483
485
|
}
|
|
484
486
|
/**
|
|
485
487
|
* Gets and resolves the static value of a JSX attribute
|
|
@@ -487,30 +489,30 @@ function getProp(props, propName, context) {
|
|
|
487
489
|
* @param context The rule context
|
|
488
490
|
* @returns The static value of the given JSX attribute
|
|
489
491
|
*/ function getPropValue(attribute, context) {
|
|
490
|
-
const
|
|
492
|
+
const initialScope = context.sourceCode.getScope?.(attribute) ?? context.getScope();
|
|
491
493
|
if (attribute.type === NodeType.JSXAttribute && "value" in attribute) {
|
|
492
494
|
const { value } = attribute;
|
|
493
495
|
if (value === null) {
|
|
494
496
|
return O.none();
|
|
495
497
|
}
|
|
496
498
|
if (value.type === NodeType.Literal) {
|
|
497
|
-
return O.some(getStaticValue(value,
|
|
499
|
+
return O.some(getStaticValue(value, initialScope));
|
|
498
500
|
}
|
|
499
501
|
if (value.type === NodeType.JSXExpressionContainer) {
|
|
500
|
-
return O.some(getStaticValue(value.expression,
|
|
502
|
+
return O.some(getStaticValue(value.expression, initialScope));
|
|
501
503
|
}
|
|
502
504
|
return O.none();
|
|
503
505
|
}
|
|
504
506
|
const { argument } = attribute;
|
|
505
|
-
return O.some(getStaticValue(argument,
|
|
507
|
+
return O.some(getStaticValue(argument, initialScope));
|
|
506
508
|
}
|
|
507
509
|
/**
|
|
508
510
|
* @param properties The properties to search in
|
|
509
511
|
* @param context The rule context
|
|
512
|
+
* @param initialScope
|
|
510
513
|
* @param seenProps The properties that have already been seen
|
|
511
514
|
* @returns A function that searches for a property in the given properties
|
|
512
|
-
*/ function findPropInProperties(properties, context, seenProps = []) {
|
|
513
|
-
const startScope = context.getScope();
|
|
515
|
+
*/ function findPropInProperties(properties, context, initialScope, seenProps = []) {
|
|
514
516
|
/**
|
|
515
517
|
* Search for a property in the given properties
|
|
516
518
|
* @param propName The name of the property to search for
|
|
@@ -522,7 +524,7 @@ function getProp(props, propName, context) {
|
|
|
522
524
|
}).when(is(NodeType.SpreadElement), (prop)=>{
|
|
523
525
|
return M.match(prop.argument).when(is(NodeType.Identifier), (argument)=>{
|
|
524
526
|
const { name } = argument;
|
|
525
|
-
const maybeInit = O.flatMap(findVariableByNameUpToGlobal(name,
|
|
527
|
+
const maybeInit = O.flatMap(findVariableByNameUpToGlobal(name, initialScope), getVariableInit(0));
|
|
526
528
|
if (O.isNone(maybeInit)) {
|
|
527
529
|
return false;
|
|
528
530
|
}
|
|
@@ -533,12 +535,12 @@ function getProp(props, propName, context) {
|
|
|
533
535
|
if (seenProps.includes(name)) {
|
|
534
536
|
return false;
|
|
535
537
|
}
|
|
536
|
-
return O.isSome(findPropInProperties(init.properties, context, [
|
|
538
|
+
return O.isSome(findPropInProperties(init.properties, context, initialScope, [
|
|
537
539
|
...seenProps,
|
|
538
540
|
name
|
|
539
541
|
])(propName));
|
|
540
542
|
}).when(is(NodeType.ObjectExpression), (argument)=>{
|
|
541
|
-
return O.isSome(findPropInProperties(argument.properties, context, seenProps)(propName));
|
|
543
|
+
return O.isSome(findPropInProperties(argument.properties, context, initialScope, seenProps)(propName));
|
|
542
544
|
}).when(is(NodeType.MemberExpression), ()=>{
|
|
543
545
|
// Not implemented
|
|
544
546
|
}).when(is(NodeType.CallExpression), ()=>{
|
|
@@ -554,9 +556,9 @@ function getProp(props, propName, context) {
|
|
|
554
556
|
/**
|
|
555
557
|
* @param attributes The attributes to search in
|
|
556
558
|
* @param context The rule context
|
|
559
|
+
* @param initialScope
|
|
557
560
|
* @returns A function that searches for a property in the given attributes
|
|
558
|
-
*/ function findPropInAttributes(attributes, context) {
|
|
559
|
-
const startScope = context.getScope();
|
|
561
|
+
*/ function findPropInAttributes(attributes, context, initialScope) {
|
|
560
562
|
/**
|
|
561
563
|
* Search for a property in the given attributes
|
|
562
564
|
* @param propName The name of the property to search for
|
|
@@ -568,7 +570,7 @@ function getProp(props, propName, context) {
|
|
|
568
570
|
type: NodeType.Identifier
|
|
569
571
|
}, (argument)=>{
|
|
570
572
|
const { name } = argument;
|
|
571
|
-
const maybeInit = O.flatMap(findVariableByNameUpToGlobal(name,
|
|
573
|
+
const maybeInit = O.flatMap(findVariableByNameUpToGlobal(name, initialScope), getVariableInit(0));
|
|
572
574
|
if (O.isNone(maybeInit)) {
|
|
573
575
|
return false;
|
|
574
576
|
}
|
|
@@ -576,9 +578,9 @@ function getProp(props, propName, context) {
|
|
|
576
578
|
if (!("properties" in init)) {
|
|
577
579
|
return false;
|
|
578
580
|
}
|
|
579
|
-
return O.isSome(findPropInProperties(init.properties, context)(propName));
|
|
581
|
+
return O.isSome(findPropInProperties(init.properties, context, initialScope)(propName));
|
|
580
582
|
}).when(is(NodeType.ObjectExpression), (argument)=>{
|
|
581
|
-
return O.isSome(findPropInProperties(argument.properties, context)(propName));
|
|
583
|
+
return O.isSome(findPropInProperties(argument.properties, context, initialScope)(propName));
|
|
582
584
|
}).when(is(NodeType.MemberExpression), ()=>{
|
|
583
585
|
// Not implemented
|
|
584
586
|
return false;
|
|
@@ -596,27 +598,30 @@ function getProp(props, propName, context) {
|
|
|
596
598
|
* @param attributes The attributes to search in
|
|
597
599
|
* @param propName The prop name to search for
|
|
598
600
|
* @param context The rule context
|
|
601
|
+
* @param initialScope
|
|
599
602
|
* @returns `true` if the given prop name is present in the given properties
|
|
600
|
-
*/ function hasProp(attributes, propName, context) {
|
|
601
|
-
return O.isSome(findPropInAttributes(attributes, context)(propName));
|
|
603
|
+
*/ function hasProp(attributes, propName, context, initialScope) {
|
|
604
|
+
return O.isSome(findPropInAttributes(attributes, context, initialScope)(propName));
|
|
602
605
|
}
|
|
603
606
|
/**
|
|
604
607
|
* Check if any of the given prop names are present in the given attributes
|
|
605
608
|
* @param attributes The attributes to search in
|
|
606
609
|
* @param propNames The prop names to search for
|
|
607
610
|
* @param context The rule context
|
|
611
|
+
* @param initialScope
|
|
608
612
|
* @returns `true` if any of the given prop names are present in the given attributes
|
|
609
|
-
*/ function hasAnyProp(attributes, propNames, context) {
|
|
610
|
-
return propNames.some((propName)=>hasProp(attributes, propName, context));
|
|
613
|
+
*/ function hasAnyProp(attributes, propNames, context, initialScope) {
|
|
614
|
+
return propNames.some((propName)=>hasProp(attributes, propName, context, initialScope));
|
|
611
615
|
}
|
|
612
616
|
/**
|
|
613
617
|
* Check if all of the given prop names are present in the given attributes
|
|
614
618
|
* @param attributes The attributes to search in
|
|
615
619
|
* @param propNames The prop names to search for
|
|
616
620
|
* @param context The rule context
|
|
621
|
+
* @param initialScope
|
|
617
622
|
* @returns `true` if all of the given prop names are present in the given attributes
|
|
618
|
-
*/ function hasEveryProp(attributes, propNames, context) {
|
|
619
|
-
return propNames.every((propName)=>hasProp(attributes, propName, context));
|
|
623
|
+
*/ function hasEveryProp(attributes, propNames, context, initialScope) {
|
|
624
|
+
return propNames.every((propName)=>hasProp(attributes, propName, context, initialScope));
|
|
620
625
|
}
|
|
621
626
|
|
|
622
627
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-react/jsx",
|
|
3
|
-
"version": "0.9.3-beta.
|
|
3
|
+
"version": "0.9.3-beta.2",
|
|
4
4
|
"description": "ESLint x React's TSESTree AST utility module for static analysis of JSX.",
|
|
5
5
|
"homepage": "https://github.com/rel1cx/eslint-react",
|
|
6
6
|
"bugs": {
|
|
@@ -38,10 +38,10 @@
|
|
|
38
38
|
"@typescript-eslint/types": "6.13.2",
|
|
39
39
|
"@typescript-eslint/utils": "6.13.2",
|
|
40
40
|
"micro-memoize": "4.1.2",
|
|
41
|
-
"@eslint-react/
|
|
42
|
-
"@eslint-react/shared": "0.9.3-beta.
|
|
43
|
-
"@eslint-react/
|
|
44
|
-
"@eslint-react/types": "0.9.3-beta.
|
|
41
|
+
"@eslint-react/ast": "0.9.3-beta.2",
|
|
42
|
+
"@eslint-react/shared": "0.9.3-beta.2",
|
|
43
|
+
"@eslint-react/tools": "0.9.3-beta.2",
|
|
44
|
+
"@eslint-react/types": "0.9.3-beta.2"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "rollup -c rollup.config.ts --configPlugin swc3 && cp dist/index.d.ts dist/index.d.mts",
|