@typescript-eslint/eslint-plugin 8.68.1-alpha.5 → 8.68.1-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -62,14 +62,40 @@ exports.default = (0, util_1.createRule)({
62
62
  create(context) {
63
63
  const parserServices = (0, util_1.getParserServices)(context);
64
64
  const typeChecker = parserServices.program.getTypeChecker();
65
+ function getModuleName(id) {
66
+ if (id.type === utils_1.AST_NODE_TYPES.Literal) {
67
+ return JSON.stringify(id.value);
68
+ }
69
+ let qualifiers = '';
70
+ let current = id;
71
+ while (current.type === utils_1.AST_NODE_TYPES.TSQualifiedName) {
72
+ qualifiers = `.${current.right.name}${qualifiers}`;
73
+ current = current.left;
74
+ }
75
+ return context.sourceCode.getText(current) + qualifiers;
76
+ }
77
+ function getMergedScopes(scope) {
78
+ const { block } = scope;
79
+ if (block.type !== utils_1.AST_NODE_TYPES.TSModuleDeclaration) {
80
+ return [scope];
81
+ }
82
+ const name = getModuleName(block.id);
83
+ return getEnclosingScopes(block).flatMap(enclosing => enclosing.childScopes.filter(childScope => childScope.block.type === utils_1.AST_NODE_TYPES.TSModuleDeclaration &&
84
+ getModuleName(childScope.block.id) === name));
85
+ }
86
+ function getEnclosingScopes(node) {
87
+ const enclosing = (0, util_1.nullThrows)(context.sourceCode.getScope(node).upper, util_1.NullThrowsReasons.MissingParent);
88
+ return node.parent.type === utils_1.AST_NODE_TYPES.ExportNamedDeclaration
89
+ ? getMergedScopes(enclosing)
90
+ : [enclosing];
91
+ }
65
92
  function collectNodeDefinitions(node) {
66
93
  const { name } = node.id;
67
94
  const found = {
68
95
  imports: [],
69
96
  previousSibling: undefined,
70
97
  };
71
- let scope = context.sourceCode.getScope(node);
72
- for (const definition of scope.upper?.set.get(name)?.defs ?? []) {
98
+ for (const definition of getEnclosingScopes(node).flatMap(enclosing => enclosing.set.get(name)?.defs ?? [])) {
73
99
  if (definition.node.type === utils_1.AST_NODE_TYPES.TSEnumDeclaration &&
74
100
  definition.node.range[0] < node.range[0] &&
75
101
  definition.node.body.members.length > 0) {
@@ -77,6 +103,7 @@ exports.default = (0, util_1.createRule)({
77
103
  break;
78
104
  }
79
105
  }
106
+ let scope = context.sourceCode.getScope(node);
80
107
  while (scope) {
81
108
  scope.set.get(name)?.defs.forEach(definition => {
82
109
  if (definition.type === scope_manager_1.DefinitionType.ImportBinding) {
@@ -138,33 +165,16 @@ exports.default = (0, util_1.createRule)({
138
165
  // Case: Multiple enum declarations in the same file
139
166
  // enum MyEnum { A }
140
167
  // enum MyEnum { B }
141
- if (previousSibling) {
142
- return getMemberType(previousSibling.body.members[0]);
143
- }
144
- // Case: Namespace declaration merging
168
+ //
169
+ // ...including ones merged across namespace declarations
145
170
  // namespace MyNamespace {
146
171
  // export enum MyEnum { A }
147
172
  // }
148
173
  // namespace MyNamespace {
149
174
  // export enum MyEnum { B }
150
175
  // }
151
- if (node.parent.type === utils_1.AST_NODE_TYPES.ExportNamedDeclaration &&
152
- node.parent.parent.type === utils_1.AST_NODE_TYPES.TSModuleBlock) {
153
- // https://github.com/typescript-eslint/typescript-eslint/issues/8352
154
- // TODO: We don't need to dip into the TypeScript type checker here!
155
- // Merged namespaces must all exist in the same file.
156
- // We could instead compare this file's nodes to find the merges.
157
- const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node.id);
158
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
159
- const declarations = typeChecker
160
- .getSymbolAtLocation(tsNode)
161
- .getDeclarations();
162
- const [{ initializer }] = declarations[0]
163
- .members;
164
- return initializer &&
165
- tsutils.isTypeFlagSet(typeChecker.getTypeAtLocation(initializer), ts.TypeFlags.StringLike)
166
- ? AllowedType.String
167
- : AllowedType.Number;
176
+ if (previousSibling) {
177
+ return getMemberType(previousSibling.body.members[0]);
168
178
  }
169
179
  // Finally, we default to the type of the first enum member
170
180
  return getMemberType(node.body.members[0]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typescript-eslint/eslint-plugin",
3
- "version": "8.68.1-alpha.5",
3
+ "version": "8.68.1-alpha.6",
4
4
  "description": "TypeScript plugin for ESLint",
5
5
  "files": [
6
6
  "dist",
@@ -47,10 +47,10 @@
47
47
  ],
48
48
  "dependencies": {
49
49
  "@eslint-community/regexpp": "^4.12.2",
50
- "@typescript-eslint/scope-manager": "8.68.1-alpha.5",
51
- "@typescript-eslint/type-utils": "8.68.1-alpha.5",
52
- "@typescript-eslint/utils": "8.68.1-alpha.5",
53
- "@typescript-eslint/visitor-keys": "8.68.1-alpha.5",
50
+ "@typescript-eslint/scope-manager": "8.68.1-alpha.6",
51
+ "@typescript-eslint/type-utils": "8.68.1-alpha.6",
52
+ "@typescript-eslint/utils": "8.68.1-alpha.6",
53
+ "@typescript-eslint/visitor-keys": "8.68.1-alpha.6",
54
54
  "ignore": "^7.0.5",
55
55
  "natural-compare": "^1.4.0",
56
56
  "ts-api-utils": "^2.5.0"
@@ -60,8 +60,8 @@
60
60
  "@types/mdast": "^4.0.4",
61
61
  "@types/natural-compare": "^1.4.3",
62
62
  "@types/react": "^18.3.21",
63
- "@typescript-eslint/rule-schema-to-typescript-types": "8.68.1-alpha.5",
64
- "@typescript-eslint/rule-tester": "8.68.1-alpha.5",
63
+ "@typescript-eslint/rule-schema-to-typescript-types": "8.68.1-alpha.6",
64
+ "@typescript-eslint/rule-tester": "8.68.1-alpha.6",
65
65
  "@typescript/native": "npm:typescript@^7.0.2",
66
66
  "@vitest/coverage-v8": "^4.0.18",
67
67
  "ajv": "^6.12.6",
@@ -81,7 +81,7 @@
81
81
  "vitest": "^4.0.18"
82
82
  },
83
83
  "peerDependencies": {
84
- "@typescript-eslint/parser": "^8.68.1-alpha.5",
84
+ "@typescript-eslint/parser": "^8.68.1-alpha.6",
85
85
  "eslint": "^8.57.0 || ^9.0.0 || ^10.0.0",
86
86
  "typescript": ">=4.8.4 <6.1.0"
87
87
  },