Skip to content

Commit

Permalink
fix: fix finally block start, follow other parsers on comment start a…
Browse files Browse the repository at this point in the history
…nd end

closes #104
  • Loading branch information
3cp committed Oct 14, 2020
1 parent 46a7d69 commit fe00a67
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 40 deletions.
16 changes: 13 additions & 3 deletions src/lexer/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function skipSingleLineComment(
type: CommentType
): LexerState {
const { index } = parser;
let end = index;
while (parser.index < parser.end) {
if (CharTypes[parser.currentChar] & CharFlags.LineTerminator) {
const isCR = parser.currentChar === Chars.CarriageReturn;
Expand All @@ -63,9 +64,18 @@ export function skipSingleLineComment(
break;
}
advanceChar(parser);
end++;
}
if (parser.onComment)
parser.onComment(CommentTypes[type & 0xff], source.slice(index, parser.index), index, parser.index);
parser.onComment(
CommentTypes[type & 0xff],
source.slice(index, end),
// For Single, start before "//",
// For HTMLOpen, start before "<!--",
// For HTMLClose, start before "\n-->"
index - (type === CommentType.Single ? 2 : 4),
end
);
return state | LexerState.NewLine;
}

Expand All @@ -91,8 +101,8 @@ export function skipMultiLineComment(parser: ParserState, source: string, state:
parser.onComment(
CommentTypes[CommentType.Multi & 0xff],
source.slice(index, parser.index - 2),
index,
parser.index
index - 2, // start before '/*'
parser.index // end after '*/'
);
return state;
}
Expand Down
10 changes: 9 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1699,7 +1699,15 @@ export function parseTryStatement(
if (parser.token === Token.FinallyKeyword) {
nextToken(parser, context | Context.AllowRegExp);
const finalizerScope = firstScope ? addChildScope(scope, ScopeKind.CatchStatement) : void 0;
finalizer = parseBlock(parser, context, finalizerScope, { $: labels }, tokenPos, linePos, colPos);
finalizer = parseBlock(
parser,
context,
finalizerScope,
{ $: labels },
parser.tokenPos,
parser.linePos,
parser.colPos
);
}

if (!handler && !finalizer) {
Expand Down
63 changes: 41 additions & 22 deletions test/parser/miscellaneous/onComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ describe('Miscellaneous - onComment', () => {
let onCommentCount = 0;
t.deepEqual(
parseScript('// Single line comment', {
onComment: (type: any, body: any) => {
ranges: true,
onComment: (type: any, body: any, start?: number, end?: number) => {
t.deepEqual(type, 'SingleLine');
t.deepEqual(body, ' Single line comment');
t.equal(start, 0);
t.equal(end, 22);
onCommentCount++;
}
}),
{
body: [],
sourceType: 'script',
type: 'Program'
type: 'Program',
start: 0,
end: 22,
range: [0, 22]
}
);
t.equal(onCommentCount, 1);
Expand All @@ -25,16 +31,22 @@ describe('Miscellaneous - onComment', () => {
let onCommentCount = 0;
t.deepEqual(
parseScript('// Single line comment\n', {
onComment: (type: any, body: any) => {
ranges: true,
onComment: (type: any, body: any, start?: number, end?: number) => {
t.deepEqual(type, 'SingleLine');
t.deepEqual(body, ' Single line comment\n');
t.deepEqual(body, ' Single line comment');
t.equal(start, 0);
t.equal(end, 22);
onCommentCount++;
}
}),
{
body: [],
sourceType: 'script',
type: 'Program'
type: 'Program',
start: 0,
end: 23,
range: [0, 23]
}
);
t.equal(onCommentCount, 1);
Expand Down Expand Up @@ -67,7 +79,7 @@ describe('Miscellaneous - onComment', () => {
onComment: (type: any, body: any, start: any, end: any) => {
t.deepEqual(type, 'MultiLine');
t.deepEqual(body, ' Multi line comment ');
t.deepEqual(start, 2);
t.deepEqual(start, 0);
t.deepEqual(end, 24);
onCommentCount++;
}
Expand Down Expand Up @@ -105,9 +117,9 @@ describe('Miscellaneous - onComment', () => {
});
t.deepEqual(arr, [
{
start: 2,
start: 0,
end: 24,
range: [2, 24],
range: [0, 24],
type: 'MultiLine',
value: ' Multi line comment '
}
Expand All @@ -122,44 +134,44 @@ describe('Miscellaneous - onComment', () => {
});
t.deepEqual(arr, [
{
start: 2,
start: 0,
end: 7,
range: [2, 7],
range: [0, 7],
type: 'MultiLine',
value: ' a '
},
{
start: 19,
start: 17,
end: 22,
range: [19, 22],
range: [17, 22],
type: 'MultiLine',
value: 'b'
},
{
start: 28,
start: 26,
end: 31,
range: [28, 31],
range: [26, 31],
type: 'MultiLine',
value: 'c'
},
{
start: 33,
start: 31,
end: 36,
range: [33, 36],
range: [31, 36],
type: 'MultiLine',
value: 'd'
},
{
start: 42,
start: 40,
end: 64,
range: [42, 64],
range: [40, 64],
type: 'MultiLine',
value: ' Multi line comment '
},
{
start: 69,
start: 67,
end: 77,
range: [69, 77],
range: [67, 77],
type: 'SingleLine',
value: ' The end'
}
Expand All @@ -169,17 +181,24 @@ describe('Miscellaneous - onComment', () => {
it('should extract html comments in array', () => {
const arr: any[] = [];
parseScript('<!--comment #1\n--> comment #2', {
ranges: true,
onComment: arr,
webcompat: true
});
t.deepEqual(arr, [
{
type: 'HTMLOpen',
value: 'comment #1\n'
value: 'comment #1',
start: 0,
end: 14,
range: [0, 14]
},
{
type: 'HTMLClose',
value: ' comment #2'
value: ' comment #2',
start: 14,
end: 29,
range: [14, 29]
}
]);
});
Expand Down

0 comments on commit fe00a67

Please sign in to comment.