Skip to content

Commit

Permalink
fix(build): avoid const enum conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 6, 2023
1 parent 39cf4cd commit d1181ad
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 38 deletions.
10 changes: 5 additions & 5 deletions packages/runtime-core/__tests__/rendererChildren.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
h,
render,
nodeOps,
NodeTypes,
TestNodeTypes,
TestElement,
serialize,
serializeInner
Expand Down Expand Up @@ -487,15 +487,15 @@ describe('renderer: unkeyed children', () => {

elm = root.children[0] as TestElement
expect(elm.children[0]).toMatchObject({
type: NodeTypes.TEXT,
type: TestNodeTypes.TEXT,
text: 'text'
})

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

elm = root.children[0] as TestElement
expect(elm.children[0]).toMatchObject({
type: NodeTypes.TEXT,
type: TestNodeTypes.TEXT,
text: 'text'
})
})
Expand All @@ -505,15 +505,15 @@ describe('renderer: unkeyed children', () => {

elm = root.children[0] as TestElement
expect(elm.children[0]).toMatchObject({
type: NodeTypes.TEXT,
type: TestNodeTypes.TEXT,
text: 'text'
})

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

elm = root.children[0] as TestElement
expect(elm.children[0]).toMatchObject({
type: NodeTypes.TEXT,
type: TestNodeTypes.TEXT,
text: 'text2'
})
})
Expand Down
12 changes: 6 additions & 6 deletions packages/runtime-core/__tests__/rendererFragment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
createVNode,
render,
nodeOps,
NodeTypes,
TestNodeTypes,
TestElement,
Fragment,
resetOps,
Expand Down Expand Up @@ -32,23 +32,23 @@ describe('renderer: fragment', () => {
expect(serializeInner(root)).toBe(`<div>one</div>two`)
expect(root.children.length).toBe(4)
expect(root.children[0]).toMatchObject({
type: NodeTypes.TEXT,
type: TestNodeTypes.TEXT,
text: ''
})
expect(root.children[1]).toMatchObject({
type: NodeTypes.ELEMENT,
type: TestNodeTypes.ELEMENT,
tag: 'div'
})
expect((root.children[1] as TestElement).children[0]).toMatchObject({
type: NodeTypes.TEXT,
type: TestNodeTypes.TEXT,
text: 'one'
})
expect(root.children[2]).toMatchObject({
type: NodeTypes.TEXT,
type: TestNodeTypes.TEXT,
text: 'two'
})
expect(root.children[3]).toMatchObject({
type: NodeTypes.TEXT,
type: TestNodeTypes.TEXT,
text: ''
})
})
Expand Down
10 changes: 5 additions & 5 deletions packages/runtime-core/__tests__/vnodeHooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
nodeOps,
VNodeProps,
TestElement,
NodeTypes,
TestNodeTypes,
VNode
} from '@vue/runtime-test'

