mirror of
https://github.com/bendtherules/eslint-plugin-import-grouped.git
synced 2026-08-18 13:43:19 +00:00
Move ImportGrouped rule logic to class
This commit is contained in:
+111
-40
@@ -8,57 +8,128 @@
|
||||
// Rule Definition
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class ImportGrouped {
|
||||
// static extensionDelimiter = '.';
|
||||
|
||||
constructor({ options }) {
|
||||
this.importNodes = [];
|
||||
this.optionsExtOrder = options[0] || [];
|
||||
this.optionsExtOrder = this.optionsExtOrder.map((ext) => ext.toLowerCase());
|
||||
console.log(options);
|
||||
|
||||
}
|
||||
|
||||
registerNode(newNode) {
|
||||
this.importNodes.push(newNode);
|
||||
}
|
||||
|
||||
sortNodesByAscending() {
|
||||
this.importNodes.sort((node1, node2) => node1.start > node2.start)
|
||||
}
|
||||
|
||||
getExtension(node) {
|
||||
let extensionWithDot = '';
|
||||
|
||||
if (node.source && node.source.value) {
|
||||
const path = node.source.value;
|
||||
|
||||
if (path.includes(ImportGrouped.extensionDelimiter)) {
|
||||
const startPos = path.lastIndexOf(ImportGrouped.extensionDelimiter);
|
||||
extensionWithDot = path.substr(startPos);
|
||||
}
|
||||
}
|
||||
|
||||
return extensionWithDot;
|
||||
}
|
||||
|
||||
findBrokenNodes() {
|
||||
const brokenNodes = [];
|
||||
|
||||
let lastMatchedIndexInOptions = -1;
|
||||
|
||||
for (const tmpImportNode of this.importNodes) {
|
||||
const tmpExt = this.getExtension(tmpImportNode).toLowerCase();
|
||||
console.log(`Got ext with ${tmpExt}`);
|
||||
|
||||
if (this.optionsExtOrder.includes(tmpExt)) {
|
||||
console.log(`which is there in options`);
|
||||
const newMatchIndex = this.optionsExtOrder.indexOf(tmpExt);
|
||||
console.log(`at index ${newMatchIndex}`);
|
||||
if (newMatchIndex >= lastMatchedIndexInOptions) {
|
||||
console.log(`which is greater than last matched index`);
|
||||
lastMatchedIndexInOptions = newMatchIndex;
|
||||
} else {
|
||||
console.log(`which is less than last matched index. PROBLEM`);
|
||||
brokenNodes.push(tmpImportNode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return brokenNodes
|
||||
}
|
||||
|
||||
const importNodes = [];
|
||||
function registerNode(importNode) {
|
||||
importNodes.push(importNode);
|
||||
}
|
||||
|
||||
ImportGrouped.importNodeSelector = 'ImportDeclaration';
|
||||
ImportGrouped.extensionDelimiter = '.';
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
docs: {
|
||||
description: "Import statements should be grouped by extensions and groups can have custom order between them",
|
||||
category: "Fill me in",
|
||||
recommended: false
|
||||
},
|
||||
fixable: null, // or "code" or "whitespace"
|
||||
schema: [
|
||||
// fill in your schema
|
||||
]
|
||||
meta: {
|
||||
docs: {
|
||||
description: "Import statements should be grouped by extensions and groups can have custom order between them",
|
||||
category: "Fill me in",
|
||||
recommended: false
|
||||
},
|
||||
fixable: null, // or "code" or "whitespace"
|
||||
schema: [
|
||||
{
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
]
|
||||
},
|
||||
|
||||
create: function (context) {
|
||||
create: function (context) {
|
||||
|
||||
// variables should be defined here
|
||||
// variables should be defined here
|
||||
const importGrouped = new ImportGrouped(context);
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
// Helpers
|
||||
//----------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------
|
||||
// Helpers
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
// any helper functions should go here or else delete this section
|
||||
// any helper functions should go here or else delete this section
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
// Public
|
||||
//----------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------
|
||||
// Public
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
return {
|
||||
ImportDeclaration: function handleImports(node) {
|
||||
registerNode(node);
|
||||
},
|
||||
'Program:exit': function reportAndReset() {
|
||||
// Calc problems and report stuff here
|
||||
return {
|
||||
[ImportGrouped.importNodeSelector]: function handleImports(node) {
|
||||
importGrouped.registerNode(node);
|
||||
},
|
||||
'Program:exit': function reportAndReset() {
|
||||
// console.log(importGrouped.importNodes);
|
||||
|
||||
// context.report({
|
||||
// node: secondNode.node,
|
||||
// message: message,
|
||||
// fix: canFix && (fixer =>
|
||||
// fixer.replaceTextRange(
|
||||
// [firstRootStart, secondRootEnd],
|
||||
// newCode + sourceCode.text.substring(firstRootStart, secondRootStart)
|
||||
// )),
|
||||
// })
|
||||
},
|
||||
// Calc problems and report stuff here
|
||||
const brokenNodes = importGrouped.findBrokenNodes();
|
||||
|
||||
};
|
||||
}
|
||||
brokenNodes.forEach((eachBrokenNode) => {
|
||||
context.report({
|
||||
node: eachBrokenNode,
|
||||
message: 'Node doesn\'t match import order',
|
||||
// fix: canFix && (fixer =>
|
||||
// fixer.replaceTextRange(
|
||||
// [firstRootStart, secondRootEnd],
|
||||
// newCode + sourceCode.text.substring(firstRootStart, secondRootStart)
|
||||
// )),
|
||||
})
|
||||
})
|
||||
|
||||
},
|
||||
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user