eslint-plugin-absolute 0.6.0 → 0.7.0
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 +107 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3525,6 +3525,112 @@ var noUnnecessaryDiv = {
|
|
|
3525
3525
|
}
|
|
3526
3526
|
};
|
|
3527
3527
|
|
|
3528
|
+
// src/rules/prefer-inline-exports.ts
|
|
3529
|
+
var isLocalDeclaration = (node) => node.type === "VariableDeclaration" || node.type === "FunctionDeclaration" || node.type === "ClassDeclaration" || node.type === "TSTypeAliasDeclaration" || node.type === "TSInterfaceDeclaration" || node.type === "TSEnumDeclaration";
|
|
3530
|
+
var declarationName = (decl) => {
|
|
3531
|
+
if (decl.type === "VariableDeclaration") {
|
|
3532
|
+
if (decl.declarations.length !== 1)
|
|
3533
|
+
return null;
|
|
3534
|
+
const [first] = decl.declarations;
|
|
3535
|
+
if (!first || first.id.type !== "Identifier")
|
|
3536
|
+
return null;
|
|
3537
|
+
return first.id.name;
|
|
3538
|
+
}
|
|
3539
|
+
if (!decl.id || decl.id.type !== "Identifier")
|
|
3540
|
+
return null;
|
|
3541
|
+
return decl.id.name;
|
|
3542
|
+
};
|
|
3543
|
+
var findOwnDeclaration = (program, name) => {
|
|
3544
|
+
for (const stmt of program.body) {
|
|
3545
|
+
if (stmt.type === "ExportNamedDeclaration" && stmt.declaration && isLocalDeclaration(stmt.declaration) && declarationName(stmt.declaration) === name) {
|
|
3546
|
+
return { alreadyExported: true, decl: stmt.declaration };
|
|
3547
|
+
}
|
|
3548
|
+
if (stmt.type === "ExportDefaultDeclaration" && stmt.declaration.type !== "Identifier" && isLocalDeclaration(stmt.declaration) && declarationName(stmt.declaration) === name) {
|
|
3549
|
+
return {
|
|
3550
|
+
alreadyExported: true,
|
|
3551
|
+
decl: stmt.declaration
|
|
3552
|
+
};
|
|
3553
|
+
}
|
|
3554
|
+
if (isLocalDeclaration(stmt) && declarationName(stmt) === name) {
|
|
3555
|
+
return { alreadyExported: false, decl: stmt };
|
|
3556
|
+
}
|
|
3557
|
+
}
|
|
3558
|
+
return null;
|
|
3559
|
+
};
|
|
3560
|
+
var preferInlineExports = {
|
|
3561
|
+
create(context) {
|
|
3562
|
+
const { sourceCode } = context;
|
|
3563
|
+
const program = sourceCode.ast;
|
|
3564
|
+
return {
|
|
3565
|
+
ExportNamedDeclaration(node) {
|
|
3566
|
+
if (node.source)
|
|
3567
|
+
return;
|
|
3568
|
+
if (node.declaration)
|
|
3569
|
+
return;
|
|
3570
|
+
if (node.specifiers.length === 0)
|
|
3571
|
+
return;
|
|
3572
|
+
if (node.exportKind === "type")
|
|
3573
|
+
return;
|
|
3574
|
+
const fixable = [];
|
|
3575
|
+
for (const spec of node.specifiers) {
|
|
3576
|
+
if (spec.type !== "ExportSpecifier")
|
|
3577
|
+
continue;
|
|
3578
|
+
if (spec.local.type !== "Identifier")
|
|
3579
|
+
continue;
|
|
3580
|
+
if (spec.exported.type !== "Identifier")
|
|
3581
|
+
continue;
|
|
3582
|
+
if (spec.local.name !== spec.exported.name)
|
|
3583
|
+
continue;
|
|
3584
|
+
if (spec.exportKind === "type")
|
|
3585
|
+
continue;
|
|
3586
|
+
const found = findOwnDeclaration(program, spec.local.name);
|
|
3587
|
+
if (!found)
|
|
3588
|
+
continue;
|
|
3589
|
+
if (found.alreadyExported)
|
|
3590
|
+
continue;
|
|
3591
|
+
fixable.push({ decl: found.decl, spec });
|
|
3592
|
+
}
|
|
3593
|
+
if (fixable.length === 0)
|
|
3594
|
+
return;
|
|
3595
|
+
const allSpecsAreFixable = fixable.length === node.specifiers.length;
|
|
3596
|
+
const names = fixable.map(({ spec }) => spec.local.type === "Identifier" ? spec.local.name : "").filter((name) => name.length > 0);
|
|
3597
|
+
context.report({
|
|
3598
|
+
data: { names: names.join(", ") },
|
|
3599
|
+
fix(fixer) {
|
|
3600
|
+
const fixes = [];
|
|
3601
|
+
for (const { decl } of fixable) {
|
|
3602
|
+
const [declStart] = decl.range;
|
|
3603
|
+
fixes.push(fixer.insertTextBeforeRange([declStart, declStart], "export "));
|
|
3604
|
+
}
|
|
3605
|
+
if (allSpecsAreFixable) {
|
|
3606
|
+
fixes.push(fixer.remove(node));
|
|
3607
|
+
} else {
|
|
3608
|
+
const survivors = node.specifiers.filter((spec) => !fixable.some((entry) => entry.spec === spec));
|
|
3609
|
+
const replacement = `export { ${survivors.map((spec) => sourceCode.getText(spec)).join(", ")} };`;
|
|
3610
|
+
fixes.push(fixer.replaceText(node, replacement));
|
|
3611
|
+
}
|
|
3612
|
+
return fixes;
|
|
3613
|
+
},
|
|
3614
|
+
messageId: "preferInline",
|
|
3615
|
+
node
|
|
3616
|
+
});
|
|
3617
|
+
}
|
|
3618
|
+
};
|
|
3619
|
+
},
|
|
3620
|
+
defaultOptions: [],
|
|
3621
|
+
meta: {
|
|
3622
|
+
docs: {
|
|
3623
|
+
description: "Prefer inlining `export` at a declaration site over a trailing `export { name }` statement when the name is a local declaration."
|
|
3624
|
+
},
|
|
3625
|
+
fixable: "code",
|
|
3626
|
+
messages: {
|
|
3627
|
+
preferInline: "Inline `export` at the declaration of `{{names}}` instead of re-exporting at the bottom of the file."
|
|
3628
|
+
},
|
|
3629
|
+
schema: [],
|
|
3630
|
+
type: "suggestion"
|
|
3631
|
+
}
|
|
3632
|
+
};
|
|
3633
|
+
|
|
3528
3634
|
// src/index.ts
|
|
3529
3635
|
var src_default = {
|
|
3530
3636
|
rules: {
|
|
@@ -3545,6 +3651,7 @@ var src_default = {
|
|
|
3545
3651
|
"no-unnecessary-div": noUnnecessaryDiv,
|
|
3546
3652
|
"no-unnecessary-key": noUnnecessaryKey,
|
|
3547
3653
|
"no-useless-function": noUselessFunction,
|
|
3654
|
+
"prefer-inline-exports": preferInlineExports,
|
|
3548
3655
|
"seperate-style-files": seperateStyleFiles,
|
|
3549
3656
|
"sort-exports": sortExports,
|
|
3550
3657
|
"sort-keys-fixable": sortKeysFixable,
|
package/package.json
CHANGED