Skip to content

Commit

Permalink
Merge pull request #1166 from ericcornelissen/1119-update-allow-expre…
Browse files Browse the repository at this point in the history
…ssions

Allow three more expression in `no-top-level-variables`
  • Loading branch information
ericcornelissen authored Sep 8, 2024
2 parents f25e6b8 + 80ed661 commit 951f12b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 51 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Versioning].

## [Unreleased]

- _No changes yet_
- (`aaa56b7`) Always allow for `ImportExpression`, `SequenceExpression`, and
`ThisExpression` for `no-top-level-variables`.

## [3.4.0] - 2024-07-29

Expand Down
7 changes: 0 additions & 7 deletions docs/rules/no-top-level-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ Examples of **correct** code when `'ObjectExpression'` is in the list:
const hello = {world: '!'};
```

Additionally, all others expression types that aren't always allowed can be
allowed, those are:

- `ImportExpression` (e.g. `import('path');`)
- `SequenceExpression` (e.g. `(3, 14);`)
- `ThisExpression` (e.g. `this;`)

#### `kind`

Examples of **correct** code when `'const'` is in the list:
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/no-top-level-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ const allowedOption = {
'ConditionalExpression',
'FunctionExpression',
'Identifier',
'ImportExpression',
'Literal',
'LogicalExpression',
'MemberExpression',
'SequenceExpression',
'TaggedTemplateExpression',
'TemplateLiteral',
'ThisExpression',
'UnaryExpression',
'UpdateExpression'
]
Expand Down
64 changes: 21 additions & 43 deletions tests/unit/no-top-level-variables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,33 @@ const valid: RuleTester.ValidTestCase[] = [
// Configurable allowed declarations
...[
{
code: `const foo = import('path');`,
code: `const foo = import('path');`
},
{
code: `const foo = (3, 5);`
},
{
code: `const foo = this;`
},
{
code: `
// Validate that 'ImportExpression' is not rejected as an allowed expression type
const foo = import('path');
`,
options: [{allowed: ['ImportExpression']}]
},
{
code: `const foo = (3, 5);`,
code: `
// Validate that 'SequenceExpression' is not rejected as an allowed expression type
const foo = (3, 5);
`,
options: [{allowed: ['SequenceExpression']}]
},
{
code: `const foo = this;`,
code: `
// Validate that 'ThisExpression' is not rejected as an allowed expression type
const foo = this;
`,
options: [{allowed: ['ThisExpression']}]
},
{
Expand Down Expand Up @@ -681,46 +699,6 @@ const invalid: RuleTester.InvalidTestCase[] = [
}
]
}
],

// Configurable allowed declarations
...[
{
code: `const foo = import('path');`,
errors: [
{
messageId: '0',
line: 1,
column: 7,
endLine: 1,
endColumn: 27
}
]
},
{
code: `const foo = (3, 5);`,
errors: [
{
messageId: '0',
line: 1,
column: 7,
endLine: 1,
endColumn: 19
}
]
},
{
code: `const foo = this;`,
errors: [
{
messageId: '0',
line: 1,
column: 7,
endLine: 1,
endColumn: 17
}
]
}
]
];

Expand Down

0 comments on commit 951f12b

Please sign in to comment.