Move ImportGrouped rule logic to class

This commit is contained in:
2018-08-29 10:14:48 +05:30
parent 3cf36a9c20
commit 0734fe6b67
+111 -40
View File
@@ -8,57 +8,128 @@
// 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);
}
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 = { module.exports = {
meta: { meta: {
docs: { docs: {
description: "Import statements should be grouped by extensions and groups can have custom order between them", description: "Import statements should be grouped by extensions and groups can have custom order between them",
category: "Fill me in", category: "Fill me in",
recommended: false recommended: false
},
fixable: null, // or "code" or "whitespace"
schema: [
// fill in your schema
]
}, },
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 { 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,
// fix: canFix && (fixer =>
// fixer.replaceTextRange(
// [firstRootStart, secondRootEnd],
// newCode + sourceCode.text.substring(firstRootStart, secondRootStart)
// )),
// })
},
}; 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)
// )),
})
})
},
};
}
}; };