-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
alphabetize rule should cover exports as well #18
Changes from 10 commits
a62d9c8
64e3165
ccc873e
94f00ca
d63befe
aef25d2
626326a
feea7b6
e072ccf
791111b
56a6a7d
5e4d5a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
./node_modules/ | ||
|
||
node_modules |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,16 @@ function extractKey(objectProperty) { | |
return objectProperty.key.name; | ||
} | ||
|
||
function extractName(exportSpecifier) { | ||
return exportSpecifier.local.name; | ||
} | ||
|
||
function checkOrderOfKeysIn(array) { | ||
return function isKeyOutOfOrder(key, index) { | ||
return index !== 0 && key < array[index-1]; | ||
}; | ||
} | ||
|
||
module.exports = { | ||
esLintRuleName: "alphabetize-properties-of-large-objects", | ||
schema: [ | ||
|
@@ -49,26 +59,34 @@ module.exports = { | |
throw new Error("Value of `exempt` option must be an array of strings (key names); saw a: " + typeof exemptions); | ||
} | ||
var removeExemptions = makeExemptionsFilter(exemptions); | ||
function checkExport(node) { | ||
if (!node.specifiers.length) { | ||
return; | ||
} | ||
var rawExportNames = node.specifiers.map(extractName); | ||
if (rawExportNames.length < limit) { | ||
return; | ||
} | ||
var exportNames = rawExportNames.filter(removeExemptions); | ||
if (exportNames.find(checkOrderOfKeysIn(exportNames))) { | ||
context.report({message: "Exports with "+limit+" or more specifiers should have specifiers in alphabetical order. (Saw "+rawExportNames.length+")", node: node}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we create the message using template strings? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had been keeping this as plain-JS as possible, with the hopes that it'll be able to work on as older versions of JS/ES... But that might not be as not as important as the readability that template strings and arrow functions offer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohh good point, then this is fine! :) |
||
} | ||
} | ||
function checkObject(node) { | ||
if (!node.properties.length) { | ||
// somewhat-earlier exit... | ||
return; | ||
} | ||
var rawPropertyKeys = node.properties.filter(isStandardProperty).map(extractKey); | ||
if (rawPropertyKeys.length < limit) { | ||
return; | ||
} | ||
var propertyKeys = rawPropertyKeys.filter(removeExemptions); | ||
function isKeyOutOfOrder(key, index) { | ||
// TODO control sorting order? | ||
return index !== 0 && key < propertyKeys[index-1]; | ||
} | ||
|
||
if (propertyKeys.find(isKeyOutOfOrder)) { | ||
if (propertyKeys.find(checkOrderOfKeysIn(propertyKeys))) { | ||
context.report({message: "Objects with "+limit+" or more properties should have properties in alphabetical order. (Saw "+rawPropertyKeys.length+")", node: node}); | ||
} | ||
} | ||
return { | ||
"ExportNamedDeclaration": checkExport, | ||
"ObjectExpression": checkObject | ||
}; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More of a FYI but I'm a big fan of always using
1.
for all items in an MD ordered list to keep it simple since it will always render the correct order.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm yeah... I like having it be readable in plain-text mode as well, although I suppose having all
1
s isn't super unreadable...