@eslint-react/jsx 0.9.8-beta.0 → 0.9.8-beta.1

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 CHANGED
@@ -1,38 +1,10 @@
1
1
  'use strict';
2
2
 
3
- var types = require('@typescript-eslint/types');
4
3
  var ast = require('@eslint-react/ast');
5
- var tools = require('@eslint-react/tools');
6
4
  var shared = require('@eslint-react/shared');
5
+ var tools = require('@eslint-react/tools');
7
6
  var memo = require('micro-memoize');
8
-
9
- function resolveMemberExpressions(object, property) {
10
- if (object.type === types.AST_NODE_TYPES.JSXMemberExpression) {
11
- return `${resolveMemberExpressions(object.object, object.property)}.${property.name}`;
12
- }
13
- if (object.type === types.AST_NODE_TYPES.JSXNamespacedName) {
14
- return `${object.namespace.name}:${object.name.name}.${property.name}`;
15
- }
16
- return `${object.name}.${property.name}`;
17
- }
18
- /**
19
- * Returns the tag name associated with a JSXOpeningElement.
20
- * @param node The visited JSXOpeningElement node object.
21
- * @returns The element's tag name.
22
- */ function elementType(node) {
23
- if (node.type === types.AST_NODE_TYPES.JSXOpeningFragment) {
24
- return "<>";
25
- }
26
- const { name } = node;
27
- if (name.type === types.AST_NODE_TYPES.JSXMemberExpression) {
28
- const { object, property } = name;
29
- return resolveMemberExpressions(object, property);
30
- }
31
- if (name.type === types.AST_NODE_TYPES.JSXNamespacedName) {
32
- return `${name.namespace.name}:${name.name.name}`;
33
- }
34
- return name.name;
35
- }
7
+ var types = require('@typescript-eslint/types');
36
8
 
37
9
  const RE_JSX_ANNOTATION_REGEX = /@jsx\s+(\S+)/u;
38
10
  // Does not check for reserved keywords or unicode characters
