Skip to content

Commit

Permalink
✨ add more parsing errors about directive keys
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Feb 7, 2019
1 parent bc89f92 commit b57cd06
Show file tree
Hide file tree
Showing 17 changed files with 1,971 additions and 236 deletions.
56 changes: 49 additions & 7 deletions src/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ function createSimpleToken(
/**
* Parse the given attribute name as a directive key.
* @param node The identifier node to parse.
* @param document The document to add parsing errors.
* @returns The directive key node.
*/
function parseDirectiveKeyStatically(node: VIdentifier): VDirectiveKey {
function parseDirectiveKeyStatically(
node: VIdentifier,
document: VDocumentFragment | null,
): VDirectiveKey {
const {
name: text,
rawName: rawText,
Expand Down Expand Up @@ -136,23 +140,53 @@ function parseDirectiveKeyStatically(node: VIdentifier): VDirectiveKey {
.slice(i)
.split(".")
.map(modifierName => {
//TODO: generate syntax error if modifierName.length === 0.
const modifier = createIdentifier(i, i + modifierName.length)
if (modifierName === "") {
insertError(
document,
new ParseError(
`Unexpected token '${text[i]}'`,
undefined,
offset + i,
line,
column + i,
),
)
}
i += modifierName.length + 1
return modifier
})

if (directiveKey.name == null) {
directiveKey.name = nameOrArgument
} else {
} else if (nameOrArgument.name !== "") {
directiveKey.argument = nameOrArgument
}
directiveKey.modifiers = modifiers
directiveKey.modifiers = modifiers.filter(isNotEmptyModifier)

if (directiveKey.name.name === "v-") {
insertError(
document,
new ParseError(
`Unexpected token '${
text[directiveKey.name.range[1] - offset]
}'`,
undefined,
directiveKey.name.range[1],
directiveKey.name.loc.end.line,
directiveKey.name.loc.end.column,
),
)
}

if (directiveKey.name.rawName === "." && !modifiers.some(isPropModifier)) {
// v-bind.prop shorthand
if (
directiveKey.name.rawName === "." &&
!directiveKey.modifiers.some(isPropModifier)
) {
const pos = (directiveKey.argument || directiveKey.name).range[1]
const propModifier = createIdentifier(pos, pos, "prop")
modifiers.unshift(propModifier)
directiveKey.modifiers.unshift(propModifier)
}

return directiveKey
Expand All @@ -166,6 +200,14 @@ function isPropModifier(node: VIdentifier): boolean {
return node.name === "prop"
}

/**
* Check whether a given identifier node is empty or not.
* @param node The identifier node to check.
*/
function isNotEmptyModifier(node: VIdentifier): boolean {
return node.name !== ""
}

/**
* Parse the tokens of a given key node.
* @param node The key node to parse.
Expand Down Expand Up @@ -335,7 +377,7 @@ function createDirectiveKey(
locationCalculator: LocationCalculator,
): VDirectiveKey {
// Parse node and tokens.
const directiveKey = parseDirectiveKeyStatically(node)
const directiveKey = parseDirectiveKeyStatically(node, document)
const tokens = parseDirectiveKeyTokens(directiveKey)
replaceTokens(document, directiveKey, tokens)

Expand Down
Loading

0 comments on commit b57cd06

Please sign in to comment.