Skip to content

Commit d1181ad

Browse files
committed
fix(build): avoid const enum conflicts
1 parent 39cf4cd commit d1181ad

File tree

7 files changed

+44
-38
lines changed

7 files changed

+44
-38
lines changed

packages/runtime-core/__tests__/rendererChildren.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
h,
44
render,
55
nodeOps,
6-
NodeTypes,
6+
TestNodeTypes,
77
TestElement,
88
serialize,
99
serializeInner
@@ -487,15 +487,15 @@ describe('renderer: unkeyed children', () => {
487487

488488
elm = root.children[0] as TestElement
489489
expect(elm.children[0]).toMatchObject({
490-
type: NodeTypes.TEXT,
490+
type: TestNodeTypes.TEXT,
491491
text: 'text'
492492
})
493493

494494
render(h('div', ['text', h('span', ['hello'])]), root)
495495

496496
elm = root.children[0] as TestElement
497497
expect(elm.children[0]).toMatchObject({
498-
type: NodeTypes.TEXT,
498+
type: TestNodeTypes.TEXT,
499499
text: 'text'
500500
})
501501
})
@@ -505,15 +505,15 @@ describe('renderer: unkeyed children', () => {
505505

506506
elm = root.children[0] as TestElement
507507
expect(elm.children[0]).toMatchObject({
508-
type: NodeTypes.TEXT,
508+
type: TestNodeTypes.TEXT,
509509
text: 'text'
510510
})
511511

512512
render(h('div', ['text2', h('span', ['hello'])]), root)
513513

514514
elm = root.children[0] as TestElement
515515
expect(elm.children[0]).toMatchObject({
516-
type: NodeTypes.TEXT,
516+
type: TestNodeTypes.TEXT,
517517
text: 'text2'
518518
})
519519
})

packages/runtime-core/__tests__/rendererFragment.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
createVNode,
44
render,
55
nodeOps,
6-
NodeTypes,
6+
TestNodeTypes,
77
TestElement,
88
Fragment,
99
resetOps,
@@ -32,23 +32,23 @@ describe('renderer: fragment', () => {
3232
expect(serializeInner(root)).toBe(`<div>one</div>two`)
3333
expect(root.children.length).toBe(4)
3434
expect(root.children[0]).toMatchObject({
35-
type: NodeTypes.TEXT,
35+
type: TestNodeTypes.TEXT,
3636
text: ''
3737
})
3838
expect(root.children[1]).toMatchObject({
39-
type: NodeTypes.ELEMENT,
39+
type: TestNodeTypes.ELEMENT,
4040
tag: 'div'
4141
})
4242
expect((root.children[1] as TestElement).children[0]).toMatchObject({
43-
type: NodeTypes.TEXT,
43+
type: TestNodeTypes.TEXT,
4444
text: 'one'
4545
})
4646
expect(root.children[2]).toMatchObject({
47-
type: NodeTypes.TEXT,
47+
type: TestNodeTypes.TEXT,
4848
text: 'two'
4949
})
5050
expect(root.children[3]).toMatchObject({
51-
type: NodeTypes.TEXT,
51+
type: TestNodeTypes.TEXT,
5252
text: ''
5353
})
5454
})

packages/runtime-core/__tests__/vnodeHooks.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
nodeOps,
66
VNodeProps,
77
TestElement,
8-
NodeTypes,
8+
TestNodeTypes,
99
VNode
1010
} from '@vue/runtime-test'
1111

