@eslint-react/jsx 1.24.0-beta.1 → 1.24.0-beta.16

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.js CHANGED
@@ -4,6 +4,7 @@ var AST3 = require('@eslint-react/ast');
4
4
  var eff = require('@eslint-react/eff');
5
5
  var types = require('@typescript-eslint/types');
6
6
  var VAR = require('@eslint-react/var');
7
+ var tsPattern = require('ts-pattern');
7
8
 
8
9
  function _interopNamespace(e) {
9
10
  if (e && e.__esModule) return e;
@@ -26,152 +27,116 @@ function _interopNamespace(e) {
26
27
  var AST3__namespace = /*#__PURE__*/_interopNamespace(AST3);
27
28
  var VAR__namespace = /*#__PURE__*/_interopNamespace(VAR);
28
29
 
29
- // src/find-parent-prop.ts
30
- function findParentProp(node, test = eff.returnTrue) {
30
+ // src/find-parent-attribute.ts
31
+ function findParentAttribute(node, test = eff.returnTrue) {
31
32
  const guard = (node2) => {
32
33
  return node2.type === types.AST_NODE_TYPES.JSXAttribute && test(node2);
33
34
  };
34
- return AST3__namespace.findParentNodeGuard(node, guard);
35
+ return AST3__namespace.findParentNode(node, guard);
35
36
  }
36
- function resolveJSXMemberExpressions(object, property) {
37
- if (object.type === types.AST_NODE_TYPES.JSXMemberExpression) {
38
- return `${resolveJSXMemberExpressions(object.object, object.property)}.${property.name}`;
39
- }
40
- if (object.type === types.AST_NODE_TYPES.JSXNamespacedName) {
41
- return `${object.namespace.name}:${object.name.name}.${property.name}`;
42
- }
43
- return `${object.name}.${property.name}`;
44
- }
45
- function getElementName(node) {
46
- if (node.type === types.AST_NODE_TYPES.JSXOpeningFragment) {
47
- return "<>";
48
- }
49
- const { name } = node;
50
- if (name.type === types.AST_NODE_TYPES.JSXMemberExpression) {
51
- const { object, property } = name;
52
- return resolveJSXMemberExpressions(object, property);
53
- }
54
- if (name.type === types.AST_NODE_TYPES.JSXNamespacedName) {
55
- return `${name.namespace.name}:${name.name.name}`;
56
- }
57
- return name.name;
58
- }
59
- function getPropName(node) {
60
- switch (node.name.type) {
37
+ function toString(node) {
38
+ switch (node.type) {
61
39
  case types.AST_NODE_TYPES.JSXIdentifier:
62
- return node.name.name;
40
+ return node.name;
63
41
  case types.AST_NODE_TYPES.JSXNamespacedName:
64
- return `${node.name.namespace.name}:${node.name.name.name}`;
65
- }
66
- }
67
- function getProp(name, initialScope, props) {
68
- return findPropInAttributes(name, initialScope, props);
69
- }
70
- function getPropValue(attribute, initialScope) {
71
- switch (attribute.type) {
72
- case types.AST_NODE_TYPES.JSXAttribute:
73
- if (attribute.value?.type === types.AST_NODE_TYPES.Literal) {
74
- return {
75
- kind: "some",
76
- node: attribute.value,
77
- initialScope,
78
- value: attribute.value.value
79
- };
80
- }
81
- if (attribute.value?.type === types.AST_NODE_TYPES.JSXExpressionContainer) {
82
- return {
83
- kind: "lazy",
84
- node: attribute.value.expression,
85
- initialScope
86
- };
87
- }
88
- return { kind: "none", node: attribute, initialScope };
89
- case types.AST_NODE_TYPES.JSXSpreadAttribute:
90
- return {
91
- kind: "lazy",
92
- node: attribute.argument,
93
- initialScope
94
- };
95
- default:
96
- return { kind: "none", node: attribute, initialScope };
42
+ return `${node.namespace.name}:${node.name.name}`;
43
+ case types.AST_NODE_TYPES.JSXMemberExpression:
44
+ return `${toString(node.object)}.${toString(node.property)}`;
45
+ case types.AST_NODE_TYPES.JSXText:
46
+ return node.value;
47
+ case types.AST_NODE_TYPES.JSXOpeningElement:
48
+ return `<${toString(node.name)}>`;
49
+ case types.AST_NODE_TYPES.JSXClosingElement:
50
+ return `</${toString(node.name)}>`;
51
+ case types.AST_NODE_TYPES.JSXOpeningFragment:
52
+ return "<>";
53
+ case types.AST_NODE_TYPES.JSXClosingFragment:
54
+ return "</>";
97
55
  }
98
56
  }
99
- function findPropInProperties(name, properties, initialScope, seenProps = []) {
100
- return properties.findLast((prop) => {
101
- if (prop.type === types.AST_NODE_TYPES.Property) {
102
- return "name" in prop.key && prop.key.name === name;
103
- }
104
- if (prop.type === types.AST_NODE_TYPES.SpreadElement) {
105
- switch (prop.argument.type) {
106
- case types.AST_NODE_TYPES.Identifier: {
107
- if (seenProps.includes(prop.argument.name)) {
108
- return false;
109
- }
110
- const variable = VAR__namespace.findVariable(prop.argument.name, initialScope);
111
- const variableNode = VAR__namespace.getVariableNode(variable, 0);
112
- if (variableNode?.type === types.AST_NODE_TYPES.ObjectExpression) {
113
- return findPropInProperties(
114
- name,
115
- variableNode.properties,
116
- initialScope,
117
- [...seenProps, prop.argument.name]
118
- ) != null;
119
- }
120
- return false;
121
- }
122
- case types.AST_NODE_TYPES.ObjectExpression: {
123
- return findPropInProperties(
124
- name,
125
- prop.argument.properties,
126
- initialScope,
127
- seenProps
128
- ) != null;
129
- }
130
- }
131
- return false;
132
- }
133
- return false;
134
- });
57
+
58
+ // src/get-attribute-name.ts
59
+ function getAttributeName(node) {
60
+ return toString(node.name);
135
61
  }
136
- function findPropInAttributes(name, initialScope, attributes) {
62
+
63
+ // src/get-attribute.ts
64
+ function getAttribute(name, attributes, initialScope) {
137
65
  return attributes.findLast((attr) => {
138
66
  if (attr.type === types.AST_NODE_TYPES.JSXAttribute) {
139
- return getPropName(attr) === name;
67
+ return getAttributeName(attr) === name;
140
68
  }
69
+ if (initialScope == null) return false;
141
70
  switch (attr.argument.type) {
142
71
  case types.AST_NODE_TYPES.Identifier: {
143
72
  const variable = VAR__namespace.findVariable(attr.argument.name, initialScope);
144
73
  const variableNode = VAR__namespace.getVariableNode(variable, 0);
145
74
  if (variableNode?.type === types.AST_NODE_TYPES.ObjectExpression) {
146
- return findPropInProperties(name, variableNode.properties, initialScope) != null;
75
+ return VAR__namespace.findPropertyInProperties(name, variableNode.properties, initialScope) != null;
147
76
  }
148
77
  return false;
149
78
  }
150
79
  case types.AST_NODE_TYPES.ObjectExpression:
151
- return findPropInProperties(name, attr.argument.properties, initialScope) != null;
80
+ return VAR__namespace.findPropertyInProperties(name, attr.argument.properties, initialScope) != null;
152
81
  }
153
82
  return false;
154
83
  });
155
84
  }
156
-
157
- // src/has-prop.ts
158
- function hasProp(propName, initialScope, attributes) {
159
- return findPropInAttributes(propName, initialScope, attributes) != null;
160
- }
161
- function hasAnyProp(attributes, propNames, initialScope) {
162
- return propNames.some((propName) => hasProp(propName, initialScope, attributes));
85
+ function getAttributeValue(node, name, initialScope) {
86
+ switch (node.type) {
87
+ case types.AST_NODE_TYPES.JSXAttribute:
88
+ if (node.value?.type === types.AST_NODE_TYPES.Literal) {
89
+ return {
90
+ kind: "some",
91
+ node: node.value,
92
+ initialScope,
93
+ value: node.value.value
94
+ };
95
+ }
96
+ if (node.value?.type === types.AST_NODE_TYPES.JSXExpressionContainer) {
97
+ return VAR__namespace.toStaticValue({
98
+ kind: "lazy",
99
+ node: node.value.expression,
100
+ initialScope
101
+ });
102
+ }
103
+ return { kind: "none", node, initialScope };
104
+ case types.AST_NODE_TYPES.JSXSpreadAttribute: {
105
+ const staticValue = VAR__namespace.toStaticValue({
106
+ kind: "lazy",
107
+ node: node.argument,
108
+ initialScope
109
+ });
110
+ if (staticValue.kind === "none") {
111
+ return staticValue;
112
+ }
113
+ return tsPattern.match(staticValue.value).with({ [name]: tsPattern.P.select(tsPattern.P.any) }, (value) => ({
114
+ kind: "some",
115
+ node: node.argument,
116
+ initialScope,
117
+ value
118
+ })).otherwise(() => ({ kind: "none", node, initialScope }));
119
+ }
120
+ default:
121
+ return { kind: "none", node, initialScope };
122
+ }
163
123
  }
164
- function hasEveryProp(attributes, propNames, initialScope) {
165
- return propNames.every((propName) => hasProp(propName, initialScope, attributes));
124
+ function getElementName(node) {
125
+ if (node.type === types.AST_NODE_TYPES.JSXFragment) {
126
+ return "";
127
+ }
128
+ return toString(node.openingElement.name);
166
129
  }
167
- function isKeyedElement(node, initialScope) {
168
- return node.type === types.AST_NODE_TYPES.JSXElement && hasProp("key", initialScope, node.openingElement.attributes);
130
+
131
+ // src/has-attribute.ts
132
+ function hasAttribute(name, attributes, initialScope) {
133
+ return getAttribute(name, attributes, initialScope) != null;
169
134
  }
170
- function isBuiltInElement(node) {
171
- return node.type === types.AST_NODE_TYPES.JSXElement && node.openingElement.name.type === types.AST_NODE_TYPES.JSXIdentifier && node.openingElement.name.name.toLowerCase() === node.openingElement.name.name && /^[a-z]/u.test(node.openingElement.name.name);
135
+ function hasAnyAttribute(names, attributes, initialScope) {
136
+ return names.some((n) => hasAttribute(n, attributes, initialScope));
172
137
  }
173
- function isUserDefinedElement(node) {
174
- return node.type === types.AST_NODE_TYPES.JSXElement && node.openingElement.name.type === types.AST_NODE_TYPES.JSXIdentifier && /^[A-Z]/u.test(node.openingElement.name.name);
138
+ function hasEveryAttribute(names, attributes, initialScope) {
139
+ return names.every((n) => hasAttribute(n, attributes, initialScope));
175
140
  }
176
141
  var JSXValueHint = {
177
142
  None: 0n,
@@ -278,6 +243,19 @@ function isJSXValue(node, jsxCtx, hint = DEFAULT_JSX_VALUE_HINT) {
278
243
  }
279
244
  return false;
280
245
  }
246
+ function isFragmentElement(node) {
247
+ if (node.type !== types.AST_NODE_TYPES.JSXElement) return false;
248
+ return getElementName(node).split(".").at(-1) === "Fragment";
249
+ }
250
+ function isKeyedElement(node, initialScope) {
251
+ return node.type === types.AST_NODE_TYPES.JSXElement && hasAttribute("key", node.openingElement.attributes, initialScope);
252
+ }
253
+ function isBuiltInElement(node) {
254
+ return node.type === types.AST_NODE_TYPES.JSXElement && node.openingElement.name.type === types.AST_NODE_TYPES.JSXIdentifier && node.openingElement.name.name.toLowerCase() === node.openingElement.name.name && /^[a-z]/u.test(node.openingElement.name.name);
255
+ }
256
+ function isUserDefinedElement(node) {
257
+ return node.type === types.AST_NODE_TYPES.JSXElement && node.openingElement.name.type === types.AST_NODE_TYPES.JSXIdentifier && /^[A-Z]/u.test(node.openingElement.name.name);
258
+ }
281
259
  var isLiteral = AST3__namespace.isOneOf([types.AST_NODE_TYPES.Literal, types.AST_NODE_TYPES.JSXText]);
282
260
  function isWhiteSpace(node) {
283
261
  return typeof node.value === "string" && node.value.trim() === "";
@@ -289,288 +267,18 @@ function isPaddingSpaces(node) {
289
267
  return isLiteral(node) && isWhiteSpace(node) && node.raw.includes("\n");
290
268
  }
291
269
 
292
- // src/xhtml-entities.ts
293
- var xhtmlEntities = {
294
- Aacute: "\xC1",
295
- aacute: "\xE1",
296
- Acirc: "\xC2",
297
- acirc: "\xE2",
298
- acute: "\xB4",
299
- AElig: "\xC6",
300
- aelig: "\xE6",
301
- Agrave: "\xC0",
302
- agrave: "\xE0",
303
- alefsym: "\u2135",
304
- Alpha: "\u0391",
305
- alpha: "\u03B1",
306
- amp: "&",
307
- and: "\u2227",
308
- ang: "\u2220",
309
- apos: "'",
310
- Aring: "\xC5",
311
- aring: "\xE5",
312
- asymp: "\u2248",
313
- Atilde: "\xC3",
314
- atilde: "\xE3",
315
- Auml: "\xC4",
316
- auml: "\xE4",
317
- bdquo: "\u201E",
318
- Beta: "\u0392",
319
- beta: "\u03B2",
320
- brvbar: "\xA6",
321
- bull: "\u2022",
322
- cap: "\u2229",
323
- Ccedil: "\xC7",
324
- ccedil: "\xE7",
325
- cedil: "\xB8",
326
- cent: "\xA2",
327
- Chi: "\u03A7",
328
- chi: "\u03C7",
329
- circ: "\u02C6",
330
- clubs: "\u2663",
331
- cong: "\u2245",
332
- copy: "\xA9",
333
- crarr: "\u21B5",
334
- cup: "\u222A",
335
- curren: "\xA4",
336
- dagger: "\u2020",
337
- Dagger: "\u2021",
338
- darr: "\u2193",
339
- dArr: "\u21D3",
340
- deg: "\xB0",
341
- Delta: "\u0394",
342
- delta: "\u03B4",
343
- diams: "\u2666",
344
- divide: "\xF7",
345
- Eacute: "\xC9",
346
- eacute: "\xE9",
347
- Ecirc: "\xCA",
348
- ecirc: "\xEA",
349
- Egrave: "\xC8",
350
- egrave: "\xE8",
351
- empty: "\u2205",
352
- emsp: "\u2003",
353
- ensp: "\u2002",
354
- Epsilon: "\u0395",
355
- epsilon: "\u03B5",
356
- equiv: "\u2261",
357
- Eta: "\u0397",
358
- eta: "\u03B7",
359
- ETH: "\xD0",
360
- eth: "\xF0",
361
- Euml: "\xCB",
362
- euml: "\xEB",
363
- euro: "\u20AC",
364
- exist: "\u2203",
365
- fnof: "\u0192",
366
- forall: "\u2200",
367
- frac12: "\xBD",
368
- frac14: "\xBC",
369
- frac34: "\xBE",
370
- frasl: "\u2044",
371
- Gamma: "\u0393",
372
- gamma: "\u03B3",
373
- ge: "\u2265",
374
- gt: ">",
375
- harr: "\u2194",
376
- hArr: "\u21D4",
377
- hearts: "\u2665",
378
- hellip: "\u2026",
379
- Iacute: "\xCD",
380
- iacute: "\xED",
381
- Icirc: "\xCE",
382
- icirc: "\xEE",
383
- iexcl: "\xA1",
384
- Igrave: "\xCC",
385
- igrave: "\xEC",
386
- image: "\u2111",
387
- infin: "\u221E",
388
- int: "\u222B",
389
- Iota: "\u0399",
390
- iota: "\u03B9",
391
- iquest: "\xBF",
392
- isin: "\u2208",
393
- Iuml: "\xCF",
394
- iuml: "\xEF",
395
- Kappa: "\u039A",
396
- kappa: "\u03BA",
397
- Lambda: "\u039B",
398
- lambda: "\u03BB",
399
- lang: "\u2329",
400
- laquo: "\xAB",
401
- larr: "\u2190",
402
- lArr: "\u21D0",
403
- lceil: "\u2308",
404
- ldquo: "\u201C",
405
- le: "\u2264",
406
- lfloor: "\u230A",
407
- lowast: "\u2217",
408
- loz: "\u25CA",
409
- lrm: "\u200E",
410
- lsaquo: "\u2039",
411
- lsquo: "\u2018",
412
- lt: "<",
413
- macr: "\xAF",
414
- mdash: "\u2014",
415
- micro: "\xB5",
416
- middot: "\xB7",
417
- minus: "\u2212",
418
- Mu: "\u039C",
419
- mu: "\u03BC",
420
- nabla: "\u2207",
421
- nbsp: "\xA0",
422
- ndash: "\u2013",
423
- ne: "\u2260",
424
- ni: "\u220B",
425
- not: "\xAC",
426
- notin: "\u2209",
427
- nsub: "\u2284",
428
- Ntilde: "\xD1",
429
- ntilde: "\xF1",
430
- Nu: "\u039D",
431
- nu: "\u03BD",
432
- Oacute: "\xD3",
433
- oacute: "\xF3",
434
- Ocirc: "\xD4",
435
- ocirc: "\xF4",
436
- OElig: "\u0152",
437
- oelig: "\u0153",
438
- Ograve: "\xD2",
439
- ograve: "\xF2",
440
- oline: "\u203E",
441
- Omega: "\u03A9",
442
- omega: "\u03C9",
443
- Omicron: "\u039F",
444
- omicron: "\u03BF",
445
- oplus: "\u2295",
446
- or: "\u2228",
447
- ordf: "\xAA",
448
- ordm: "\xBA",
449
- Oslash: "\xD8",
450
- oslash: "\xF8",
451
- Otilde: "\xD5",
452
- otilde: "\xF5",
453
- otimes: "\u2297",
454
- Ouml: "\xD6",
455
- ouml: "\xF6",
456
- para: "\xB6",
457
- part: "\u2202",
458
- permil: "\u2030",
459
- perp: "\u22A5",
460
- Phi: "\u03A6",
461
- phi: "\u03C6",
462
- Pi: "\u03A0",
463
- pi: "\u03C0",
464
- piv: "\u03D6",
465
- plusmn: "\xB1",
466
- pound: "\xA3",
467
- prime: "\u2032",
468
- Prime: "\u2033",
469
- prod: "\u220F",
470
- prop: "\u221D",
471
- Psi: "\u03A8",
472
- psi: "\u03C8",
473
- quot: '"',
474
- radic: "\u221A",
475
- rang: "\u232A",
476
- raquo: "\xBB",
477
- rarr: "\u2192",
478
- rArr: "\u21D2",
479
- rceil: "\u2309",
480
- rdquo: "\u201D",
481
- real: "\u211C",
482
- reg: "\xAE",
483
- rfloor: "\u230B",
484
- Rho: "\u03A1",
485
- rho: "\u03C1",
486
- rlm: "\u200F",
487
- rsaquo: "\u203A",
488
- rsquo: "\u2019",
489
- sbquo: "\u201A",
490
- Scaron: "\u0160",
491
- scaron: "\u0161",
492
- sdot: "\u22C5",
493
- sect: "\xA7",
494
- shy: "\xAD",
495
- Sigma: "\u03A3",
496
- sigma: "\u03C3",
497
- sigmaf: "\u03C2",
498
- sim: "\u223C",
499
- spades: "\u2660",
500
- sub: "\u2282",
501
- sube: "\u2286",
502
- sum: "\u2211",
503
- sup: "\u2283",
504
- sup1: "\xB9",
505
- sup2: "\xB2",
506
- sup3: "\xB3",
507
- supe: "\u2287",
508
- szlig: "\xDF",
509
- Tau: "\u03A4",
510
- tau: "\u03C4",
511
- there4: "\u2234",
512
- Theta: "\u0398",
513
- theta: "\u03B8",
514
- thetasym: "\u03D1",
515
- thinsp: "\u2009",
516
- THORN: "\xDE",
517
- thorn: "\xFE",
518
- tilde: "\u02DC",
519
- times: "\xD7",
520
- trade: "\u2122",
521
- Uacute: "\xDA",
522
- uacute: "\xFA",
523
- uarr: "\u2191",
524
- uArr: "\u21D1",
525
- Ucirc: "\xDB",
526
- ucirc: "\xFB",
527
- Ugrave: "\xD9",
528
- ugrave: "\xF9",
529
- uml: "\xA8",
530
- upsih: "\u03D2",
531
- Upsilon: "\u03A5",
532
- upsilon: "\u03C5",
533
- Uuml: "\xDC",
534
- uuml: "\xFC",
535
- weierp: "\u2118",
536
- Xi: "\u039E",
537
- xi: "\u03BE",
538
- Yacute: "\xDD",
539
- yacute: "\xFD",
540
- yen: "\xA5",
541
- yuml: "\xFF",
542
- Yuml: "\u0178",
543
- Zeta: "\u0396",
544
- zeta: "\u03B6",
545
- zwj: "\u200D",
546
- zwnj: "\u200C"
547
- };
548
-
549
- // src/unescape-string-literal-text.ts
550
- function unescapeStringLiteralText(text) {
551
- return text.replaceAll(/&(?:#\d+|#x[\da-fA-F]+|[0-9a-zA-Z]+);/g, (entity) => {
552
- const item = entity.slice(1, -1);
553
- if (item[0] === "#") {
554
- const codePoint = item[1] === "x" ? parseInt(item.slice(2), 16) : parseInt(item.slice(1), 10);
555
- return codePoint > 1114111 ? entity : String.fromCodePoint(codePoint);
556
- }
557
- return xhtmlEntities[item] ?? entity;
558
- });
559
- }
560
-
561
270
  exports.DEFAULT_JSX_VALUE_HINT = DEFAULT_JSX_VALUE_HINT;
562
271
  exports.JSXValueHint = JSXValueHint;
563
- exports.findParentProp = findParentProp;
564
- exports.findPropInAttributes = findPropInAttributes;
565
- exports.findPropInProperties = findPropInProperties;
272
+ exports.findParentAttribute = findParentAttribute;
273
+ exports.getAttribute = getAttribute;
274
+ exports.getAttributeName = getAttributeName;
275
+ exports.getAttributeValue = getAttributeValue;
566
276
  exports.getElementName = getElementName;
567
- exports.getProp = getProp;
568
- exports.getPropName = getPropName;
569
- exports.getPropValue = getPropValue;
570
- exports.hasAnyProp = hasAnyProp;
571
- exports.hasEveryProp = hasEveryProp;
572
- exports.hasProp = hasProp;
277
+ exports.hasAnyAttribute = hasAnyAttribute;
278
+ exports.hasAttribute = hasAttribute;
279
+ exports.hasEveryAttribute = hasEveryAttribute;
573
280
  exports.isBuiltInElement = isBuiltInElement;
281
+ exports.isFragmentElement = isFragmentElement;
574
282
  exports.isJSXValue = isJSXValue;
575
283
  exports.isKeyedElement = isKeyedElement;
576
284
  exports.isLineBreak = isLineBreak;
@@ -578,5 +286,4 @@ exports.isLiteral = isLiteral;
578
286
  exports.isPaddingSpaces = isPaddingSpaces;
579
287
  exports.isUserDefinedElement = isUserDefinedElement;
580
288
  exports.isWhiteSpace = isWhiteSpace;
581
- exports.unescapeStringLiteralText = unescapeStringLiteralText;
582
- exports.xhtmlEntities = xhtmlEntities;
289
+ exports.toString = toString;