Move ImportGrouped rule logic to class

This commit is contained in:
2018-08-29 10:14:48 +05:30
parent 3cf36a9c20
commit 0734fe6b67
+82 -11
View File
@@ -8,12 +8,71 @@
// 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: {
@@ -23,13 +82,19 @@ module.exports = {
},
fixable: null, // or "code" or "whitespace"
schema: [
// fill in your schema
{
"type": "array",
"items": {
"type": "string"
}
},
]
},
create: function (context) {
// variables should be defined here
const importGrouped = new ImportGrouped(context);
//----------------------------------------------------------------------
// Helpers
@@ -42,21 +107,27 @@ module.exports = {
//----------------------------------------------------------------------
return {
ImportDeclaration: function handleImports(node) {
registerNode(node);
[ImportGrouped.importNodeSelector]: function handleImports(node) {
importGrouped.registerNode(node);
},
'Program:exit': function reportAndReset() {
// Calc problems and report stuff here
// console.log(importGrouped.importNodes);
// context.report({
// node: secondNode.node,
// message: message,
// 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)
// )),
// })
})
})
},
};