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:
+82
-11
@@ -8,12 +8,71 @@
|
|||||||
// Rule Definition
|
// 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);
|
||||||
|
|
||||||
const importNodes = [];
|
|
||||||
function registerNode(importNode) {
|
|
||||||
importNodes.push(importNode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ImportGrouped.importNodeSelector = 'ImportDeclaration';
|
||||||
|
ImportGrouped.extensionDelimiter = '.';
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
meta: {
|
meta: {
|
||||||
docs: {
|
docs: {
|
||||||
@@ -23,13 +82,19 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
fixable: null, // or "code" or "whitespace"
|
fixable: null, // or "code" or "whitespace"
|
||||||
schema: [
|
schema: [
|
||||||
// fill in your 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
|
||||||
@@ -42,21 +107,27 @@ module.exports = {
|
|||||||
//----------------------------------------------------------------------
|
//----------------------------------------------------------------------
|
||||||
|
|
||||||
return {
|
return {
|
||||||
ImportDeclaration: function handleImports(node) {
|
[ImportGrouped.importNodeSelector]: function handleImports(node) {
|
||||||
registerNode(node);
|
importGrouped.registerNode(node);
|
||||||
},
|
},
|
||||||
'Program:exit': function reportAndReset() {
|
'Program:exit': function reportAndReset() {
|
||||||
// Calc problems and report stuff here
|
// console.log(importGrouped.importNodes);
|
||||||
|
|
||||||
// context.report({
|
// Calc problems and report stuff here
|
||||||
// node: secondNode.node,
|
const brokenNodes = importGrouped.findBrokenNodes();
|
||||||
// message: message,
|
|
||||||
|
brokenNodes.forEach((eachBrokenNode) => {
|
||||||
|
context.report({
|
||||||
|
node: eachBrokenNode,
|
||||||
|
message: 'Node doesn\'t match import order',
|
||||||
// fix: canFix && (fixer =>
|
// fix: canFix && (fixer =>
|
||||||
// fixer.replaceTextRange(
|
// fixer.replaceTextRange(
|
||||||
// [firstRootStart, secondRootEnd],
|
// [firstRootStart, secondRootEnd],
|
||||||
// newCode + sourceCode.text.substring(firstRootStart, secondRootStart)
|
// newCode + sourceCode.text.substring(firstRootStart, secondRootStart)
|
||||||
// )),
|
// )),
|
||||||
// })
|
})
|
||||||
|
})
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user