@eslint-react/jsx 1.6.1-next.2 → 1.7.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { TSESTree } from '@typescript-eslint/types';
2
- import { O } from '@eslint-react/tools';
3
2
  import { RuleContext } from '@eslint-react/types';
3
+ import { O } from '@eslint-react/tools';
4
4
  import { Scope } from '@typescript-eslint/scope-manager';
5
5
  import { TSESTree as TSESTree$1 } from '@typescript-eslint/utils';
6
6
 
@@ -9,7 +9,9 @@ import { TSESTree as TSESTree$1 } from '@typescript-eslint/utils';
9
9
  * @param node The visited JSXOpeningElement node object.
10
10
  * @returns The element's tag name.
11
11
  */
12
- declare function elementName(node: TSESTree.JSXOpeningElement | TSESTree.JSXOpeningFragment): string;
12
+ declare function getElementName(node: TSESTree.JSXOpeningElement | TSESTree.JSXOpeningFragment): string;
13
+
14
+ declare function getElementType(context: RuleContext, polymorphicPropName?: string): (node: TSESTree.JSXOpeningElement) => string;
13
15
 
14
16
  declare function getJSXPragma(context: RuleContext): O.Option<string>;
15
17
 
@@ -145,4 +147,4 @@ declare function isPaddingSpaces(node: TSESTree.Node): boolean;
145
147
  */
146
148
  declare function traverseUpProp(node: TSESTree.Node, predicate?: (node: TSESTree.JSXAttribute) => boolean): O.Option<TSESTree.JSXAttribute>;
147
149
 
148
- export { DEFAULT_JSX_VALUE_HINT, JSXValueHint, elementName, findPropInAttributes, findPropInProperties, getJSXPragma, getProp, getPropName, getPropValue, hasAnyProp, hasEveryProp, hasProp, isBuiltInElement, isJSXValue, isKeyedElement, isLineBreak, isLiteral, isPaddingSpaces, isUserDefinedElement, isWhiteSpace, traverseUpProp };
150
+ export { DEFAULT_JSX_VALUE_HINT, JSXValueHint, findPropInAttributes, findPropInProperties, getElementName, getElementType, getJSXPragma, getProp, getPropName, getPropValue, hasAnyProp, hasEveryProp, hasProp, isBuiltInElement, isJSXValue, isKeyedElement, isLineBreak, isLiteral, isPaddingSpaces, isUserDefinedElement, isWhiteSpace, traverseUpProp };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { TSESTree } from '@typescript-eslint/types';
2
- import { O } from '@eslint-react/tools';
3
2
  import { RuleContext } from '@eslint-react/types';
3
+ import { O } from '@eslint-react/tools';
4
4
  import { Scope } from '@typescript-eslint/scope-manager';
5
5
  import { TSESTree as TSESTree$1 } from '@typescript-eslint/utils';
6
6
 
@@ -9,7 +9,9 @@ import { TSESTree as TSESTree$1 } from '@typescript-eslint/utils';
9
9
  * @param node The visited JSXOpeningElement node object.
10
10
  * @returns The element's tag name.
11
11
  */
12
- declare function elementName(node: TSESTree.JSXOpeningElement | TSESTree.JSXOpeningFragment): string;
12
+ declare function getElementName(node: TSESTree.JSXOpeningElement | TSESTree.JSXOpeningFragment): string;
13
+
14
+ declare function getElementType(context: RuleContext, polymorphicPropName?: string): (node: TSESTree.JSXOpeningElement) => string;
13
15
 
14
16
  declare function getJSXPragma(context: RuleContext): O.Option<string>;
15
17
 
@@ -145,4 +147,4 @@ declare function isPaddingSpaces(node: TSESTree.Node): boolean;
145
147
  */
146
148
  declare function traverseUpProp(node: TSESTree.Node, predicate?: (node: TSESTree.JSXAttribute) => boolean): O.Option<TSESTree.JSXAttribute>;
147
149
 
