eslint-plugin-unicorn 65.0.0 → 65.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-unicorn",
3
- "version": "65.0.0",
3
+ "version": "65.0.1",
4
4
  "description": "More than 100 powerful ESLint rules",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/eslint-plugin-unicorn",
@@ -177,7 +177,10 @@ function resolveReceiver(node, context, visitedNodes = new Set()) {
177
177
  return value === undefined ? node : resolveReceiver(value, context, visitedNodes);
178
178
  }
179
179
 
180
- if (node.type === 'ChainExpression') {
180
+ // Transparent wrappers that do not change the receiver's runtime value.
181
+ if (
182
+ ['ChainExpression', 'TSNonNullExpression', 'TSSatisfiesExpression'].includes(node.type)
183
+ ) {
181
184
  return resolveReceiver(node.expression, context, visitedNodes);
182
185
  }
183
186
 
@@ -185,6 +188,27 @@ function resolveReceiver(node, context, visitedNodes = new Set()) {
185
188
  return uncertainValue;
186
189
  }
187
190
 
191
+ if (node.type === 'TSAsExpression' || node.type === 'TSTypeAssertion') {
192
+ let {typeAnnotation} = node;
193
+
194
+ // Unwrap `readonly Foo[]` / `readonly [A, B]` to the underlying type.
195
+ if (typeAnnotation.type === 'TSTypeOperator' && typeAnnotation.operator === 'readonly') {
196
+ typeAnnotation = typeAnnotation.typeAnnotation;
197
+ }
198
+
199
+ // An array-type assertion (`Foo[]`, `Array<…>`, `ReadonlyArray<…>`) guarantees
200
+ // an array receiver, regardless of the asserted expression, so report it.
201
+ // Anything else (`Foo`, `any`, `unknown`, unions, tuples, …) stays unresolved.
202
+ const isArrayTypeAssertion = typeAnnotation.type === 'TSArrayType'
203
+ || (
204
+ typeAnnotation.type === 'TSTypeReference'
205
+ && typeAnnotation.typeName.type === 'Identifier'
206
+ && (typeAnnotation.typeName.name === 'Array' || typeAnnotation.typeName.name === 'ReadonlyArray')
207
+ );
208
+
209
+ return isArrayTypeAssertion ? node : uncertainValue;
210
+ }
211
+
188
212
  // Supported receiver inference boundary:
189
213
  // - the receiver expression itself, if it is direct array syntax like `[]`
190
214
  // - trivial identifier aliases to that same initializer, like `const alias = values`
@@ -31,6 +31,14 @@ function shouldReport(authority) {
31
31
  return Boolean(hostname && hasPublicTld(hostname));
32
32
  }
33
33
 
34
+ function isXmlNamespaceValue(text, matchIndex) {
35
+ // 30 chars covers `xmlns:somePrefix="` with a prefix up to 22 chars long.
36
+ const preceding = text.slice(Math.max(0, matchIndex - 30), matchIndex);
37
+ // \b prevents matching words ending in "xmlns" (e.g. notxmlns).
38
+ // [\w.-]+ covers XML NCNames, which allow hyphens and dots (e.g. xmlns:xsl-fo).
39
+ return /\bxmlns(?::[\w.-]+)?\s*=\s*["']?$/i.test(preceding);
40
+ }
41
+
34
42
  /** @param {import('eslint').Rule.RuleContext} context */
35
43
  const create = context => {
36
44
  let checked = false;
@@ -46,11 +54,16 @@ const create = context => {
46
54
  const {text} = sourceCode;
47
55
 
48
56
  for (const match of text.matchAll(HTTP_URL)) {
57
+ const start = match.index;
58
+
59
+ if (isXmlNamespaceValue(text, start)) {
60
+ continue;
61
+ }
62
+
49
63
  if (!shouldReport(match.groups.authority)) {
50
64
  continue;
51
65
  }
52
66
 
53
- const start = match.index;
54
67
  const end = start + match[0].length;
55
68
 
56
69
  context.report({