Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sfc): avoid to deindent when pad option is specified #7647

Merged
merged 2 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/sfc/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,15 @@ export function parseComponent (
function end (tag: string, start: number, end: 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
Expand Down
33 changes: 27 additions & 6 deletions test/unit/modules/sfc/sfc-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') + `
<div></div>
`)
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') + `
<div></div>
`)
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(`<template>`.replace(/./g, ' ') + `
<div></div>
`)
expect(padSpace.script.content).toBe(`<template>
<div></div>
</template>
<script>`.replace(/./g, ' ') + '\nexport default {}\n')
<script>`.replace(/./g, ' ') + `
export default {}
`)
expect(padSpace.styles[0].content).toBe(`<template>
<div></div>
</template>
<script>
export default {}
</script>
<style>`.replace(/./g, ' ') + '\nh1 { color: red }\n')
<style>`.replace(/./g, ' ') + `
h1 { color: red }
`)
})

it('should handle template blocks with lang as special text', () => {
Expand Down