diff --git a/src/sfc/parser.js b/src/sfc/parser.js index d7868a40ab8..f6e83d362a3 100644 --- a/src/sfc/parser.js +++ b/src/sfc/parser.js @@ -83,11 +83,15 @@ export function parseComponent ( function end (tag: string, start: number) { if (depth === 1 && currentBlock) { currentBlock.end = start - let text = deindent(content.slice(currentBlock.start, currentBlock.end)) + let text = content.slice(currentBlock.start, currentBlock.end) // pad content so that linters and pre-processors can output correct // line numbers in errors and warnings - if (currentBlock.type !== 'template' && options.pad) { + if (options.pad) { text = padContent(currentBlock, options.pad) + text + } else { + // avoid to deindent if pad option is specified + // to retain original source position. + text = deindent(text) } currentBlock.content = text currentBlock = null diff --git a/test/unit/modules/sfc/sfc-parser.spec.js b/test/unit/modules/sfc/sfc-parser.spec.js index 4cf6bad574a..30a9f9e2c43 100644 --- a/test/unit/modules/sfc/sfc-parser.spec.js +++ b/test/unit/modules/sfc/sfc-parser.spec.js @@ -71,21 +71,42 @@ describe('Single File Component parser', () => { const padLine = parseComponent(content.trim(), { pad: 'line' }) const padSpace = parseComponent(content.trim(), { pad: 'space' }) - expect(padDefault.script.content).toBe(Array(3 + 1).join('//\n') + '\nexport default {}\n') - expect(padDefault.styles[0].content).toBe(Array(6 + 1).join('\n') + '\nh1 { color: red }\n') - 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(padDefault.template.content).toBe(Array(1).join('\n') + ` +
+ `) + expect(padDefault.script.content).toBe(Array(3 + 1).join('//\n') + ` + export default {} + `) + expect(padDefault.styles[0].content).toBe(Array(6 + 1).join('\n') + ` + h1 { color: red } + `) + expect(padLine.template.content).toBe(Array(1).join('\n') + ` +
+ `) + expect(padLine.script.content).toBe(Array(3 + 1).join('//\n') + ` + export default {} + `) + expect(padLine.styles[0].content).toBe(Array(6 + 1).join('\n') + ` + h1 { color: red } + `) + expect(padSpace.template.content).toBe(`