fix(build): avoid const enum conflicts

This commit is contained in:
Evan You 2023-02-06 09:44:12 +08:00
parent 39cf4cd30b
commit d1181ad692
7 changed files with 44 additions and 38 deletions

View File

@ -3,7 +3,7 @@ import {
h, h,
render, render,
nodeOps, nodeOps,
NodeTypes, TestNodeTypes,
TestElement, TestElement,
serialize, serialize,
serializeInner serializeInner
@ -487,7 +487,7 @@ describe('renderer: unkeyed children', () => {
elm = root.children[0] as TestElement elm = root.children[0] as TestElement
expect(elm.children[0]).toMatchObject({ expect(elm.children[0]).toMatchObject({
type: NodeTypes.TEXT, type: TestNodeTypes.TEXT,
text: 'text' text: 'text'
}) })
@ -495,7 +495,7 @@ describe('renderer: unkeyed children', () => {
elm = root.children[0] as TestElement elm = root.children[0] as TestElement
expect(elm.children[0]).toMatchObject({ expect(elm.children[0]).toMatchObject({
type: NodeTypes.TEXT, type: TestNodeTypes.TEXT,
text: 'text' text: 'text'
}) })
}) })
@ -505,7 +505,7 @@ describe('renderer: unkeyed children', () => {
elm = root.children[0] as TestElement elm = root.children[0] as TestElement
expect(elm.children[0]).toMatchObject({ expect(elm.children[0]).toMatchObject({
type: NodeTypes.TEXT, type: TestNodeTypes.TEXT,
text: 'text' text: 'text'
}) })
@ -513,7 +513,7 @@ describe('renderer: unkeyed children', () => {
elm = root.children[0] as TestElement elm = root.children[0] as TestElement
expect(elm.children[0]).toMatchObject({ expect(elm.children[0]).toMatchObject({
type: NodeTypes.TEXT, type: TestNodeTypes.TEXT,
text: 'text2' text: 'text2'
}) })
}) })

View File

@ -3,7 +3,7 @@ import {
createVNode, createVNode,
render, render,
nodeOps, nodeOps,
NodeTypes, TestNodeTypes,
TestElement, TestElement,
Fragment, Fragment,
resetOps, resetOps,
@ -32,23 +32,23 @@ describe('renderer: fragment', () => {
expect(serializeInner(root)).toBe(`<div>one</div>two`) expect(serializeInner(root)).toBe(`<div>one</div>two`)
expect(root.children.length).toBe(4) expect(root.children.length).toBe(4)
expect(root.children[0]).toMatchObject({ expect(root.children[0]).toMatchObject({
type: NodeTypes.TEXT, type: TestNodeTypes.TEXT,
text: '' text: ''
}) })
expect(root.children[1]).toMatchObject({ expect(root.children[1]).toMatchObject({
type: NodeTypes.ELEMENT, type: TestNodeTypes.ELEMENT,
tag: 'div' tag: 'div'
}) })
expect((root.children[1] as TestElement).children[0]).toMatchObject({ expect((root.children[1] as TestElement).children[0]).toMatchObject({
type: NodeTypes.TEXT, type: TestNodeTypes.TEXT,
text: 'one' text: 'one'
}) })
expect(root.children[2]).toMatchObject({ expect(root.children[2]).toMatchObject({
type: NodeTypes.TEXT, type: TestNodeTypes.TEXT,
text: 'two' text: 'two'
}) })
expect(root.children[3]).toMatchObject({ expect(root.children[3]).toMatchObject({
type: NodeTypes.TEXT, type: TestNodeTypes.TEXT,
text: '' text: ''
}) })
}) })

View File

