|
| 1 | +const FIRST_OBJECT_PROP_NAME = 'firstObject'; |
| 2 | +const LAST_OBJECT_PROP_NAME = 'lastObject'; |
| 3 | + |
| 4 | +const ERROR_MESSAGES = { |
| 5 | + LAST_OBJECT: 'Array prototype extension property lastObject usage is disallowed.', |
| 6 | + FIRST_OBJECT: |
| 7 | + "Array prototype extension property firstObject usage is disallowed. Please use Ember's get helper instead, e.g. `(get @list '0')`.", |
| 8 | +}; |
| 9 | + |
| 10 | +/** |
| 11 | + * Check if the path should be allowed. `@firstObject.test`, `@lastObject`, and |
| 12 | + * `this.firstObject` are allowed (they are property names, not extensions). |
| 13 | + */ |
| 14 | +function isAllowed(originalStr, matchedStr) { |
| 15 | + // allow `@firstObject.test`, `@lastObject` |
| 16 | + if (originalStr.startsWith(`@${matchedStr}`)) { |
| 17 | + return true; |
| 18 | + } |
| 19 | + |
| 20 | + const originalParts = originalStr.split('.'); |
| 21 | + const matchStrIndex = originalParts.indexOf(matchedStr); |
| 22 | + |
| 23 | + // if not found |
| 24 | + if (matchStrIndex === -1) { |
| 25 | + return true; |
| 26 | + } |
| 27 | + // allow this.firstObject (direct property, not extension) |
| 28 | + return !matchStrIndex || originalParts[matchStrIndex - 1] === 'this'; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Check if current node is a `get` helper and its string literal contains matchedStr. |
| 33 | + * For example `{{get this 'list.firstObject'}}` returns true, |
| 34 | + * but `{{get this 'firstObject'}}` returns false (that's a direct property). |
| 35 | + */ |
| 36 | +function isGetHelperWithMatchedLiteral(node, matchedStr) { |
| 37 | + if (node.original !== 'get') { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + const parent = node.parent; |
| 42 | + if ( |
| 43 | + parent && |
| 44 | + (parent.type === 'GlimmerMustacheStatement' || parent.type === 'GlimmerSubExpression') && |
| 45 | + parent.params && |
| 46 | + parent.params[1] && |
| 47 | + parent.params[1].type === 'GlimmerStringLiteral' |
| 48 | + ) { |
| 49 | + const literal = parent.params[1].value || parent.params[1].original; |
| 50 | + const parts = literal.split('.'); |
| 51 | + const matchStrIndex = parts.indexOf(matchedStr); |
| 52 | + |
| 53 | + // matchedStr is found and not the `{{get this 'firstObject'}}` case |
| 54 | + return ( |
| 55 | + matchStrIndex !== -1 && |
| 56 | + !(matchStrIndex === 0 && parent.params[0] && parent.params[0].original === 'this') |
| 57 | + ); |
| 58 | + } |
| 59 | + return false; |
| 60 | +} |
| 61 | + |
| 62 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 63 | +module.exports = { |
| 64 | + meta: { |
| 65 | + type: 'suggestion', |
| 66 | + docs: { |
| 67 | + description: 'disallow usage of Ember Array prototype extensions', |
| 68 | + category: 'Best Practices', |
| 69 | + strictGjs: true, |
| 70 | + strictGts: true, |
| 71 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-array-prototype-extensions.md', |
| 72 | + }, |
| 73 | + fixable: null, |
| 74 | + schema: [], |
| 75 | + messages: { |
| 76 | + lastObject: ERROR_MESSAGES.LAST_OBJECT, |
| 77 | + firstObject: ERROR_MESSAGES.FIRST_OBJECT, |
| 78 | + }, |
| 79 | + }, |
| 80 | + |
| 81 | + create(context) { |
| 82 | + return { |
| 83 | + GlimmerPathExpression(node) { |
| 84 | + if (!node.original) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + // Handle lastObject — no fixer available |
| 89 | + if ( |
| 90 | + !isAllowed(node.original, LAST_OBJECT_PROP_NAME) || |
| 91 | + isGetHelperWithMatchedLiteral(node, LAST_OBJECT_PROP_NAME) |
| 92 | + ) { |
| 93 | + context.report({ |
| 94 | + node, |
| 95 | + messageId: 'lastObject', |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + // Handle firstObject |
| 100 | + if ( |
| 101 | + !isAllowed(node.original, FIRST_OBJECT_PROP_NAME) || |
| 102 | + isGetHelperWithMatchedLiteral(node, FIRST_OBJECT_PROP_NAME) |
| 103 | + ) { |
| 104 | + context.report({ |
| 105 | + node, |
| 106 | + messageId: 'firstObject', |
| 107 | + }); |
| 108 | + } |
| 109 | + }, |
| 110 | + }; |
| 111 | + }, |
| 112 | +}; |
0 commit comments