-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(eslint-plugin-experience): add decorator key sort
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
projects/eslint-plugin-experience/rules/decorator-key-sort.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const DEFAULT_OPTIONS = { | ||
decorators: {}, | ||
}; | ||
|
||
/** | ||
* @type {import(`eslint`).Rule.RuleModule} | ||
*/ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
fixable: 'code', | ||
schema: [ | ||
{ | ||
type: `object`, | ||
properties: { | ||
decorators: { | ||
type: `object`, | ||
description: `Decorators names and his keys order`, | ||
properties: { | ||
additionalProperties: true, | ||
}, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
create(context) { | ||
const {decorators: ORDER} = { | ||
...DEFAULT_OPTIONS, | ||
...(context.options[0] || {}), | ||
}; | ||
|
||
return { | ||
ClassDeclaration(node) { | ||
const decorators = Array.from(node.decorators ?? []); | ||
|
||
for (const decorator of decorators) { | ||
const expression = decorator.expression; | ||
const decoratorName = expression.callee.name; | ||
|
||
if (decoratorName in (ORDER || {})) { | ||
const orderList = ORDER[decoratorName]; | ||
const arguments = Array.from(expression.arguments ?? []); | ||
|
||
for (const argument of arguments) { | ||
const properties = Array.from(argument.properties ?? []); | ||
const current = properties.map(prop => prop.key.name); | ||
const correct = getCorrectOrderRelative(orderList, current); | ||
|
||
if (!isCorrectSortedAccording(correct, current)) { | ||
context.report({ | ||
node: expression, | ||
message: `Incorrect order keys in @${decoratorName} decorator, please sort by [${correct.join( | ||
' -> ', | ||
)}]`, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
function isCorrectSortedAccording(correctOrder, currentOrder) { | ||
let excludeUnknown = currentOrder.filter(item => correctOrder.includes(item)); | ||
|
||
return JSON.stringify(correctOrder) === JSON.stringify(excludeUnknown); | ||
} | ||
|
||
function getCorrectOrderRelative(correctOrder, currentOrder) { | ||
return correctOrder.filter(item => currentOrder.includes(item)); | ||
} |