pasika 0.10.75 → 0.10.76
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/eslint/pasika/index.js +63 -9
- package/package.json +1 -1
|
@@ -6101,7 +6101,7 @@ function isStableObjectSource(source) {
|
|
|
6101
6101
|
function objectUsage(scope, sourceText) {
|
|
6102
6102
|
let usage = scope.objects.get(sourceText);
|
|
6103
6103
|
if (!usage) {
|
|
6104
|
-
usage = { propertyNames: /* @__PURE__ */ new Set(), directMembers:
|
|
6104
|
+
usage = { propertyNames: /* @__PURE__ */ new Set(), directMembers: [] };
|
|
6105
6105
|
scope.objects.set(sourceText, usage);
|
|
6106
6106
|
}
|
|
6107
6107
|
return usage;
|
|
@@ -6128,8 +6128,8 @@ function recordMemberUsage(context, scope, member) {
|
|
|
6128
6128
|
const usage = objectUsage(scope, sourceText);
|
|
6129
6129
|
const propertyName5 = member.property.name;
|
|
6130
6130
|
usage.propertyNames.add(propertyName5);
|
|
6131
|
-
if (!memberIsDirectVariableInitializer(context, member)
|
|
6132
|
-
usage.directMembers.
|
|
6131
|
+
if (!memberIsDirectVariableInitializer(context, member)) {
|
|
6132
|
+
usage.directMembers.push({ member, propertyName: propertyName5 });
|
|
6133
6133
|
}
|
|
6134
6134
|
}
|
|
6135
6135
|
function recordDestructuringUsage(context, scope, declaration) {
|
|
@@ -6138,16 +6138,40 @@ function recordDestructuringUsage(context, scope, declaration) {
|
|
|
6138
6138
|
if (declarator.id.type !== "ObjectPattern" || !declarator.init || !isStableObjectSource(declarator.init)) continue;
|
|
6139
6139
|
const sourceText = context.sourceCode.getText(declarator.init);
|
|
6140
6140
|
const usage = objectUsage(scope, sourceText);
|
|
6141
|
+
const propertyNames = /* @__PURE__ */ new Set();
|
|
6141
6142
|
for (const property of declarator.id.properties) {
|
|
6142
6143
|
if (property.type !== "Property" || property.computed || property.key.type !== "Identifier") continue;
|
|
6144
|
+
propertyNames.add(property.key.name);
|
|
6143
6145
|
usage.propertyNames.add(property.key.name);
|
|
6144
6146
|
}
|
|
6147
|
+
usage.destructuring ??= { pattern: declarator.id, propertyNames };
|
|
6145
6148
|
}
|
|
6146
6149
|
}
|
|
6147
6150
|
function scopeUsageMessage(sourceText, propertyNames) {
|
|
6148
6151
|
const names = [...propertyNames].map((name) => `"${name}"`).join(", ");
|
|
6149
6152
|
return `${names} are read from "${sourceText}" in the same block. Destructure them together in one declaration. ${DOC5}`;
|
|
6150
6153
|
}
|
|
6154
|
+
function hasBinding(context, node, name) {
|
|
6155
|
+
const initialScope = context.sourceCode.getScope(node);
|
|
6156
|
+
if (initialScope.variables.some((variable) => variable.name === name)) return true;
|
|
6157
|
+
for (let scope = initialScope.upper; scope !== null; scope = scope.upper) {
|
|
6158
|
+
if (scope.variables.some((variable) => variable.name === name)) return true;
|
|
6159
|
+
}
|
|
6160
|
+
return false;
|
|
6161
|
+
}
|
|
6162
|
+
function containingStatement(context, scopeNode, node) {
|
|
6163
|
+
if (scopeNode.type !== "Program" && scopeNode.type !== "BlockStatement") return void 0;
|
|
6164
|
+
const [nodeStart, nodeEnd] = context.sourceCode.getRange(node);
|
|
6165
|
+
return scopeNode.body.find((statement) => {
|
|
6166
|
+
const [statementStart, statementEnd] = context.sourceCode.getRange(statement);
|
|
6167
|
+
return statementStart <= nodeStart && statementEnd >= nodeEnd;
|
|
6168
|
+
});
|
|
6169
|
+
}
|
|
6170
|
+
function statementIndentation(context, statement) {
|
|
6171
|
+
const [start] = context.sourceCode.getRange(statement);
|
|
6172
|
+
const lineStart = context.sourceCode.text.lastIndexOf("\n", start - 1) + 1;
|
|
6173
|
+
return context.sourceCode.text.slice(lineStart, start);
|
|
6174
|
+
}
|
|
6151
6175
|
function whitespaceKeepsStatementsAdjacent(text) {
|
|
6152
6176
|
if (text.trim() !== "") return false;
|
|
6153
6177
|
return (text.match(/\n/gu) ?? []).length <= 1;
|
|
@@ -6265,19 +6289,49 @@ var preferObjectDestructuringRule = {
|
|
|
6265
6289
|
const grouped = /* @__PURE__ */ new Set();
|
|
6266
6290
|
const usageScopes = [];
|
|
6267
6291
|
const currentUsageScope = () => usageScopes.at(-1);
|
|
6268
|
-
const enterUsageScope = () => {
|
|
6269
|
-
usageScopes.push({ objects: /* @__PURE__ */ new Map() });
|
|
6292
|
+
const enterUsageScope = (node) => {
|
|
6293
|
+
usageScopes.push({ node, objects: /* @__PURE__ */ new Map() });
|
|
6270
6294
|
};
|
|
6271
6295
|
const exitUsageScope = () => {
|
|
6272
6296
|
const scope = usageScopes.pop();
|
|
6273
6297
|
if (!scope) return;
|
|
6274
6298
|
for (const [sourceText, usage] of scope.objects) {
|
|
6275
|
-
if (usage.propertyNames.size < 2 || usage.directMembers.
|
|
6276
|
-
const
|
|
6277
|
-
if (!
|
|
6299
|
+
if (usage.propertyNames.size < 2 || usage.directMembers.length === 0) continue;
|
|
6300
|
+
const firstDirectUsage = usage.directMembers[0];
|
|
6301
|
+
if (!firstDirectUsage) continue;
|
|
6302
|
+
const { member: firstDirectMember } = firstDirectUsage;
|
|
6278
6303
|
context.report({
|
|
6279
6304
|
node: firstDirectMember,
|
|
6280
|
-
message: scopeUsageMessage(sourceText, usage.propertyNames)
|
|
6305
|
+
message: scopeUsageMessage(sourceText, usage.propertyNames),
|
|
6306
|
+
fix(fixer) {
|
|
6307
|
+
const propertyNames = [...usage.propertyNames];
|
|
6308
|
+
const missingPropertyNames = propertyNames.filter(
|
|
6309
|
+
(propertyName5) => !usage.destructuring?.propertyNames.has(propertyName5)
|
|
6310
|
+
);
|
|
6311
|
+
if (missingPropertyNames.some((propertyName5) => hasBinding(context, firstDirectMember, propertyName5))) {
|
|
6312
|
+
return null;
|
|
6313
|
+
}
|
|
6314
|
+
const memberFixes = usage.directMembers.map(
|
|
6315
|
+
({ member, propertyName: propertyName5 }) => fixer.replaceText(member, propertyName5)
|
|
6316
|
+
);
|
|
6317
|
+
if (usage.destructuring) {
|
|
6318
|
+
if (missingPropertyNames.length === 0) return memberFixes;
|
|
6319
|
+
const patternText = context.sourceCode.getText(usage.destructuring.pattern);
|
|
6320
|
+
const closeIndex = patternText.lastIndexOf("}");
|
|
6321
|
+
const beforeClose = patternText.slice(0, closeIndex);
|
|
6322
|
+
const contentEnd = beforeClose.trimEnd().length;
|
|
6323
|
+
const trailingWhitespace = beforeClose.slice(contentEnd);
|
|
6324
|
+
const separator = usage.destructuring.propertyNames.size > 0 ? ", " : "";
|
|
6325
|
+
const replacement = `${beforeClose.slice(0, contentEnd)}${separator}${missingPropertyNames.join(", ")}${trailingWhitespace}${patternText.slice(closeIndex)}`;
|
|
6326
|
+
return [fixer.replaceText(usage.destructuring.pattern, replacement), ...memberFixes];
|
|
6327
|
+
}
|
|
6328
|
+
const statement = containingStatement(context, scope.node, firstDirectMember);
|
|
6329
|
+
if (!statement) return null;
|
|
6330
|
+
const indentation = statementIndentation(context, statement);
|
|
6331
|
+
const declaration = `const { ${propertyNames.join(", ")} } = ${sourceText};
|
|
6332
|
+
${indentation}`;
|
|
6333
|
+
return [fixer.insertTextBefore(statement, declaration), ...memberFixes];
|
|
6334
|
+
}
|
|
6281
6335
|
});
|
|
6282
6336
|
}
|
|
6283
6337
|
};
|