| 8 | module.exports = function (context) { |
| 9 | return { |
| 10 | Identifier(node) { |
| 11 | let name = node.name.replace(/^_+|_+$/g, ''); |
| 12 | |
| 13 | // allow SCREAMING_SNAKE_CASE |
| 14 | if (name === name.toUpperCase()) { |
| 15 | return; |
| 16 | } |
| 17 | if (name === 'var_args') { |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | if (name.startsWith('opt_')) { |
| 22 | name = name.slice('opt_'.length); |
| 23 | } |
| 24 | if (name.endsWith('_Enum')) { |
| 25 | name = name.slice(0, -'_Enum'.length); |
| 26 | } |
| 27 | |
| 28 | if (!name.includes('_')) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | // Excuse membership access, unless we're in the LHS of an assignment. |
| 33 | // This attempts to prevent us from defining new properties with underscores, |
| 34 | // while allowing access to external objects that already have them. |
| 35 | // This mirrors google-camelcase's logic. |
| 36 | const {parent} = node; |
| 37 | if (parent.type === 'MemberExpression' && parent.property === node) { |
| 38 | const grandParent = parent.parent; |
| 39 | if ( |
| 40 | !( |
| 41 | grandParent.type === 'AssignmentExpression' && |
| 42 | grandParent.left === parent |
| 43 | ) |
| 44 | ) { |
| 45 | return; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // Permit object destructuring, since that is similar to membership access. |
| 50 | // Requires that the key is immediately renamed to a conforming value. |
| 51 | if ( |
| 52 | parent.type === 'Property' && |
| 53 | parent.parent.type === 'ObjectPattern' && |
| 54 | parent.key === node |
| 55 | ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | context.report({ |
| 60 | node, |
| 61 | message: `"${node.name}" must use camelCaseCapitalization.`, |
| 62 | }); |
| 63 | }, |
| 64 | }; |
| 65 | }; |