Skip to content
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

[WIP] Update order-in-* rules to handle native class syntax #657

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/rules/order-in-components.js
Expand Up @@ -25,6 +25,8 @@ const ORDER = [
'willDestroyElement',
'willClearRender',
'didDestroyElement',
'action-single-line-function',
'action-multi-line-function',
'actions',
'method',
];
Expand Down Expand Up @@ -91,6 +93,12 @@ module.exports = {

reportUnorderedProperties(node, context, 'component', order);
},
ClassDeclaration(node) {
if (!ember.isEmberComponent(context, node)) {
return;
}
reportUnorderedProperties(node, context, 'component', order);
},
};
},
};
9 changes: 8 additions & 1 deletion lib/utils/property-order.js
Expand Up @@ -12,6 +12,8 @@ module.exports = {

const NAMES = {
actions: 'actions hash',
'action-single-line-function': 'action single-line function',
'action-multi-line-function': 'action multi-line function',
activate: 'lifecycle hook',
afterModel: 'lifecycle hook',
attribute: 'attribute',
Expand Down Expand Up @@ -105,10 +107,16 @@ function determinePropertyType(node, parentType) {
}

if (ember.isSingleLineFn(node)) {
if (types.isMethodDefinitionWithDecorator(node, 'action')) {
return 'action-single-line-function';
}
return 'single-line-function';
}

if (ember.isMultiLineFn(node)) {
if (types.isMethodDefinitionWithDecorator(node, 'action')) {
return 'action-multi-line-function';
}
return 'multi-line-function';
}

Expand All @@ -120,7 +128,6 @@ function determinePropertyType(node, parentType) {
if (utils.isEmptyMethod(node)) {
return 'empty-method';
}

return 'method';
}

Expand Down
44 changes: 33 additions & 11 deletions lib/utils/types.js
Expand Up @@ -22,6 +22,7 @@ module.exports = {
isLogicalExpression,
isMemberExpression,
isMethodDefinition,
isMethodDefinitionWithDecorator,
isNewExpression,
isObjectExpression,
isObjectPattern,
Expand Down Expand Up @@ -130,17 +131,7 @@ function isClassProperty(node) {
* @returns {boolean} Whether or not the node is a ClassProperty with the given decorator.
*/
function isClassPropertyWithDecorator(node, decoratorName) {
return (
isClassProperty(node) &&
node.decorators &&
node.decorators.some(decorator => {
const expression = decorator.expression;
return (
(isIdentifier(expression) && expression.name === decoratorName) ||
(isCallExpression(expression) && expression.callee.name === decoratorName)
);
})
);
return isClassProperty(node) && hasDecorator(node, decoratorName);
}

/**
Expand Down Expand Up @@ -265,6 +256,37 @@ function isMethodDefinition(node) {
return node !== undefined && node.type === 'MethodDefinition';
}

/**
* Check whether or not a node is a MethodDefinition.
*
* @param {Object} node The node to check.
* @param {string} decoratorName The decorator to look for.
* @return {boolean} Whether or not the node is a MethodDefinition with a decorator.
*/
function isMethodDefinitionWithDecorator(node, decoratorName) {
return isMethodDefinition(node) && hasDecorator(node, decoratorName);
}

/**
* Check whether or not a node is an NewExpression.
*
* @param {Object} node The node to check.
* @param {string} decoratorName The decorator to look for.
* @return {boolean} Whether or not the node has a decorator.
*/
function hasDecorator(node, decoratorName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this hasDecorator function to utils/utils.js? types.js is only supposed to be for basic type checks.

return (
node.decorators &&
node.decorators.some(decorator => {
const expression = decorator.expression;
return (
(isIdentifier(expression) && expression.name === decoratorName) ||
(isCallExpression(expression) && expression.callee.name === decoratorName)
);
})
);
}

/**
* Check whether or not a node is an NewExpression.
*
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -18,6 +18,7 @@
"test": "jest",
"test:coverage": "jest --coverage",
"test:watch": "jest --watchAll",
"test:debug": "node --inspect node_modules/.bin/jest --runInBand",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we document this somewhere?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course! I find this incredibly useful. If you think it's a good addition, I will add it to the README

"update": "node ./scripts/update-rules.js"
},
"repository": {
Expand Down