@eslint-react/jsx 0.10.5-beta.0 → 0.10.6-beta.0
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 +22 -21
- package/dist/index.d.mts +5 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.js +22 -21
- package/dist/index.mjs +16 -15
- package/package.json +7 -6
package/dist/index.cjs
CHANGED
|
@@ -4,6 +4,7 @@ var ast = require('@eslint-react/ast');
|
|
|
4
4
|
var shared = require('@eslint-react/shared');
|
|
5
5
|
var tools = require('@eslint-react/tools');
|
|
6
6
|
var memo = require('micro-memoize');
|
|
7
|
+
var _var = require('@eslint-react/var');
|
|
7
8
|
var types = require('@typescript-eslint/types');
|
|
8
9
|
|
|
9
10
|
const RE_JSX_ANNOTATION_REGEX = /@jsx\s+(\S+)/u;
|
|
@@ -26,8 +27,8 @@ const getPragmaFromContext = memo((context)=>{
|
|
|
26
27
|
const t=Symbol.for("@ts-pattern/matcher"),e=Symbol.for("@ts-pattern/isVariadic"),n="@ts-pattern/anonymous-select-key",r=t=>Boolean(t&&"object"==typeof t),i=e=>e&&!!e[t],s=(n,o,c)=>{if(i(n)){const e=n[t](),{matched:r,selections:i}=e.match(o);return r&&i&&Object.keys(i).forEach(t=>c(t,i[t])),r}if(r(n)){if(!r(o))return !1;if(Array.isArray(n)){if(!Array.isArray(o))return !1;let t=[],r=[],a=[];for(const s of n.keys()){const o=n[s];i(o)&&o[e]?a.push(o):a.length?r.push(o):t.push(o);}if(a.length){if(a.length>1)throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");if(o.length<t.length+r.length)return !1;const e=o.slice(0,t.length),n=0===r.length?[]:o.slice(-r.length),i=o.slice(t.length,0===r.length?Infinity:-r.length);return t.every((t,n)=>s(t,e[n],c))&&r.every((t,e)=>s(t,n[e],c))&&(0===a.length||s(a[0],i,c))}return n.length===o.length&&n.every((t,e)=>s(t,o[e],c))}return Object.keys(n).every(e=>{const r=n[e];return (e in o||i(a=r)&&"optional"===a[t]().matcherType)&&s(r,o[e],c);var a;})}return Object.is(o,n)},o=e=>{var n,s,a;return r(e)?i(e)?null!=(n=null==(s=(a=e[t]()).getSelectionKeys)?void 0:s.call(a))?n:[]:Array.isArray(e)?c(e,o):c(Object.values(e),o):[]},c=(t,e)=>t.reduce((t,n)=>t.concat(e(n)),[]);function a(...t){if(1===t.length){const[e]=t;return t=>s(e,t,()=>{})}if(2===t.length){const[e,n]=t;return s(e,n,()=>{})}throw new Error(`isMatching wasn't given the right number of arguments: expected 1 or 2, received ${t.length}.`)}function u(t){return Object.assign(t,{optional:()=>l(t),and:e=>m(t,e),or:e=>y(t,e),select:e=>void 0===e?p(t):p(e,t)})}function h(t){return Object.assign((t=>Object.assign(t,{*[Symbol.iterator](){yield Object.assign(t,{[e]:!0});}}))(t),{optional:()=>h(l(t)),select:e=>h(void 0===e?p(t):p(e,t))})}function l(e){return u({[t]:()=>({match:t=>{let n={};const r=(t,e)=>{n[t]=e;};return void 0===t?(o(e).forEach(t=>r(t,void 0)),{matched:!0,selections:n}):{matched:s(e,t,r),selections:n}},getSelectionKeys:()=>o(e),matcherType:"optional"})})}const f=(t,e)=>{for(const n of t)if(!e(n))return !1;return !0},g=(t,e)=>{for(const[n,r]of t.entries())if(!e(r,n))return !1;return !0};function m(...e){return u({[t]:()=>({match:t=>{let n={};const r=(t,e)=>{n[t]=e;};return {matched:e.every(e=>s(e,t,r)),selections:n}},getSelectionKeys:()=>c(e,o),matcherType:"and"})})}function y(...e){return u({[t]:()=>({match:t=>{let n={};const r=(t,e)=>{n[t]=e;};return c(e,o).forEach(t=>r(t,void 0)),{matched:e.some(e=>s(e,t,r)),selections:n}},getSelectionKeys:()=>c(e,o),matcherType:"or"})})}function d(e){return {[t]:()=>({match:t=>({matched:Boolean(e(t))})})}}function p(...e){const r="string"==typeof e[0]?e[0]:void 0,i=2===e.length?e[1]:"string"==typeof e[0]?void 0:e[0];return u({[t]:()=>({match:t=>{let e={[null!=r?r:n]:t};return {matched:void 0===i||s(i,t,(t,n)=>{e[t]=n;}),selections:e}},getSelectionKeys:()=>[null!=r?r:n].concat(void 0===i?[]:o(i))})})}function v(t){return "number"==typeof t}function b(t){return "string"==typeof t}function w(t){return "bigint"==typeof t}const S=u(d(function(t){return !0})),O=S,j=t=>Object.assign(u(t),{startsWith:e=>{return j(m(t,(n=e,d(t=>b(t)&&t.startsWith(n)))));var n;},endsWith:e=>{return j(m(t,(n=e,d(t=>b(t)&&t.endsWith(n)))));var n;},minLength:e=>j(m(t,(t=>d(e=>b(e)&&e.length>=t))(e))),maxLength:e=>j(m(t,(t=>d(e=>b(e)&&e.length<=t))(e))),includes:e=>{return j(m(t,(n=e,d(t=>b(t)&&t.includes(n)))));var n;},regex:e=>{return j(m(t,(n=e,d(t=>b(t)&&Boolean(t.match(n))))));var n;}}),E=j(d(b)),K=t=>Object.assign(u(t),{between:(e,n)=>K(m(t,((t,e)=>d(n=>v(n)&&t<=n&&e>=n))(e,n))),lt:e=>K(m(t,(t=>d(e=>v(e)&&e<t))(e))),gt:e=>K(m(t,(t=>d(e=>v(e)&&e>t))(e))),lte:e=>K(m(t,(t=>d(e=>v(e)&&e<=t))(e))),gte:e=>K(m(t,(t=>d(e=>v(e)&&e>=t))(e))),int:()=>K(m(t,d(t=>v(t)&&Number.isInteger(t)))),finite:()=>K(m(t,d(t=>v(t)&&Number.isFinite(t)))),positive:()=>K(m(t,d(t=>v(t)&&t>0))),negative:()=>K(m(t,d(t=>v(t)&&t<0)))}),A=K(d(v)),x=t=>Object.assign(u(t),{between:(e,n)=>x(m(t,((t,e)=>d(n=>w(n)&&t<=n&&e>=n))(e,n))),lt:e=>x(m(t,(t=>d(e=>w(e)&&e<t))(e))),gt:e=>x(m(t,(t=>d(e=>w(e)&&e>t))(e))),lte:e=>x(m(t,(t=>d(e=>w(e)&&e<=t))(e))),gte:e=>x(m(t,(t=>d(e=>w(e)&&e>=t))(e))),positive:()=>x(m(t,d(t=>w(t)&&t>0))),negative:()=>x(m(t,d(t=>w(t)&&t<0)))}),P=x(d(w)),T=u(d(function(t){return "boolean"==typeof t})),k=u(d(function(t){return "symbol"==typeof t})),B=u(d(function(t){return null==t}));var _={__proto__:null,matcher:t,optional:l,array:function(...e){return h({[t]:()=>({match:t=>{if(!Array.isArray(t))return {matched:!1};if(0===e.length)return {matched:!0};const n=e[0];let r={};if(0===t.length)return o(n).forEach(t=>{r[t]=[];}),{matched:!0,selections:r};const i=(t,e)=>{r[t]=(r[t]||[]).concat([e]);};return {matched:t.every(t=>s(n,t,i)),selections:r}},getSelectionKeys:()=>0===e.length?[]:o(e[0])})})},set:function(...e){return u({[t]:()=>({match:t=>{if(!(t instanceof Set))return {matched:!1};let n={};if(0===t.size)return {matched:!0,selections:n};if(0===e.length)return {matched:!0};const r=(t,e)=>{n[t]=(n[t]||[]).concat([e]);},i=e[0];return {matched:f(t,t=>s(i,t,r)),selections:n}},getSelectionKeys:()=>0===e.length?[]:o(e[0])})})},map:function(...e){return u({[t]:()=>({match:t=>{if(!(t instanceof Map))return {matched:!1};let n={};if(0===t.size)return {matched:!0,selections:n};const r=(t,e)=>{n[t]=(n[t]||[]).concat([e]);};if(0===e.length)return {matched:!0};var i;if(1===e.length)throw new Error(`\`P.map\` wasn't given enough arguments. Expected (key, value), received ${null==(i=e[0])?void 0:i.toString()}`);const[o,c]=e;return {matched:g(t,(t,e)=>{const n=s(o,e,r),i=s(c,t,r);return n&&i}),selections:n}},getSelectionKeys:()=>0===e.length?[]:[...o(e[0]),...o(e[1])]})})},intersection:m,union:y,not:function(e){return u({[t]:()=>({match:t=>({matched:!s(e,t,()=>{})}),getSelectionKeys:()=>[],matcherType:"not"})})},when:d,select:p,any:S,_:O,string:E,number:A,bigint:P,boolean:T,symbol:k,nullish:B,instanceOf:function(t){return u(d(function(t){return e=>e instanceof t}(t)))},shape:function(t){return u(d(a(t)))}};const W={matched:!1,value:void 0};function N(t){return new $(t,W)}class ${constructor(t,e){this.input=void 0,this.state=void 0,this.input=t,this.state=e;}with(...t){if(this.state.matched)return this;const e=t[t.length-1],r=[t[0]];let i;3===t.length&&"function"==typeof t[1]?i=t[1]:t.length>2&&r.push(...t.slice(1,t.length-1));let o=!1,c={};const a=(t,e)=>{o=!0,c[t]=e;},u=!r.some(t=>s(t,this.input,a))||i&&!Boolean(i(this.input))?W:{matched:!0,value:e(o?n in c?c[n]:c:this.input,this.input)};return new $(this.input,u)}when(t,e){if(this.state.matched)return this;const n=Boolean(t(this.input));return new $(this.input,n?{matched:!0,value:e(this.input,this.input)}:W)}otherwise(t){return this.state.matched?this.state.value:t(this.input)}exhaustive(){if(this.state.matched)return this.state.value;let t;try{t=JSON.stringify(this.input);}catch(e){t=this.input;}throw new Error(`Pattern matching error: no pattern matches value ${t}`)}run(){return this.exhaustive()}returnType(){return this}}
|
|
27
28
|
|
|
28
29
|
function isInitializedFromPragma(variableName, context, initialScope, pragma = getPragmaFromContext(context)) {
|
|
29
|
-
const variables =
|
|
30
|
-
const maybeVariable =
|
|
30
|
+
const variables = _var.getVariablesUpToGlobal(initialScope);
|
|
31
|
+
const maybeVariable = _var.findVariableByName(variableName)(variables);
|
|
31
32
|
const maybeLatestDef = tools.O.flatMapNullable(maybeVariable, (variable)=>variable.defs.at(-1));
|
|
32
33
|
if (tools.O.isNone(maybeLatestDef)) return false;
|
|
33
34
|
const latestDef = maybeLatestDef.value;
|
|
@@ -260,7 +261,7 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
260
261
|
// | DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES[
|
|
261
262
|
// keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES
|
|
262
263
|
// ];
|
|
263
|
-
/* eslint-disable perfectionist/sort-objects */ const
|
|
264
|
+
/* eslint-disable perfectionist/sort-objects */ const JSXValueHint = {
|
|
264
265
|
None: 0n,
|
|
265
266
|
SkipNullLiteral: 1n << 0n,
|
|
266
267
|
SkipUndefinedLiteral: 1n << 1n,
|
|
@@ -272,14 +273,14 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
272
273
|
StrictLogical: 1n << 7n,
|
|
273
274
|
StrictConditional: 1n << 8n
|
|
274
275
|
};
|
|
275
|
-
/* eslint-enable perfectionist/sort-objects */ const
|
|
276
|
+
/* eslint-enable perfectionist/sort-objects */ const DEFAULT_JSX_VALUE_HINT = JSXValueHint.SkipUndefinedLiteral | JSXValueHint.SkipBooleanLiteral;
|
|
276
277
|
/**
|
|
277
278
|
* Check if a node is a JSX value
|
|
278
279
|
* @param node The AST node to check
|
|
279
280
|
* @param context The rule context
|
|
280
|
-
* @param hint The `
|
|
281
|
+
* @param hint The `JSXValueHint` to use
|
|
281
282
|
* @returns boolean
|
|
282
|
-
*/ function isJSXValue(node, context, hint =
|
|
283
|
+
*/ function isJSXValue(node, context, hint = DEFAULT_JSX_VALUE_HINT) {
|
|
283
284
|
if (!node) return false;
|
|
284
285
|
return N(node).with({
|
|
285
286
|
type: ast.NodeType.JSXElement
|
|
@@ -292,20 +293,20 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
292
293
|
}, tools.F.constTrue).with({
|
|
293
294
|
type: ast.NodeType.Literal
|
|
294
295
|
}, (node)=>{
|
|
295
|
-
return N(node.value).with(null, ()=>!(hint &
|
|
296
|
+
return N(node.value).with(null, ()=>!(hint & JSXValueHint.SkipNullLiteral)).with(_.boolean, ()=>!(hint & JSXValueHint.SkipBooleanLiteral)).with(_.string, ()=>!(hint & JSXValueHint.SkipStringLiteral)).with(_.number, ()=>!(hint & JSXValueHint.SkipNumberLiteral)).otherwise(tools.F.constFalse);
|
|
296
297
|
}).with({
|
|
297
298
|
type: ast.NodeType.TemplateLiteral
|
|
298
|
-
}, ()=>!(hint &
|
|
299
|
+
}, ()=>!(hint & JSXValueHint.SkipStringLiteral)).with({
|
|
299
300
|
type: ast.NodeType.ArrayExpression
|
|
300
301
|
}, (node)=>{
|
|
301
|
-
if (hint &
|
|
302
|
+
if (hint & JSXValueHint.StrictArray) return node.elements.every((n)=>isJSXValue(n, context, hint));
|
|
302
303
|
return node.elements.some((n)=>isJSXValue(n, context, hint));
|
|
303
304
|
}).with({
|
|
304
305
|
type: ast.NodeType.ConditionalExpression
|
|
305
306
|
}, (node)=>{
|
|
306
307
|
function leftHasJSX(node) {
|
|
307
308
|
if (Array.isArray(node.consequent)) {
|
|
308
|
-
if (hint &
|
|
309
|
+
if (hint & JSXValueHint.StrictArray) {
|
|
309
310
|
return node.consequent.every((n)=>isJSXValue(n, context, hint));
|
|
310
311
|
}
|
|
311
312
|
return node.consequent.some((n)=>isJSXValue(n, context, hint));
|
|
@@ -315,7 +316,7 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
315
316
|
function rightHasJSX(node) {
|
|
316
317
|
return isJSXValue(node.alternate, context, hint);
|
|
317
318
|
}
|
|
318
|
-
if (hint &
|
|
319
|
+
if (hint & JSXValueHint.StrictConditional) {
|
|
319
320
|
return leftHasJSX(node) && rightHasJSX(node);
|
|
320
321
|
}
|
|
321
322
|
return leftHasJSX(node) || rightHasJSX(node);
|
|
@@ -331,17 +332,17 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
331
332
|
}).with({
|
|
332
333
|
type: ast.NodeType.CallExpression
|
|
333
334
|
}, (node)=>{
|
|
334
|
-
if (hint &
|
|
335
|
+
if (hint & JSXValueHint.SkipCreateElement) return false;
|
|
335
336
|
return isCreateElementCall(node, context);
|
|
336
337
|
}).with({
|
|
337
338
|
type: ast.NodeType.Identifier
|
|
338
339
|
}, (node)=>{
|
|
339
340
|
const { name } = node;
|
|
340
|
-
if (name === "undefined") return !(hint &
|
|
341
|
+
if (name === "undefined") return !(hint & JSXValueHint.SkipUndefinedLiteral);
|
|
341
342
|
if (ast.isJSXTagNameExpression(node)) return true;
|
|
342
343
|
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
343
|
-
const maybeVariable =
|
|
344
|
-
return tools.F.pipe(maybeVariable, tools.O.flatMap(
|
|
344
|
+
const maybeVariable = _var.findVariableByNameUpToGlobal(name, initialScope);
|
|
345
|
+
return tools.F.pipe(maybeVariable, tools.O.flatMap(_var.getVariableInit(0)), tools.O.exists((n)=>isJSXValue(n, context, hint)));
|
|
345
346
|
}).otherwise(tools.F.constFalse);
|
|
346
347
|
}
|
|
347
348
|
|
|
@@ -349,9 +350,9 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
349
350
|
* Check if function is returning JSX
|
|
350
351
|
* @param node The return statement node to check
|
|
351
352
|
* @param context The rule context
|
|
352
|
-
* @param hint The `
|
|
353
|
+
* @param hint The `JSXValueHint` to use
|
|
353
354
|
* @returns boolean
|
|
354
|
-
*/ function isFunctionReturningJSXValue(node, context, hint =
|
|
355
|
+
*/ function isFunctionReturningJSXValue(node, context, hint = DEFAULT_JSX_VALUE_HINT) {
|
|
355
356
|
if (node.body.type !== ast.NodeType.BlockStatement) {
|
|
356
357
|
return isJSXValue(node.body, context, hint);
|
|
357
358
|
}
|
|
@@ -405,7 +406,7 @@ function getProp(props, propName, context, initialScope) {
|
|
|
405
406
|
}).when(ast.is(ast.NodeType.SpreadElement), (prop)=>{
|
|
406
407
|
return N(prop.argument).when(ast.is(ast.NodeType.Identifier), (argument)=>{
|
|
407
408
|
const { name } = argument;
|
|
408
|
-
const maybeInit = tools.O.flatMap(
|
|
409
|
+
const maybeInit = tools.O.flatMap(_var.findVariableByNameUpToGlobal(name, initialScope), _var.getVariableInit(0));
|
|
409
410
|
if (tools.O.isNone(maybeInit)) return false;
|
|
410
411
|
const init = maybeInit.value;
|
|
411
412
|
if (init.type !== ast.NodeType.ObjectExpression) return false;
|
|
@@ -445,7 +446,7 @@ function getProp(props, propName, context, initialScope) {
|
|
|
445
446
|
type: ast.NodeType.Identifier
|
|
446
447
|
}, (argument)=>{
|
|
447
448
|
const { name } = argument;
|
|
448
|
-
const maybeInit = tools.O.flatMap(
|
|
449
|
+
const maybeInit = tools.O.flatMap(_var.findVariableByNameUpToGlobal(name, initialScope), _var.getVariableInit(0));
|
|
449
450
|
if (tools.O.isNone(maybeInit)) return false;
|
|
450
451
|
const init = maybeInit.value;
|
|
451
452
|
if (!("properties" in init)) return false;
|
|
@@ -546,8 +547,8 @@ function getProp(props, propName, context, initialScope) {
|
|
|
546
547
|
return isLiteral(node) && isWhiteSpace(node) && node.raw.includes("\n");
|
|
547
548
|
}
|
|
548
549
|
|
|
549
|
-
exports.
|
|
550
|
-
exports.
|
|
550
|
+
exports.DEFAULT_JSX_VALUE_HINT = DEFAULT_JSX_VALUE_HINT;
|
|
551
|
+
exports.JSXValueHint = JSXValueHint;
|
|
551
552
|
exports.elementType = elementType;
|
|
552
553
|
exports.findPropInAttributes = findPropInAttributes;
|
|
553
554
|
exports.findPropInProperties = findPropInProperties;
|
package/dist/index.d.mts
CHANGED
|
@@ -72,7 +72,7 @@ declare function isFragmentElement(node: TSESTree.JSXElement, pragma: string, fr
|
|
|
72
72
|
* Check if function is returning JSX
|
|
73
73
|
* @param node The return statement node to check
|
|
74
74
|
* @param context The rule context
|
|
75
|
-
* @param hint The `
|
|
75
|
+
* @param hint The `JSXValueHint` to use
|
|
76
76
|
* @returns boolean
|
|
77
77
|
*/
|
|
78
78
|
declare function isFunctionReturningJSXValue(node: TSESTreeFunction, context: RuleContext, hint?: bigint): boolean;
|
|
@@ -200,7 +200,7 @@ declare function isLineBreak(node: TSESTree.Node): boolean;
|
|
|
200
200
|
*/
|
|
201
201
|
declare function isPaddingSpaces(node: TSESTree.Node): boolean;
|
|
202
202
|
|
|
203
|
-
declare const
|
|
203
|
+
declare const JSXValueHint: {
|
|
204
204
|
readonly None: 0n;
|
|
205
205
|
readonly SkipNullLiteral: bigint;
|
|
206
206
|
readonly SkipUndefinedLiteral: bigint;
|
|
@@ -212,14 +212,14 @@ declare const JSXValueCheckHint: {
|
|
|
212
212
|
readonly StrictLogical: bigint;
|
|
213
213
|
readonly StrictConditional: bigint;
|
|
214
214
|
};
|
|
215
|
-
declare const
|
|
215
|
+
declare const DEFAULT_JSX_VALUE_HINT: bigint;
|
|
216
216
|
/**
|
|
217
217
|
* Check if a node is a JSX value
|
|
218
218
|
* @param node The AST node to check
|
|
219
219
|
* @param context The rule context
|
|
220
|
-
* @param hint The `
|
|
220
|
+
* @param hint The `JSXValueHint` to use
|
|
221
221
|
* @returns boolean
|
|
222
222
|
*/
|
|
223
223
|
declare function isJSXValue(node: TSESTree$1.Node | null | undefined, context: RuleContext, hint?: bigint): boolean;
|
|
224
224
|
|
|
225
|
-
export {
|
|
225
|
+
export { DEFAULT_JSX_VALUE_HINT, JSXValueHint, elementType, findPropInAttributes, findPropInProperties, getFragmentFromContext, getPragmaFromContext, getProp, getPropName, getPropValue, hasAnyProp, hasChildren, hasEveryProp, hasProp, isCallFromPragma, isCallFromPragmaMember, isChildOfJSXElement, isChildrenOfCreateElement, isCloneElement, isCloneElementCall, isCreateElement, isCreateElementCall, isFragment, isFragmentElement, isFragmentSyntax, isFromPragma, isFromPragmaMember, isFunctionReturningJSXValue, isInitializedFromPragma, isInsideCreateElementProps, isInsidePropValue, isJSXElementOfBuiltinComponent, isJSXElementOfUserDefinedComponent, isJSXValue, isLineBreak, isLiteral, isPaddingSpaces, isPropertyOfPragma, isWhiteSpace, traverseUpProp };
|
package/dist/index.d.ts
CHANGED
|
@@ -72,7 +72,7 @@ declare function isFragmentElement(node: TSESTree.JSXElement, pragma: string, fr
|
|
|
72
72
|
* Check if function is returning JSX
|
|
73
73
|
* @param node The return statement node to check
|
|
74
74
|
* @param context The rule context
|
|
75
|
-
* @param hint The `
|
|
75
|
+
* @param hint The `JSXValueHint` to use
|
|
76
76
|
* @returns boolean
|
|
77
77
|
*/
|
|
78
78
|
declare function isFunctionReturningJSXValue(node: TSESTreeFunction, context: RuleContext, hint?: bigint): boolean;
|
|
@@ -200,7 +200,7 @@ declare function isLineBreak(node: TSESTree.Node): boolean;
|
|
|
200
200
|
*/
|
|
201
201
|
declare function isPaddingSpaces(node: TSESTree.Node): boolean;
|
|
202
202
|
|
|
203
|
-
declare const
|
|
203
|
+
declare const JSXValueHint: {
|
|
204
204
|
readonly None: 0n;
|
|
205
205
|
readonly SkipNullLiteral: bigint;
|
|
206
206
|
readonly SkipUndefinedLiteral: bigint;
|
|
@@ -212,14 +212,14 @@ declare const JSXValueCheckHint: {
|
|
|
212
212
|
readonly StrictLogical: bigint;
|
|
213
213
|
readonly StrictConditional: bigint;
|
|
214
214
|
};
|
|
215
|
-
declare const
|
|
215
|
+
declare const DEFAULT_JSX_VALUE_HINT: bigint;
|
|
216
216
|
/**
|
|
217
217
|
* Check if a node is a JSX value
|
|
218
218
|
* @param node The AST node to check
|
|
219
219
|
* @param context The rule context
|
|
220
|
-
* @param hint The `
|
|
220
|
+
* @param hint The `JSXValueHint` to use
|
|
221
221
|
* @returns boolean
|
|
222
222
|
*/
|
|
223
223
|
declare function isJSXValue(node: TSESTree$1.Node | null | undefined, context: RuleContext, hint?: bigint): boolean;
|
|
224
224
|
|
|
225
|
-
export {
|
|
225
|
+
export { DEFAULT_JSX_VALUE_HINT, JSXValueHint, elementType, findPropInAttributes, findPropInProperties, getFragmentFromContext, getPragmaFromContext, getProp, getPropName, getPropValue, hasAnyProp, hasChildren, hasEveryProp, hasProp, isCallFromPragma, isCallFromPragmaMember, isChildOfJSXElement, isChildrenOfCreateElement, isCloneElement, isCloneElementCall, isCreateElement, isCreateElementCall, isFragment, isFragmentElement, isFragmentSyntax, isFromPragma, isFromPragmaMember, isFunctionReturningJSXValue, isInitializedFromPragma, isInsideCreateElementProps, isInsidePropValue, isJSXElementOfBuiltinComponent, isJSXElementOfUserDefinedComponent, isJSXValue, isLineBreak, isLiteral, isPaddingSpaces, isPropertyOfPragma, isWhiteSpace, traverseUpProp };
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ var ast = require('@eslint-react/ast');
|
|
|
4
4
|
var shared = require('@eslint-react/shared');
|
|
5
5
|
var tools = require('@eslint-react/tools');
|
|
6
6
|
var memo = require('micro-memoize');
|
|
7
|
+
var _var = require('@eslint-react/var');
|
|
7
8
|
var types = require('@typescript-eslint/types');
|
|
8
9
|
|
|
9
10
|
const RE_JSX_ANNOTATION_REGEX = /@jsx\s+(\S+)/u;
|
|
@@ -26,8 +27,8 @@ const getPragmaFromContext = memo((context)=>{
|
|
|
26
27
|
const t=Symbol.for("@ts-pattern/matcher"),e=Symbol.for("@ts-pattern/isVariadic"),n="@ts-pattern/anonymous-select-key",r=t=>Boolean(t&&"object"==typeof t),i=e=>e&&!!e[t],s=(n,o,c)=>{if(i(n)){const e=n[t](),{matched:r,selections:i}=e.match(o);return r&&i&&Object.keys(i).forEach(t=>c(t,i[t])),r}if(r(n)){if(!r(o))return !1;if(Array.isArray(n)){if(!Array.isArray(o))return !1;let t=[],r=[],a=[];for(const s of n.keys()){const o=n[s];i(o)&&o[e]?a.push(o):a.length?r.push(o):t.push(o);}if(a.length){if(a.length>1)throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");if(o.length<t.length+r.length)return !1;const e=o.slice(0,t.length),n=0===r.length?[]:o.slice(-r.length),i=o.slice(t.length,0===r.length?Infinity:-r.length);return t.every((t,n)=>s(t,e[n],c))&&r.every((t,e)=>s(t,n[e],c))&&(0===a.length||s(a[0],i,c))}return n.length===o.length&&n.every((t,e)=>s(t,o[e],c))}return Object.keys(n).every(e=>{const r=n[e];return (e in o||i(a=r)&&"optional"===a[t]().matcherType)&&s(r,o[e],c);var a;})}return Object.is(o,n)},o=e=>{var n,s,a;return r(e)?i(e)?null!=(n=null==(s=(a=e[t]()).getSelectionKeys)?void 0:s.call(a))?n:[]:Array.isArray(e)?c(e,o):c(Object.values(e),o):[]},c=(t,e)=>t.reduce((t,n)=>t.concat(e(n)),[]);function a(...t){if(1===t.length){const[e]=t;return t=>s(e,t,()=>{})}if(2===t.length){const[e,n]=t;return s(e,n,()=>{})}throw new Error(`isMatching wasn't given the right number of arguments: expected 1 or 2, received ${t.length}.`)}function u(t){return Object.assign(t,{optional:()=>l(t),and:e=>m(t,e),or:e=>y(t,e),select:e=>void 0===e?p(t):p(e,t)})}function h(t){return Object.assign((t=>Object.assign(t,{*[Symbol.iterator](){yield Object.assign(t,{[e]:!0});}}))(t),{optional:()=>h(l(t)),select:e=>h(void 0===e?p(t):p(e,t))})}function l(e){return u({[t]:()=>({match:t=>{let n={};const r=(t,e)=>{n[t]=e;};return void 0===t?(o(e).forEach(t=>r(t,void 0)),{matched:!0,selections:n}):{matched:s(e,t,r),selections:n}},getSelectionKeys:()=>o(e),matcherType:"optional"})})}const f=(t,e)=>{for(const n of t)if(!e(n))return !1;return !0},g=(t,e)=>{for(const[n,r]of t.entries())if(!e(r,n))return !1;return !0};function m(...e){return u({[t]:()=>({match:t=>{let n={};const r=(t,e)=>{n[t]=e;};return {matched:e.every(e=>s(e,t,r)),selections:n}},getSelectionKeys:()=>c(e,o),matcherType:"and"})})}function y(...e){return u({[t]:()=>({match:t=>{let n={};const r=(t,e)=>{n[t]=e;};return c(e,o).forEach(t=>r(t,void 0)),{matched:e.some(e=>s(e,t,r)),selections:n}},getSelectionKeys:()=>c(e,o),matcherType:"or"})})}function d(e){return {[t]:()=>({match:t=>({matched:Boolean(e(t))})})}}function p(...e){const r="string"==typeof e[0]?e[0]:void 0,i=2===e.length?e[1]:"string"==typeof e[0]?void 0:e[0];return u({[t]:()=>({match:t=>{let e={[null!=r?r:n]:t};return {matched:void 0===i||s(i,t,(t,n)=>{e[t]=n;}),selections:e}},getSelectionKeys:()=>[null!=r?r:n].concat(void 0===i?[]:o(i))})})}function v(t){return "number"==typeof t}function b(t){return "string"==typeof t}function w(t){return "bigint"==typeof t}const S=u(d(function(t){return !0})),O=S,j=t=>Object.assign(u(t),{startsWith:e=>{return j(m(t,(n=e,d(t=>b(t)&&t.startsWith(n)))));var n;},endsWith:e=>{return j(m(t,(n=e,d(t=>b(t)&&t.endsWith(n)))));var n;},minLength:e=>j(m(t,(t=>d(e=>b(e)&&e.length>=t))(e))),maxLength:e=>j(m(t,(t=>d(e=>b(e)&&e.length<=t))(e))),includes:e=>{return j(m(t,(n=e,d(t=>b(t)&&t.includes(n)))));var n;},regex:e=>{return j(m(t,(n=e,d(t=>b(t)&&Boolean(t.match(n))))));var n;}}),E=j(d(b)),K=t=>Object.assign(u(t),{between:(e,n)=>K(m(t,((t,e)=>d(n=>v(n)&&t<=n&&e>=n))(e,n))),lt:e=>K(m(t,(t=>d(e=>v(e)&&e<t))(e))),gt:e=>K(m(t,(t=>d(e=>v(e)&&e>t))(e))),lte:e=>K(m(t,(t=>d(e=>v(e)&&e<=t))(e))),gte:e=>K(m(t,(t=>d(e=>v(e)&&e>=t))(e))),int:()=>K(m(t,d(t=>v(t)&&Number.isInteger(t)))),finite:()=>K(m(t,d(t=>v(t)&&Number.isFinite(t)))),positive:()=>K(m(t,d(t=>v(t)&&t>0))),negative:()=>K(m(t,d(t=>v(t)&&t<0)))}),A=K(d(v)),x=t=>Object.assign(u(t),{between:(e,n)=>x(m(t,((t,e)=>d(n=>w(n)&&t<=n&&e>=n))(e,n))),lt:e=>x(m(t,(t=>d(e=>w(e)&&e<t))(e))),gt:e=>x(m(t,(t=>d(e=>w(e)&&e>t))(e))),lte:e=>x(m(t,(t=>d(e=>w(e)&&e<=t))(e))),gte:e=>x(m(t,(t=>d(e=>w(e)&&e>=t))(e))),positive:()=>x(m(t,d(t=>w(t)&&t>0))),negative:()=>x(m(t,d(t=>w(t)&&t<0)))}),P=x(d(w)),T=u(d(function(t){return "boolean"==typeof t})),k=u(d(function(t){return "symbol"==typeof t})),B=u(d(function(t){return null==t}));var _={__proto__:null,matcher:t,optional:l,array:function(...e){return h({[t]:()=>({match:t=>{if(!Array.isArray(t))return {matched:!1};if(0===e.length)return {matched:!0};const n=e[0];let r={};if(0===t.length)return o(n).forEach(t=>{r[t]=[];}),{matched:!0,selections:r};const i=(t,e)=>{r[t]=(r[t]||[]).concat([e]);};return {matched:t.every(t=>s(n,t,i)),selections:r}},getSelectionKeys:()=>0===e.length?[]:o(e[0])})})},set:function(...e){return u({[t]:()=>({match:t=>{if(!(t instanceof Set))return {matched:!1};let n={};if(0===t.size)return {matched:!0,selections:n};if(0===e.length)return {matched:!0};const r=(t,e)=>{n[t]=(n[t]||[]).concat([e]);},i=e[0];return {matched:f(t,t=>s(i,t,r)),selections:n}},getSelectionKeys:()=>0===e.length?[]:o(e[0])})})},map:function(...e){return u({[t]:()=>({match:t=>{if(!(t instanceof Map))return {matched:!1};let n={};if(0===t.size)return {matched:!0,selections:n};const r=(t,e)=>{n[t]=(n[t]||[]).concat([e]);};if(0===e.length)return {matched:!0};var i;if(1===e.length)throw new Error(`\`P.map\` wasn't given enough arguments. Expected (key, value), received ${null==(i=e[0])?void 0:i.toString()}`);const[o,c]=e;return {matched:g(t,(t,e)=>{const n=s(o,e,r),i=s(c,t,r);return n&&i}),selections:n}},getSelectionKeys:()=>0===e.length?[]:[...o(e[0]),...o(e[1])]})})},intersection:m,union:y,not:function(e){return u({[t]:()=>({match:t=>({matched:!s(e,t,()=>{})}),getSelectionKeys:()=>[],matcherType:"not"})})},when:d,select:p,any:S,_:O,string:E,number:A,bigint:P,boolean:T,symbol:k,nullish:B,instanceOf:function(t){return u(d(function(t){return e=>e instanceof t}(t)))},shape:function(t){return u(d(a(t)))}};const W={matched:!1,value:void 0};function N(t){return new $(t,W)}class ${constructor(t,e){this.input=void 0,this.state=void 0,this.input=t,this.state=e;}with(...t){if(this.state.matched)return this;const e=t[t.length-1],r=[t[0]];let i;3===t.length&&"function"==typeof t[1]?i=t[1]:t.length>2&&r.push(...t.slice(1,t.length-1));let o=!1,c={};const a=(t,e)=>{o=!0,c[t]=e;},u=!r.some(t=>s(t,this.input,a))||i&&!Boolean(i(this.input))?W:{matched:!0,value:e(o?n in c?c[n]:c:this.input,this.input)};return new $(this.input,u)}when(t,e){if(this.state.matched)return this;const n=Boolean(t(this.input));return new $(this.input,n?{matched:!0,value:e(this.input,this.input)}:W)}otherwise(t){return this.state.matched?this.state.value:t(this.input)}exhaustive(){if(this.state.matched)return this.state.value;let t;try{t=JSON.stringify(this.input);}catch(e){t=this.input;}throw new Error(`Pattern matching error: no pattern matches value ${t}`)}run(){return this.exhaustive()}returnType(){return this}}
|
|
27
28
|
|
|
28
29
|
function isInitializedFromPragma(variableName, context, initialScope, pragma = getPragmaFromContext(context)) {
|
|
29
|
-
const variables =
|
|
30
|
-
const maybeVariable =
|
|
30
|
+
const variables = _var.getVariablesUpToGlobal(initialScope);
|
|
31
|
+
const maybeVariable = _var.findVariableByName(variableName)(variables);
|
|
31
32
|
const maybeLatestDef = tools.O.flatMapNullable(maybeVariable, (variable)=>variable.defs.at(-1));
|
|
32
33
|
if (tools.O.isNone(maybeLatestDef)) return false;
|
|
33
34
|
const latestDef = maybeLatestDef.value;
|
|
@@ -260,7 +261,7 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
260
261
|
// | DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES[
|
|
261
262
|
// keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES
|
|
262
263
|
// ];
|
|
263
|
-
/* eslint-disable perfectionist/sort-objects */ const
|
|
264
|
+
/* eslint-disable perfectionist/sort-objects */ const JSXValueHint = {
|
|
264
265
|
None: 0n,
|
|
265
266
|
SkipNullLiteral: 1n << 0n,
|
|
266
267
|
SkipUndefinedLiteral: 1n << 1n,
|
|
@@ -272,14 +273,14 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
272
273
|
StrictLogical: 1n << 7n,
|
|
273
274
|
StrictConditional: 1n << 8n
|
|
274
275
|
};
|
|
275
|
-
/* eslint-enable perfectionist/sort-objects */ const
|
|
276
|
+
/* eslint-enable perfectionist/sort-objects */ const DEFAULT_JSX_VALUE_HINT = JSXValueHint.SkipUndefinedLiteral | JSXValueHint.SkipBooleanLiteral;
|
|
276
277
|
/**
|
|
277
278
|
* Check if a node is a JSX value
|
|
278
279
|
* @param node The AST node to check
|
|
279
280
|
* @param context The rule context
|
|
280
|
-
* @param hint The `
|
|
281
|
+
* @param hint The `JSXValueHint` to use
|
|
281
282
|
* @returns boolean
|
|
282
|
-
*/ function isJSXValue(node, context, hint =
|
|
283
|
+
*/ function isJSXValue(node, context, hint = DEFAULT_JSX_VALUE_HINT) {
|
|
283
284
|
if (!node) return false;
|
|
284
285
|
return N(node).with({
|
|
285
286
|
type: ast.NodeType.JSXElement
|
|
@@ -292,20 +293,20 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
292
293
|
}, tools.F.constTrue).with({
|
|
293
294
|
type: ast.NodeType.Literal
|
|
294
295
|
}, (node)=>{
|
|
295
|
-
return N(node.value).with(null, ()=>!(hint &
|
|
296
|
+
return N(node.value).with(null, ()=>!(hint & JSXValueHint.SkipNullLiteral)).with(_.boolean, ()=>!(hint & JSXValueHint.SkipBooleanLiteral)).with(_.string, ()=>!(hint & JSXValueHint.SkipStringLiteral)).with(_.number, ()=>!(hint & JSXValueHint.SkipNumberLiteral)).otherwise(tools.F.constFalse);
|
|
296
297
|
}).with({
|
|
297
298
|
type: ast.NodeType.TemplateLiteral
|
|
298
|
-
}, ()=>!(hint &
|
|
299
|
+
}, ()=>!(hint & JSXValueHint.SkipStringLiteral)).with({
|
|
299
300
|
type: ast.NodeType.ArrayExpression
|
|
300
301
|
}, (node)=>{
|
|
301
|
-
if (hint &
|
|
302
|
+
if (hint & JSXValueHint.StrictArray) return node.elements.every((n)=>isJSXValue(n, context, hint));
|
|
302
303
|
return node.elements.some((n)=>isJSXValue(n, context, hint));
|
|
303
304
|
}).with({
|
|
304
305
|
type: ast.NodeType.ConditionalExpression
|
|
305
306
|
}, (node)=>{
|
|
306
307
|
function leftHasJSX(node) {
|
|
307
308
|
if (Array.isArray(node.consequent)) {
|
|
308
|
-
if (hint &
|
|
309
|
+
if (hint & JSXValueHint.StrictArray) {
|
|
309
310
|
return node.consequent.every((n)=>isJSXValue(n, context, hint));
|
|
310
311
|
}
|
|
311
312
|
return node.consequent.some((n)=>isJSXValue(n, context, hint));
|
|
@@ -315,7 +316,7 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
315
316
|
function rightHasJSX(node) {
|
|
316
317
|
return isJSXValue(node.alternate, context, hint);
|
|
317
318
|
}
|
|
318
|
-
if (hint &
|
|
319
|
+
if (hint & JSXValueHint.StrictConditional) {
|
|
319
320
|
return leftHasJSX(node) && rightHasJSX(node);
|
|
320
321
|
}
|
|
321
322
|
return leftHasJSX(node) || rightHasJSX(node);
|
|
@@ -331,17 +332,17 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
331
332
|
}).with({
|
|
332
333
|
type: ast.NodeType.CallExpression
|
|
333
334
|
}, (node)=>{
|
|
334
|
-
if (hint &
|
|
335
|
+
if (hint & JSXValueHint.SkipCreateElement) return false;
|
|
335
336
|
return isCreateElementCall(node, context);
|
|
336
337
|
}).with({
|
|
337
338
|
type: ast.NodeType.Identifier
|
|
338
339
|
}, (node)=>{
|
|
339
340
|
const { name } = node;
|
|
340
|
-
if (name === "undefined") return !(hint &
|
|
341
|
+
if (name === "undefined") return !(hint & JSXValueHint.SkipUndefinedLiteral);
|
|
341
342
|
if (ast.isJSXTagNameExpression(node)) return true;
|
|
342
343
|
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
343
|
-
const maybeVariable =
|
|
344
|
-
return tools.F.pipe(maybeVariable, tools.O.flatMap(
|
|
344
|
+
const maybeVariable = _var.findVariableByNameUpToGlobal(name, initialScope);
|
|
345
|
+
return tools.F.pipe(maybeVariable, tools.O.flatMap(_var.getVariableInit(0)), tools.O.exists((n)=>isJSXValue(n, context, hint)));
|
|
345
346
|
}).otherwise(tools.F.constFalse);
|
|
346
347
|
}
|
|
347
348
|
|
|
@@ -349,9 +350,9 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
349
350
|
* Check if function is returning JSX
|
|
350
351
|
* @param node The return statement node to check
|
|
351
352
|
* @param context The rule context
|
|
352
|
-
* @param hint The `
|
|
353
|
+
* @param hint The `JSXValueHint` to use
|
|
353
354
|
* @returns boolean
|
|
354
|
-
*/ function isFunctionReturningJSXValue(node, context, hint =
|
|
355
|
+
*/ function isFunctionReturningJSXValue(node, context, hint = DEFAULT_JSX_VALUE_HINT) {
|
|
355
356
|
if (node.body.type !== ast.NodeType.BlockStatement) {
|
|
356
357
|
return isJSXValue(node.body, context, hint);
|
|
357
358
|
}
|
|
@@ -405,7 +406,7 @@ function getProp(props, propName, context, initialScope) {
|
|
|
405
406
|
}).when(ast.is(ast.NodeType.SpreadElement), (prop)=>{
|
|
406
407
|
return N(prop.argument).when(ast.is(ast.NodeType.Identifier), (argument)=>{
|
|
407
408
|
const { name } = argument;
|
|
408
|
-
const maybeInit = tools.O.flatMap(
|
|
409
|
+
const maybeInit = tools.O.flatMap(_var.findVariableByNameUpToGlobal(name, initialScope), _var.getVariableInit(0));
|
|
409
410
|
if (tools.O.isNone(maybeInit)) return false;
|
|
410
411
|
const init = maybeInit.value;
|
|
411
412
|
if (init.type !== ast.NodeType.ObjectExpression) return false;
|
|
@@ -445,7 +446,7 @@ function getProp(props, propName, context, initialScope) {
|
|
|
445
446
|
type: ast.NodeType.Identifier
|
|
446
447
|
}, (argument)=>{
|
|
447
448
|
const { name } = argument;
|
|
448
|
-
const maybeInit = tools.O.flatMap(
|
|
449
|
+
const maybeInit = tools.O.flatMap(_var.findVariableByNameUpToGlobal(name, initialScope), _var.getVariableInit(0));
|
|
449
450
|
if (tools.O.isNone(maybeInit)) return false;
|
|
450
451
|
const init = maybeInit.value;
|
|
451
452
|
if (!("properties" in init)) return false;
|
|
@@ -546,8 +547,8 @@ function getProp(props, propName, context, initialScope) {
|
|
|
546
547
|
return isLiteral(node) && isWhiteSpace(node) && node.raw.includes("\n");
|
|
547
548
|
}
|
|
548
549
|
|
|
549
|
-
exports.
|
|
550
|
-
exports.
|
|
550
|
+
exports.DEFAULT_JSX_VALUE_HINT = DEFAULT_JSX_VALUE_HINT;
|
|
551
|
+
exports.JSXValueHint = JSXValueHint;
|
|
551
552
|
exports.elementType = elementType;
|
|
552
553
|
exports.findPropInAttributes = findPropInAttributes;
|
|
553
554
|
exports.findPropInProperties = findPropInProperties;
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { NodeType, isOneOf, is, traverseUp, isJSXTagNameExpression, getNestedReturnStatements, ESLintCommunityESLintUtils, traverseUpGuard, isStringLiteral, isMultiLine } from '@eslint-react/ast';
|
|
2
2
|
import { parseSchema, ESLintSettingsSchema } from '@eslint-react/shared';
|
|
3
3
|
import { F, O as O$1, _ as _$1 } from '@eslint-react/tools';
|
|
4
4
|
import memo from 'micro-memoize';
|
|
5
|
+
import { getVariablesUpToGlobal, findVariableByName, findVariableByNameUpToGlobal, getVariableInit } from '@eslint-react/var';
|
|
5
6
|
import { AST_NODE_TYPES } from '@typescript-eslint/types';
|
|
6
7
|
|
|
7
8
|
const RE_JSX_ANNOTATION_REGEX = /@jsx\s+(\S+)/u;
|
|
@@ -258,7 +259,7 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
258
259
|
// | DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES[
|
|
259
260
|
// keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES
|
|
260
261
|
// ];
|
|
261
|
-
/* eslint-disable perfectionist/sort-objects */ const
|
|
262
|
+
/* eslint-disable perfectionist/sort-objects */ const JSXValueHint = {
|
|
262
263
|
None: 0n,
|
|
263
264
|
SkipNullLiteral: 1n << 0n,
|
|
264
265
|
SkipUndefinedLiteral: 1n << 1n,
|
|
@@ -270,14 +271,14 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
270
271
|
StrictLogical: 1n << 7n,
|
|
271
272
|
StrictConditional: 1n << 8n
|
|
272
273
|
};
|
|
273
|
-
/* eslint-enable perfectionist/sort-objects */ const
|
|
274
|
+
/* eslint-enable perfectionist/sort-objects */ const DEFAULT_JSX_VALUE_HINT = JSXValueHint.SkipUndefinedLiteral | JSXValueHint.SkipBooleanLiteral;
|
|
274
275
|
/**
|
|
275
276
|
* Check if a node is a JSX value
|
|
276
277
|
* @param node The AST node to check
|
|
277
278
|
* @param context The rule context
|
|
278
|
-
* @param hint The `
|
|
279
|
+
* @param hint The `JSXValueHint` to use
|
|
279
280
|
* @returns boolean
|
|
280
|
-
*/ function isJSXValue(node, context, hint =
|
|
281
|
+
*/ function isJSXValue(node, context, hint = DEFAULT_JSX_VALUE_HINT) {
|
|
281
282
|
if (!node) return false;
|
|
282
283
|
return N(node).with({
|
|
283
284
|
type: NodeType.JSXElement
|
|
@@ -290,20 +291,20 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
290
291
|
}, F.constTrue).with({
|
|
291
292
|
type: NodeType.Literal
|
|
292
293
|
}, (node)=>{
|
|
293
|
-
return N(node.value).with(null, ()=>!(hint &
|
|
294
|
+
return N(node.value).with(null, ()=>!(hint & JSXValueHint.SkipNullLiteral)).with(_.boolean, ()=>!(hint & JSXValueHint.SkipBooleanLiteral)).with(_.string, ()=>!(hint & JSXValueHint.SkipStringLiteral)).with(_.number, ()=>!(hint & JSXValueHint.SkipNumberLiteral)).otherwise(F.constFalse);
|
|
294
295
|
}).with({
|
|
295
296
|
type: NodeType.TemplateLiteral
|
|
296
|
-
}, ()=>!(hint &
|
|
297
|
+
}, ()=>!(hint & JSXValueHint.SkipStringLiteral)).with({
|
|
297
298
|
type: NodeType.ArrayExpression
|
|
298
299
|
}, (node)=>{
|
|
299
|
-
if (hint &
|
|
300
|
+
if (hint & JSXValueHint.StrictArray) return node.elements.every((n)=>isJSXValue(n, context, hint));
|
|
300
301
|
return node.elements.some((n)=>isJSXValue(n, context, hint));
|
|
301
302
|
}).with({
|
|
302
303
|
type: NodeType.ConditionalExpression
|
|
303
304
|
}, (node)=>{
|
|
304
305
|
function leftHasJSX(node) {
|
|
305
306
|
if (Array.isArray(node.consequent)) {
|
|
306
|
-
if (hint &
|
|
307
|
+
if (hint & JSXValueHint.StrictArray) {
|
|
307
308
|
return node.consequent.every((n)=>isJSXValue(n, context, hint));
|
|
308
309
|
}
|
|
309
310
|
return node.consequent.some((n)=>isJSXValue(n, context, hint));
|
|
@@ -313,7 +314,7 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
313
314
|
function rightHasJSX(node) {
|
|
314
315
|
return isJSXValue(node.alternate, context, hint);
|
|
315
316
|
}
|
|
316
|
-
if (hint &
|
|
317
|
+
if (hint & JSXValueHint.StrictConditional) {
|
|
317
318
|
return leftHasJSX(node) && rightHasJSX(node);
|
|
318
319
|
}
|
|
319
320
|
return leftHasJSX(node) || rightHasJSX(node);
|
|
@@ -329,13 +330,13 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
329
330
|
}).with({
|
|
330
331
|
type: NodeType.CallExpression
|
|
331
332
|
}, (node)=>{
|
|
332
|
-
if (hint &
|
|
333
|
+
if (hint & JSXValueHint.SkipCreateElement) return false;
|
|
333
334
|
return isCreateElementCall(node, context);
|
|
334
335
|
}).with({
|
|
335
336
|
type: NodeType.Identifier
|
|
336
337
|
}, (node)=>{
|
|
337
338
|
const { name } = node;
|
|
338
|
-
if (name === "undefined") return !(hint &
|
|
339
|
+
if (name === "undefined") return !(hint & JSXValueHint.SkipUndefinedLiteral);
|
|
339
340
|
if (isJSXTagNameExpression(node)) return true;
|
|
340
341
|
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
341
342
|
const maybeVariable = findVariableByNameUpToGlobal(name, initialScope);
|
|
@@ -347,9 +348,9 @@ const isFragment = (node, pragma, fragment)=>{
|
|
|
347
348
|
* Check if function is returning JSX
|
|
348
349
|
* @param node The return statement node to check
|
|
349
350
|
* @param context The rule context
|
|
350
|
-
* @param hint The `
|
|
351
|
+
* @param hint The `JSXValueHint` to use
|
|
351
352
|
* @returns boolean
|
|
352
|
-
*/ function isFunctionReturningJSXValue(node, context, hint =
|
|
353
|
+
*/ function isFunctionReturningJSXValue(node, context, hint = DEFAULT_JSX_VALUE_HINT) {
|
|
353
354
|
if (node.body.type !== NodeType.BlockStatement) {
|
|
354
355
|
return isJSXValue(node.body, context, hint);
|
|
355
356
|
}
|
|
@@ -544,4 +545,4 @@ function getProp(props, propName, context, initialScope) {
|
|
|
544
545
|
return isLiteral(node) && isWhiteSpace(node) && node.raw.includes("\n");
|
|
545
546
|
}
|
|
546
547
|
|
|
547
|
-
export {
|
|
548
|
+
export { DEFAULT_JSX_VALUE_HINT, JSXValueHint, elementType, findPropInAttributes, findPropInProperties, getFragmentFromContext, getPragmaFromContext, getProp, getPropName, getPropValue, hasAnyProp, hasChildren, hasEveryProp, hasProp, isCallFromPragma, isCallFromPragmaMember, isChildOfJSXElement, isChildrenOfCreateElement, isCloneElement, isCloneElementCall, isCreateElement, isCreateElementCall, isFragment, isFragmentElement, isFragmentSyntax, isFromPragma, isFromPragmaMember, isFunctionReturningJSXValue, isInitializedFromPragma, isInsideCreateElementProps, isInsidePropValue, isJSXElementOfBuiltinComponent, isJSXElementOfUserDefinedComponent, isJSXValue, isLineBreak, isLiteral, isPaddingSpaces, isPropertyOfPragma, isWhiteSpace, traverseUpProp };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-react/jsx",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.6-beta.0",
|
|
4
4
|
"description": "ESLint React's TSESTree AST utility module for static analysis of JSX.",
|
|
5
5
|
"homepage": "https://github.com/rel1cx/eslint-react",
|
|
6
6
|
"bugs": {
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
11
|
"url": "https://github.com/Rel1cx/eslint-react",
|
|
12
|
-
"directory": "packages/jsx"
|
|
12
|
+
"directory": "packages/utilities/jsx"
|
|
13
13
|
},
|
|
14
14
|
"license": "MIT",
|
|
15
15
|
"author": "Eva1ent<let@ik.me>",
|
|
@@ -39,10 +39,11 @@
|
|
|
39
39
|
"@typescript-eslint/types": "6.18.0",
|
|
40
40
|
"@typescript-eslint/utils": "6.18.0",
|
|
41
41
|
"micro-memoize": "4.1.2",
|
|
42
|
-
"@eslint-react/ast": "0.10.
|
|
43
|
-
"@eslint-react/shared": "0.10.
|
|
44
|
-
"@eslint-react/tools": "0.10.
|
|
45
|
-
"@eslint-react/types": "0.10.
|
|
42
|
+
"@eslint-react/ast": "0.10.6-beta.0",
|
|
43
|
+
"@eslint-react/shared": "0.10.6-beta.0",
|
|
44
|
+
"@eslint-react/tools": "0.10.6-beta.0",
|
|
45
|
+
"@eslint-react/types": "0.10.6-beta.0",
|
|
46
|
+
"@eslint-react/var": "0.10.6-beta.0"
|
|
46
47
|
},
|
|
47
48
|
"scripts": {
|
|
48
49
|
"build": "rollup -c rollup.config.ts --configPlugin swc3 && cp dist/index.d.ts dist/index.d.mts",
|