Skip to content

feat(typescript-estree): forbid invalid keys in EnumMember #11232

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enum Foo {
1n = 2
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enum Foo {
1 = 2
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 8 additions & 37 deletions packages/ast-spec/src/element/TSEnumMember/spec.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,15 @@
import type { AST_NODE_TYPES } from '../../ast-node-types';
import type { BaseNode } from '../../base/BaseNode';
import type { Identifier } from '../../expression/Identifier/spec';
import type { StringLiteral } from '../../expression/literal/StringLiteral/spec';
import type { Expression } from '../../unions/Expression';
import type {
PropertyNameComputed,
PropertyNameNonComputed,
} from '../../unions/PropertyName';

interface TSEnumMemberBase extends BaseNode {
export interface TSEnumMember extends BaseNode {
type: AST_NODE_TYPES.TSEnumMember;
computed: boolean;
id:
| PropertyNameComputed // this should only happen in semantically invalid code (ts error 1164)
| PropertyNameNonComputed;
id: Identifier | StringLiteral;
initializer: Expression | undefined;
/**
* @deprecated the enum member is always non-computed.
*/
computed: boolean;
}

/**
* this should only really happen in semantically invalid code (errors 1164 and 2452)
*
* @example
* ```ts
* // VALID:
* enum Foo { ['a'] }
*
* // INVALID:
* const x = 'a';
* enum Foo { [x] }
* enum Bar { ['a' + 'b'] }
* ```
*/
export interface TSEnumMemberComputedName extends TSEnumMemberBase {
computed: true;
id: PropertyNameComputed;
}

export interface TSEnumMemberNonComputedName extends TSEnumMemberBase {
computed: false;
id: PropertyNameNonComputed;
}

export type TSEnumMember =
| TSEnumMemberComputedName
| TSEnumMemberNonComputedName;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions packages/ast-spec/tests/fixtures-with-differences-errors.shot

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions packages/eslint-plugin/src/rules/naming-convention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,12 +631,10 @@ export default createRule<Options, MessageIds>({

// #region interface

'TSEnumMember[computed != true]': {
handler: (
node: TSESTree.TSEnumMemberNonComputedName,
validator,
): void => {
const id = node.id;
TSEnumMember: {
handler: (node: TSESTree.TSEnumMember, validator): void => {
// Unknown reason, can't get the correct type
const id = node.id as TSESTree.Identifier | TSESTree.StringLiteral;
Comment on lines +636 to +637
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the build script is using the published package instead of local one. I tried to use workspace:^, but still can't build.

Copy link
Member

Choose a reason for hiding this comment

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

it might be due to nx not seeing the changes to ast-spec?

const modifiers = new Set<Modifiers>();

if (requiresQuoting(id, compilerOptions.target)) {
Expand Down
Loading