import { parse } from '../src' import { ElementTypes, NodeTypes, baseCompile, createRoot, } from '@vue/compiler-core' import { SourceMapConsumer } from 'source-map-js' describe('compiler:sfc', () => { describe('source map', () => { test('style block', () => { // Padding determines how many blank lines will there be before the style block const padding = Math.round(Math.random() * 10) const src = `${'\n'.repeat(padding)}` + ` ` const { descriptor: { styles }, } = parse(src) expect(styles[0].map).not.toBeUndefined() const consumer = new SourceMapConsumer(styles[0].map!) const lineOffset = src.slice(0, src.indexOf(` { "greeting": "hello" } ` const padFalse = parse(content.trim(), { pad: false }).descriptor expect(padFalse.template!.content).toBe('\n
\n') expect(padFalse.script!.content).toBe('\nexport default {}\n') expect(padFalse.styles[0].content).toBe('\nh1 { color: red }\n') expect(padFalse.customBlocks[0].content).toBe('\n{ "greeting": "hello" }\n') const padTrue = parse(content.trim(), { pad: true }).descriptor expect(padTrue.script!.content).toBe( Array(3 + 1).join('//\n') + '\nexport default {}\n', ) expect(padTrue.styles[0].content).toBe( Array(6 + 1).join('\n') + '\nh1 { color: red }\n', ) expect(padTrue.customBlocks[0].content).toBe( Array(9 + 1).join('\n') + '\n{ "greeting": "hello" }\n', ) const padLine = parse(content.trim(), { pad: 'line' }).descriptor expect(padLine.script!.content).toBe( Array(3 + 1).join('//\n') + '\nexport default {}\n', ) expect(padLine.styles[0].content).toBe( Array(6 + 1).join('\n') + '\nh1 { color: red }\n', ) expect(padLine.customBlocks[0].content).toBe( Array(9 + 1).join('\n') + '\n{ "greeting": "hello" }\n', ) const padSpace = parse(content.trim(), { pad: 'space' }).descriptor expect(padSpace.script!.content).toBe( `\n\n\n`.replace( /./g, ' ', ) + '\n{ "greeting": "hello" }\n', ) }) test('should parse correct range for root level self closing tag', () => { const content = `\n
\n` const { descriptor } = parse(``) expect(descriptor.template).toBeTruthy() expect(descriptor.template!.content).toBe(content) expect(descriptor.template!.loc).toMatchObject({ start: { line: 1, column: 11, offset: 10 }, end: { line: 3, column: 1, offset: 10 + content.length, }, }) }) test('should parse correct range for blocks with no content (self closing)', () => { const { descriptor } = parse(`