148
- export { DEFAULT_JSX_VALUE_HINT, JSXValueHint, elementName, findPropInAttributes, findPropInProperties, getJSXPragma, getProp, getPropName, getPropValue, hasAnyProp, hasEveryProp, hasProp, isBuiltInElement, isJSXValue, isKeyedElement, isLineBreak, isLiteral, isPaddingSpaces, isUserDefinedElement, isWhiteSpace, traverseUpProp };
150
+ export { DEFAULT_JSX_VALUE_HINT, JSXValueHint, findPropInAttributes, findPropInProperties, getElementName, getElementType, getJSXPragma, getProp, getPropName, getPropValue, hasAnyProp, hasEveryProp, hasProp, isBuiltInElement, isJSXValue, isKeyedElement, isLineBreak, isLiteral, isPaddingSpaces, isUserDefinedElement, isWhiteSpace, traverseUpProp };
package/dist/index.js CHANGED
@@ -1,47 +1,56 @@
1
1
  'use strict';
2
2
 
3
- var types = require('@typescript-eslint/types');
4
- var tools = require('@eslint-react/tools');
5
3
  var ast = require('@eslint-react/ast');
4
+ var tools = require('@eslint-react/tools');
5
+ var R = require('remeda');
6
6
  var _var = require('@eslint-react/var');
7
7
  var astUtils = require('@typescript-eslint/utils/ast-utils');
8
8
  var tsPattern = require('ts-pattern');
9
9
 
