Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions lib/utils/ember.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ module.exports = {
isEmberHelper,
isEmberProxy,

isActionMethod,
isSingleLineAccessor,
isSingleLineFn,
isMultiLineAccessor,
isMultiLineFn,
isFunctionExpression,

Expand Down Expand Up @@ -478,10 +481,12 @@ function isObjectProp(node) {
return types.isObjectExpression(node.value);
}

function isCustomProp(property) {
const value = property.value;
function isCustomProp(property, isNativeClass = false) {
const { value } = property;
if (!value) {
return false;
// Native classes allow for empty values
// e.g. class Example { foo; }
return isNativeClass;
}
const isCustomObjectProp = types.isObjectExpression(value) && property.key.name !== 'actions';

Expand Down Expand Up @@ -509,6 +514,13 @@ function isActionsProp(property) {
);
}

function isActionMethod(property) {
if (property?.type === 'MethodDefinition') {
return decoratorUtils.hasDecorator(property, 'action');
}
return false;
}

function isComponentLifecycleHookName(name) {
return [
'didDestroyElement',
Expand Down Expand Up @@ -635,6 +647,13 @@ function getEmberImportAliasName(importDeclaration) {
return importDeclaration.specifiers[0].local.name;
}

function isSingleLineAccessor(property) {
return (
(types.isPropAccessor(property) || decoratorUtils.hasDecorator(property, 'computed')) &&
utils.getSize(property) === 1
);
}

function isSingleLineFn(property, importedEmberName, importedObserverName) {
return (
(types.isMethodDefinition(property) && utils.getSize(property) === 1) ||
Expand All @@ -647,6 +666,13 @@ function isSingleLineFn(property, importedEmberName, importedObserverName) {
);
}

function isMultiLineAccessor(property) {
return (
(types.isPropAccessor(property) || decoratorUtils.hasDecorator(property, 'computed')) &&
utils.getSize(property) > 1
);
}

function isMultiLineFn(property, importedEmberName, importedObserverName) {
return (
(types.isMethodDefinition(property) && utils.getSize(property) > 1) ||
Expand All @@ -663,7 +689,8 @@ function isFunctionExpression(property) {
return (
types.isFunctionExpression(property) ||
types.isArrowFunctionExpression(property) ||
types.isCallWithFunctionExpression(property)
types.isCallWithFunctionExpression(property) ||
types.isCallWithArrowFunctionExpression(property)
);
}

Expand Down
90 changes: 90 additions & 0 deletions lib/utils/property-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const decoratorUtils = require('../utils/decorators');

module.exports = {
determinePropertyType,
determinePropertyTypeInNativeClass,
reportUnorderedProperties,
addBackwardsPosition,
};
Expand Down Expand Up @@ -148,6 +149,95 @@ function determinePropertyType(
return 'unknown';
}

function determinePropertyTypeInNativeClass(
node,
parentType,
ORDER,
importedEmberName,
importedInjectName,
importedObserverName,
importedControllerName
) {
if (node === undefined) {
return 'unknown';
}

if (ember.isInjectedServiceProp(node, importedEmberName, importedInjectName)) {
return 'service';
}

if (ember.isInjectedControllerProp(node, importedEmberName, importedControllerName)) {
return 'controller';
}

if (node.type === 'MethodDefinition' && node.key.name === 'constructor') {
return 'constructor';
}

if (parentType === 'component') {
if (node.type === 'MethodDefinition' && ember.isComponentLifecycleHook(node)) {
return node.key.name;
}
} else if (parentType === 'controller') {
if (
node.key !== undefined &&
node.key.type === 'Identifier' &&
node.key.name === 'queryParams'
) {
return 'query-params';
} else if (ember.isControllerDefaultProp(node)) {
return 'inherited-property';
}
} else if (parentType === 'model') {
if (decoratorUtils.isClassPropertyOrPropertyDefinitionWithDecorator(node, 'attr')) {
return 'attribute';
} else if (ember.isRelation(node)) {
return 'relationship';
}
} else if (parentType === 'route') {
if (ember.isRouteDefaultProp(node)) {
return 'inherited-property';
} else if (ember.isRouteLifecycleHook(node)) {
return node.key.name;
}
}

if (parentType !== 'model' && ember.isActionMethod(node)) {
return 'action';
}

if (ember.isSingleLineAccessor(node)) {
return 'single-line-accessor';
}

if (ember.isMultiLineAccessor(node)) {
return 'multi-line-accessor';
}

const propName = getNodeKeyName(node);
const possibleOrderName = `custom:${propName}`;
if (ORDER.includes(possibleOrderName)) {
return possibleOrderName;
}

if (
(node.type === 'ClassProperty' || node.type === 'PropertyDefinition') &&
ember.isCustomProp(node, true)
) {
return 'property';
}

if (node.value && ember.isFunctionExpression(node.value)) {
if (utils.isEmptyMethod(node)) {
return 'empty-method';
}

return 'method';
}

return 'unknown';
}

function getOrder(ORDER, type) {
for (let i = 0, len = ORDER.length; i < len; i++) {
const value = ORDER[i];
Expand Down
42 changes: 42 additions & 0 deletions lib/utils/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
isAssignmentExpression,
isBinaryExpression,
isCallExpression,
isCallWithArrowFunctionExpression,
isCallWithFunctionExpression,
isClassDeclaration,
isClassPropertyOrPropertyDefinition,
Expand All @@ -36,6 +37,7 @@ module.exports = {
isObjectPattern,
isOptionalCallExpression,
isOptionalMemberExpression,
isPropAccessor,
isProperty,
isReturnStatement,
isSpreadElement,
Expand Down Expand Up @@ -113,6 +115,36 @@ function isCallExpression(node) {
return node !== undefined && node.type === 'CallExpression';
}

/**
* Check whether or not a node is a CallExpression that has a
* ArrowExpression as the first argument
*
* @example
* ```js
* // Native class
* tSomeAction = mysteriousFnc(() => {})
*
* // Classic Ember object
* tSomeAction: mysteriousFnc(() => {})
* ```
*
* @param {Object} node The node to check
* @return {boolean} Whether or not the node is a call with a arrow function expression as the first argument
*/
function isCallWithArrowFunctionExpression(node) {
if (!node?.type === 'CallExpression') {
return false;
}
const callObj = node.callee?.type === 'MemberExpression' ? node.callee.object : node;
const firstArg = callObj.arguments ? callObj.arguments[0] : null;
return (
callObj !== undefined &&
callObj?.type === 'CallExpression' &&
firstArg &&
firstArg?.type === 'ArrowFunctionExpression'
);
}

/**
* Check whether or not a node is a CallExpression that has a FunctionExpression
* as first argument, eg.:
Expand Down Expand Up @@ -358,6 +390,16 @@ function isOptionalMemberExpression(node) {
return node.type === 'OptionalMemberExpression';
}

/**
* Check whether a node is a property accessor (get/set).
*
* @param {Node} node the node to check
* @returns {boolean} whether the node is an accessor
*/
function isPropAccessor(node) {
return node?.type === 'MethodDefinition' && ['get', 'set'].includes(node?.kind);
}

/**
* Check whether or not a node is an Property.
*
Expand Down
53 changes: 51 additions & 2 deletions tests/lib/utils/ember-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ const emberUtils = require('../../../lib/utils/ember');
const { FauxContext } = require('../../helpers/faux-context');

function parse(code) {
return babelESLintParse(code).body[0].expression;
const { body } = babelESLintParse(code);
const [firstBodyNode] = body;
if (firstBodyNode.type === 'ClassDeclaration') {
// return first node within ClassBody
return firstBodyNode.body.body[0];
}
return firstBodyNode.expression;
}

function getProperty(code) {
Expand Down Expand Up @@ -320,7 +326,7 @@ describe('isEmberComponent', () => {
});
});

describe('isGlimerComponent', () => {
describe('isGlimmerComponent', () => {
describe("should check if it's a Glimmer Component", () => {
it('should detect Component when using native classes', () => {
const context = new FauxContext(`
Expand Down Expand Up @@ -1475,6 +1481,18 @@ describe('isActionsProp', () => {
});
});

describe('isActionMethod', () => {
const node = parse(
`class Test {
@action foo() {}
}`
);

it('should be actions method', () => {
expect(emberUtils.isActionMethod(node)).toBeTruthy();
});
});

describe('getModuleProperties', () => {
it("returns module's properties", () => {
const code = `
Expand Down Expand Up @@ -1535,6 +1553,18 @@ describe('getModuleProperties', () => {
});
});

describe('isSingleLineAccessor', () => {
const node = parse(
`class Test {
get foo() { return 'bar'; }
}`
);

it('should be single line accessor', () => {
expect(emberUtils.isSingleLineAccessor(node)).toBeTruthy();
});
});

describe('isSingleLineFn', () => {
const property = getProperty(`test = {
test: computed.or('asd', 'qwe')
Expand Down Expand Up @@ -1563,6 +1593,20 @@ describe('isSingleLineFn', () => {
});
});

describe('isMultiLineAccessor', () => {
const node = parse(
`class Test {
get foo() {
return 'bar';
}
}`
);

it('should be multi line accessor', () => {
expect(emberUtils.isMultiLineAccessor(node)).toBeTruthy();
});
});

describe('isMultiLineFn', () => {
const property = getProperty(`test = {
test: computed('asd', function() {
Expand Down Expand Up @@ -1616,6 +1660,11 @@ describe('isFunctionExpression', () => {
test: () => {}
}`);
expect(emberUtils.isFunctionExpression(property.value)).toBeTruthy();

property = getProperty(`test = {
test: someFn(() => {})
}`);
expect(emberUtils.isFunctionExpression(property.value)).toBeTruthy();
});
});

Expand Down
Loading