Enhance acronym ESLint rule to enforce lowercase when applicable
Feature Description
Our custom sitekit/camelcase-acronyms ESLint rule enforces our conventions around when acronyms should be capitalized.
While this works great for acronyms used within a variable name, it misses the inverse of this logic which applies to variable names that start with an acronym (e.g. ampMode), or variables which are an acronym (e.g. url).
In these cases, the acronyms should be required to be lowercase instead.
Perhaps the rule itself should be renamed to sitekit/acronym-case since the logic applies to a bit more than just camel-case ?
Note: one exception to this case are React components, so if the name is a class or function that starts with a capital letter and an acronym, then it must be all capitalized 😄
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- Acronyms defined in the
sitekit/camelcase-acronymsrule should be required to be lowercase whenever a qualifying name to be validated starts with the acronym - React component names (or just all functions) that begin with an acronym must require the acronym to be all uppercase (due to React component naming restrictions)
Implementation Brief
- Rename
packages/eslint-plugin/rules/camelcase-acronyms.jstopackages/eslint-plugin/rules/acronym-case.js - Add checks for lowercase acronyms to https://github.com/google/site-kit-wp/blob/4708e025503a007f27f5b45e9d15295f7d8f8952/packages/eslint-plugin/rules/camelcase-acronyms.js#L106
- This should entail checking to see if the match was found at the start of the variable name, and if so, that it is entirely lowercase instead of uppercase.
- Add a check to see if the variable is a function, in which case it can begin with either:
- an all-lowercase acronym (normal function)
- an all-uppercase acronym (a React component)
There's no way to determine if a function is a React component, so allowing either is the best option here. Any React component will start with uppercase and thus would require the all-caps uppercase anyway, so AmpError should fail the linter but ampError and AMPError would work.
Test Coverage
- Add ESlint tests for this component to cover the above cases.
Visual Regression Changes
- No changes to VRT.
QA Brief
npm run test:jsshould pass as our rules cover the changes made to the linter here.
Changelog entry
- N/A
IB ✅
@tofumatt If we are renaming the rule, I'm guessing we need to rename all the exceptions throughout the codebase as well? For example instances of:
- // eslint-disable-next-line sitekit/camelcase-acronyms
+ // eslint-disable-next-line sitekit/acronym-case
@johnPhillips correct, that would be necessary.
QA :heavy_check_mark: Everything works as expected. I see eslint errors if I use the wrong cases for acronyms.