Skip to content

feat: use enum to replace const enum #9261

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

Merged
merged 5 commits into from
Nov 29, 2023
Merged
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
16 changes: 13 additions & 3 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -3,6 +3,15 @@
const DOMGlobals = ['window', 'document']
const NodeGlobals = ['module', 'require']

const banConstEnum = {
selector: 'TSEnumDeclaration[const=true]',
message:
'Please use non-const enums. This project automatically inlines enums.'
}

/**
* @type {import('eslint-define-config').ESLintConfig}
*/
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
@@ -16,6 +25,7 @@ module.exports = {

'no-restricted-syntax': [
'error',
banConstEnum,
// since we target ES2015 for baseline support, we need to forbid object
// rest spread usage in destructure as it compiles into a verbose helper.
'ObjectPattern > RestElement',
@@ -55,15 +65,15 @@ module.exports = {
files: ['packages/{compiler-sfc,compiler-ssr,server-renderer}/**'],
rules: {
'no-restricted-globals': ['error', ...DOMGlobals],
'no-restricted-syntax': 'off'
'no-restricted-syntax': ['error', banConstEnum]
}
},
// Private package, browser only + no syntax restrictions
{
files: ['packages/template-explorer/**', 'packages/sfc-playground/**'],
rules: {
'no-restricted-globals': ['error', ...NodeGlobals],
'no-restricted-syntax': 'off'
'no-restricted-syntax': ['error', banConstEnum]
}
},
// JavaScript files
@@ -79,7 +89,7 @@ module.exports = {
files: ['scripts/**', '*.{js,ts}', 'packages/**/index.js'],
rules: {
'no-restricted-globals': 'off',
'no-restricted-syntax': 'off'
'no-restricted-syntax': ['error', banConstEnum]
}
}
]
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -66,7 +66,9 @@
"@rollup/plugin-replace": "^5.0.4",
"@rollup/plugin-terser": "^0.4.4",
"@types/hash-sum": "^1.0.2",
"@types/minimist": "^1.2.5",
"@types/node": "^20.10.0",
"@types/semver": "^7.5.5",
"@typescript-eslint/parser": "^6.13.0",
"@vitest/coverage-istanbul": "^0.34.6",
"@vue/consolidate": "0.17.3",
@@ -75,6 +77,7 @@
"esbuild": "^0.19.5",
"esbuild-plugin-polyfill-node": "^0.3.0",
"eslint": "^8.54.0",
"eslint-define-config": "^1.24.1",
"eslint-plugin-jest": "^27.6.0",
"estree-walker": "^2.0.2",
"execa": "^8.0.1",
8 changes: 4 additions & 4 deletions packages/compiler-core/src/ast.ts
Original file line number Diff line number Diff line change
@@ -19,13 +19,13 @@ import { ImportItem, TransformContext } from './transform'
// More namespaces can be declared by platform specific compilers.
export type Namespace = number

export const enum Namespaces {
export enum Namespaces {
HTML,
SVG,
MATH_ML
}

export const enum NodeTypes {
export enum NodeTypes {
ROOT,
ELEMENT,
TEXT,
@@ -59,7 +59,7 @@ export const enum NodeTypes {
JS_RETURN_STATEMENT
}

export const enum ElementTypes {
export enum ElementTypes {
ELEMENT,
COMPONENT,
SLOT,
@@ -214,7 +214,7 @@ export interface DirectiveNode extends Node {
* Higher levels implies lower levels. e.g. a node that can be stringified
* can always be hoisted and skipped for patch.
*/
export const enum ConstantTypes {
export enum ConstantTypes {
NOT_CONSTANT = 0,
CAN_SKIP_PATCH,
CAN_HOIST,
2 changes: 1 addition & 1 deletion packages/compiler-core/src/codegen.ts
Original file line number Diff line number Diff line change
@@ -69,7 +69,7 @@ export interface CodegenResult {
map?: RawSourceMap
}

const enum NewlineType {
enum NewlineType {
Start = 0,
End = -1,
None = -2,
2 changes: 1 addition & 1 deletion packages/compiler-core/src/compat/compatConfig.ts
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ export interface CompilerCompatOptions {
compatConfig?: CompilerCompatConfig
}

export const enum CompilerDeprecationTypes {
export enum CompilerDeprecationTypes {
COMPILER_IS_ON_ELEMENT = 'COMPILER_IS_ON_ELEMENT',
COMPILER_V_BIND_SYNC = 'COMPILER_V_BIND_SYNC',
COMPILER_V_BIND_OBJECT_ORDER = 'COMPILER_V_BIND_OBJECT_ORDER',
2 changes: 1 addition & 1 deletion packages/compiler-core/src/errors.ts
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ export function createCompilerError<T extends number>(
return error
}

export const enum ErrorCodes {
export enum ErrorCodes {
// parse errors
ABRUPT_CLOSING_OF_EMPTY_COMMENT,
CDATA_IN_HTML_CONTENT,
2 changes: 1 addition & 1 deletion packages/compiler-core/src/options.ts
Original file line number Diff line number Diff line change
@@ -94,7 +94,7 @@ export type HoistTransform = (
parent: ParentNode
) => void

export const enum BindingTypes {
export enum BindingTypes {
/**
* returned from data()
*/
9 changes: 4 additions & 5 deletions packages/compiler-core/src/tokenizer.ts
Original file line number Diff line number Diff line change
@@ -38,13 +38,13 @@ import {
fromCodePoint
} from 'entities/lib/decode.js'

export const enum ParseMode {
export enum ParseMode {
BASE,
HTML,
SFC
}

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

/** All the states the tokenizer can be in. */
export const enum State {
export enum State {
Text = 1,

// interpolation
@@ -820,7 +819,7 @@ export default class Tokenizer {
}
}
private stateBeforeDeclaration(c: number): void {
if (c === CharCodes.OpeningSquareBracket) {
if (c === CharCodes.LeftSqaure) {
this.state = State.CDATASequence
this.sequenceIndex = 0
} else {
2 changes: 1 addition & 1 deletion packages/compiler-core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ const nonIdentifierRE = /^\d|[^\$\w]/
export const isSimpleIdentifier = (name: string): boolean =>
!nonIdentifierRE.test(name)

const enum MemberExpLexState {
enum MemberExpLexState {
inMemberExp,
inBrackets,
inParens,
4 changes: 2 additions & 2 deletions packages/compiler-dom/src/errors.ts
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ export function createDOMCompilerError(
) as DOMCompilerError
}

export const enum DOMErrorCodes {
export enum DOMErrorCodes {
X_V_HTML_NO_EXPRESSION = 53 /* ErrorCodes.__EXTEND_POINT__ */,
X_V_HTML_WITH_CHILDREN,
X_V_TEXT_NO_EXPRESSION,
@@ -36,7 +36,7 @@ export const enum DOMErrorCodes {
}

if (__TEST__) {
// esbuild cannot infer const enum increments if first value is from another
// esbuild cannot infer enum increments if first value is from another
// file, so we have to manually keep them in sync. this check ensures it
// errors out if there are collisions.
if (DOMErrorCodes.X_V_HTML_NO_EXPRESSION < ErrorCodes.__EXTEND_POINT__) {
2 changes: 1 addition & 1 deletion packages/compiler-dom/src/transforms/stringifyStatic.ts
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ import {
isBooleanAttr
} from '@vue/shared'

export const enum StringifyThresholds {
export enum StringifyThresholds {
ELEMENT_WITH_BINDING_COUNT = 5,
NODE_COUNT = 20
}
2 changes: 1 addition & 1 deletion packages/compiler-sfc/src/style/cssVars.ts
Original file line number Diff line number Diff line change
@@ -70,7 +70,7 @@ export function parseCssVars(sfc: SFCDescriptor): string[] {
return vars
}

const enum LexerState {
enum LexerState {
inParens,
inSingleQuoteString,
inDoubleQuoteString
4 changes: 2 additions & 2 deletions packages/compiler-ssr/src/errors.ts
Original file line number Diff line number Diff line change
@@ -16,14 +16,14 @@ export function createSSRCompilerError(
return createCompilerError(code, loc, SSRErrorMessages) as SSRCompilerError
}

export const enum SSRErrorCodes {
export enum SSRErrorCodes {
X_SSR_UNSAFE_ATTR_NAME = 65 /* DOMErrorCodes.__EXTEND_POINT__ */,
X_SSR_NO_TELEPORT_TARGET,
X_SSR_INVALID_AST_NODE
}

if (__TEST__) {
// esbuild cannot infer const enum increments if first value is from another
// esbuild cannot infer enum increments if first value is from another
// file, so we have to manually keep them in sync. this check ensures it
// errors out if there are collisions.
if (SSRErrorCodes.X_SSR_UNSAFE_ATTR_NAME < DOMErrorCodes.__EXTEND_POINT__) {
8 changes: 4 additions & 4 deletions packages/reactivity/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
// using literal strings instead of numbers so that it's easier to inspect
// debugger events

export const enum TrackOpTypes {
export enum TrackOpTypes {
GET = 'get',
HAS = 'has',
ITERATE = 'iterate'
}

export const enum TriggerOpTypes {
export enum TriggerOpTypes {
SET = 'set',
ADD = 'add',
DELETE = 'delete',
CLEAR = 'clear'
}

export const enum ReactiveFlags {
export enum ReactiveFlags {
SKIP = '__v_skip',
IS_REACTIVE = '__v_isReactive',
IS_READONLY = '__v_isReadonly',
IS_SHALLOW = '__v_isShallow',
RAW = '__v_raw'
}

export const enum DirtyLevels {
export enum DirtyLevels {
NotDirty = 0,
ComputedValueMaybeDirty = 1,
ComputedValueDirty = 2,
6 changes: 1 addition & 5 deletions packages/reactivity/src/index.ts
Original file line number Diff line number Diff line change
@@ -68,8 +68,4 @@ export {
getCurrentScope,
onScopeDispose
} from './effectScope'
export {
TrackOpTypes /* @remove */,
TriggerOpTypes /* @remove */,
ReactiveFlags /* @remove */
} from './constants'
export { TrackOpTypes, TriggerOpTypes, ReactiveFlags } from './constants'
2 changes: 1 addition & 1 deletion packages/reactivity/src/reactive.ts
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ export const shallowReactiveMap = new WeakMap<Target, any>()
export const readonlyMap = new WeakMap<Target, any>()
export const shallowReadonlyMap = new WeakMap<Target, any>()

const enum TargetType {
enum TargetType {
INVALID = 0,
COMMON = 1,
COLLECTION = 2
2 changes: 1 addition & 1 deletion packages/runtime-core/src/compat/compatConfig.ts
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ import {
} from '../component'
import { warn } from '../warning'

export const enum DeprecationTypes {
export enum DeprecationTypes {
GLOBAL_MOUNT = 'GLOBAL_MOUNT',
GLOBAL_MOUNT_CONTAINER = 'GLOBAL_MOUNT_CONTAINER',
GLOBAL_EXTEND = 'GLOBAL_EXTEND',
2 changes: 1 addition & 1 deletion packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
@@ -586,7 +586,7 @@ export type OptionTypesType<
Defaults: Defaults
}

const enum OptionTypes {
enum OptionTypes {
PROPS = 'Props',
DATA = 'Data',
COMPUTED = 'Computed',
2 changes: 1 addition & 1 deletion packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
@@ -164,7 +164,7 @@ export type ExtractPublicPropTypes<O> = {
[K in keyof Pick<O, PublicOptionalKeys<O>>]?: InferPropType<O[K]>
}

const enum BooleanFlags {
enum BooleanFlags {
shouldCast,
shouldCastTrue
}
2 changes: 1 addition & 1 deletion packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
@@ -281,7 +281,7 @@ if (__COMPAT__) {
installCompatInstanceProperties(publicPropertiesMap)
}

const enum AccessTypes {
enum AccessTypes {
OTHER,
SETUP,
DATA,
2 changes: 1 addition & 1 deletion packages/runtime-core/src/components/Teleport.ts
Original file line number Diff line number Diff line change
@@ -269,7 +269,7 @@ export const TeleportImpl = {
hydrate: hydrateTeleport
}

export const enum TeleportMoveTypes {
export enum TeleportMoveTypes {
TARGET_CHANGE,
TOGGLE, // enable / disable
REORDER // moved in the main view
2 changes: 1 addition & 1 deletion packages/runtime-core/src/devtools.ts
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ interface AppRecord {
types: Record<string, string | Symbol>
}

const enum DevtoolsHooks {
enum DevtoolsHooks {
APP_INIT = 'app:init',
APP_UNMOUNT = 'app:unmount',
COMPONENT_UPDATED = 'component:updated',
2 changes: 1 addition & 1 deletion packages/runtime-core/src/enums.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const enum LifecycleHooks {
export enum LifecycleHooks {
BEFORE_CREATE = 'bc',
CREATED = 'c',
BEFORE_MOUNT = 'bm',
2 changes: 1 addition & 1 deletion packages/runtime-core/src/errorHandling.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import { LifecycleHooks } from './enums'

// contexts where user provided function may be executed, in addition to
// lifecycle hooks.
export const enum ErrorCodes {
export enum ErrorCodes {
SETUP_FUNCTION,
RENDER_FUNCTION,
WATCH_GETTER,
2 changes: 1 addition & 1 deletion packages/runtime-core/src/hydration.ts
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ export type RootHydrateFunction = (
container: (Element | ShadowRoot) & { _vnode?: VNode }
) => void

const enum DOMNodeTypes {
enum DOMNodeTypes {
ELEMENT = 1,
TEXT = 3,
COMMENT = 8
6 changes: 5 additions & 1 deletion packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -361,7 +361,7 @@ export const ssrUtils = (__SSR__ ? _ssrUtils : null) as typeof _ssrUtils

// 2.x COMPAT ------------------------------------------------------------------

export { DeprecationTypes } from './compat/compatConfig'
import { DeprecationTypes as _DeprecationTypes } from './compat/compatConfig'
export type { CompatVue } from './compat/global'
export type { LegacyConfig } from './compat/globalConfig'

@@ -393,3 +393,7 @@ const _compatUtils = {
export const compatUtils = (
__COMPAT__ ? _compatUtils : null
) as typeof _compatUtils

export const DeprecationTypes = (
__COMPAT__ ? _DeprecationTypes : null
) as typeof _DeprecationTypes
2 changes: 1 addition & 1 deletion packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
@@ -265,7 +265,7 @@ export type SetupRenderEffectFn = (
optimized: boolean
) => void

export const enum MoveType {
export enum MoveType {
ENTER,
LEAVE,
REORDER
Loading