Skip to content

Commit

Permalink
fix(compiler): report invalid directive name error (#4494) (#4495)
Browse files Browse the repository at this point in the history
  • Loading branch information
HerringtonDarkholme committed Sep 2, 2021
1 parent d7f1b77 commit c00925e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/compiler-core/__tests__/parse.spec.ts
Expand Up @@ -1244,6 +1244,27 @@ describe('compiler: parse', () => {
}
})
})
test('directive with no name', () => {
let errorCode = -1
const ast = baseParse('<div v-/>', {
onError: err => {
errorCode = err.code as number
}
})
const directive = (ast.children[0] as ElementNode).props[0]

expect(errorCode).toBe(ErrorCodes.X_MISSING_DIRECTIVE_NAME)
expect(directive).toStrictEqual({
type: NodeTypes.ATTRIBUTE,
name: 'v-',
value: undefined,
loc: {
start: { offset: 5, line: 1, column: 6 },
end: { offset: 7, line: 1, column: 8 },
source: 'v-'
}
})
})

test('v-bind shorthand', () => {
const ast = baseParse('<div :a=b />')
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler-core/src/errors.ts
Expand Up @@ -67,6 +67,7 @@ export const enum ErrorCodes {
X_INVALID_END_TAG,
X_MISSING_END_TAG,
X_MISSING_INTERPOLATION_END,
X_MISSING_DIRECTIVE_NAME,
X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END,

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

// transform errors
[ErrorCodes.X_V_IF_NO_EXPRESSION]: `v-if/v-else-if is missing expression.`,
Expand Down
7 changes: 6 additions & 1 deletion packages/compiler-core/src/parse.ts
Expand Up @@ -775,7 +775,7 @@ function parseAttribute(
}
const loc = getSelection(context, start)

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

// missing directive name or illegal directive name
if (!context.inVPre && startsWith(name, 'v-')) {
emitError(context, ErrorCodes.X_MISSING_DIRECTIVE_NAME)
}

return {
type: NodeTypes.ATTRIBUTE,
name,
Expand Down

0 comments on commit c00925e

Please sign in to comment.