10
- // src/element-name.ts
11
- function resolveMemberExpressions(object, property) {
12
- if (object.type === types.AST_NODE_TYPES.JSXMemberExpression) {
13
- return `${resolveMemberExpressions(object.object, object.property)}.${property.name}`;
10
+ function _interopNamespace(e) {
11
+ if (e && e.__esModule) return e;
12
+ var n = Object.create(null);
13
+ if (e) {
14
+ Object.keys(e).forEach(function (k) {
15
+ if (k !== 'default') {
16
+ var d = Object.getOwnPropertyDescriptor(e, k);
17
+ Object.defineProperty(n, k, d.get ? d : {
18
+ enumerable: true,
19
+ get: function () { return e[k]; }
20
+ });
21
+ }
22
+ });
14
23
  }
15
- if (object.type === types.AST_NODE_TYPES.JSXNamespacedName) {
24
+ n.default = e;
25
+ return Object.freeze(n);
26
+ }
27
+
28
+ var R__namespace = /*#__PURE__*/_interopNamespace(R);
29
+
30
+ // src/get-element-name.ts
31
+ function resolveJSXMemberExpressions(object, property) {
32
+ if (object.type === ast.NodeType.JSXMemberExpression) {
33
+ return `${resolveJSXMemberExpressions(object.object, object.property)}.${property.name}`;
34
+ }
35
+ if (object.type === ast.NodeType.JSXNamespacedName) {
16
36
  return `${object.namespace.name}:${object.name.name}.${property.name}`;
17
37
  }
18
38
  return `${object.name}.${property.name}`;
19
39
  }
20
- function elementName(node) {
21
- if (node.type === types.AST_NODE_TYPES.JSXOpeningFragment) {
40
+ function getElementName(node) {
41
+ if (node.type === ast.NodeType.JSXOpeningFragment) {
22
42
  return "<>";
23
43
  }
24
44
  const { name } = node;
25
- if (name.type === types.AST_NODE_TYPES.JSXMemberExpression) {
45
+ if (name.type === ast.NodeType.JSXMemberExpression) {
26
46
  const { object, property } = name;
27
- return resolveMemberExpressions(object, property);
47
+ return resolveJSXMemberExpressions(object, property);
28
48
  }
29
- if (name.type === types.AST_NODE_TYPES.JSXNamespacedName) {
49
+ if (name.type === ast.NodeType.JSXNamespacedName) {
30
50
  return `${name.namespace.name}:${name.name.name}`;
31
51
  }
32
52
  return name.name;
33
53
  }
34
- var RE_JSX_ANNOTATION_REGEX = /@jsx\s+(\S+)/u;
35
- var RE_JS_IDENTIFIER_REGEX = /^[$A-Z_a-z][\w$]*$/u;
36
- function getJSXPragma(context) {
37
- const { sourceCode } = context;
38
- return tools.F.pipe(
39
- tools.O.fromNullable(sourceCode.getAllComments().find((node) => RE_JSX_ANNOTATION_REGEX.test(node.value))),
40
- tools.O.map(({ value }) => RE_JSX_ANNOTATION_REGEX.exec(value)),
41
- tools.O.flatMapNullable((matches) => matches?.[1]),
42
- tools.O.filter((pragma) => RE_JS_IDENTIFIER_REGEX.test(pragma))
43
- );
44
- }
45
54
  function getPropName(node) {
46
55
  return tsPattern.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();
47
56
  }
@@ -119,6 +128,32 @@ function findPropInAttributes(attributes, context, initialScope) {
119
128
  );
120
129
  };
121
130
  }
131
+
132
+ // src/get-element-type.ts
133
+ function getElementType(context, polymorphicPropName) {
134
+ return (node) => {
135
+ const initialScope = context.sourceCode.getScope(node);
136
+ return tools.F.pipe(
137
+ tools.O.fromNullable(polymorphicPropName),
138
+ tools.O.flatMap(findPropInAttributes(node.attributes, context, initialScope)),
139
+ tools.O.flatMap((attr) => getPropValue(attr, context)),
140
+ tools.O.flatMapNullable((v) => v?.value),
141
+ tools.O.filter(R__namespace.isString),
142
+ tools.O.getOrElse(() => getElementName(node))
143
+ );
144
+ };
145
+ }
146
+ var RE_JSX_ANNOTATION_REGEX = /@jsx\s+(\S+)/u;
147
+ var RE_JS_IDENTIFIER_REGEX = /^[$A-Z_a-z][\w$]*$/u;
148
+ function getJSXPragma(context) {
149
+ const { sourceCode } = context;
150
+ return tools.F.pipe(
151
+ tools.O.fromNullable(sourceCode.getAllComments().find((node) => RE_JSX_ANNOTATION_REGEX.test(node.value))),
152
+ tools.O.map(({ value }) => RE_JSX_ANNOTATION_REGEX.exec(value)),
153
+ tools.O.flatMapNullable((matches) => matches?.[1]),
154
+ tools.O.filter((pragma) => RE_JS_IDENTIFIER_REGEX.test(pragma))
155
+ );
156
+ }
122
157
  function hasProp(attributes, propName, context, initialScope) {
123
158
  return tools.O.isSome(findPropInAttributes(attributes, context, initialScope)(propName));
124
159
  }
@@ -129,16 +164,13 @@ function hasEveryProp(attributes, propNames, context, initialScope) {
129
164
  return propNames.every((propName) => hasProp(attributes, propName, context, initialScope));
130
165
  }
131
166
  function isKeyedElement(node, context) {
132
- if (node.type !== ast.NodeType.JSXElement) return false;
133
- return hasProp(node.openingElement.attributes, "key", context, context.sourceCode.getScope(node));
167
+ return node.type === ast.NodeType.JSXElement && hasProp(node.openingElement.attributes, "key", context, context.sourceCode.getScope(node));
134
168
  }
135
169
  function isUserDefinedElement(node) {
136
- if (node.type !== ast.NodeType.JSXElement) return false;
137
- return node.openingElement.name.type === ast.NodeType.JSXIdentifier && /^[A-Z]/u.test(node.openingElement.name.name);
170
+ return node.type === ast.NodeType.JSXElement && node.openingElement.name.type === ast.NodeType.JSXIdentifier && /^[A-Z]/u.test(node.openingElement.name.name);
138
171
  }
139
172
  function isBuiltInElement(node) {
140
- if (node.type !== ast.NodeType.JSXElement) return false;
141
- return node.openingElement.name.type === ast.NodeType.JSXIdentifier && node.openingElement.name.name.toLowerCase() === node.openingElement.name.name && /^[a-z]/u.test(node.openingElement.name.name);
173
+ return node.type === ast.NodeType.JSXElement && node.openingElement.name.type === ast.NodeType.JSXIdentifier && node.openingElement.name.name.toLowerCase() === node.openingElement.name.name && /^[a-z]/u.test(node.openingElement.name.name);
142
174
  }
143
175
  var JSXValueHint = {
144
176
  None: 0n,
@@ -205,7 +237,7 @@ function isJSXValue(node, context, hint = DEFAULT_JSX_VALUE_HINT) {
205
237
  }
206
238
  var isLiteral = ast.isOneOf([ast.NodeType.Literal, ast.NodeType.JSXText]);
207
239
  function isWhiteSpace(node) {
208
- return tools.Pred.isString(node.value) && node.value.trim() === "";
240
+ return R__namespace.isString(node.value) && node.value.trim() === "";
209
241
  }
210
242
  function isLineBreak(node) {
211
243
  return isLiteral(node) && isWhiteSpace(node) && ast.isMultiLine(node);
@@ -222,9 +254,10 @@ function traverseUpProp(node, predicate = tools.F.constTrue) {
222
254
 
223
255
  exports.DEFAULT_JSX_VALUE_HINT = DEFAULT_JSX_VALUE_HINT;
224
256
  exports.JSXValueHint = JSXValueHint;
225
- exports.elementName = elementName;
226
257
  exports.findPropInAttributes = findPropInAttributes;
227
258
  exports.findPropInProperties = findPropInProperties;
259
+ exports.getElementName = getElementName;
260
+ exports.getElementType = getElementType;
228
261
  exports.getJSXPragma = getJSXPragma;
229
262
  exports.getProp = getProp;
230
263
  exports.getPropName = getPropName;
package/dist/index.mjs CHANGED
@@ -1,45 +1,34 @@
1
- import { AST_NODE_TYPES } from '@typescript-eslint/types';
2
- import { F, O, Pred } from '@eslint-react/tools';
3
1
  import { isOneOf, NodeType, is, isJSXTagNameExpression, isMultiLine, traverseUpGuard } from '@eslint-react/ast';
2
+ import { O, F } from '@eslint-react/tools';
3
+ import * as R from 'remeda';
4
4
  import { findVariable, getVariableNode } from '@eslint-react/var';
5
5
  import { getStaticValue } from '@typescript-eslint/utils/ast-utils';
6
6
  import { match, P } from 'ts-pattern';
7
7
 
8
- // src/element-name.ts
9
- function resolveMemberExpressions(object, property) {
10
- if (object.type === AST_NODE_TYPES.JSXMemberExpression) {
11
- return `${resolveMemberExpressions(object.object, object.property)}.${property.name}`;
8
+ // src/get-element-name.ts
9
+ function resolveJSXMemberExpressions(object, property) {
10
+ if (object.type === NodeType.JSXMemberExpression) {
11
+ return `${resolveJSXMemberExpressions(object.object, object.property)}.${property.name}`;
12
12
  }
13
- if (object.type === AST_NODE_TYPES.JSXNamespacedName) {
13
+ if (object.type === NodeType.JSXNamespacedName) {
14
14
  return `${object.namespace.name}:${object.name.name}.${property.name}`;
15
15
  }
16
16
  return `${object.name}.${property.name}`;
17
17
  }
18
- function elementName(node) {
19
- if (node.type === AST_NODE_TYPES.JSXOpeningFragment) {
18
+ function getElementName(node) {
19
+ if (node.type === NodeType.JSXOpeningFragment) {
20
20
  return "<>";
21
21
  }
22
22
  const { name } = node;
23
- if (name.type === AST_NODE_TYPES.JSXMemberExpression) {
23
+ if (name.type === NodeType.JSXMemberExpression) {
24
24
  const { object, property } = name;
25
- return resolveMemberExpressions(object, property);
25
+ return resolveJSXMemberExpressions(object, property);
26
26
  }
27
- if (name.type === AST_NODE_TYPES.JSXNamespacedName) {
27
+ if (name.type === NodeType.JSXNamespacedName) {
28
28
  return `${name.namespace.name}:${name.name.name}`;
29
29
  }
30
30
  return name.name;
31
31
  }
32
- var RE_JSX_ANNOTATION_REGEX = /@jsx\s+(\S+)/u;
33
- var RE_JS_IDENTIFIER_REGEX = /^[$A-Z_a-z][\w$]*$/u;
34
- function getJSXPragma(context) {
35
- const { sourceCode } = context;
36
- return F.pipe(
37
- O.fromNullable(sourceCode.getAllComments().find((node) => RE_JSX_ANNOTATION_REGEX.test(node.value))),
38
- O.map(({ value }) => RE_JSX_ANNOTATION_REGEX.exec(value)),
39
- O.flatMapNullable((matches) => matches?.[1]),
40
- O.filter((pragma) => RE_JS_IDENTIFIER_REGEX.test(pragma))
41
- );
42
- }
43
32
  function getPropName(node) {
44
33
  return match(node.name).when(is(NodeType.JSXIdentifier), (n) => n.name).when(is(NodeType.JSXNamespacedName), (n) => `${n.namespace.name}:${n.name.name}`).exhaustive();
45
34
  }
@@ -117,6 +106,32 @@ function findPropInAttributes(attributes, context, initialScope) {
117
106
  );
118
107
  };
119
108
  }
109
+
110
+ // src/get-element-type.ts
111
+ function getElementType(context, polymorphicPropName) {
112
+ return (node) => {
113
+ const initialScope = context.sourceCode.getScope(node);
114
+ return F.pipe(
115
+ O.fromNullable(polymorphicPropName),
116
+ O.flatMap(findPropInAttributes(node.attributes, context, initialScope)),
117
+ O.flatMap((attr) => getPropValue(attr, context)),
118
+ O.flatMapNullable((v) => v?.value),
119
+ O.filter(R.isString),
120
+ O.getOrElse(() => getElementName(node))
121
+ );
122
+ };
123
+ }
124
+ var RE_JSX_ANNOTATION_REGEX = /@jsx\s+(\S+)/u;
125
+ var RE_JS_IDENTIFIER_REGEX = /^[$A-Z_a-z][\w$]*$/u;
126
+ function getJSXPragma(context) {
127
+ const { sourceCode } = context;
128
+ return F.pipe(
129
+ O.fromNullable(sourceCode.getAllComments().find((node) => RE_JSX_ANNOTATION_REGEX.test(node.value))),
130
+ O.map(({ value }) => RE_JSX_ANNOTATION_REGEX.exec(value)),
131
+ O.flatMapNullable((matches) => matches?.[1]),
132
+ O.filter((pragma) => RE_JS_IDENTIFIER_REGEX.test(pragma))
133
+ );
134
+ }
120
135
  function hasProp(attributes, propName, context, initialScope) {
121
136
  return O.isSome(findPropInAttributes(attributes, context, initialScope)(propName));
122
137
  }
@@ -127,16 +142,13 @@ function hasEveryProp(attributes, propNames, context, initialScope) {
127
142
  return propNames.every((propName) => hasProp(attributes, propName, context, initialScope));
128
143
  }
129
144
  function isKeyedElement(node, context) {
130
- if (node.type !== NodeType.JSXElement) return false;
131
- return hasProp(node.openingElement.attributes, "key", context, context.sourceCode.getScope(node));
145
+ return node.type === NodeType.JSXElement && hasProp(node.openingElement.attributes, "key", context, context.sourceCode.getScope(node));
132
146
  }
133
147
  function isUserDefinedElement(node) {
134
- if (node.type !== NodeType.JSXElement) return false;
135
- return node.openingElement.name.type === NodeType.JSXIdentifier && /^[A-Z]/u.test(node.openingElement.name.name);
148
+ return node.type === NodeType.JSXElement && node.openingElement.name.type === NodeType.JSXIdentifier && /^[A-Z]/u.test(node.openingElement.name.name);
136
149
  }
137
150
  function isBuiltInElement(node) {
138
- if (node.type !== NodeType.JSXElement) return false;
139
- return node.openingElement.name.type === NodeType.JSXIdentifier && node.openingElement.name.name.toLowerCase() === node.openingElement.name.name && /^[a-z]/u.test(node.openingElement.name.name);
151
+ return node.type === NodeType.JSXElement && node.openingElement.name.type === NodeType.JSXIdentifier && node.openingElement.name.name.toLowerCase() === node.openingElement.name.name && /^[a-z]/u.test(node.openingElement.name.name);
140
152
  }
141
153
  var JSXValueHint = {
142
154
  None: 0n,
@@ -203,7 +215,7 @@ function isJSXValue(node, context, hint = DEFAULT_JSX_VALUE_HINT) {
203
215
  }
204
216
  var isLiteral = isOneOf([NodeType.Literal, NodeType.JSXText]);
205
217
  function isWhiteSpace(node) {
206
- return Pred.isString(node.value) && node.value.trim() === "";
218
+ return R.isString(node.value) && node.value.trim() === "";
207
219
  }
208
220
  function isLineBreak(node) {
209
221
  return isLiteral(node) && isWhiteSpace(node) && isMultiLine(node);
@@ -218,4 +230,4 @@ function traverseUpProp(node, predicate = F.constTrue) {
218
230
  return traverseUpGuard(node, guard);
219
231
  }
220
232
 
221
- export { DEFAULT_JSX_VALUE_HINT, JSXValueHint, elementName, findPropInAttributes, findPropInProperties, getJSXPragma, getProp, getPropName, getPropValue, hasAnyProp, hasEveryProp, hasProp, isBuiltInElement, isJSXValue, isKeyedElement, isLineBreak, isLiteral, isPaddingSpaces, isUserDefinedElement, isWhiteSpace, traverseUpProp };
233
+ export { DEFAULT_JSX_VALUE_HINT, JSXValueHint, findPropInAttributes, findPropInProperties, getElementName, getElementType, getJSXPragma, getProp, getPropName, getPropValue, hasAnyProp, hasEveryProp, hasProp, isBuiltInElement, isJSXValue, isKeyedElement, isLineBreak, isLiteral, isPaddingSpaces, isUserDefinedElement, isWhiteSpace, traverseUpProp };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint-react/jsx",
3
- "version": "1.6.1-next.2",
3
+ "version": "1.7.0-beta.2",
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": {
@@ -35,14 +35,15 @@
35
35
  "./package.json"
36
36
  ],
37
37
  "dependencies": {
38
- "@typescript-eslint/scope-manager": "^7.17.0",
39
- "@typescript-eslint/types": "^7.17.0",
40
- "@typescript-eslint/utils": "^7.17.0",
38
+ "@typescript-eslint/scope-manager": "^7.18.0",
39
+ "@typescript-eslint/types": "^7.18.0",
40
+ "@typescript-eslint/utils": "^7.18.0",
41
+ "remeda": "^2.6.0",
41
42
  "ts-pattern": "^5.2.0",
42
- "@eslint-react/var": "1.6.1-next.2",
43
- "@eslint-react/ast": "1.6.1-next.2",
44
- "@eslint-react/tools": "1.6.1-next.2",
45
- "@eslint-react/types": "1.6.1-next.2"
43
+ "@eslint-react/ast": "1.7.0-beta.2",
44
+ "@eslint-react/tools": "1.7.0-beta.2",
45
+ "@eslint-react/types": "1.7.0-beta.2",
46
+ "@eslint-react/var": "1.7.0-beta.2"
46
47
  },
47
48
  "devDependencies": {
48
49
  "tsup": "8.2.3"