|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { isStringLiteral } = require('../utils/types'); |
| 4 | +const { getImportIdentifier } = require('../utils/import'); |
| 5 | +const { isThisSet: isThisSetNative } = require('../utils/property-setter'); |
| 6 | +const { nodeToDependentKey } = require('../utils/property-getter'); |
| 7 | + |
| 8 | +module.exports = { |
| 9 | + meta: { |
| 10 | + type: 'problem', |
| 11 | + docs: { |
| 12 | + description: 'disallow modifying the specified properties', |
| 13 | + category: 'Miscellaneous', |
| 14 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/no-restricted-property-modifications.md', |
| 15 | + }, |
| 16 | + fixable: 'code', |
| 17 | + schema: { |
| 18 | + type: 'array', |
| 19 | + minItems: 1, |
| 20 | + maxItems: 1, |
| 21 | + items: [ |
| 22 | + { |
| 23 | + type: 'object', |
| 24 | + properties: { |
| 25 | + properties: { |
| 26 | + type: 'array', |
| 27 | + items: { |
| 28 | + type: 'string', |
| 29 | + }, |
| 30 | + minItems: 1, |
| 31 | + uniqueItems: true, |
| 32 | + }, |
| 33 | + }, |
| 34 | + required: ['properties'], |
| 35 | + additionalProperties: false, |
| 36 | + }, |
| 37 | + ], |
| 38 | + }, |
| 39 | + messages: { |
| 40 | + doNotUseAssignment: 'Do not use assignment on properties that should not be modified.', |
| 41 | + doNotUseSet: 'Do not call `set` on properties that should not be modified.', |
| 42 | + useReadOnlyMacro: |
| 43 | + 'Use the `readOnly` computed property macro for properties that should not be modified.', |
| 44 | + }, |
| 45 | + }, |
| 46 | + create(context) { |
| 47 | + let importedComputedName; |
| 48 | + let importedReadsName; |
| 49 | + let importedAliasName; |
| 50 | + |
| 51 | + const readOnlyProperties = context.options[0].properties; |
| 52 | + |
| 53 | + return { |
| 54 | + ImportDeclaration(node) { |
| 55 | + if (node.source.value === '@ember/object') { |
| 56 | + importedComputedName = |
| 57 | + importedComputedName || getImportIdentifier(node, '@ember/object', 'computed'); |
| 58 | + } else if (node.source.value === '@ember/object/computed') { |
| 59 | + importedReadsName = |
| 60 | + importedReadsName || getImportIdentifier(node, '@ember/object/computed', 'reads'); |
| 61 | + importedAliasName = |
| 62 | + importedAliasName || getImportIdentifier(node, '@ember/object/computed', 'alias'); |
| 63 | + } |
| 64 | + }, |
| 65 | + |
| 66 | + AssignmentExpression(node) { |
| 67 | + if (!isThisSetNative(node)) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const dependentKey = nodeToDependentKey(node.left, context); |
| 72 | + if ( |
| 73 | + readOnlyProperties.includes(dependentKey) || |
| 74 | + readOnlyProperties.some((property) => dependentKey.startsWith(`${property}.`)) |
| 75 | + ) { |
| 76 | + context.report({ |
| 77 | + node, |
| 78 | + messageId: 'doNotUseAssignment', |
| 79 | + }); |
| 80 | + } |
| 81 | + }, |
| 82 | + |
| 83 | + CallExpression(node) { |
| 84 | + if ( |
| 85 | + isReadOnlyPropertyUsingAliasOrReads( |
| 86 | + node, |
| 87 | + readOnlyProperties, |
| 88 | + importedComputedName, |
| 89 | + importedAliasName, |
| 90 | + importedReadsName |
| 91 | + ) |
| 92 | + ) { |
| 93 | + context.report({ |
| 94 | + node, |
| 95 | + messageId: 'useReadOnlyMacro', |
| 96 | + fix(fixer) { |
| 97 | + const argumentText0 = context.getSourceCode().getText(node.arguments[0]); |
| 98 | + return node.callee.type === 'MemberExpression' |
| 99 | + ? fixer.replaceText(node, `${importedComputedName}.readOnly(${argumentText0})`) |
| 100 | + : fixer.replaceText(node, `readOnly(${argumentText0})`); |
| 101 | + }, |
| 102 | + }); |
| 103 | + } else if (isThisSetReadOnlyProperty(node, readOnlyProperties)) { |
| 104 | + context.report({ node, messageId: 'doNotUseSet' }); |
| 105 | + } |
| 106 | + }, |
| 107 | + }; |
| 108 | + }, |
| 109 | +}; |
| 110 | + |
| 111 | +function isReadOnlyPropertyUsingAliasOrReads( |
| 112 | + node, |
| 113 | + readOnlyProperties, |
| 114 | + importedComputedName, |
| 115 | + importedAliasName, |
| 116 | + importedReadsName |
| 117 | +) { |
| 118 | + // Looks for: reads('readOnlyProperty') or alias('readOnlyProperty') |
| 119 | + return ( |
| 120 | + (isAliasComputedProperty(node, importedComputedName, importedAliasName) || |
| 121 | + isReadsComputedProperty(node, importedComputedName, importedReadsName)) && |
| 122 | + node.arguments.length === 1 && |
| 123 | + isStringLiteral(node.arguments[0]) && |
| 124 | + isReadOnlyProperty(node.arguments[0].value, readOnlyProperties) |
| 125 | + ); |
| 126 | +} |
| 127 | + |
| 128 | +function isThisSet(node) { |
| 129 | + // Looks for: this.set('readOnlyProperty...', ...); |
| 130 | + return ( |
| 131 | + node.callee.type === 'MemberExpression' && |
| 132 | + node.callee.object.type === 'ThisExpression' && |
| 133 | + node.callee.property.type === 'Identifier' && |
| 134 | + node.callee.property.name === 'set' && |
| 135 | + node.arguments.length === 2 && |
| 136 | + isStringLiteral(node.arguments[0]) |
| 137 | + ); |
| 138 | +} |
| 139 | + |
| 140 | +function isThisSetReadOnlyProperty(node, readOnlyProperties) { |
| 141 | + return isThisSet(node) && isReadOnlyProperty(node.arguments[0].value, readOnlyProperties); |
| 142 | +} |
| 143 | + |
| 144 | +function isAliasComputedProperty(node, importedComputedName, importedAliasName) { |
| 145 | + return ( |
| 146 | + isIdentifierCall(node, importedAliasName) || |
| 147 | + isMemberExpressionCall(node, importedComputedName, 'alias') |
| 148 | + ); |
| 149 | +} |
| 150 | + |
| 151 | +function isReadsComputedProperty(node, importedComputedName, importedReadsName) { |
| 152 | + return ( |
| 153 | + isIdentifierCall(node, importedReadsName) || |
| 154 | + isMemberExpressionCall(node, importedComputedName, 'reads') |
| 155 | + ); |
| 156 | +} |
| 157 | + |
| 158 | +function isIdentifierCall(node, name) { |
| 159 | + return ( |
| 160 | + node.type === 'CallExpression' && node.callee.type === 'Identifier' && node.callee.name === name |
| 161 | + ); |
| 162 | +} |
| 163 | + |
| 164 | +function isMemberExpressionCall(node, object, name) { |
| 165 | + return ( |
| 166 | + node.type === 'CallExpression' && |
| 167 | + node.callee.type === 'MemberExpression' && |
| 168 | + node.callee.object.type === 'Identifier' && |
| 169 | + node.callee.object.name === object && |
| 170 | + node.callee.property.type === 'Identifier' && |
| 171 | + node.callee.property.name === name |
| 172 | + ); |
| 173 | +} |
| 174 | + |
| 175 | +function isReadOnlyProperty(property, readOnlyProperties) { |
| 176 | + return ( |
| 177 | + readOnlyProperties.includes(property) || |
| 178 | + readOnlyProperties.some((propertyCurrent) => property.startsWith(`${propertyCurrent}.`)) |
| 179 | + ); |
| 180 | +} |
0 commit comments