@eslint-react/jsx
Advanced tools
Comparing version 0.9.3-beta.1 to 0.9.3-beta.2
import { TSESTree } from '@typescript-eslint/types'; | ||
import { RuleContext } from '@eslint-react/shared'; | ||
import { Scope } from '@typescript-eslint/scope-manager'; | ||
import { TSESTreeFunction } from '@eslint-react/ast'; | ||
@@ -54,3 +55,3 @@ import { O } from '@eslint-react/tools'; | ||
declare function isInitializedFromPragma(variableName: string, context: RuleContext, pragma?: string): boolean; | ||
declare function isInitializedFromPragma(variableName: string, context: RuleContext, initialScope: Scope, pragma?: string): boolean; | ||
declare function isPropertyOfPragma(name: string, context: RuleContext, pragma?: string): (node: TSESTree.Node) => boolean; | ||
@@ -123,3 +124,3 @@ type CallFromPragmaPredicate = (node: TSESTree.Node, context: RuleContext) => node is TSESTree.CallExpression; | ||
declare function getPropName(node: TSESTree$1.JSXAttribute): string; | ||
declare function getProp(props: (TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute)[], propName: string, context: RuleContext): O.Option<TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute>; | ||
declare function getProp(props: (TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute)[], propName: string, context: RuleContext, initialScope: Scope): O.Option<TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute>; | ||
/** | ||
@@ -139,12 +140,14 @@ * Gets and resolves the static value of a JSX attribute | ||
* @param context The rule context | ||
* @param initialScope | ||
* @param seenProps The properties that have already been seen | ||
* @returns A function that searches for a property in the given properties | ||
*/ | ||
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]>; | ||
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]>; | ||
/** | ||
* @param attributes The attributes to search in | ||
* @param context The rule context | ||
* @param initialScope | ||
* @returns A function that searches for a property in the given attributes | ||
*/ | ||
declare function findPropInAttributes(attributes: (TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute)[], context: RuleContext): (propName: string) => O.Option<NonNullable<TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute | undefined>>; | ||
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>>; | ||
@@ -156,5 +159,6 @@ /** | ||
* @param context The rule context | ||
* @param initialScope | ||
* @returns `true` if the given prop name is present in the given properties | ||
*/ | ||
declare function hasProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propName: string, context: RuleContext): boolean; | ||
declare function hasProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propName: string, context: RuleContext, initialScope: Scope): boolean; | ||
/** | ||
@@ -165,5 +169,6 @@ * Check if any of the given prop names are present in the given attributes | ||
* @param context The rule context | ||
* @param initialScope | ||
* @returns `true` if any of the given prop names are present in the given attributes | ||
*/ | ||
declare function hasAnyProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propNames: string[], context: RuleContext): boolean; | ||
declare function hasAnyProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propNames: string[], context: RuleContext, initialScope: Scope): boolean; | ||
/** | ||
@@ -174,5 +179,6 @@ * Check if all of the given prop names are present in the given attributes | ||
* @param context The rule context | ||
* @param initialScope | ||
* @returns `true` if all of the given prop names are present in the given attributes | ||
*/ | ||
declare function hasEveryProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propNames: string[], context: RuleContext): boolean; | ||
declare function hasEveryProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propNames: string[], context: RuleContext, initialScope: Scope): boolean; | ||
@@ -179,0 +185,0 @@ /** |
@@ -51,3 +51,3 @@ 'use strict'; | ||
const settings = context.settings; | ||
const sourceCode = context.getSourceCode(); | ||
const { sourceCode } = context; | ||
const pragmaNode = sourceCode.getAllComments().find((node)=>RE_JSX_ANNOTATION_REGEX.test(node.value)); | ||
@@ -57,4 +57,4 @@ 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"))); | ||
function isInitializedFromPragma(variableName, context, pragma = getPragmaFromContext(context)) { | ||
const variables = ast.getVariablesUpToGlobal(context.getScope()); | ||
function isInitializedFromPragma(variableName, context, initialScope, pragma = getPragmaFromContext(context)) { | ||
const variables = ast.getVariablesUpToGlobal(initialScope); | ||
const maybeVariable = ast.findVariableByName(variableName)(variables); | ||
@@ -140,2 +140,3 @@ const maybeLatestDef = tools.O.flatMapNullable(maybeVariable, (variable)=>variable.defs.at(-1)); | ||
return (node, context)=>{ | ||
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope(); | ||
if (node.type !== ast.NodeType.CallExpression || !("callee" in node)) { | ||
@@ -148,3 +149,3 @@ return false; | ||
if ("name" in node.callee && node.callee.name === name) { | ||
return isInitializedFromPragma(name, context); | ||
return isInitializedFromPragma(name, context, initialScope); | ||
} | ||
@@ -461,3 +462,4 @@ return false; | ||
} | ||
const maybeVariable = ast.findVariableByNameUpToGlobal(name, context.getScope()); | ||
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope(); | ||
const maybeVariable = ast.findVariableByNameUpToGlobal(name, initialScope); | ||
return tools.F.pipe(maybeVariable, tools.O.flatMap(ast.getVariableInit(0)), tools.O.exists((n)=>isJSXValue(n, context, hint))); | ||
@@ -488,4 +490,4 @@ }).otherwise(tools.F.constFalse); | ||
} | ||
function getProp(props, propName, context) { | ||
return findPropInAttributes(props, context)(propName); | ||
function getProp(props, propName, context, initialScope) { | ||
return findPropInAttributes(props, context, initialScope)(propName); | ||
} | ||
@@ -498,3 +500,3 @@ /** | ||
*/ function getPropValue(attribute, context) { | ||
const scope = context.getScope(); | ||
const initialScope = context.sourceCode.getScope?.(attribute) ?? context.getScope(); | ||
if (attribute.type === ast.NodeType.JSXAttribute && "value" in attribute) { | ||
@@ -506,6 +508,6 @@ const { value } = attribute; | ||
if (value.type === ast.NodeType.Literal) { | ||
return tools.O.some(ast.getStaticValue(value, scope)); | ||
return tools.O.some(ast.getStaticValue(value, initialScope)); | ||
} | ||
if (value.type === ast.NodeType.JSXExpressionContainer) { | ||
return tools.O.some(ast.getStaticValue(value.expression, scope)); | ||
return tools.O.some(ast.getStaticValue(value.expression, initialScope)); | ||
} | ||
@@ -515,3 +517,3 @@ return tools.O.none(); | ||
const { argument } = attribute; | ||
return tools.O.some(ast.getStaticValue(argument, scope)); | ||
return tools.O.some(ast.getStaticValue(argument, initialScope)); | ||
} | ||
@@ -521,6 +523,6 @@ /** | ||
* @param context The rule context | ||
* @param initialScope | ||
* @param seenProps The properties that have already been seen | ||
* @returns A function that searches for a property in the given properties | ||
*/ function findPropInProperties(properties, context, seenProps = []) { | ||
const startScope = context.getScope(); | ||
*/ function findPropInProperties(properties, context, initialScope, seenProps = []) { | ||
/** | ||
@@ -537,3 +539,3 @@ * Search for a property in the given properties | ||
const { name } = argument; | ||
const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name, startScope), ast.getVariableInit(0)); | ||
const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name, initialScope), ast.getVariableInit(0)); | ||
if (tools.O.isNone(maybeInit)) { | ||
@@ -549,3 +551,3 @@ return false; | ||
} | ||
return tools.O.isSome(findPropInProperties(init.properties, context, [ | ||
return tools.O.isSome(findPropInProperties(init.properties, context, initialScope, [ | ||
...seenProps, | ||
@@ -555,3 +557,3 @@ name | ||
}).when(ast.is(ast.NodeType.ObjectExpression), (argument)=>{ | ||
return tools.O.isSome(findPropInProperties(argument.properties, context, seenProps)(propName)); | ||
return tools.O.isSome(findPropInProperties(argument.properties, context, initialScope, seenProps)(propName)); | ||
}).when(ast.is(ast.NodeType.MemberExpression), ()=>{ | ||
@@ -572,5 +574,5 @@ // Not implemented | ||
* @param context The rule context | ||
* @param initialScope | ||
* @returns A function that searches for a property in the given attributes | ||
*/ function findPropInAttributes(attributes, context) { | ||
const startScope = context.getScope(); | ||
*/ function findPropInAttributes(attributes, context, initialScope) { | ||
/** | ||
@@ -587,3 +589,3 @@ * Search for a property in the given attributes | ||
const { name } = argument; | ||
const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name, startScope), ast.getVariableInit(0)); | ||
const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name, initialScope), ast.getVariableInit(0)); | ||
if (tools.O.isNone(maybeInit)) { | ||
@@ -596,5 +598,5 @@ return false; | ||
} | ||
return tools.O.isSome(findPropInProperties(init.properties, context)(propName)); | ||
return tools.O.isSome(findPropInProperties(init.properties, context, initialScope)(propName)); | ||
}).when(ast.is(ast.NodeType.ObjectExpression), (argument)=>{ | ||
return tools.O.isSome(findPropInProperties(argument.properties, context)(propName)); | ||
return tools.O.isSome(findPropInProperties(argument.properties, context, initialScope)(propName)); | ||
}).when(ast.is(ast.NodeType.MemberExpression), ()=>{ | ||
@@ -617,5 +619,6 @@ // Not implemented | ||
* @param context The rule context | ||
* @param initialScope | ||
* @returns `true` if the given prop name is present in the given properties | ||
*/ function hasProp(attributes, propName, context) { | ||
return tools.O.isSome(findPropInAttributes(attributes, context)(propName)); | ||
*/ function hasProp(attributes, propName, context, initialScope) { | ||
return tools.O.isSome(findPropInAttributes(attributes, context, initialScope)(propName)); | ||
} | ||
@@ -627,5 +630,6 @@ /** | ||
* @param context The rule context | ||
* @param initialScope | ||
* @returns `true` if any of the given prop names are present in the given attributes | ||
*/ function hasAnyProp(attributes, propNames, context) { | ||
return propNames.some((propName)=>hasProp(attributes, propName, context)); | ||
*/ function hasAnyProp(attributes, propNames, context, initialScope) { | ||
return propNames.some((propName)=>hasProp(attributes, propName, context, initialScope)); | ||
} | ||
@@ -637,5 +641,6 @@ /** | ||
* @param context The rule context | ||
* @param initialScope | ||
* @returns `true` if all of the given prop names are present in the given attributes | ||
*/ function hasEveryProp(attributes, propNames, context) { | ||
return propNames.every((propName)=>hasProp(attributes, propName, context)); | ||
*/ function hasEveryProp(attributes, propNames, context, initialScope) { | ||
return propNames.every((propName)=>hasProp(attributes, propName, context, initialScope)); | ||
} | ||
@@ -642,0 +647,0 @@ |
{ | ||
"name": "@eslint-react/jsx", | ||
"version": "0.9.3-beta.1", | ||
"version": "0.9.3-beta.2", | ||
"description": "ESLint x React's TSESTree AST utility module for static analysis of JSX.", | ||
@@ -41,6 +41,6 @@ "homepage": "https://github.com/rel1cx/eslint-react", | ||
"micro-memoize": "4.1.2", | ||
"@eslint-react/tools": "0.9.3-beta.1", | ||
"@eslint-react/shared": "0.9.3-beta.1", | ||
"@eslint-react/ast": "0.9.3-beta.1", | ||
"@eslint-react/types": "0.9.3-beta.1" | ||
"@eslint-react/ast": "0.9.3-beta.2", | ||
"@eslint-react/shared": "0.9.3-beta.2", | ||
"@eslint-react/tools": "0.9.3-beta.2", | ||
"@eslint-react/types": "0.9.3-beta.2" | ||
}, | ||
@@ -47,0 +47,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
109223
2334
+ Added@eslint-react/ast@0.9.3-beta.2(transitive)
+ Added@eslint-react/shared@0.9.3-beta.2(transitive)
+ Added@eslint-react/tools@0.9.3-beta.2(transitive)
+ Added@eslint-react/types@0.9.3-beta.2(transitive)
- Removed@eslint-react/ast@0.9.3-beta.1(transitive)
- Removed@eslint-react/shared@0.9.3-beta.1(transitive)
- Removed@eslint-react/tools@0.9.3-beta.1(transitive)
- Removed@eslint-react/types@0.9.3-beta.1(transitive)