Skip to content

Commit 5b3d26c

Browse files
committed
fix(compiler-sfc): check lang before attempt to compile script
1 parent 5f8314c commit 5b3d26c

File tree

5 files changed

+42
-17
lines changed

5 files changed

+42
-17
lines changed

packages/compiler-sfc/__tests__/compileScript.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,4 +1543,19 @@ describe('compileScript', () => {
15431543
)
15441544
assertCode(content)
15451545
})
1546+
1547+
test('should not compile unrecognized language', () => {
1548+
const { content, lang, scriptAst } = compile(
1549+
`<script lang="coffee">
1550+
export default
1551+
data: ->
1552+
myVal: 0
1553+
</script>`,
1554+
)
1555+
expect(content).toMatch(`export default
1556+
data: ->
1557+
myVal: 0`)
1558+
expect(lang).toBe('coffee')
1559+
expect(scriptAst).not.toBeDefined()
1560+
})
15461561
})

packages/compiler-sfc/src/compileScript.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ import { DEFINE_EXPOSE, processDefineExpose } from './script/defineExpose'
5555
import { DEFINE_OPTIONS, processDefineOptions } from './script/defineOptions'
5656
import { DEFINE_SLOTS, processDefineSlots } from './script/defineSlots'
5757
import { DEFINE_MODEL, processDefineModel } from './script/defineModel'
58-
import { getImportedName, isCallOf, isLiteralNode } from './script/utils'
58+
import {
59+
getImportedName,
60+
isCallOf,
61+
isJS,
62+
isLiteralNode,
63+
isTS,
64+
} from './script/utils'
5965
import { analyzeScriptBindings } from './script/analyzeScriptBindings'
6066
import { isImportUsed } from './script/importUsageCheck'
6167
import { processAwait } from './script/topLevelAwait'
@@ -167,18 +173,26 @@ export function compileScript(
167173
)
168174
}
169175

170-
const ctx = new ScriptCompileContext(sfc, options)
171176
const { script, scriptSetup, source, filename } = sfc
172177
const hoistStatic = options.hoistStatic !== false && !script
173178
const scopeId = options.id ? options.id.replace(/^data-v-/, '') : ''
174179
const scriptLang = script && script.lang
175180
const scriptSetupLang = scriptSetup && scriptSetup.lang
181+
const isJSOrTS =
182+
isJS(scriptLang, scriptSetupLang) || isTS(scriptLang, scriptSetupLang)
176183

177184
if (!scriptSetup) {
178185
if (!script) {
179186
throw new Error(`[@vue/compiler-sfc] SFC contains no <script> tags.`)
180187
}
188+
181189
// normal <script> only
190+
if (script.lang && !isJSOrTS) {
191+
// do not process non js/ts script blocks
192+
return script
193+
}
194+
195+
const ctx = new ScriptCompileContext(sfc, options)
182196
return processNormalScript(ctx, scopeId)
183197
}
184198

@@ -189,11 +203,13 @@ export function compileScript(
189203
)
190204
}
191205

192-
if (scriptSetupLang && !ctx.isJS && !ctx.isTS) {
206+
if (scriptSetupLang && !isJSOrTS) {
193207
// do not process non js/ts script blocks
194208
return scriptSetup
195209
}
196210

211+
const ctx = new ScriptCompileContext(sfc, options)
212+
197213
// metadata that needs to be returned
198214
// const ctx.bindingMetadata: BindingMetadata = {}
199215
const scriptBindings: Record<string, BindingTypes> = Object.create(null)

packages/compiler-sfc/src/script/context.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { BindingMetadata } from '../../../compiler-core/src'
99
import MagicString from 'magic-string'
1010
import type { TypeScope } from './resolveType'
1111
import { warn } from '../warn'
12+
import { isJS, isTS } from './utils'
1213

1314
export class ScriptCompileContext {
1415
isJS: boolean
@@ -87,16 +88,8 @@ export class ScriptCompileContext {
8788
const scriptLang = script && script.lang
8889
const scriptSetupLang = scriptSetup && scriptSetup.lang
8990

90-
this.isJS =
91-
scriptLang === 'js' ||
92-
scriptLang === 'jsx' ||
93-
scriptSetupLang === 'js' ||
94-
scriptSetupLang === 'jsx'
95-
this.isTS =
96-
scriptLang === 'ts' ||
97-
scriptLang === 'tsx' ||
98-
scriptSetupLang === 'ts' ||
99-
scriptSetupLang === 'tsx'
91+
this.isJS = isJS(scriptLang, scriptSetupLang)
92+
this.isTS = isTS(scriptLang, scriptSetupLang)
10093

10194
const customElement = options.customElement
10295
const filename = this.descriptor.filename

packages/compiler-sfc/src/script/normalScript.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ export function processNormalScript(
1212
scopeId: string,
1313
): SFCScriptBlock {
1414
const script = ctx.descriptor.script!
15-
if (script.lang && !ctx.isJS && !ctx.isTS) {
16-
// do not process non js/ts script blocks
17-
return script
18-
}
1915
try {
2016
let content = script.content
2117
let map = script.map

packages/compiler-sfc/src/script/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,8 @@ export const propNameEscapeSymbolsRE: RegExp =
121121
export function getEscapedPropName(key: string): string {
122122
return propNameEscapeSymbolsRE.test(key) ? JSON.stringify(key) : key
123123
}
124+
125+
export const isJS = (...langs: (string | null | undefined)[]): boolean =>
126+
langs.some(lang => lang === 'js' || lang === 'jsx')
127+
export const isTS = (...langs: (string | null | undefined)[]): boolean =>
128+
langs.some(lang => lang === 'ts' || lang === 'tsx')

0 commit comments

Comments
 (0)