Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): add global modifier to naming-convention #2691

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/eslint-plugin/docs/rules/naming-convention.md
Expand Up @@ -194,7 +194,7 @@ There are two types of selectors, individual selectors, and grouped selectors.
Individual Selectors match specific, well-defined sets. There is no overlap between each of the individual selectors.

- `variable` - matches any `var` / `let` / `const` variable name.
- Allowed `modifiers`: `const`.
- Allowed `modifiers`: `const`, `global`.
- Allowed `types`: `boolean`, `string`, `number`, `function`, `array`.
- `function` - matches any named function declaration or named function expression.
- Allowed `modifiers`: none.
Expand Down Expand Up @@ -313,6 +313,8 @@ Group Selectors are provided for convenience, and essentially bundle up sets of

### Enforce that all const variables are in UPPER_CASE

Optionally, a `global` modifier can be used to only validate variables defined in the top level of the file / module.

```json
{
"@typescript-eslint/naming-convention": [
Expand Down
8 changes: 7 additions & 1 deletion packages/eslint-plugin/src/rules/naming-convention.ts
Expand Up @@ -87,6 +87,7 @@ enum Modifiers {
protected = 1 << 4,
private = 1 << 5,
abstract = 1 << 6,
global = 1 << 7,
}
type ModifiersString = keyof typeof Modifiers;

Expand Down Expand Up @@ -309,7 +310,7 @@ const SCHEMA: JSONSchema.JSONSchema4 = {
...selectorSchema('default', false, util.getEnumNames(Modifiers)),

...selectorSchema('variableLike', false),
...selectorSchema('variable', true, ['const']),
...selectorSchema('variable', true, ['const', 'global']),
...selectorSchema('function', false),
...selectorSchema('parameter', true),

Expand Down Expand Up @@ -503,6 +504,11 @@ export default util.createRule<Options, MessageIds>({
modifiers.add(Modifiers.const);
}

const scope = context.getScope();
if (scope.type === 'global' || scope.type === 'module') {
modifiers.add(Modifiers.global);
}

identifiers.forEach(i => {
validator(i, modifiers);
});
Expand Down
16 changes: 16 additions & 0 deletions packages/eslint-plugin/tests/rules/naming-convention.test.ts
Expand Up @@ -637,6 +637,22 @@ ruleTester.run('naming-convention', rule, {
},
],
},
{
code: `
const CONSTANT_VALUE = 42;
function inner() {
const localConstant = 2;
}
`,
parserOptions,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - you don't need to use the parserOptions here, and instead should do:

Suggested change
parserOptions,
parserOptions: {
sourceType: 'script',
},

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, please duplicate this test case and additionally use sourceType: 'module' to ensure both scripts and modules work as expected.

options: [
{
selector: 'variable',
modifiers: ['const', 'global'],
format: ['UPPER_CASE'],
},
],
},
{
code: `
declare const ANY_UPPER_CASE: any;
Expand Down