@@ -45,13 +45,13 @@ describe('renderer: vnode hooks', () => {
4545
onVnodeMounted: vi.fn(),
4646
onVnodeBeforeUpdate: vi.fn(vnode => {
4747
expect((vnode.el as TestElement).children[0]).toMatchObject({
48-
type: NodeTypes.TEXT,
48+
type: TestNodeTypes.TEXT,
4949
text: 'foo'
5050
})
5151
}),
5252
onVnodeUpdated: vi.fn(vnode => {
5353
expect((vnode.el as TestElement).children[0]).toMatchObject({
54-
type: NodeTypes.TEXT,
54+
type: TestNodeTypes.TEXT,
5555
text: 'bar'
5656
})
5757
}),
@@ -70,13 +70,13 @@ describe('renderer: vnode hooks', () => {
7070
onVnodeMounted: vi.fn(),
7171
onVnodeBeforeUpdate: vi.fn(vnode => {
7272
expect(vnode.el as TestElement).toMatchObject({
73-
type: NodeTypes.TEXT,
73+
type: TestNodeTypes.TEXT,
7474
text: 'foo'
7575
})
7676
}),
7777
onVnodeUpdated: vi.fn(vnode => {
7878
expect(vnode.el as TestElement).toMatchObject({
79-
type: NodeTypes.TEXT,
79+
type: TestNodeTypes.TEXT,
8080
text: 'bar'
8181
})
8282
}),

packages/runtime-test/__tests__/testRuntime.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
h,
33
render,
44
nodeOps,
5-
NodeTypes,
5+
TestNodeTypes,
66
TestElement,
77
TestText,
88
ref,
@@ -32,12 +32,12 @@ describe('test renderer', () => {
3232
expect(root.children.length).toBe(1)
3333

3434
const el = root.children[0] as TestElement
35-
expect(el.type).toBe(NodeTypes.ELEMENT)
35+
expect(el.type).toBe(TestNodeTypes.ELEMENT)
3636
expect(el.props.id).toBe('test')
3737
expect(el.children.length).toBe(1)
3838

3939
const text = el.children[0] as TestText
40-
expect(text.type).toBe(NodeTypes.TEXT)
40+
expect(text.type).toBe(TestNodeTypes.TEXT)
4141
expect(text.text).toBe('hello')
4242
})
4343

@@ -68,7 +68,7 @@ describe('test renderer', () => {
6868

6969
expect(ops[0]).toEqual({
7070
type: NodeOpTypes.CREATE,
71-
nodeType: NodeTypes.ELEMENT,
71+
nodeType: TestNodeTypes.ELEMENT,
7272
tag: 'div',
7373
targetNode: root.children[0]
7474
})

packages/runtime-test/src/nodeOps.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { markRaw } from '@vue/reactivity'
22

3-
export const enum NodeTypes {
3+
export const enum TestNodeTypes {
44
TEXT = 'text',
55
ELEMENT = 'element',
66
COMMENT = 'comment'
@@ -17,7 +17,7 @@ export const enum NodeOpTypes {
1717

1818
export interface TestElement {
1919
id: number
20-
type: NodeTypes.ELEMENT
20+
type: TestNodeTypes.ELEMENT
2121
parentNode: TestElement | null
2222
tag: string
2323
children: TestNode[]
@@ -27,14 +27,14 @@ export interface TestElement {
2727

2828
export interface TestText {
2929
id: number
30-
type: NodeTypes.TEXT
30+
type: TestNodeTypes.TEXT
3131
parentNode: TestElement | null
3232
text: string
3333
}
3434

3535
export interface TestComment {
3636
id: number
37-
type: NodeTypes.COMMENT
37+
type: TestNodeTypes.COMMENT
3838
parentNode: TestElement | null
3939
text: string
4040
}
@@ -43,7 +43,7 @@ export type TestNode = TestElement | TestText | TestComment
4343

4444
export interface NodeOp {
4545
type: NodeOpTypes
46-
nodeType?: NodeTypes
46+
nodeType?: TestNodeTypes
4747
tag?: string
4848
text?: string
4949
targetNode?: TestNode
@@ -74,7 +74,7 @@ export function dumpOps(): NodeOp[] {
7474
function createElement(tag: string): TestElement {
7575
const node: TestElement = {
7676
id: nodeId++,
77-
type: NodeTypes.ELEMENT,
77+
type: TestNodeTypes.ELEMENT,
7878
tag,
7979
children: [],
8080
props: {},
@@ -83,7 +83,7 @@ function createElement(tag: string): TestElement {
8383
}
8484
logNodeOp({
8585
type: NodeOpTypes.CREATE,
86-
nodeType: NodeTypes.ELEMENT,
86+
nodeType: TestNodeTypes.ELEMENT,
8787
targetNode: node,
8888
tag
8989
})
@@ -95,13 +95,13 @@ function createElement(tag: string): TestElement {
9595
function createText(text: string): TestText {
9696
const node: TestText = {
9797
id: nodeId++,
98-
type: NodeTypes.TEXT,
98+
type: TestNodeTypes.TEXT,
9999
text,
100100
parentNode: null
101101
}
102102
logNodeOp({
103103
type: NodeOpTypes.CREATE,
104-
nodeType: NodeTypes.TEXT,
104+
nodeType: TestNodeTypes.TEXT,
105105
targetNode: node,
106106
text
107107
})
@@ -113,13 +113,13 @@ function createText(text: string): TestText {
113113
function createComment(text: string): TestComment {
114114
const node: TestComment = {
115115
id: nodeId++,
116-
type: NodeTypes.COMMENT,
116+
type: TestNodeTypes.COMMENT,
117117
text,
118118
parentNode: null
119119
}
120120
logNodeOp({
121121
type: NodeOpTypes.CREATE,
122-
nodeType: NodeTypes.COMMENT,
122+
nodeType: TestNodeTypes.COMMENT,
123123
targetNode: node,
124124
text
125125
})
@@ -203,7 +203,7 @@ function setElementText(el: TestElement, text: string) {
203203
el.children = [
204204
{
205205
id: nodeId++,
206-
type: NodeTypes.TEXT,
206+
type: TestNodeTypes.TEXT,
207207
text,
208208
parentNode: el
209209
}

packages/runtime-test/src/serialize.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
TestElement,
33
TestNode,
4-
NodeTypes,
4+
TestNodeTypes,
55
TestText,
66
TestComment
77
} from './nodeOps'
@@ -12,7 +12,7 @@ export function serialize(
1212
indent: number = 0,
1313
depth: number = 0
1414
): string {
15-
if (node.type === NodeTypes.ELEMENT) {
15+
if (node.type === TestNodeTypes.ELEMENT) {
1616
return serializeElement(node, indent, depth)
1717
} else {
1818
return serializeText(node, indent, depth)
@@ -64,6 +64,6 @@ function serializeText(
6464
const padding = indent ? ` `.repeat(indent).repeat(depth) : ``
6565
return (
6666
padding +
67-
(node.type === NodeTypes.COMMENT ? `<!--${node.text}-->` : node.text)
67+
(node.type === TestNodeTypes.COMMENT ? `<!--${node.text}-->` : node.text)
6868
)
6969
}

scripts/const-enum.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ export function scanEnums() {
8181
}
8282
const key = e.id.type === 'Identifier' ? e.id.name : e.id.value
8383
const fullKey = `${id}.${key}`
84+
const saveValue = value => {
85+
if (fullKey in enumData.defines) {
86+
throw new Error(`name conflict for enum ${id} in ${file}`)
87+
}
88+
enumData.defines[fullKey] = JSON.stringify(value)
89+
}
8490
const init = e.initializer
8591
if (init) {
8692
let value
@@ -138,15 +144,15 @@ export function scanEnums() {
138144
`unhandled initializer type ${init.type} for ${fullKey} in ${file}`
139145
)
140146
}
141-
enumData.defines[fullKey] = JSON.stringify(value)
147+
saveValue(value)
142148
lastInitialized = value
143149
} else {
144150
if (lastInitialized === undefined) {
145151
// first initialized
146-
enumData.defines[fullKey] = `0`
152+
saveValue(`0`)
147153
lastInitialized = 0
148154
} else if (typeof lastInitialized === 'number') {
149-
enumData.defines[fullKey] = String(++lastInitialized)
155+
saveValue(String(++lastInitialized))
150156
} else {
151157
// should not happen
152158
throw new Error(`wrong enum initialization sequence in ${file}`)

0 commit comments

Comments
 (0)