Skip to content

Commit

Permalink
fix(compiler-sfc): use safer deindent default for compatibility with …
Browse files Browse the repository at this point in the history
…previous behavior
  • Loading branch information
yyx990803 committed Jul 8, 2022
1 parent ca7daef commit b70a258
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
11 changes: 7 additions & 4 deletions packages/compiler-sfc/src/parseComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export const DEFAULT_FILENAME = 'anonymous.vue'
const splitRE = /\r?\n/g
const replaceRE = /./g
const isSpecialTag = makeMap('script,style,template', true)
const isNeedIndentLang = makeMap('pug,jade')

export interface SFCCustomBlock {
type: string
Expand Down Expand Up @@ -179,9 +178,13 @@ export function parseComponent(
currentBlock.end = start
let text = source.slice(currentBlock.start, currentBlock.end)
if (
options.deindent ||
// certain langs like pug are indent sensitive, preserve old behavior
(currentBlock.lang && isNeedIndentLang(currentBlock.lang))
options.deindent === true ||
// by default, deindent unless it's script with default lang or ts
(options.deindent !== false &&
!(
currentBlock.type === 'script' &&
(!currentBlock.lang || currentBlock.lang === 'ts')
))
) {
text = deindent(text)
}
Expand Down
13 changes: 6 additions & 7 deletions packages/compiler-sfc/test/parseComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ describe('Single File Component parser', () => {
<div>
<style>nested should be ignored</style>
</div>
`,
{ deindent: true }
`
)
expect(res.template!.content.trim()).toBe('<div>hi</div>')
expect(res.styles.length).toBe(4)
Expand Down Expand Up @@ -76,8 +75,7 @@ describe('Single File Component parser', () => {
</style>
`
const deindentDefault = parseComponent(content.trim(), {
pad: false,
deindent: true
pad: false
})
const deindentEnabled = parseComponent(content.trim(), {
pad: false,
Expand All @@ -89,7 +87,9 @@ describe('Single File Component parser', () => {
})

expect(deindentDefault.template!.content).toBe('\n<div></div>\n')
expect(deindentDefault.script!.content).toBe('\nexport default {}\n')
expect(deindentDefault.script!.content).toBe(
'\n export default {}\n '
)
expect(deindentDefault.styles[0].content).toBe('\nh1 { color: red }\n')
expect(deindentEnabled.template!.content).toBe('\n<div></div>\n')
expect(deindentEnabled.script!.content).toBe('\nexport default {}\n')
Expand Down Expand Up @@ -203,8 +203,7 @@ describe('Single File Component parser', () => {
}
</test>
<custom src="./x.json"></custom>
`,
{ deindent: true }
`
)
expect(res.customBlocks.length).toBe(4)

Expand Down

0 comments on commit b70a258

Please sign in to comment.