Skip to content

Commit fff7b86

Browse files
authored
feat: use enum to replace const enum (#9261)
close #1228
1 parent 47ea285 commit fff7b86

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+397
-333
lines changed

.eslintrc.cjs

+13-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
const DOMGlobals = ['window', 'document']
44
const NodeGlobals = ['module', 'require']
55

6+
const banConstEnum = {
7+
selector: 'TSEnumDeclaration[const=true]',
8+
message:
9+
'Please use non-const enums. This project automatically inlines enums.'
10+
}
11+
12+
/**
13+
* @type {import('eslint-define-config').ESLintConfig}
14+
*/
615
module.exports = {
716
parser: '@typescript-eslint/parser',
817
parserOptions: {
@@ -16,6 +25,7 @@ module.exports = {
1625

1726
'no-restricted-syntax': [
1827
'error',
28+
banConstEnum,
1929
// since we target ES2015 for baseline support, we need to forbid object
2030
// rest spread usage in destructure as it compiles into a verbose helper.
2131
'ObjectPattern > RestElement',
@@ -55,15 +65,15 @@ module.exports = {
5565
files: ['packages/{compiler-sfc,compiler-ssr,server-renderer}/**'],
5666
rules: {
5767
'no-restricted-globals': ['error', ...DOMGlobals],
58-
'no-restricted-syntax': 'off'
68+
'no-restricted-syntax': ['error', banConstEnum]
5969
}
6070
},
6171
// Private package, browser only + no syntax restrictions
6272
{
6373
files: ['packages/template-explorer/**', 'packages/sfc-playground/**'],
6474
rules: {
6575
'no-restricted-globals': ['error', ...NodeGlobals],
66-
'no-restricted-syntax': 'off'
76+
'no-restricted-syntax': ['error', banConstEnum]
6777
}
6878
},
6979
// JavaScript files
@@ -79,7 +89,7 @@ module.exports = {
7989
files: ['scripts/**', '*.{js,ts}', 'packages/**/index.js'],
8090
rules: {
8191
'no-restricted-globals': 'off',
82-
'no-restricted-syntax': 'off'
92+
'no-restricted-syntax': ['error', banConstEnum]
8393
}
8494
}
8595
]

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@
6666
"@rollup/plugin-replace": "^5.0.4",
6767
"@rollup/plugin-terser": "^0.4.4",
6868
"@types/hash-sum": "^1.0.2",
69+
"@types/minimist": "^1.2.5",
6970
"@types/node": "^20.10.0",
71+
"@types/semver": "^7.5.5",
7072
"@typescript-eslint/parser": "^6.13.0",
7173
"@vitest/coverage-istanbul": "^0.34.6",
7274
"@vue/consolidate": "0.17.3",
@@ -75,6 +77,7 @@
7577
"esbuild": "^0.19.5",
7678
"esbuild-plugin-polyfill-node": "^0.3.0",
7779
"eslint": "^8.54.0",
80+
"eslint-define-config": "^1.24.1",
7881
"eslint-plugin-jest": "^27.6.0",
7982
"estree-walker": "^2.0.2",
8083
"execa": "^8.0.1",

packages/compiler-core/src/ast.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ import { ImportItem, TransformContext } from './transform'
1919
// More namespaces can be declared by platform specific compilers.
2020
export type Namespace = number
2121

22-
export const enum Namespaces {
22+
export enum Namespaces {
2323
HTML,
2424
SVG,
2525
MATH_ML
2626
}
2727

28-
export const enum NodeTypes {
28+
export enum NodeTypes {
2929
ROOT,
3030
ELEMENT,
3131
TEXT,
@@ -59,7 +59,7 @@ export const enum NodeTypes {
5959
JS_RETURN_STATEMENT
6060
}
6161

62-
export const enum ElementTypes {
62+
export enum ElementTypes {
6363
ELEMENT,
6464
COMPONENT,
6565
SLOT,
@@ -214,7 +214,7 @@ export interface DirectiveNode extends Node {
214214
* Higher levels implies lower levels. e.g. a node that can be stringified
215215
* can always be hoisted and skipped for patch.
216216
*/
217-
export const enum ConstantTypes {
217+
export enum ConstantTypes {
218218
NOT_CONSTANT = 0,
219219
CAN_SKIP_PATCH,
220220
CAN_HOIST,

packages/compiler-core/src/codegen.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export interface CodegenResult {
6969
map?: RawSourceMap
7070
}
7171

72-
const enum NewlineType {
72+
enum NewlineType {
7373
Start = 0,
7474
End = -1,
7575
None = -2,

packages/compiler-core/src/compat/compatConfig.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface CompilerCompatOptions {
1313
compatConfig?: CompilerCompatConfig
1414
}
1515

16-
export const enum CompilerDeprecationTypes {
16+
export enum CompilerDeprecationTypes {
1717
COMPILER_IS_ON_ELEMENT = 'COMPILER_IS_ON_ELEMENT',
1818
COMPILER_V_BIND_SYNC = 'COMPILER_V_BIND_SYNC',
1919
COMPILER_V_BIND_OBJECT_ORDER = 'COMPILER_V_BIND_OBJECT_ORDER',

packages/compiler-core/src/errors.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function createCompilerError<T extends number>(
3737
return error
3838
}
3939

40-
export const enum ErrorCodes {
40+
export enum ErrorCodes {
4141
// parse errors
4242
ABRUPT_CLOSING_OF_EMPTY_COMMENT,
4343
CDATA_IN_HTML_CONTENT,

packages/compiler-core/src/options.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export type HoistTransform = (
9494
parent: ParentNode
9595
) => void
9696

97-
export const enum BindingTypes {
97+
export enum BindingTypes {
9898
/**
9999
* returned from data()
100100
*/

packages/compiler-core/src/tokenizer.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ import {
3838
fromCodePoint
3939
} from 'entities/lib/decode.js'
4040

41-
export const enum ParseMode {
41+
export enum ParseMode {
4242
BASE,
4343
HTML,
4444
SFC
4545
}
4646

47-
export const enum CharCodes {
47+
export enum CharCodes {
4848
Tab = 0x9, // "\t"
4949
NewLine = 0xa, // "\n"
5050
FormFeed = 0xc, // "\f"
@@ -72,7 +72,6 @@ export const enum CharCodes {
7272
UpperZ = 0x5a, // "Z"
7373
LowerZ = 0x7a, // "z"
7474
LowerX = 0x78, // "x"
75-
OpeningSquareBracket = 0x5b, // "["
7675
LowerV = 0x76, // "v"
7776
Dot = 0x2e, // "."
7877
Colon = 0x3a, // ":"
@@ -85,7 +84,7 @@ const defaultDelimitersOpen = new Uint8Array([123, 123]) // "{{"
8584
const defaultDelimitersClose = new Uint8Array([125, 125]) // "}}"
8685

8786
/** All the states the tokenizer can be in. */
88-
export const enum State {
87+
export enum State {
8988
Text = 1,
9089

9190
// interpolation
@@ -820,7 +819,7 @@ export default class Tokenizer {
820819
}
821820
}
822821
private stateBeforeDeclaration(c: number): void {
823-
if (c === CharCodes.OpeningSquareBracket) {
822+
if (c === CharCodes.LeftSqaure) {
824823
this.state = State.CDATASequence
825824
this.sequenceIndex = 0
826825
} else {

packages/compiler-core/src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const nonIdentifierRE = /^\d|[^\$\w]/
6565
export const isSimpleIdentifier = (name: string): boolean =>
6666
!nonIdentifierRE.test(name)
6767

68-
const enum MemberExpLexState {
68+
enum MemberExpLexState {
6969
inMemberExp,
7070
inBrackets,
7171
inParens,

packages/compiler-dom/src/errors.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function createDOMCompilerError(
2020
) as DOMCompilerError
2121
}
2222

23-
export const enum DOMErrorCodes {
23+
export enum DOMErrorCodes {
2424
X_V_HTML_NO_EXPRESSION = 53 /* ErrorCodes.__EXTEND_POINT__ */,
2525
X_V_HTML_WITH_CHILDREN,
2626
X_V_TEXT_NO_EXPRESSION,
@@ -36,7 +36,7 @@ export const enum DOMErrorCodes {
3636
}
3737

3838
if (__TEST__) {
39-
// esbuild cannot infer const enum increments if first value is from another
39+
// esbuild cannot infer enum increments if first value is from another
4040
// file, so we have to manually keep them in sync. this check ensures it
4141
// errors out if there are collisions.
4242
if (DOMErrorCodes.X_V_HTML_NO_EXPRESSION < ErrorCodes.__EXTEND_POINT__) {

packages/compiler-dom/src/transforms/stringifyStatic.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
isBooleanAttr
3434
} from '@vue/shared'
3535

36-
export const enum StringifyThresholds {
36+
export enum StringifyThresholds {
3737
ELEMENT_WITH_BINDING_COUNT = 5,
3838
NODE_COUNT = 20
3939
}

packages/compiler-sfc/src/style/cssVars.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function parseCssVars(sfc: SFCDescriptor): string[] {
7070
return vars
7171
}
7272

73-
const enum LexerState {
73+
enum LexerState {
7474
inParens,
7575
inSingleQuoteString,
7676
inDoubleQuoteString

packages/compiler-ssr/src/errors.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ export function createSSRCompilerError(
1616
return createCompilerError(code, loc, SSRErrorMessages) as SSRCompilerError
1717
}
1818

19-
export const enum SSRErrorCodes {
19+
export enum SSRErrorCodes {
2020
X_SSR_UNSAFE_ATTR_NAME = 65 /* DOMErrorCodes.__EXTEND_POINT__ */,
2121
X_SSR_NO_TELEPORT_TARGET,
2222
X_SSR_INVALID_AST_NODE
2323
}
2424

2525
if (__TEST__) {
26-
// esbuild cannot infer const enum increments if first value is from another
26+
// esbuild cannot infer enum increments if first value is from another
2727
// file, so we have to manually keep them in sync. this check ensures it
2828
// errors out if there are collisions.
2929
if (SSRErrorCodes.X_SSR_UNSAFE_ATTR_NAME < DOMErrorCodes.__EXTEND_POINT__) {

packages/reactivity/src/constants.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
// using literal strings instead of numbers so that it's easier to inspect
22
// debugger events
33

4-
export const enum TrackOpTypes {
4+
export enum TrackOpTypes {
55
GET = 'get',
66
HAS = 'has',
77
ITERATE = 'iterate'
88
}
99

10-
export const enum TriggerOpTypes {
10+
export enum TriggerOpTypes {
1111
SET = 'set',
1212
ADD = 'add',
1313
DELETE = 'delete',
1414
CLEAR = 'clear'
1515
}
1616

17-
export const enum ReactiveFlags {
17+
export enum ReactiveFlags {
1818
SKIP = '__v_skip',
1919
IS_REACTIVE = '__v_isReactive',
2020
IS_READONLY = '__v_isReadonly',
2121
IS_SHALLOW = '__v_isShallow',
2222
RAW = '__v_raw'
2323
}
2424

25-
export const enum DirtyLevels {
25+
export enum DirtyLevels {
2626
NotDirty = 0,
2727
ComputedValueMaybeDirty = 1,
2828
ComputedValueDirty = 2,

packages/reactivity/src/index.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,4 @@ export {
6868
getCurrentScope,
6969
onScopeDispose
7070
} from './effectScope'
71-
export {
72-
TrackOpTypes /* @remove */,
73-
TriggerOpTypes /* @remove */,
74-
ReactiveFlags /* @remove */
75-
} from './constants'
71+
export { TrackOpTypes, TriggerOpTypes, ReactiveFlags } from './constants'

packages/reactivity/src/reactive.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const shallowReactiveMap = new WeakMap<Target, any>()
2727
export const readonlyMap = new WeakMap<Target, any>()
2828
export const shallowReadonlyMap = new WeakMap<Target, any>()
2929

30-
const enum TargetType {
30+
enum TargetType {
3131
INVALID = 0,
3232
COMMON = 1,
3333
COLLECTION = 2

packages/runtime-core/src/compat/compatConfig.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from '../component'
1111
import { warn } from '../warning'
1212

13-
export const enum DeprecationTypes {
13+
export enum DeprecationTypes {
1414
GLOBAL_MOUNT = 'GLOBAL_MOUNT',
1515
GLOBAL_MOUNT_CONTAINER = 'GLOBAL_MOUNT_CONTAINER',
1616
GLOBAL_EXTEND = 'GLOBAL_EXTEND',

packages/runtime-core/src/componentOptions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ export type OptionTypesType<
586586
Defaults: Defaults
587587
}
588588

589-
const enum OptionTypes {
589+
enum OptionTypes {
590590
PROPS = 'Props',
591591
DATA = 'Data',
592592
COMPUTED = 'Computed',

packages/runtime-core/src/componentProps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export type ExtractPublicPropTypes<O> = {
164164
[K in keyof Pick<O, PublicOptionalKeys<O>>]?: InferPropType<O[K]>
165165
}
166166

167-
const enum BooleanFlags {
167+
enum BooleanFlags {
168168
shouldCast,
169169
shouldCastTrue
170170
}

packages/runtime-core/src/componentPublicInstance.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ if (__COMPAT__) {
281281
installCompatInstanceProperties(publicPropertiesMap)
282282
}
283283

284-
const enum AccessTypes {
284+
enum AccessTypes {
285285
OTHER,
286286
SETUP,
287287
DATA,

packages/runtime-core/src/components/Teleport.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ export const TeleportImpl = {
269269
hydrate: hydrateTeleport
270270
}
271271

272-
export const enum TeleportMoveTypes {
272+
export enum TeleportMoveTypes {
273273
TARGET_CHANGE,
274274
TOGGLE, // enable / disable
275275
REORDER // moved in the main view

packages/runtime-core/src/devtools.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface AppRecord {
1010
types: Record<string, string | Symbol>
1111
}
1212

13-
const enum DevtoolsHooks {
13+
enum DevtoolsHooks {
1414
APP_INIT = 'app:init',
1515
APP_UNMOUNT = 'app:unmount',
1616
COMPONENT_UPDATED = 'component:updated',

packages/runtime-core/src/enums.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const enum LifecycleHooks {
1+
export enum LifecycleHooks {
22
BEFORE_CREATE = 'bc',
33
CREATED = 'c',
44
BEFORE_MOUNT = 'bm',

packages/runtime-core/src/errorHandling.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { LifecycleHooks } from './enums'
66

77
// contexts where user provided function may be executed, in addition to
88
// lifecycle hooks.
9-
export const enum ErrorCodes {
9+
export enum ErrorCodes {
1010
SETUP_FUNCTION,
1111
RENDER_FUNCTION,
1212
WATCH_GETTER,

packages/runtime-core/src/hydration.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export type RootHydrateFunction = (
3030
container: (Element | ShadowRoot) & { _vnode?: VNode }
3131
) => void
3232

33-
const enum DOMNodeTypes {
33+
enum DOMNodeTypes {
3434
ELEMENT = 1,
3535
TEXT = 3,
3636
COMMENT = 8

packages/runtime-core/src/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ export const ssrUtils = (__SSR__ ? _ssrUtils : null) as typeof _ssrUtils
361361

362362
// 2.x COMPAT ------------------------------------------------------------------
363363

364-
export { DeprecationTypes } from './compat/compatConfig'
364+
import { DeprecationTypes as _DeprecationTypes } from './compat/compatConfig'
365365
export type { CompatVue } from './compat/global'
366366
export type { LegacyConfig } from './compat/globalConfig'
367367

@@ -393,3 +393,7 @@ const _compatUtils = {
393393
export const compatUtils = (
394394
__COMPAT__ ? _compatUtils : null
395395
) as typeof _compatUtils
396+
397+
export const DeprecationTypes = (
398+
__COMPAT__ ? _DeprecationTypes : null
399+
) as typeof _DeprecationTypes

packages/runtime-core/src/renderer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export type SetupRenderEffectFn = (
265265
optimized: boolean
266266
) => void
267267

268-
export const enum MoveType {
268+
export enum MoveType {
269269
ENTER,
270270
LEAVE,
271271
REORDER

0 commit comments

Comments
 (0)