Skip to content

Commit e2c9ab1

Browse files
authored
test(compiler-core): more test cases for baseParse (#2211)
1 parent 1589830 commit e2c9ab1

File tree

2 files changed

+206
-1
lines changed

2 files changed

+206
-1
lines changed

packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,58 @@ Object {
14711471
}
14721472
`;
14731473
1474+
exports[`compiler: parse Errors EOF_IN_TAG <div></div 1`] = `
1475+
Object {
1476+
"cached": 0,
1477+
"children": Array [
1478+
Object {
1479+
"children": Array [],
1480+
"codegenNode": undefined,
1481+
"isSelfClosing": false,
1482+
"loc": Object {
1483+
"end": Object {
1484+
"column": 11,
1485+
"line": 1,
1486+
"offset": 10,
1487+
},
1488+
"source": "<div></div",
1489+
"start": Object {
1490+
"column": 1,
1491+
"line": 1,
1492+
"offset": 0,
1493+
},
1494+
},
1495+
"ns": 0,
1496+
"props": Array [],
1497+
"tag": "div",
1498+
"tagType": 0,
1499+
"type": 1,
1500+
},
1501+
],
1502+
"codegenNode": undefined,
1503+
"components": Array [],
1504+
"directives": Array [],
1505+
"helpers": Array [],
1506+
"hoists": Array [],
1507+
"imports": Array [],
1508+
"loc": Object {
1509+
"end": Object {
1510+
"column": 11,
1511+
"line": 1,
1512+
"offset": 10,
1513+
},
1514+
"source": "<div></div",
1515+
"start": Object {
1516+
"column": 1,
1517+
"line": 1,
1518+
"offset": 0,
1519+
},
1520+
},
1521+
"temps": 0,
1522+
"type": 0,
1523+
}
1524+
`;
1525+
14741526
exports[`compiler: parse Errors EOF_IN_TAG <template><div 1`] = `
14751527
Object {
14761528
"cached": 0,
@@ -4293,6 +4345,76 @@ Object {
42934345
}
42944346
`;
42954347
4348+
exports[`compiler: parse Errors NESTED_COMMENT <template><!--a<!--b<!----></template> 1`] = `
4349+
Object {
4350+
"cached": 0,
4351+
"children": Array [
4352+
Object {
4353+
"children": Array [
4354+
Object {
4355+
"content": "a<!--b<!--",
4356+
"loc": Object {
4357+
"end": Object {
4358+
"column": 28,
4359+
"line": 1,
4360+
"offset": 27,
4361+
},
4362+
"source": "<!--a<!--b<!---->",
4363+
"start": Object {
4364+
"column": 11,
4365+
"line": 1,
4366+
"offset": 10,
4367+
},
4368+
},
4369+
"type": 3,
4370+
},
4371+
],
4372+
"codegenNode": undefined,
4373+
"isSelfClosing": false,
4374+
"loc": Object {
4375+
"end": Object {
4376+
"column": 39,
4377+
"line": 1,
4378+
"offset": 38,
4379+
},
4380+
"source": "<template><!--a<!--b<!----></template>",
4381+
"start": Object {
4382+
"column": 1,
4383+
"line": 1,
4384+
"offset": 0,
4385+
},
4386+
},
4387+
"ns": 0,
4388+
"props": Array [],
4389+
"tag": "template",
4390+
"tagType": 0,
4391+
"type": 1,
4392+
},
4393+
],
4394+
"codegenNode": undefined,
4395+
"components": Array [],
4396+
"directives": Array [],
4397+
"helpers": Array [],
4398+
"hoists": Array [],
4399+
"imports": Array [],
4400+
"loc": Object {
4401+
"end": Object {
4402+
"column": 39,
4403+
"line": 1,
4404+
"offset": 38,
4405+
},
4406+
"source": "<template><!--a<!--b<!----></template>",
4407+
"start": Object {
4408+
"column": 1,
4409+
"line": 1,
4410+
"offset": 0,
4411+
},
4412+
},
4413+
"temps": 0,
4414+
"type": 0,
4415+
}
4416+
`;
4417+
42964418
exports[`compiler: parse Errors NESTED_COMMENT <template><!--a<!--b<!--c--></template> 1`] = `
42974419
Object {
42984420
"cached": 0,

packages/compiler-core/__tests__/parse.spec.ts

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,12 @@ describe('compiler: parse', () => {
377377

378378
test('comments option', () => {
379379
__DEV__ = false
380-
const astNoComment = baseParse('<!--abc-->')
380+
const astDefaultComment = baseParse('<!--abc-->')
381+
const astNoComment = baseParse('<!--abc-->', { comments: false })
381382
const astWithComments = baseParse('<!--abc-->', { comments: true })
382383
__DEV__ = true
383384

385+
expect(astDefaultComment.children).toHaveLength(0)
384386
expect(astNoComment.children).toHaveLength(0)
385387
expect(astWithComments.children).toHaveLength(1)
386388
})
@@ -636,6 +638,40 @@ describe('compiler: parse', () => {
636638
})
637639
})
638640

641+
test('built-in component', () => {
642+
const ast = baseParse('<div></div><comp></comp>', {
643+
isBuiltInComponent: tag => (tag === 'comp' ? Symbol() : void 0)
644+
})
645+
646+
expect(ast.children[0]).toMatchObject({
647+
type: NodeTypes.ELEMENT,
648+
tag: 'div',
649+
tagType: ElementTypes.ELEMENT
650+
})
651+
652+
expect(ast.children[1]).toMatchObject({
653+
type: NodeTypes.ELEMENT,
654+
tag: 'comp',
655+
tagType: ElementTypes.COMPONENT
656+
})
657+
})
658+
659+
test('slot element', () => {
660+
const ast = baseParse('<slot></slot><Comp></Comp>')
661+
662+
expect(ast.children[0]).toMatchObject({
663+
type: NodeTypes.ELEMENT,
664+
tag: 'slot',
665+
tagType: ElementTypes.SLOT
666+
})
667+
668+
expect(ast.children[1]).toMatchObject({
669+
type: NodeTypes.ELEMENT,
670+
tag: 'Comp',
671+
tagType: ElementTypes.COMPONENT
672+
})
673+
})
674+
639675
test('attribute with no value', () => {
640676
const ast = baseParse('<div id></div>')
641677
const element = ast.children[0] as ElementNode
@@ -1679,6 +1715,14 @@ foo
16791715
})
16801716

16811717
describe('decodeEntities option', () => {
1718+
test('use default map', () => {
1719+
const ast: any = baseParse('&gt;&lt;&amp;&apos;&quot;&foo;')
1720+
1721+
expect(ast.children.length).toBe(1)
1722+
expect(ast.children[0].type).toBe(NodeTypes.TEXT)
1723+
expect(ast.children[0].content).toBe('><&\'"&foo;')
1724+
})
1725+
16821726
test('use the given map', () => {
16831727
const ast: any = baseParse('&amp;&cups;', {
16841728
decodeEntities: text => text.replace('&cups;', '\u222A\uFE00'),
@@ -1746,6 +1790,27 @@ foo
17461790
const ast = baseParse(` foo \n bar baz `)
17471791
expect((ast.children[0] as TextNode).content).toBe(` foo bar baz `)
17481792
})
1793+
1794+
it('should remove leading newline character immediately following the pre element start tag', () => {
1795+
const ast = baseParse(`<pre>\n foo bar </pre>`, {
1796+
isPreTag: tag => tag === 'pre'
1797+
})
1798+
expect(ast.children).toHaveLength(1)
1799+
const preElement = ast.children[0] as ElementNode
1800+
expect(preElement.children).toHaveLength(1)
1801+
expect((preElement.children[0] as TextNode).content).toBe(` foo bar `)
1802+
})
1803+
1804+
it('should NOT remove leading newline character immediately following child-tag of pre element', () => {
1805+
const ast = baseParse(`<pre><span></span>\n foo bar </pre>`, {
1806+
isPreTag: tag => tag === 'pre'
1807+
})
1808+
const preElement = ast.children[0] as ElementNode
1809+
expect(preElement.children).toHaveLength(2)
1810+
expect((preElement.children[1] as TextNode).content).toBe(
1811+
`\n foo bar `
1812+
)
1813+
})
17491814
})
17501815

17511816
describe('Errors', () => {
@@ -2222,6 +2287,15 @@ foo
22222287
loc: { offset: 0, line: 1, column: 1 }
22232288
}
22242289
]
2290+
},
2291+
{
2292+
code: '<div></div',
2293+
errors: [
2294+
{
2295+
type: ErrorCodes.EOF_IN_TAG,
2296+
loc: { offset: 10, line: 1, column: 11 }
2297+
}
2298+
]
22252299
}
22262300
],
22272301
INCORRECTLY_CLOSED_COMMENT: [
@@ -2391,6 +2465,15 @@ foo
23912465
}
23922466
]
23932467
},
2468+
{
2469+
code: '<template><!--a<!--b<!----></template>',
2470+
errors: [
2471+
{
2472+
type: ErrorCodes.NESTED_COMMENT,
2473+
loc: { offset: 15, line: 1, column: 16 }
2474+
}
2475+
]
2476+
},
23942477
{
23952478
code: '<template><!--a<!--></template>',
23962479
errors: []

0 commit comments

Comments
 (0)