Expand Down Expand Up @@ -45,13 +45,13 @@ describe('renderer: vnode hooks', () => {
onVnodeMounted: vi.fn(),
onVnodeBeforeUpdate: vi.fn(vnode => {
expect((vnode.el as TestElement).children[0]).toMatchObject({
type: NodeTypes.TEXT,
type: TestNodeTypes.TEXT,
text: 'foo'
})
}),
onVnodeUpdated: vi.fn(vnode => {
expect((vnode.el as TestElement).children[0]).toMatchObject({
type: NodeTypes.TEXT,
type: TestNodeTypes.TEXT,
text: 'bar'
})
}),
Expand All @@ -70,13 +70,13 @@ describe('renderer: vnode hooks', () => {
onVnodeMounted: vi.fn(),
onVnodeBeforeUpdate: vi.fn(vnode => {
expect(vnode.el as TestElement).toMatchObject({
type: NodeTypes.TEXT,
type: TestNodeTypes.TEXT,
text: 'foo'
})
}),
onVnodeUpdated: vi.fn(vnode => {
expect(vnode.el as TestElement).toMatchObject({
type: NodeTypes.TEXT,
type: TestNodeTypes.TEXT,
text: 'bar'
})
}),
Expand Down
8 changes: 4 additions & 4 deletions packages/runtime-test/__tests__/testRuntime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
h,
render,
nodeOps,
NodeTypes,
TestNodeTypes,
TestElement,
TestText,
ref,
Expand Down Expand Up @@ -32,12 +32,12 @@ describe('test renderer', () => {
expect(root.children.length).toBe(1)

const el = root.children[0] as TestElement
expect(el.type).toBe(NodeTypes.ELEMENT)
expect(el.type).toBe(TestNodeTypes.ELEMENT)
expect(el.props.id).toBe('test')
expect(el.children.length).toBe(1)

const text = el.children[0] as TestText
expect(text.type).toBe(NodeTypes.TEXT)
expect(text.type).toBe(TestNodeTypes.TEXT)
expect(text.text).toBe('hello')
})

Expand Down Expand Up @@ -68,7 +68,7 @@ describe('test renderer', () => {

expect(ops[0]).toEqual({
type: NodeOpTypes.CREATE,
nodeType: NodeTypes.ELEMENT,
nodeType: TestNodeTypes.ELEMENT,
tag: 'div',
targetNode: root.children[0]
})
Expand Down
24 changes: 12 additions & 12 deletions packages/runtime-test/src/nodeOps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { markRaw } from '@vue/reactivity'

export const enum NodeTypes {
export const enum TestNodeTypes {
TEXT = 'text',
ELEMENT = 'element',
COMMENT = 'comment'
Expand All @@ -17,7 +17,7 @@ export const enum NodeOpTypes {

export interface TestElement {
id: number
type: NodeTypes.ELEMENT
type: TestNodeTypes.ELEMENT
parentNode: TestElement | null
tag: string
children: TestNode[]
Expand All @@ -27,14 +27,14 @@ export interface TestElement {

export interface TestText {
id: number
type: NodeTypes.TEXT
type: TestNodeTypes.TEXT
parentNode: TestElement | null
text: string
}

export interface TestComment {
id: number
type: NodeTypes.COMMENT
type: TestNodeTypes.COMMENT
parentNode: TestElement | null
text: string
}
Expand All @@ -43,7 +43,7 @@ export type TestNode = TestElement | TestText | TestComment

export interface NodeOp {
type: NodeOpTypes
nodeType?: NodeTypes
nodeType?: TestNodeTypes
tag?: string
text?: string
targetNode?: TestNode
Expand Down Expand Up @@ -74,7 +74,7 @@ export function dumpOps(): NodeOp[] {
function createElement(tag: string): TestElement {
const node: TestElement = {
id: nodeId++,
type: NodeTypes.ELEMENT,
type: TestNodeTypes.ELEMENT,
tag,
children: [],
props: {},
Expand All @@ -83,7 +83,7 @@ function createElement(tag: string): TestElement {
}
logNodeOp({
type: NodeOpTypes.CREATE,
nodeType: NodeTypes.ELEMENT,
nodeType: TestNodeTypes.ELEMENT,
targetNode: node,
tag
})
Expand All @@ -95,13 +95,13 @@ function createElement(tag: string): TestElement {
function createText(text: string): TestText {
const node: TestText = {
id: nodeId++,
type: NodeTypes.TEXT,
type: TestNodeTypes.TEXT,
text,
parentNode: null
}
logNodeOp({
type: NodeOpTypes.CREATE,
nodeType: NodeTypes.TEXT,
nodeType: TestNodeTypes.TEXT,
targetNode: node,
text
})
Expand All @@ -113,13 +113,13 @@ function createText(text: string): TestText {
function createComment(text: string): TestComment {
const node: TestComment = {
id: nodeId++,
type: NodeTypes.COMMENT,
type: TestNodeTypes.COMMENT,
text,
parentNode: null
}
logNodeOp({
type: NodeOpTypes.CREATE,
nodeType: NodeTypes.COMMENT,
nodeType: TestNodeTypes.COMMENT,
targetNode: node,
text
})
Expand Down Expand Up @@ -203,7 +203,7 @@ function setElementText(el: TestElement, text: string) {
el.children = [
{
id: nodeId++,
type: NodeTypes.TEXT,
type: TestNodeTypes.TEXT,
text,
parentNode: el
}
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime-test/src/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
TestElement,
TestNode,
NodeTypes,
TestNodeTypes,
TestText,
TestComment
} from './nodeOps'
Expand All @@ -12,7 +12,7 @@ export function serialize(
indent: number = 0,
depth: number = 0
): string {
if (node.type === NodeTypes.ELEMENT) {
if (node.type === TestNodeTypes.ELEMENT) {
return serializeElement(node, indent, depth)
} else {
return serializeText(node, indent, depth)
Expand Down Expand Up @@ -64,6 +64,6 @@ function serializeText(
const padding = indent ? ` `.repeat(indent).repeat(depth) : ``
return (
padding +
(node.type === NodeTypes.COMMENT ? `<!--${node.text}-->` : node.text)
(node.type === TestNodeTypes.COMMENT ? `<!--${node.text}-->` : node.text)
)
}
12 changes: 9 additions & 3 deletions scripts/const-enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ export function scanEnums() {
}
const key = e.id.type === 'Identifier' ? e.id.name : e.id.value
const fullKey = `${id}.${key}`
const saveValue = value => {
if (fullKey in enumData.defines) {
throw new Error(`name conflict for enum ${id} in ${file}`)
}
enumData.defines[fullKey] = JSON.stringify(value)
}
const init = e.initializer
if (init) {
let value
Expand Down Expand Up @@ -138,15 +144,15 @@ export function scanEnums() {
`unhandled initializer type ${init.type} for ${fullKey} in ${file}`
)
}
enumData.defines[fullKey] = JSON.stringify(value)
saveValue(value)
lastInitialized = value
} else {
if (lastInitialized === undefined) {
// first initialized
enumData.defines[fullKey] = `0`
saveValue(`0`)
lastInitialized = 0
} else if (typeof lastInitialized === 'number') {
enumData.defines[fullKey] = String(++lastInitialized)
saveValue(String(++lastInitialized))
} else {
// should not happen
throw new Error(`wrong enum initialization sequence in ${file}`)
Expand Down

0 comments on commit d1181ad

Please sign in to comment.