Skip to content

Commit c00925e

Browse files
fix(compiler): report invalid directive name error (#4494) (#4495)
1 parent d7f1b77 commit c00925e

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

packages/compiler-core/__tests__/parse.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,27 @@ describe('compiler: parse', () => {
12441244
}
12451245
})
12461246
})
1247+
test('directive with no name', () => {
1248+
let errorCode = -1
1249+
const ast = baseParse('<div v-/>', {
1250+
onError: err => {
1251+
errorCode = err.code as number
1252+
}
1253+
})
1254+
const directive = (ast.children[0] as ElementNode).props[0]
1255+
1256+
expect(errorCode).toBe(ErrorCodes.X_MISSING_DIRECTIVE_NAME)
1257+
expect(directive).toStrictEqual({
1258+
type: NodeTypes.ATTRIBUTE,
1259+
name: 'v-',
1260+
value: undefined,
1261+
loc: {
1262+
start: { offset: 5, line: 1, column: 6 },
1263+
end: { offset: 7, line: 1, column: 8 },
1264+
source: 'v-'
1265+
}
1266+
})
1267+
})
12471268

12481269
test('v-bind shorthand', () => {
12491270
const ast = baseParse('<div :a=b />')

packages/compiler-core/src/errors.ts

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export const enum ErrorCodes {
6767
X_INVALID_END_TAG,
6868
X_MISSING_END_TAG,
6969
X_MISSING_INTERPOLATION_END,
70+
X_MISSING_DIRECTIVE_NAME,
7071
X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END,
7172

7273
// transform errors
@@ -143,6 +144,7 @@ export const errorMessages: Record<ErrorCodes, string> = {
143144
[ErrorCodes.X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END]:
144145
'End bracket for dynamic directive argument was not found. ' +
145146
'Note that dynamic directive argument cannot contain spaces.',
147+
[ErrorCodes.X_MISSING_DIRECTIVE_NAME]: 'Legal directive name was expected.',
146148

147149
// transform errors
148150
[ErrorCodes.X_V_IF_NO_EXPRESSION]: `v-if/v-else-if is missing expression.`,

packages/compiler-core/src/parse.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ function parseAttribute(
775775
}
776776
const loc = getSelection(context, start)
777777

778-
if (!context.inVPre && /^(v-|:|\.|@|#)/.test(name)) {
778+
if (!context.inVPre && /^(v-[A-Za-z0-9-]|:|\.|@|#)/.test(name)) {
779779
const match =
780780
/(?:^v-([a-z0-9-]+))?(?:(?::|^\.|^@|^#)(\[[^\]]+\]|[^\.]+))?(.+)?$/i.exec(
781781
name
@@ -888,6 +888,11 @@ function parseAttribute(
888888
}
889889
}
890890

891+
// missing directive name or illegal directive name
892+
if (!context.inVPre && startsWith(name, 'v-')) {
893+
emitError(context, ErrorCodes.X_MISSING_DIRECTIVE_NAME)
894+
}
895+
891896
return {
892897
type: NodeTypes.ATTRIBUTE,
893898
name,

0 commit comments

Comments
 (0)