Skip to content

Commit 775ce57

Browse files
committed
Fix: bugs about short name directives and improve error messages.
1 parent b802138 commit 775ce57

File tree

56 files changed

+2903
-148
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2903
-148
lines changed

src/ast/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class ParseError extends SyntaxError {
7070
* @param column The column number of this error.
7171
*/
7272
constructor(message: string, code: ErrorCode | undefined, offset: number, line: number, column: number) {
73-
super(`${message} (${line}:${column})`)
73+
super(message)
7474
this.code = code
7575
this.index = offset
7676
this.lineNumber = line

src/common/location-calculator.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ export class LocationCalculator {
8181
}
8282

8383
/**
84-
* Calculate the location of the given offset.
85-
* @param offset The offset to calculate their location.
84+
* Calculate the location of the given index.
85+
* @param index The index to calculate their location.
8686
* @param lineTerminators The list of the offset of line terminators.
8787
* @returns The location of the offset.
8888
*/
89-
getLocation(offset: number): Location {
90-
return this._getLocation(this.baseOffset + offset)
89+
getLocation(index: number): Location {
90+
return this._getLocation(this.baseOffset + index)
9191
}
9292

9393
/**
@@ -123,7 +123,10 @@ export class LocationCalculator {
123123
* @param error The error to modify their location.
124124
*/
125125
fixErrorLocation(error: ParseError) {
126-
error.index = error.index + this.baseOffset
126+
const gap = this._getGap(error.index)
127+
const diff = this.baseOffset + Math.max(0, gap)
128+
129+
error.index += diff
127130

128131
const loc = this._getLocation(error.index)
129132
error.lineNumber = loc.line

src/html/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {HTML_CAN_BE_LEFT_OPEN_TAGS, HTML_NON_FHRASING_TAGS, HTML_RAWTEXT_TAGS, H
1414
import {IntermediateToken, IntermediateTokenizer, EndTag, Mustache, StartTag, Text} from "./intermediate-tokenizer"
1515
import {Tokenizer} from "./tokenizer"
1616

17-
const DIRECTIVE_NAME = /^(?:v-|[:@]).+[^.:@]$/
17+
const DIRECTIVE_NAME = /^(?:v-|[:@]).*[^.:@]$/
1818
const DT_DD = /^d[dt]$/
1919
const DUMMY_PARENT: any = Object.freeze({})
2020

src/script/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,24 @@ function removeByName(references: Reference[], name: string): void {
107107
}
108108
}
109109

110+
/**
111+
* Throw syntax error for empty.
112+
* @param locationCalculator The location calculator to get line/column.
113+
*/
114+
function throwEmptyError(locationCalculator: LocationCalculator, expected: string): never {
115+
const loc = locationCalculator.getLocation(0)
116+
const err = new ParseError(
117+
`Expected to be ${expected}, but got empty.`,
118+
undefined,
119+
0,
120+
loc.line,
121+
loc.column
122+
)
123+
locationCalculator.fixErrorLocation(err)
124+
125+
throw err
126+
}
127+
110128
/**
111129
* Parse the given source code.
112130
*
@@ -218,6 +236,10 @@ export function parseScriptElement(node: VElement, globalLocationCalculator: Loc
218236
export function parseExpression(code: string, locationCalculator: LocationCalculator, parserOptions: any): ExpressionParseResult {
219237
debug("[script] parse expression: \"(%s)\"", code)
220238

239+
if (code.trim() === "") {
240+
throwEmptyError(locationCalculator, "an expression")
241+
}
242+
221243
const ast = parseScriptFragment(
222244
`(${code})`,
223245
locationCalculator.getSubCalculatorAfter(-1),
@@ -246,6 +268,10 @@ export function parseVForExpression(code: string, locationCalculator: LocationCa
246268
const processedCode = replaceAliasParens(code)
247269
debug("[script] parse v-for expression: \"for(%s);\"", processedCode)
248270

271+
if (code.trim() === "") {
272+
throwEmptyError(locationCalculator, "'<alias> in <expression>'")
273+
}
274+
249275
const replaced = processedCode !== code
250276
const ast = parseScriptFragment(
251277
`for(let ${processedCode});`,
@@ -313,6 +339,10 @@ export function parseVForExpression(code: string, locationCalculator: LocationCa
313339
export function parseVOnExpression(code: string, locationCalculator: LocationCalculator, parserOptions: any): ExpressionParseResult {
314340
debug("[script] parse v-on expression: \"{%s}\"", code)
315341

342+
if (code.trim() === "") {
343+
throwEmptyError(locationCalculator, "statements")
344+
}
345+
316346
const ast = parseScriptFragment(
317347
`{${code}}`,
318348
locationCalculator.getSubCalculatorAfter(-1),

src/template/index.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,6 @@ function insertError(document: VDocumentFragment | null, error: ParseError): voi
221221
* @param directiveName The name of this directive.
222222
*/
223223
function parseAttributeValue(code: string, parserOptions: any, globalLocationCalculator: LocationCalculator, node: VLiteral, directiveName: string): ExpressionParseResult {
224-
if (node.value.trim() === "") {
225-
throw new ParseError(
226-
"Unexpected empty",
227-
undefined,
228-
node.range[0],
229-
node.loc.start.line,
230-
node.loc.end.line
231-
)
232-
}
233-
234224
const firstChar = code[node.range[0]]
235225
const quoted = (firstChar === "\"" || firstChar === "'")
236226
const locationCalculator = globalLocationCalculator.getSubCalculatorAfter(node.range[0] + (quoted ? 1 : 0))

test/fixtures/ast/attributes/ast.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,7 @@
17571757
"comments": [],
17581758
"errors": [
17591759
{
1760-
"message": "missing-attribute-value (3:11)",
1760+
"message": "missing-attribute-value",
17611761
"index": 40,
17621762
"lineNumber": 3,
17631763
"column": 11

test/fixtures/ast/bogus-comments/ast.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,25 +372,25 @@
372372
],
373373
"errors": [
374374
{
375-
"message": "unexpected-question-mark-instead-of-tag-name (2:5)",
375+
"message": "unexpected-question-mark-instead-of-tag-name",
376376
"index": 16,
377377
"lineNumber": 2,
378378
"column": 5
379379
},
380380
{
381-
"message": "invalid-first-character-of-tag-name (3:6)",
381+
"message": "invalid-first-character-of-tag-name",
382382
"index": 43,
383383
"lineNumber": 3,
384384
"column": 6
385385
},
386386
{
387-
"message": "cdata-in-html-content (5:12)",
387+
"message": "cdata-in-html-content",
388388
"index": 80,
389389
"lineNumber": 5,
390390
"column": 12
391391
},
392392
{
393-
"message": "incorrectly-opened-comment (6:6)",
393+
"message": "incorrectly-opened-comment",
394394
"index": 96,
395395
"lineNumber": 6,
396396
"column": 6

test/fixtures/ast/comments-2/ast.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,25 +1090,25 @@
10901090
],
10911091
"errors": [
10921092
{
1093-
"message": "abrupt-closing-of-empty-comment (15:8)",
1093+
"message": "abrupt-closing-of-empty-comment",
10941094
"index": 209,
10951095
"lineNumber": 15,
10961096
"column": 8
10971097
},
10981098
{
1099-
"message": "abrupt-closing-of-empty-comment (16:9)",
1099+
"message": "abrupt-closing-of-empty-comment",
11001100
"index": 220,
11011101
"lineNumber": 16,
11021102
"column": 9
11031103
},
11041104
{
1105-
"message": "nested-comment (17:12)",
1105+
"message": "nested-comment",
11061106
"index": 234,
11071107
"lineNumber": 17,
11081108
"column": 12
11091109
},
11101110
{
1111-
"message": "incorrectly-closed-comment (18:12)",
1111+
"message": "incorrectly-closed-comment",
11121112
"index": 249,
11131113
"lineNumber": 18,
11141114
"column": 12

test/fixtures/ast/directive-arguments/ast.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@
993993
"comments": [],
994994
"errors": [
995995
{
996-
"message": "missing-attribute-value (3:15)",
996+
"message": "missing-attribute-value",
997997
"index": 48,
998998
"lineNumber": 3,
999999
"column": 15

0 commit comments

Comments
 (0)