@ -5,7 +5,7 @@ import {
nodeOps, nodeOps,
VNodeProps, VNodeProps,
TestElement, TestElement,
NodeTypes, TestNodeTypes,
VNode VNode
} from '@vue/runtime-test' } from '@vue/runtime-test'
@ -45,13 +45,13 @@ describe('renderer: vnode hooks', () => {
onVnodeMounted: vi.fn(), onVnodeMounted: vi.fn(),
onVnodeBeforeUpdate: vi.fn(vnode => { onVnodeBeforeUpdate: vi.fn(vnode => {
expect((vnode.el as TestElement).children[0]).toMatchObject({ expect((vnode.el as TestElement).children[0]).toMatchObject({
type: NodeTypes.TEXT, type: TestNodeTypes.TEXT,
text: 'foo' text: 'foo'
}) })
}), }),
onVnodeUpdated: vi.fn(vnode => { onVnodeUpdated: vi.fn(vnode => {
expect((vnode.el as TestElement).children[0]).toMatchObject({ expect((vnode.el as TestElement).children[0]).toMatchObject({
type: NodeTypes.TEXT, type: TestNodeTypes.TEXT,
text: 'bar' text: 'bar'
}) })
}), }),
@ -70,13 +70,13 @@ describe('renderer: vnode hooks', () => {
onVnodeMounted: vi.fn(), onVnodeMounted: vi.fn(),
onVnodeBeforeUpdate: vi.fn(vnode => { onVnodeBeforeUpdate: vi.fn(vnode => {
expect(vnode.el as TestElement).toMatchObject({ expect(vnode.el as TestElement).toMatchObject({
type: NodeTypes.TEXT, type: TestNodeTypes.TEXT,
text: 'foo' text: 'foo'
}) })
}), }),
onVnodeUpdated: vi.fn(vnode => { onVnodeUpdated: vi.fn(vnode => {
expect(vnode.el as TestElement).toMatchObject({ expect(vnode.el as TestElement).toMatchObject({
type: NodeTypes.TEXT, type: TestNodeTypes.TEXT,
text: 'bar' text: 'bar'
}) })
}), }),

View File

@ -2,7 +2,7 @@ import {
h, h,
render, render,
nodeOps, nodeOps,
NodeTypes, TestNodeTypes,
TestElement, TestElement,
TestText, TestText,
ref, ref,
@ -32,12 +32,12 @@ describe('test renderer', () => {
expect(root.children.length).toBe(1) expect(root.children.length).toBe(1)
const el = root.children[0] as TestElement 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.props.id).toBe('test')
expect(el.children.length).toBe(1) expect(el.children.length).toBe(1)
const text = el.children[0] as TestText const text = el.children[0] as TestText
expect(text.type).toBe(NodeTypes.TEXT) expect(text.type).toBe(TestNodeTypes.TEXT)
expect(text.text).toBe('hello') expect(text.text).toBe('hello')
}) })
@ -68,7 +68,7 @@ describe('test renderer', () => {
expect(ops[0]).toEqual({ expect(ops[0]).toEqual({
type: NodeOpTypes.CREATE, type: NodeOpTypes.CREATE,
nodeType: NodeTypes.ELEMENT, nodeType: TestNodeTypes.ELEMENT,
tag: 'div', tag: 'div',
targetNode: root.children[0] targetNode: root.children[0]
}) })

View File

@ -1,6 +1,6 @@
import { markRaw } from '@vue/reactivity' import { markRaw } from '@vue/reactivity'
export const enum NodeTypes { export const enum TestNodeTypes {
TEXT = 'text', TEXT = 'text',
ELEMENT = 'element', ELEMENT = 'element',
COMMENT = 'comment' COMMENT = 'comment'
@ -17,7 +17,7 @@ export const enum NodeOpTypes {
export interface TestElement { export interface TestElement {
id: number id: number
type: NodeTypes.ELEMENT type: TestNodeTypes.ELEMENT
parentNode: TestElement | null parentNode: TestElement | null
tag: string tag: string
children: TestNode[] children: TestNode[]
@ -27,14 +27,14 @@ export interface TestElement {
export interface TestText { export interface TestText {
id: number id: number
type: NodeTypes.TEXT type: TestNodeTypes.TEXT
parentNode: TestElement | null parentNode: TestElement | null
text: string text: string
} }
export interface TestComment { export interface TestComment {
id: number id: number
type: NodeTypes.COMMENT type: TestNodeTypes.COMMENT
parentNode: TestElement | null parentNode: TestElement | null
text: string text: string
} }
@ -43,7 +43,7 @@ export type TestNode = TestElement | TestText | TestComment
export interface NodeOp { export interface NodeOp {
type: NodeOpTypes type: NodeOpTypes
nodeType?: NodeTypes nodeType?: TestNodeTypes
tag?: string tag?: string
text?: string text?: string
targetNode?: TestNode targetNode?: TestNode
@ -74,7 +74,7 @@ export function dumpOps(): NodeOp[] {
function createElement(tag: string): TestElement { function createElement(tag: string): TestElement {
const node: TestElement = { const node: TestElement = {
id: nodeId++, id: nodeId++,
type: NodeTypes.ELEMENT, type: TestNodeTypes.ELEMENT,
tag, tag,
children: [], children: [],
props: {}, props: {},
@ -83,7 +83,7 @@ function createElement(tag: string): TestElement {
} }
logNodeOp({ logNodeOp({
type: NodeOpTypes.CREATE, type: NodeOpTypes.CREATE,
nodeType: NodeTypes.ELEMENT, nodeType: TestNodeTypes.ELEMENT,
targetNode: node, targetNode: node,
tag tag
}) })
@ -95,13 +95,13 @@ function createElement(tag: string): TestElement {
function createText(text: string): TestText { function createText(text: string): TestText {
const node: TestText = { const node: TestText = {
id: nodeId++, id: nodeId++,
type: NodeTypes.TEXT, type: TestNodeTypes.TEXT,
text, text,
parentNode: null parentNode: null
} }
logNodeOp({ logNodeOp({
type: NodeOpTypes.CREATE, type: NodeOpTypes.CREATE,
nodeType: NodeTypes.TEXT, nodeType: TestNodeTypes.TEXT,
targetNode: node, targetNode: node,
text text
}) })
@ -113,13 +113,13 @@ function createText(text: string): TestText {
function createComment(text: string): TestComment { function createComment(text: string): TestComment {
const node: TestComment = { const node: TestComment = {
id: nodeId++, id: nodeId++,
type: NodeTypes.COMMENT, type: TestNodeTypes.COMMENT,
text, text,
parentNode: null parentNode: null
} }
logNodeOp({ logNodeOp({
type: NodeOpTypes.CREATE, type: NodeOpTypes.CREATE,
nodeType: NodeTypes.COMMENT, nodeType: TestNodeTypes.COMMENT,
targetNode: node, targetNode: node,
text text
}) })
@ -203,7 +203,7 @@ function setElementText(el: TestElement, text: string) {
el.children = [ el.children = [
{ {
id: nodeId++, id: nodeId++,
type: NodeTypes.TEXT, type: TestNodeTypes.TEXT,
text, text,
parentNode: el parentNode: el
} }

View File

@ -1,7 +1,7 @@
import { import {
TestElement, TestElement,
TestNode, TestNode,
NodeTypes, TestNodeTypes,
TestText, TestText,
TestComment TestComment
} from './nodeOps' } from './nodeOps'
@ -12,7 +12,7 @@ export function serialize(
indent: number = 0, indent: number = 0,
depth: number = 0 depth: number = 0
): string { ): string {
if (node.type === NodeTypes.ELEMENT) { if (node.type === TestNodeTypes.ELEMENT) {
return serializeElement(node, indent, depth) return serializeElement(node, indent, depth)
} else { } else {
return serializeText(node, indent, depth) return serializeText(node, indent, depth)
@ -64,6 +64,6 @@ function serializeText(
const padding = indent ? ` `.repeat(indent).repeat(depth) : `` const padding = indent ? ` `.repeat(indent).repeat(depth) : ``
return ( return (
padding + padding +
(node.type === NodeTypes.COMMENT ? `<!--${node.text}-->` : node.text) (node.type === TestNodeTypes.COMMENT ? `<!--${node.text}-->` : node.text)
) )
} }

View File

@ -81,6 +81,12 @@ export function scanEnums() {
} }
const key = e.id.type === 'Identifier' ? e.id.name : e.id.value const key = e.id.type === 'Identifier' ? e.id.name : e.id.value
const fullKey = `${id}.${key}` 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 const init = e.initializer
if (init) { if (init) {
let value let value
@ -138,15 +144,15 @@ export function scanEnums() {
`unhandled initializer type ${init.type} for ${fullKey} in ${file}` `unhandled initializer type ${init.type} for ${fullKey} in ${file}`
) )
} }
enumData.defines[fullKey] = JSON.stringify(value) saveValue(value)
lastInitialized = value lastInitialized = value
} else { } else {
if (lastInitialized === undefined) { if (lastInitialized === undefined) {
// first initialized // first initialized
enumData.defines[fullKey] = `0` saveValue(`0`)
lastInitialized = 0 lastInitialized = 0
} else if (typeof lastInitialized === 'number') { } else if (typeof lastInitialized === 'number') {
enumData.defines[fullKey] = String(++lastInitialized) saveValue(String(++lastInitialized))
} else { } else {
// should not happen // should not happen
throw new Error(`wrong enum initialization sequence in ${file}`) throw new Error(`wrong enum initialization sequence in ${file}`)