@@ -40,7 +12,7 @@ const RE_JS_IDENTIFIER_REGEX = /^[$A-Z_a-z][\w$]*$/u;
40
12
  function getFragmentFromContext(context) {
41
13
  const settings = shared.parse(shared.ESLintSettingsSchema, context.settings);
42
14
  const fragment = settings.eslintReact?.jsx?.fragment;
43
- if (tools.P.isString(fragment) && RE_JS_IDENTIFIER_REGEX.test(fragment)) {
15
+ if (tools._.isString(fragment) && RE_JS_IDENTIFIER_REGEX.test(fragment)) {
44
16
  return fragment;
45
17
  }
46
18
  return "Fragment";
@@ -53,6 +25,8 @@ const getPragmaFromContext = memo((context)=>{
53
25
  return tools.F.pipe(tools.O.orElse(tools.O.fromNullable(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
26
  });
55
27
 
28
+ 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}}
29
+
56
30
  function isInitializedFromPragma(variableName, context, initialScope, pragma = getPragmaFromContext(context)) {
57
31
  const variables = ast.getVariablesUpToGlobal(initialScope);
58
32
  const maybeVariable = ast.findVariableByName(variableName)(variables);
@@ -65,7 +39,7 @@ function isInitializedFromPragma(variableName, context, initialScope, pragma = g
65
39
  if (node.type === ast.NodeType.VariableDeclarator && node.init) {
66
40
  const { init } = node;
67
41
  // check for: `variable = pragma.variable`
68
- if (tools.M.isMatching({
42
+ if (a({
69
43
  type: "MemberExpression",
70
44
  object: {
71
45
  type: "Identifier",
@@ -75,14 +49,14 @@ function isInitializedFromPragma(variableName, context, initialScope, pragma = g
75
49
  return true;
76
50
  }
77
51
  // check for: `{ variable } = pragma`
78
- if (tools.M.isMatching({
52
+ if (a({
79
53
  type: "Identifier",
80
54
  name: pragma
81
55
  }, init)) {
82
56
  return true;
83
57
  }
84
58
  // check if from a require call: `require("react")`
85
- const maybeRequireExpression = tools.M.match(init).with({
59
+ const maybeRequireExpression = N(init).with({
86
60
  type: ast.NodeType.CallExpression,
87
61
  callee: {
88
62
  type: ast.NodeType.Identifier,
@@ -109,7 +83,7 @@ function isInitializedFromPragma(variableName, context, initialScope, pragma = g
109
83
  return firstArg.value === pragma.toLowerCase();
110
84
  }
111
85
  // latest definition is an import declaration: import { variable } from 'react'
112
- return tools.M.isMatching({
86
+ return a({
113
87
  type: "ImportDeclaration",
114
88
  source: {
115
89
  value: pragma.toLowerCase()
@@ -117,7 +91,7 @@ function isInitializedFromPragma(variableName, context, initialScope, pragma = g
117
91
  }, parent);
118
92
  }
119
93
  function isPropertyOfPragma(name, context, pragma = getPragmaFromContext(context)) {
120
- const isMatch = tools.M.isMatching({
94
+ const isMatch = a({
121
95
  type: ast.NodeType.MemberExpression,
122
96
  object: {
123
97
  type: ast.NodeType.Identifier,
@@ -133,34 +107,106 @@ function isPropertyOfPragma(name, context, pragma = getPragmaFromContext(context
133
107
  * Checks if the given node is a call expression to the given function or method of the pragma
134
108
  * @param name The name of the function or method to check
135
109
  * @returns A predicate that checks if the given node is a call expression to the given function or method
136
- */ function isCallFromPragma(name) {
110
+ */ function isFromPragma(name) {
137
111
  return (node, context)=>{
138
112
  const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
139
- if (node.type !== ast.NodeType.CallExpression || !("callee" in node)) {
113
+ if (node.type === ast.NodeType.MemberExpression) {
114
+ return isPropertyOfPragma(name, context)(node);
115
+ }
116
+ if (node.name === name) {
117
+ return isInitializedFromPragma(name, context, initialScope);
118
+ }
119
+ return false;
120
+ };
121
+ }
122
+ /**
123
+ * @internal
124
+ * @param pragmaMemberName
125
+ * @param name
126
+ * @returns A function that checks if a given node is a member expression of a Pragma member.
127
+ */ function isFromPragmaMember(pragmaMemberName, name) {
128
+ return (node, context, pragma = getPragmaFromContext(context))=>{
129
+ const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
130
+ if (node.property.type !== ast.NodeType.Identifier || node.property.name !== name) {
140
131
  return false;
141
132
  }
142
- if (node.callee.type === ast.NodeType.MemberExpression) {
143
- return isPropertyOfPragma(name, context)(node.callee);
133
+ if (node.object.type === ast.NodeType.Identifier && node.object.name === pragmaMemberName) {
134
+ return isInitializedFromPragma(node.object.name, context, initialScope, pragma);
144
135
  }
145
- if ("name" in node.callee && node.callee.name === name) {
146
- return isInitializedFromPragma(name, context, initialScope);
136
+ if (node.object.type === ast.NodeType.MemberExpression && node.object.object.type === ast.NodeType.Identifier && node.object.object.name === pragma && node.object.property.type === ast.NodeType.Identifier) {
137
+ return node.object.property.name === pragmaMemberName;
147
138
  }
148
139
  return false;
149
140
  };
150
141
  }
142
+ function isCallFromPragma(name) {
143
+ return (node, context)=>{
144
+ if (!ast.isOneOf([
145
+ ast.NodeType.Identifier,
146
+ ast.NodeType.MemberExpression
147
+ ])(node.callee)) {
148
+ return false;
149
+ }
150
+ return isFromPragma(name)(node.callee, context);
151
+ };
152
+ }
153
+ function isCallFromPragmaMember(pragmaMemberName, name) {
154
+ return (node, context)=>{
155
+ if (!ast.is(ast.NodeType.MemberExpression)(node.callee)) {
156
+ return false;
157
+ }
158
+ return isFromPragmaMember(pragmaMemberName, name)(node.callee, context);
159
+ };
160
+ }
151
161
 
162
+ const isCreateElement = isFromPragma("createElement");
163
+ const isCreateElementCall = (node, context)=>{
164
+ if (!ast.isOneOf([
165
+ ast.NodeType.Identifier,
166
+ ast.NodeType.MemberExpression
167
+ ])(node.callee)) {
168
+ return false;
169
+ }
170
+ return isCreateElement(node.callee, context);
171
+ };
172
+ const isCloneElement = isFromPragma("cloneElement");
173
+ const isCloneElementCall = (node, context)=>{
174
+ if (!ast.isOneOf([
175
+ ast.NodeType.Identifier,
176
+ ast.NodeType.MemberExpression
177
+ ])(node.callee)) {
178
+ return false;
179
+ }
180
+ return isCloneElement(node.callee, context);
181
+ };
182
+
183
+ function resolveMemberExpressions(object, property) {
184
+ if (object.type === types.AST_NODE_TYPES.JSXMemberExpression) {
185
+ return `${resolveMemberExpressions(object.object, object.property)}.${property.name}`;
186
+ }
187
+ if (object.type === types.AST_NODE_TYPES.JSXNamespacedName) {
188
+ return `${object.namespace.name}:${object.name.name}.${property.name}`;
189
+ }
190
+ return `${object.name}.${property.name}`;
191
+ }
152
192
  /**
153
- * Checks if the given node is a call expression to `createElement`
154
- * @param node The AST node to check
155
- * @param context The rule context
156
- * @returns `true` if the node is a call expression to `createElement`
157
- */ const isCreateElementCall = isCallFromPragma("createElement");
158
- /**
159
- * Checks if the given node is a call expression to `cloneElement`
160
- * @param node The AST node to check
161
- * @param context The rule context
162
- * @returns `true` if the node is a call expression to `cloneElement`
163
- */ const isCloneElementCall = isCallFromPragma("cloneElement");
193
+ * Returns the tag name associated with a JSXOpeningElement.
194
+ * @param node The visited JSXOpeningElement node object.
195
+ * @returns The element's tag name.
196
+ */ function elementType(node) {
197
+ if (node.type === types.AST_NODE_TYPES.JSXOpeningFragment) {
198
+ return "<>";
199
+ }
200
+ const { name } = node;
201
+ if (name.type === types.AST_NODE_TYPES.JSXMemberExpression) {
202
+ const { object, property } = name;
203
+ return resolveMemberExpressions(object, property);
204
+ }
205
+ if (name.type === types.AST_NODE_TYPES.JSXNamespacedName) {
206
+ return `${name.namespace.name}:${name.name.name}`;
207
+ }
208
+ return name.name;
209
+ }
164
210
 
165
211
  /**
166
212
  * Determines whether inside createElement's props.
@@ -168,14 +214,10 @@ function isPropertyOfPragma(name, context, pragma = getPragmaFromContext(context
168
214
  * @param context The rule context
169
215
  * @returns `true` if the node is inside createElement's props
170
216
  */ function isInsideCreateElementProps(node, context) {
171
- return tools.F.pipe(ast.traverseUp(node, (n)=>isCreateElementCall(n, context)), tools.O.filter(ast.is(ast.NodeType.CallExpression)), tools.O.flatMapNullable((c)=>c.arguments.at(1)), tools.O.filter(ast.is(ast.NodeType.ObjectExpression)), tools.O.zipWith(ast.traverseUp(node, ast.is(ast.NodeType.ObjectExpression)), (a, b)=>a === b), tools.O.getOrElse(tools.F.constFalse));
217
+ return tools.F.pipe(ast.traverseUp(node, (n)=>ast.is(ast.NodeType.CallExpression)(n) && isCreateElementCall(n, context)), tools.O.filter(ast.is(ast.NodeType.CallExpression)), tools.O.flatMapNullable((c)=>c.arguments.at(1)), tools.O.filter(ast.is(ast.NodeType.ObjectExpression)), tools.O.zipWith(ast.traverseUp(node, ast.is(ast.NodeType.ObjectExpression)), (a, b)=>a === b), tools.O.getOrElse(tools.F.constFalse));
172
218
  }
173
219
  function isChildrenOfCreateElement(node, context) {
174
- const maybeCallExpression = node.parent;
175
- if (!maybeCallExpression || !isCreateElementCall(maybeCallExpression, context)) {
176
- return false;
177
- }
178
- return maybeCallExpression.arguments.slice(2).some((child)=>child === node);
220
+ return tools.F.pipe(tools.O.fromNullable(node.parent), tools.O.filter(ast.is(ast.NodeType.CallExpression)), tools.O.filter((n)=>isCreateElementCall(n, context)), tools.O.exists((n)=>n.arguments.slice(2).some((arg)=>arg === node)));
179
221
  }
180
222
  /**
181
223
  * Check if a `JSXElement` or `JSXFragment` has children
@@ -387,7 +429,7 @@ const isFragment = (node, pragma, fragment)=>{
387
429
  if (!node) {
388
430
  return false;
389
431
  }
390
- return tools.M.match(node).with({
432
+ return N(node).with({
391
433
  type: ast.NodeType.JSXElement
392
434
  }, tools.F.constTrue).with({
393
435
  type: ast.NodeType.JSXFragment
@@ -398,7 +440,7 @@ const isFragment = (node, pragma, fragment)=>{
398
440
  }, tools.F.constTrue).with({
399
441
  type: ast.NodeType.Literal
400
442
  }, (node)=>{
401
- return tools.M.match(node.value).with(null, ()=>!(hint & JSXValueCheckHint.SkipNullLiteral)).with(tools.M.P.boolean, ()=>!(hint & JSXValueCheckHint.SkipBooleanLiteral)).with(tools.M.P.string, ()=>!(hint & JSXValueCheckHint.SkipStringLiteral)).with(tools.M.P.number, ()=>!(hint & JSXValueCheckHint.SkipNumberLiteral)).otherwise(tools.F.constFalse);
443
+ return N(node.value).with(null, ()=>!(hint & JSXValueCheckHint.SkipNullLiteral)).with(_.boolean, ()=>!(hint & JSXValueCheckHint.SkipBooleanLiteral)).with(_.string, ()=>!(hint & JSXValueCheckHint.SkipStringLiteral)).with(_.number, ()=>!(hint & JSXValueCheckHint.SkipNumberLiteral)).otherwise(tools.F.constFalse);
402
444
  }).with({
403
445
  type: ast.NodeType.TemplateLiteral
404
446
  }, ()=>{
@@ -475,12 +517,13 @@ const isFragment = (node, pragma, fragment)=>{
475
517
  return statements.some((statement)=>isJSXValue(statement.argument, context, hint));
476
518
  }
477
519
 
520
+ const { getStaticValue } = ast.ESLintCommunityESLintUtils;
478
521
  /**
479
522
  * Get the name of a JSX attribute with namespace
480
523
  * @param node The JSX attribute node
481
524
  * @returns string
482
525
  */ function getPropName(node) {
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();
526
+ return N(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();
484
527
  }
485
528
  function getProp(props, propName, context, initialScope) {
486
529
  return findPropInAttributes(props, context, initialScope)(propName);
@@ -498,15 +541,15 @@ function getProp(props, propName, context, initialScope) {
498
541
  return tools.O.none();
499
542
  }
500
543
  if (value.type === ast.NodeType.Literal) {
501
- return tools.O.some(ast.getStaticValue(value, initialScope));
544
+ return tools.O.some(getStaticValue(value, initialScope));
502
545
  }
503
546
  if (value.type === ast.NodeType.JSXExpressionContainer) {
504
- return tools.O.some(ast.getStaticValue(value.expression, initialScope));
547
+ return tools.O.some(getStaticValue(value.expression, initialScope));
505
548
  }
506
549
  return tools.O.none();
507
550
  }
508
551
  const { argument } = attribute;
509
- return tools.O.some(ast.getStaticValue(argument, initialScope));
552
+ return tools.O.some(getStaticValue(argument, initialScope));
510
553
  }
511
554
  /**
512
555
  * @param properties The properties to search in
@@ -521,10 +564,10 @@ function getProp(props, propName, context, initialScope) {
521
564
  * @returns The property if found
522
565
  */ return (propName)=>{
523
566
  return tools.O.fromNullable(properties.find((prop)=>{
524
- return tools.M.match(prop).when(ast.is(ast.NodeType.Property), (prop)=>{
567
+ return N(prop).when(ast.is(ast.NodeType.Property), (prop)=>{
525
568
  return "name" in prop.key && prop.key.name === propName;
526
569
  }).when(ast.is(ast.NodeType.SpreadElement), (prop)=>{
527
- return tools.M.match(prop.argument).when(ast.is(ast.NodeType.Identifier), (argument)=>{
570
+ return N(prop.argument).when(ast.is(ast.NodeType.Identifier), (argument)=>{
528
571
  const { name } = argument;
529
572
  const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name, initialScope), ast.getVariableInit(0));
530
573
  if (tools.O.isNone(maybeInit)) {
@@ -567,8 +610,8 @@ function getProp(props, propName, context, initialScope) {
567
610
  * @returns The property if found
568
611
  */ return (propName)=>{
569
612
  return tools.O.fromNullable(attributes.find((attr)=>{
570
- return tools.M.match(attr).when(ast.is(ast.NodeType.JSXAttribute), (attr)=>getPropName(attr) === propName).when(ast.is(ast.NodeType.JSXSpreadAttribute), (attr)=>{
571
- return tools.M.match(attr.argument).with({
613
+ return N(attr).when(ast.is(ast.NodeType.JSXAttribute), (attr)=>getPropName(attr) === propName).when(ast.is(ast.NodeType.JSXSpreadAttribute), (attr)=>{
614
+ return N(attr.argument).with({
572
615
  type: ast.NodeType.Identifier
573
616
  }, (argument)=>{
574
617
  const { name } = argument;
@@ -662,7 +705,7 @@ function getProp(props, propName, context, initialScope) {
662
705
  * @param node The AST node to check
663
706
  * @returns boolean `true` if the node is whitespace
664
707
  */ function isWhiteSpace(node) {
665
- return tools.P.isString(node.value) && node.value.trim() === "";
708
+ return tools._.isString(node.value) && node.value.trim() === "";
666
709
  }
667
710
  /**
668
711
  * Check if a Literal or JSXText node is a line break
@@ -708,13 +751,18 @@ exports.hdlTouch = hdlTouch;
708
751
  exports.hdlTransition = hdlTransition;
709
752
  exports.hdlWheel = hdlWheel;
710
753
  exports.isCallFromPragma = isCallFromPragma;
754
+ exports.isCallFromPragmaMember = isCallFromPragmaMember;
711
755
  exports.isChildOfJSXElement = isChildOfJSXElement;
712
756
  exports.isChildrenOfCreateElement = isChildrenOfCreateElement;
757
+ exports.isCloneElement = isCloneElement;
713
758
  exports.isCloneElementCall = isCloneElementCall;
759
+ exports.isCreateElement = isCreateElement;
714
760
  exports.isCreateElementCall = isCreateElementCall;
715
761
  exports.isFragment = isFragment;
716
762
  exports.isFragmentElement = isFragmentElement;
717
763
  exports.isFragmentSyntax = isFragmentSyntax;
764
+ exports.isFromPragma = isFromPragma;
765
+ exports.isFromPragmaMember = isFromPragmaMember;
718
766
  exports.isFunctionReturningJSXValue = isFunctionReturningJSXValue;
719
767
  exports.isInitializedFromPragma = isInitializedFromPragma;
720
768
  exports.isInsideCreateElementProps = isInsideCreateElementProps;
package/dist/index.d.mts CHANGED
@@ -1,10 +1,16 @@
1
- import { TSESTree } from '@typescript-eslint/types';
2
1
  import { RuleContext } from '@eslint-react/types';
3
- import { Scope } from '@typescript-eslint/scope-manager';
2
+ import { TSESTree } from '@typescript-eslint/types';
4
3
  import { TSESTreeFunction } from '@eslint-react/ast';
4
+ import { Scope } from '@typescript-eslint/scope-manager';
5
5
  import { O } from '@eslint-react/tools';
6
6
  import { TSESTree as TSESTree$1 } from '@typescript-eslint/utils';
7
7
 
8
+ type _ = (node: TSESTree.Identifier | TSESTree.MemberExpression, context: RuleContext) => boolean;
9
+ declare const isCreateElement: _;
10
+ declare const isCreateElementCall: (node: TSESTree.CallExpression, context: RuleContext) => boolean;
11
+ declare const isCloneElement: _;
12
+ declare const isCloneElementCall: (node: TSESTree.CallExpression, context: RuleContext) => boolean;
13
+
8
14
  /**
9
15
  * Returns the tag name associated with a JSXOpeningElement.
10
16
  * @param node The visited JSXOpeningElement node object.
@@ -49,34 +55,6 @@ declare function isJSXElementOfUserDefinedComponent(node: TSESTree.Node): node i
49
55
  */
50
56
  declare function isJSXElementOfBuiltinComponent(node: TSESTree.Node): node is TSESTree.JSXFragment;
51
57
 
52
- declare function getFragmentFromContext<T extends RuleContext>(context: T): string;
53
- declare const getPragmaFromContext: <T extends RuleContext>(context: T) => string;
54
-
55
- declare function isInitializedFromPragma(variableName: string, context: RuleContext, initialScope: Scope, pragma?: string): boolean;
56
- declare function isPropertyOfPragma(name: string, context: RuleContext, pragma?: string): (node: TSESTree.Node) => boolean;
57
- type CallFromPragmaPredicate = (node: TSESTree.Node, context: RuleContext) => node is TSESTree.CallExpression;
58
- /**
59
- * Checks if the given node is a call expression to the given function or method of the pragma
60
- * @param name The name of the function or method to check
61
- * @returns A predicate that checks if the given node is a call expression to the given function or method
62
- */
63
- declare function isCallFromPragma(name: string): (node: TSESTree.Node, context: RuleContext) => node is TSESTree.CallExpression;
64
-
65
- /**
66
- * Checks if the given node is a call expression to `createElement`
67
- * @param node The AST node to check
68
- * @param context The rule context
69
- * @returns `true` if the node is a call expression to `createElement`
70
- */
71
- declare const isCreateElementCall: CallFromPragmaPredicate;
72
- /**
73
- * Checks if the given node is a call expression to `cloneElement`
74
- * @param node The AST node to check
75
- * @param context The rule context
76
- * @returns `true` if the node is a call expression to `cloneElement`
77
- */
78
- declare const isCloneElementCall: CallFromPragmaPredicate;
79
-
80
58
  declare const hdlAnimation: readonly ["onAnimationStart", "onAnimationEnd", "onAnimationIteration"];
81
59
  declare const hdlClipboard: readonly ["onCopy", "onCut", "onPaste"];
82
60
  declare const hdlComposition: readonly ["onCompositionEnd", "onCompositionStart", "onCompositionUpdate"];
@@ -114,6 +92,27 @@ declare function isFragmentElement(node: TSESTree.JSXElement, pragma: string, fr
114
92
  */
115
93
  declare function isFunctionReturningJSXValue(node: TSESTreeFunction, context: RuleContext, hint?: bigint): boolean;
116
94
 
95
+ declare function getFragmentFromContext<T extends RuleContext>(context: T): string;
96
+ declare const getPragmaFromContext: <T extends RuleContext>(context: T) => string;
97
+
98
+ declare function isInitializedFromPragma(variableName: string, context: RuleContext, initialScope: Scope, pragma?: string): boolean;
99
+ declare function isPropertyOfPragma(name: string, context: RuleContext, pragma?: string): (node: TSESTree.Node) => boolean;
100
+ /**
101
+ * Checks if the given node is a call expression to the given function or method of the pragma
102
+ * @param name The name of the function or method to check
103
+ * @returns A predicate that checks if the given node is a call expression to the given function or method
104
+ */
105
+ declare function isFromPragma(name: string): (node: TSESTree.Identifier | TSESTree.MemberExpression, context: RuleContext) => boolean;
106
+ /**
107
+ * @internal
108
+ * @param pragmaMemberName
109
+ * @param name
110
+ * @returns A function that checks if a given node is a member expression of a Pragma member.
111
+ */
112
+ declare function isFromPragmaMember(pragmaMemberName: string, name: string): (node: TSESTree.MemberExpression, context: RuleContext, pragma?: string) => boolean;
113
+ declare function isCallFromPragma(name: string): (node: TSESTree.CallExpression, context: RuleContext) => boolean;
114
+ declare function isCallFromPragmaMember(pragmaMemberName: string, name: string): (node: TSESTree.CallExpression, context: RuleContext) => boolean;
115
+
117
116
  /**
118
117
  * Get the name of a JSX attribute with namespace
119
118
  * @param node The JSX attribute node
@@ -238,4 +237,4 @@ declare const DEFAULT_JSX_VALUE_CHECK_HINT: bigint;
238
237
  */
239
238
  declare function isJSXValue(node: TSESTree$1.Node | null | undefined, context: RuleContext, hint?: bigint): boolean;
240
239
 
241
- export { type CallFromPragmaPredicate, DEFAULT_JSX_VALUE_CHECK_HINT, JSXValueCheckHint, elementType, findPropInAttributes, findPropInProperties, getFragmentFromContext, getPragmaFromContext, getProp, getPropName, getPropValue, hasAnyProp, hasChildren, hasEveryProp, hasProp, hdlAnimation, hdlClipboard, hdlComposition, hdlFocus, hdlForm, hdlImage, hdlKeyboard, hdlMedia, hdlMouse, hdlScroll, hdlSelection, hdlTouch, hdlTransition, hdlWheel, isCallFromPragma, isChildOfJSXElement, isChildrenOfCreateElement, isCloneElementCall, isCreateElementCall, isFragment, isFragmentElement, isFragmentSyntax, isFunctionReturningJSXValue, isInitializedFromPragma, isInsideCreateElementProps, isInsidePropValue, isJSXElementOfBuiltinComponent, isJSXElementOfUserDefinedComponent, isJSXValue, isLineBreak, isLiteral, isPaddingSpaces, isPropertyOfPragma, isWhiteSpace, traverseUpProp };
240
+ export { DEFAULT_JSX_VALUE_CHECK_HINT, JSXValueCheckHint, elementType, findPropInAttributes, findPropInProperties, getFragmentFromContext, getPragmaFromContext, getProp, getPropName, getPropValue, hasAnyProp, hasChildren, hasEveryProp, hasProp, hdlAnimation, hdlClipboard, hdlComposition, hdlFocus, hdlForm, hdlImage, hdlKeyboard, hdlMedia, hdlMouse, hdlScroll, hdlSelection, hdlTouch, hdlTransition, hdlWheel, 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
@@ -1,10 +1,16 @@
1
- import { TSESTree } from '@typescript-eslint/types';
2
1
  import { RuleContext } from '@eslint-react/types';
3
- import { Scope } from '@typescript-eslint/scope-manager';
2
+ import { TSESTree } from '@typescript-eslint/types';
4
3
  import { TSESTreeFunction } from '@eslint-react/ast';
4
+ import { Scope } from '@typescript-eslint/scope-manager';
5
5
  import { O } from '@eslint-react/tools';
6
6
  import { TSESTree as TSESTree$1 } from '@typescript-eslint/utils';
7
7
 
8
+ type _ = (node: TSESTree.Identifier | TSESTree.MemberExpression, context: RuleContext) => boolean;
9
+ declare const isCreateElement: _;
10
+ declare const isCreateElementCall: (node: TSESTree.CallExpression, context: RuleContext) => boolean;
11
+ declare const isCloneElement: _;
12
+ declare const isCloneElementCall: (node: TSESTree.CallExpression, context: RuleContext) => boolean;
13
+
8
14
  /**
9
15
  * Returns the tag name associated with a JSXOpeningElement.
10
16
  * @param node The visited JSXOpeningElement node object.
@@ -49,34 +55,6 @@ declare function isJSXElementOfUserDefinedComponent(node: TSESTree.Node): node i
49
55
  */
50
56
  declare function isJSXElementOfBuiltinComponent(node: TSESTree.Node): node is TSESTree.JSXFragment;
51
57
 
52
- declare function getFragmentFromContext<T extends RuleContext>(context: T): string;
53
- declare const getPragmaFromContext: <T extends RuleContext>(context: T) => string;
54
-
55
- declare function isInitializedFromPragma(variableName: string, context: RuleContext, initialScope: Scope, pragma?: string): boolean;
56
- declare function isPropertyOfPragma(name: string, context: RuleContext, pragma?: string): (node: TSESTree.Node) => boolean;
57
- type CallFromPragmaPredicate = (node: TSESTree.Node, context: RuleContext) => node is TSESTree.CallExpression;
58
- /**
59
- * Checks if the given node is a call expression to the given function or method of the pragma
60
- * @param name The name of the function or method to check
61
- * @returns A predicate that checks if the given node is a call expression to the given function or method
62
- */
63
- declare function isCallFromPragma(name: string): (node: TSESTree.Node, context: RuleContext) => node is TSESTree.CallExpression;
64
-
65
- /**
66
- * Checks if the given node is a call expression to `createElement`
67
- * @param node The AST node to check
68
- * @param context The rule context
69
- * @returns `true` if the node is a call expression to `createElement`
70
- */
71
- declare const isCreateElementCall: CallFromPragmaPredicate;
72
- /**
73
- * Checks if the given node is a call expression to `cloneElement`
74
- * @param node The AST node to check
75
- * @param context The rule context
76
- * @returns `true` if the node is a call expression to `cloneElement`
77
- */
78
- declare const isCloneElementCall: CallFromPragmaPredicate;
79
-
80
58
  declare const hdlAnimation: readonly ["onAnimationStart", "onAnimationEnd", "onAnimationIteration"];
81
59
  declare const hdlClipboard: readonly ["onCopy", "onCut", "onPaste"];
82
60
  declare const hdlComposition: readonly ["onCompositionEnd", "onCompositionStart", "onCompositionUpdate"];
@@ -114,6 +92,27 @@ declare function isFragmentElement(node: TSESTree.JSXElement, pragma: string, fr
114
92
  */
115
93
  declare function isFunctionReturningJSXValue(node: TSESTreeFunction, context: RuleContext, hint?: bigint): boolean;
116
94
 
95
+ declare function getFragmentFromContext<T extends RuleContext>(context: T): string;
96
+ declare const getPragmaFromContext: <T extends RuleContext>(context: T) => string;
97
+
98
+ declare function isInitializedFromPragma(variableName: string, context: RuleContext, initialScope: Scope, pragma?: string): boolean;
99
+ declare function isPropertyOfPragma(name: string, context: RuleContext, pragma?: string): (node: TSESTree.Node) => boolean;
100
+ /**
101
+ * Checks if the given node is a call expression to the given function or method of the pragma
102
+ * @param name The name of the function or method to check
103
+ * @returns A predicate that checks if the given node is a call expression to the given function or method
104
+ */
105
+ declare function isFromPragma(name: string): (node: TSESTree.Identifier | TSESTree.MemberExpression, context: RuleContext) => boolean;
106
+ /**
107
+ * @internal
108
+ * @param pragmaMemberName
109
+ * @param name
110
+ * @returns A function that checks if a given node is a member expression of a Pragma member.
111
+ */
112
+ declare function isFromPragmaMember(pragmaMemberName: string, name: string): (node: TSESTree.MemberExpression, context: RuleContext, pragma?: string) => boolean;
113
+ declare function isCallFromPragma(name: string): (node: TSESTree.CallExpression, context: RuleContext) => boolean;
114
+ declare function isCallFromPragmaMember(pragmaMemberName: string, name: string): (node: TSESTree.CallExpression, context: RuleContext) => boolean;
115
+
117
116
  /**
118
117
  * Get the name of a JSX attribute with namespace
119
118
  * @param node The JSX attribute node
@@ -238,4 +237,4 @@ declare const DEFAULT_JSX_VALUE_CHECK_HINT: bigint;
238
237
  */
239
238
  declare function isJSXValue(node: TSESTree$1.Node | null | undefined, context: RuleContext, hint?: bigint): boolean;
240
239
 
241
- export { type CallFromPragmaPredicate, DEFAULT_JSX_VALUE_CHECK_HINT, JSXValueCheckHint, elementType, findPropInAttributes, findPropInProperties, getFragmentFromContext, getPragmaFromContext, getProp, getPropName, getPropValue, hasAnyProp, hasChildren, hasEveryProp, hasProp, hdlAnimation, hdlClipboard, hdlComposition, hdlFocus, hdlForm, hdlImage, hdlKeyboard, hdlMedia, hdlMouse, hdlScroll, hdlSelection, hdlTouch, hdlTransition, hdlWheel, isCallFromPragma, isChildOfJSXElement, isChildrenOfCreateElement, isCloneElementCall, isCreateElementCall, isFragment, isFragmentElement, isFragmentSyntax, isFunctionReturningJSXValue, isInitializedFromPragma, isInsideCreateElementProps, isInsidePropValue, isJSXElementOfBuiltinComponent, isJSXElementOfUserDefinedComponent, isJSXValue, isLineBreak, isLiteral, isPaddingSpaces, isPropertyOfPragma, isWhiteSpace, traverseUpProp };
240
+ export { DEFAULT_JSX_VALUE_CHECK_HINT, JSXValueCheckHint, elementType, findPropInAttributes, findPropInProperties, getFragmentFromContext, getPragmaFromContext, getProp, getPropName, getPropValue, hasAnyProp, hasChildren, hasEveryProp, hasProp, hdlAnimation, hdlClipboard, hdlComposition, hdlFocus, hdlForm, hdlImage, hdlKeyboard, hdlMedia, hdlMouse, hdlScroll, hdlSelection, hdlTouch, hdlTransition, hdlWheel, 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 };