mirror of
https://github.com/angular/angular.git
synced 2026-11-22 22:55:25 -05:00
build: add a noDuplicateEnumValue rule (#63483)
It caught several legitimate issues. In the cases I wasn't sure, I just disabled the rule. fixes #45843 PR Close #63483
This commit is contained in:
committed by
Jessica Janiuk
parent
ec141a322b
commit
5220cea223
@@ -21,7 +21,7 @@ export const enum RuntimeErrorCode {
|
||||
// (undocumented)
|
||||
INVALID_PIPE_ARGUMENT = 2100,
|
||||
// (undocumented)
|
||||
INVALID_TO_DATE_CONVERSION = 2302,
|
||||
INVALID_TO_DATE_CONVERSION = 2311,
|
||||
// (undocumented)
|
||||
LCP_IMG_MISSING_PRIORITY = 2955,
|
||||
// (undocumented)
|
||||
@@ -73,7 +73,7 @@ export const enum RuntimeErrorCode {
|
||||
// (undocumented)
|
||||
UNKNOWN_DATE_TYPE_VALUE = 2301,
|
||||
// (undocumented)
|
||||
UNKNOWN_ZONE_WIDTH = 2302,
|
||||
UNKNOWN_ZONE_WIDTH = 2310,
|
||||
// (undocumented)
|
||||
VALUE_NOT_A_NUMBER = 2309
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ export const enum RuntimeErrorCode {
|
||||
// (undocumented)
|
||||
INVALID_ROUTE_CONFIG = 4014,
|
||||
// (undocumented)
|
||||
INVALID_ROUTER_LINK_INPUTS = 4016,
|
||||
INVALID_ROUTER_LINK_INPUTS = 4017,
|
||||
// (undocumented)
|
||||
MISPLACED_OUTLETS_COMMAND = 4004,
|
||||
// (undocumented)
|
||||
|
||||
@@ -28,8 +28,6 @@ export const enum RuntimeErrorCode {
|
||||
SUSPICIOUS_DATE_FORMAT = 2300,
|
||||
UNKNOWN_DATE_TYPE_VALUE = 2301,
|
||||
UNEXPECTED_TRANSLATION_TYPE = 2302,
|
||||
UNKNOWN_ZONE_WIDTH = 2302,
|
||||
INVALID_TO_DATE_CONVERSION = 2302,
|
||||
MISSING_EXTRA_LOCALE_DATA_FOR_LOCALE = 2303,
|
||||
LOCALE_DATA_UNDEFINED = 2304,
|
||||
INVALID_INTEGER_LITERAL = 2305,
|
||||
@@ -37,6 +35,8 @@ export const enum RuntimeErrorCode {
|
||||
INVALID_NUMBER_OF_DIGITS_AFTER_FRACTION = 2307,
|
||||
NO_PLURAL_MESSAGE_FOUND = 2308,
|
||||
VALUE_NOT_A_NUMBER = 2309,
|
||||
UNKNOWN_ZONE_WIDTH = 2310,
|
||||
INVALID_TO_DATE_CONVERSION = 2311,
|
||||
|
||||
// Miscellaneous errors
|
||||
SCROLL_RESTORATION_UNSUPPORTED = 2400,
|
||||
|
||||
@@ -43,6 +43,8 @@ export interface I18nRemoveOpCodes extends Array<number> {
|
||||
* See: `I18nCreateOpCodes` for example of usage.
|
||||
*/
|
||||
export const enum IcuCreateOpCode {
|
||||
/* tslint:disable:no-duplicate-enum-values */
|
||||
|
||||
/**
|
||||
* Stores shift amount for bits 17-3 that contain reference index.
|
||||
*/
|
||||
@@ -72,6 +74,7 @@ export const enum IcuCreateOpCode {
|
||||
* Instruction to set the attribute of a node.
|
||||
*/
|
||||
Attr = 0b1,
|
||||
/* tslint:enable:no-duplicate-enum-values */
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,6 +129,7 @@ export interface IcuCreateOpCodes
|
||||
}
|
||||
|
||||
export const enum I18nUpdateOpCode {
|
||||
/* tslint:disable:no-duplicate-enum-values */
|
||||
/**
|
||||
* Stores shift amount for bits 17-2 that contain reference index.
|
||||
*/
|
||||
@@ -151,6 +155,7 @@ export const enum I18nUpdateOpCode {
|
||||
* Instruction to update the current ICU case.
|
||||
*/
|
||||
IcuUpdate = 0b11,
|
||||
/* tslint:enable:no-duplicate-enum-values */
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -230,6 +235,7 @@ export interface I18nCreateOpCodes extends Array<number | string>, I18nDebug {
|
||||
* See `I18nCreateOpCodes`
|
||||
*/
|
||||
export enum I18nCreateOpCode {
|
||||
/* tslint:disable:no-duplicate-enum-values */
|
||||
/**
|
||||
* Number of bits to shift index so that it can be combined with the `APPEND_EAGERLY` and
|
||||
* `COMMENT`.
|
||||
@@ -245,6 +251,7 @@ export enum I18nCreateOpCode {
|
||||
* If set the node should be comment (rather than a text) node.
|
||||
*/
|
||||
COMMENT = 0b10,
|
||||
/* tslint:enable:no-duplicate-enum-values */
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -52,9 +52,11 @@ import {LView, TData} from './view';
|
||||
* ```
|
||||
*/
|
||||
export const enum NodeInjectorOffset {
|
||||
/* tslint:disable:no-duplicate-enum-values */
|
||||
TNODE = 8,
|
||||
PARENT = 8,
|
||||
BLOOM_SIZE = 8,
|
||||
/* tslint:enable: no-duplicate-enum-values */
|
||||
SIZE = 9,
|
||||
}
|
||||
|
||||
|
||||
@@ -145,6 +145,7 @@ export const enum StylingRange {
|
||||
* have previous duplicate set. The implication is that if `width` binding becomes `null`, it is
|
||||
* necessary to defer the value to `map.width`. (Because `width` overwrites `map.width`.)
|
||||
*/
|
||||
/* tslint:disable-next-line:no-duplicate-enum-values */
|
||||
PREV_DUPLICATE = 0x02,
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,7 +19,7 @@ export const enum CharCode {
|
||||
CLOSE_PAREN = 41, // ")"
|
||||
STAR = 42, // "*"
|
||||
SLASH = 47, // "/"
|
||||
_0 = 48, // "0"
|
||||
ZERO = 48, // "0"
|
||||
_1 = 49, // "1"
|
||||
_2 = 50, // "2"
|
||||
_3 = 51, // "3"
|
||||
@@ -28,15 +28,13 @@ export const enum CharCode {
|
||||
_6 = 54, // "6"
|
||||
_7 = 55, // "7"
|
||||
_8 = 56, // "8"
|
||||
_9 = 57, // "9"
|
||||
NINE = 57, // "9"
|
||||
COLON = 58, // ":"
|
||||
DASH = 45, // "-"
|
||||
UNDERSCORE = 95, // "_"
|
||||
SEMI_COLON = 59, // ";"
|
||||
BACK_SLASH = 92, // "\\"
|
||||
AT_SIGN = 64, // "@"
|
||||
ZERO = 48, // "0"
|
||||
NINE = 57, // "9"
|
||||
A = 65, // "A"
|
||||
U = 85, // "U"
|
||||
R = 82, // "R"
|
||||
|
||||
@@ -27,5 +27,5 @@ export const enum RuntimeErrorCode {
|
||||
INVALID_ROUTE_CONFIG = 4014,
|
||||
INVALID_ROOT_URL_SEGMENT = 4015,
|
||||
INFINITE_REDIRECT = 4016,
|
||||
INVALID_ROUTER_LINK_INPUTS = 4016,
|
||||
INVALID_ROUTER_LINK_INPUTS = 4017,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google LLC All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.dev/license
|
||||
*/
|
||||
|
||||
import {RuleFailure} from 'tslint';
|
||||
import {TypedRule} from 'tslint/lib/rules';
|
||||
import ts from 'typescript';
|
||||
|
||||
/**
|
||||
* Rule that detects duplicate enum values.
|
||||
*
|
||||
* This is useful to prevent collisons on ErrorCodes.
|
||||
*
|
||||
* ```ts
|
||||
* enum E {
|
||||
* A = 1,
|
||||
* B = 1, // Wrong!
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export class Rule extends TypedRule {
|
||||
override applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[] {
|
||||
const checker = program.getTypeChecker();
|
||||
|
||||
return this.applyWithFunction(sourceFile, (ctx) => {
|
||||
ts.forEachChild(sourceFile, function walk(node) {
|
||||
if (ts.isEnumDeclaration(node)) {
|
||||
const seenValues = new Map<string | number, ts.Node>();
|
||||
for (const member of node.members) {
|
||||
const value = checker.getConstantValue(member);
|
||||
if (value !== undefined) {
|
||||
if (seenValues.has(value)) {
|
||||
ctx.addFailureAtNode(
|
||||
member,
|
||||
`Enum member has a duplicate value '${value}'. First occurrence is on line ${
|
||||
sourceFile.getLineAndCharacterOfPosition(seenValues.get(value)!.getStart())
|
||||
.line + 1
|
||||
}.`,
|
||||
);
|
||||
} else {
|
||||
seenValues.set(value, member);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ts.forEachChild(node, walk);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@
|
||||
"ts-node-loader": true,
|
||||
// Custom rules written in TypeScript.
|
||||
"no-exported-inferred-call-type": true,
|
||||
"no-duplicate-enum-values": true,
|
||||
"require-internal-with-underscore": true,
|
||||
"no-implicit-override-abstract": true,
|
||||
"validate-import-for-esm-cjs-interop": [
|
||||
|
||||
Reference in New Issue
Block a user