From e2c9ab1991bec7ce2218092d2c7d92ee88355106 Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Wed, 14 Oct 2020 04:03:33 +0800 Subject: [PATCH] test(compiler-core): more test cases for baseParse (#2211) --- .../__snapshots__/parse.spec.ts.snap | 122 ++++++++++++++++++ .../compiler-core/__tests__/parse.spec.ts | 85 +++++++++++- 2 files changed, 206 insertions(+), 1 deletion(-) diff --git a/packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap b/packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap index 998b00ab..a645e631 100644 --- a/packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap +++ b/packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap @@ -1471,6 +1471,58 @@ Object { } `; +exports[`compiler: parse Errors EOF_IN_TAG
1`] = ` +Object { + "cached": 0, + "children": Array [ + Object { + "children": Array [ + Object { + "content": "a", + "start": Object { + "column": 11, + "line": 1, + "offset": 10, + }, + }, + "type": 3, + }, + ], + "codegenNode": undefined, + "isSelfClosing": false, + "loc": Object { + "end": Object { + "column": 39, + "line": 1, + "offset": 38, + }, + "source": "", + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "ns": 0, + "props": Array [], + "tag": "template", + "tagType": 0, + "type": 1, + }, + ], + "codegenNode": undefined, + "components": Array [], + "directives": Array [], + "helpers": Array [], + "hoists": Array [], + "imports": Array [], + "loc": Object { + "end": Object { + "column": 39, + "line": 1, + "offset": 38, + }, + "source": "", + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "temps": 0, + "type": 0, +} +`; + exports[`compiler: parse Errors NESTED_COMMENT 1`] = ` Object { "cached": 0, diff --git a/packages/compiler-core/__tests__/parse.spec.ts b/packages/compiler-core/__tests__/parse.spec.ts index b73609ee..3fdb3d95 100644 --- a/packages/compiler-core/__tests__/parse.spec.ts +++ b/packages/compiler-core/__tests__/parse.spec.ts @@ -377,10 +377,12 @@ describe('compiler: parse', () => { test('comments option', () => { __DEV__ = false - const astNoComment = baseParse('') + const astDefaultComment = baseParse('') + const astNoComment = baseParse('', { comments: false }) const astWithComments = baseParse('', { comments: true }) __DEV__ = true + expect(astDefaultComment.children).toHaveLength(0) expect(astNoComment.children).toHaveLength(0) expect(astWithComments.children).toHaveLength(1) }) @@ -636,6 +638,40 @@ describe('compiler: parse', () => { }) }) + test('built-in component', () => { + const ast = baseParse('
', { + isBuiltInComponent: tag => (tag === 'comp' ? Symbol() : void 0) + }) + + expect(ast.children[0]).toMatchObject({ + type: NodeTypes.ELEMENT, + tag: 'div', + tagType: ElementTypes.ELEMENT + }) + + expect(ast.children[1]).toMatchObject({ + type: NodeTypes.ELEMENT, + tag: 'comp', + tagType: ElementTypes.COMPONENT + }) + }) + + test('slot element', () => { + const ast = baseParse('') + + expect(ast.children[0]).toMatchObject({ + type: NodeTypes.ELEMENT, + tag: 'slot', + tagType: ElementTypes.SLOT + }) + + expect(ast.children[1]).toMatchObject({ + type: NodeTypes.ELEMENT, + tag: 'Comp', + tagType: ElementTypes.COMPONENT + }) + }) + test('attribute with no value', () => { const ast = baseParse('
') const element = ast.children[0] as ElementNode @@ -1679,6 +1715,14 @@ foo }) describe('decodeEntities option', () => { + test('use default map', () => { + const ast: any = baseParse('><&'"&foo;') + + expect(ast.children.length).toBe(1) + expect(ast.children[0].type).toBe(NodeTypes.TEXT) + expect(ast.children[0].content).toBe('><&\'"&foo;') + }) + test('use the given map', () => { const ast: any = baseParse('&∪︀', { decodeEntities: text => text.replace('∪︀', '\u222A\uFE00'), @@ -1746,6 +1790,27 @@ foo const ast = baseParse(` foo \n bar baz `) expect((ast.children[0] as TextNode).content).toBe(` foo bar baz `) }) + + it('should remove leading newline character immediately following the pre element start tag', () => { + const ast = baseParse(`
\n  foo  bar  
`, { + isPreTag: tag => tag === 'pre' + }) + expect(ast.children).toHaveLength(1) + const preElement = ast.children[0] as ElementNode + expect(preElement.children).toHaveLength(1) + expect((preElement.children[0] as TextNode).content).toBe(` foo bar `) + }) + + it('should NOT remove leading newline character immediately following child-tag of pre element', () => { + const ast = baseParse(`
\n  foo  bar  
`, { + isPreTag: tag => tag === 'pre' + }) + const preElement = ast.children[0] as ElementNode + expect(preElement.children).toHaveLength(2) + expect((preElement.children[1] as TextNode).content).toBe( + `\n foo bar ` + ) + }) }) describe('Errors', () => { @@ -2222,6 +2287,15 @@ foo loc: { offset: 0, line: 1, column: 1 } } ] + }, + { + code: '
', + errors: [ + { + type: ErrorCodes.NESTED_COMMENT, + loc: { offset: 15, line: 1, column: 16 } + } + ] + }, { code: '', errors: []