Skip to content

Commit

Permalink
feat(parser): add .range: [start, end] to improve compatibility with …
Browse files Browse the repository at this point in the history
…ESTree tools

src/parser.ts was formatted by prettier.
  • Loading branch information
3cp committed May 27, 2020
1 parent 4c6ae0e commit f60ae26
Show file tree
Hide file tree
Showing 11 changed files with 776 additions and 255 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ This is the available options:
// The flag to enable stage 3 support (ESNext)
next: false;

// The flag to enable start and end offsets to each node
// The flag to enable start, end offsets and range: [start, end] to each node
ranges: false;

// Enable web compability
Expand Down
2 changes: 2 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ export function finishNode<T extends Node>(
if (context & Context.OptionsRanges) {
node.start = start;
node.end = parser.startPos;
node.range = [start, parser.startPos];
}

if (context & Context.OptionsLoc) {
Expand Down Expand Up @@ -775,6 +776,7 @@ export function pushComment(context: Context, array: any[]): any {
if (context & Context.OptionsRanges) {
comment.start = start;
comment.end = end;
comment.range = [start, end];
}
array.push(comment);
};
Expand Down
1 change: 1 addition & 0 deletions src/estree.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface _Node {
start?: number;
end?: number;
range?: [number, number];
loc?: SourceLocation | null;
}

Expand Down
118 changes: 62 additions & 56 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export function parseSource(source: string, options: Options | void, context: Co
if (context & Context.OptionsRanges) {
node.start = 0;
node.end = source.length;
node.range = [0, source.length];
}

if (context & Context.OptionsLoc) {
Expand Down Expand Up @@ -336,16 +337,18 @@ export function parseStatementList(
}

while (parser.token !== Token.EOF) {
statements.push(parseStatementListItem(
parser,
context,
scope,
Origin.TopLevel,
{},
parser.tokenPos,
parser.linePos,
parser.colPos
) as ESTree.Statement);
statements.push(
parseStatementListItem(
parser,
context,
scope,
Origin.TopLevel,
{},
parser.tokenPos,
parser.linePos,
parser.colPos
) as ESTree.Statement
);
}
return statements;
}
Expand All @@ -362,7 +365,7 @@ export function parseModuleItemList(
parser: ParserState,
context: Context,
scope: ScopeState | undefined
): (ReturnType<typeof parseDirective | typeof parseModuleItem>)[] {
): ReturnType<typeof parseDirective | typeof parseModuleItem>[] {
// ecma262/#prod-Module
// Module :
// ModuleBody?
Expand All @@ -373,7 +376,7 @@ export function parseModuleItemList(

nextToken(parser, context | Context.AllowRegExp);

const statements: (ReturnType<typeof parseDirective | typeof parseModuleItem>)[] = [];
const statements: ReturnType<typeof parseDirective | typeof parseModuleItem>[] = [];

// Avoid this if we're not going to create any directive nodes. This is likely to be the case
// most of the time, considering the prevalence of strict mode and the fact modules
Expand All @@ -386,14 +389,9 @@ export function parseModuleItemList(
}

while (parser.token !== Token.EOF) {
statements.push(parseModuleItem(
parser,
context,
scope,
parser.tokenPos,
parser.linePos,
parser.colPos
) as ESTree.Statement);
statements.push(
parseModuleItem(parser, context, scope, parser.tokenPos, parser.linePos, parser.colPos) as ESTree.Statement
);
}
return statements;
}
Expand Down Expand Up @@ -804,16 +802,18 @@ export function parseBlock(
const body: ESTree.Statement[] = [];
consume(parser, context | Context.AllowRegExp, Token.LeftBrace);
while (parser.token !== Token.RightBrace) {
body.push(parseStatementListItem(
parser,
context,
scope,
Origin.BlockStatement,
{ $: labels },
parser.tokenPos,
parser.linePos,
parser.colPos
) as any);
body.push(
parseStatementListItem(
parser,
context,
scope,
Origin.BlockStatement,
{ $: labels },
parser.tokenPos,
parser.linePos,
parser.colPos
) as any
);
}

consume(parser, context | Context.AllowRegExp, Token.RightBrace);
Expand Down Expand Up @@ -1405,18 +1405,20 @@ export function parseSwitchStatement(
parser.token !== Token.RightBrace &&
parser.token !== Token.DefaultKeyword
) {
consequent.push(parseStatementListItem(
parser,
context | Context.InSwitch,
scope,
Origin.BlockStatement,
{
$: labels
},
parser.tokenPos,
parser.linePos,
parser.colPos
) as ESTree.Statement);
consequent.push(
parseStatementListItem(
parser,
context | Context.InSwitch,
scope,
Origin.BlockStatement,
{
$: labels
},
parser.tokenPos,
parser.linePos,
parser.colPos
) as ESTree.Statement
);
}

cases.push(
Expand Down Expand Up @@ -3221,7 +3223,7 @@ export function parseAssignmentExpression(
if ((token & Token.IsAssignOp) === Token.IsAssignOp) {
if (parser.assignable & AssignmentKind.CannotAssign) report(parser, Errors.CantAssignTo);
if (
(!isPattern && (token === Token.Assign && ((left as ESTree.Expression).type as string) === 'ArrayExpression')) ||
(!isPattern && token === Token.Assign && ((left as ESTree.Expression).type as string) === 'ArrayExpression') ||
((left as ESTree.Expression).type as string) === 'ObjectExpression'
) {
reinterpretToPattern(parser, left);
Expand Down Expand Up @@ -3715,7 +3717,9 @@ export function parseFunctionBody(
if (
context & Context.OptionsLexical &&
scope &&
(scopeError !== void 0 && (prevContext & Context.Strict) < 1 && (context & Context.InGlobal) < 1)
scopeError !== void 0 &&
(prevContext & Context.Strict) < 1 &&
(context & Context.InGlobal) < 1
) {
reportScopeError(scopeError);
}
Expand All @@ -3728,16 +3732,18 @@ export function parseFunctionBody(
parser.destructible = (parser.destructible | DestructuringKind.Yield) ^ DestructuringKind.Yield;

while (parser.token !== Token.RightBrace) {
body.push(parseStatementListItem(
parser,
context,
scope,
Origin.TopLevel,
{},
parser.tokenPos,
parser.linePos,
parser.colPos
) as ESTree.Statement);
body.push(
parseStatementListItem(
parser,
context,
scope,
Origin.TopLevel,
{},
parser.tokenPos,
parser.linePos,
parser.colPos
) as ESTree.Statement
);
}

consume(
Expand Down Expand Up @@ -7181,7 +7187,7 @@ export function parseFormalParametersOrFormalList(
scope: ScopeState | undefined,
inGroup: 0 | 1,
kind: BindingKind
): (ESTree.Parameter)[] {
): ESTree.Parameter[] {
/**
* FormalParameter :
* BindingElement
Expand Down Expand Up @@ -7324,7 +7330,7 @@ export function parseFormalParametersOrFormalList(

if (isSimpleParameterList) parser.flags |= Flags.SimpleParameterList;

if (scope && ((isSimpleParameterList || context & Context.Strict) && scope.scopeError !== void 0)) {
if (scope && (isSimpleParameterList || context & Context.Strict) && scope.scopeError !== void 0) {
reportScopeError(scope.scopeError);
}

Expand Down
13 changes: 13 additions & 0 deletions test/parser/miscellaneous/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ describe('Expressions - API', () => {
name: 'a',
start: 0,
end: 1,
range: [0, 1],
loc: {
start: {
line: 1,
Expand All @@ -298,6 +299,7 @@ describe('Expressions - API', () => {
name: 'x',
start: 6,
end: 7,
range: [6, 7],
loc: {
start: {
line: 1,
Expand All @@ -316,6 +318,7 @@ describe('Expressions - API', () => {
name: 'dd',
start: 11,
end: 13,
range: [11, 13],
loc: {
start: {
line: 1,
Expand All @@ -332,6 +335,7 @@ describe('Expressions - API', () => {
name: 'aa',
start: 17,
end: 19,
range: [17, 19],
loc: {
start: {
line: 1,
Expand All @@ -346,6 +350,7 @@ describe('Expressions - API', () => {
operator: '&&',
start: 11,
end: 19,
range: [11, 19],
loc: {
start: {
line: 1,
Expand All @@ -360,6 +365,7 @@ describe('Expressions - API', () => {
operator: '||',
start: 6,
end: 19,
range: [6, 19],
loc: {
start: {
line: 1,
Expand All @@ -376,6 +382,7 @@ describe('Expressions - API', () => {
name: 'y',
start: 23,
end: 24,
range: [23, 24],
loc: {
start: {
line: 1,
Expand All @@ -390,6 +397,7 @@ describe('Expressions - API', () => {
operator: '/',
start: 5,
end: 24,
range: [5, 24],
loc: {
start: {
line: 1,
Expand All @@ -406,6 +414,7 @@ describe('Expressions - API', () => {
name: 'foo',
start: 27,
end: 30,
range: [27, 30],
loc: {
start: {
line: 1,
Expand All @@ -420,6 +429,7 @@ describe('Expressions - API', () => {
operator: '-',
start: 5,
end: 30,
range: [5, 30],
loc: {
start: {
line: 1,
Expand All @@ -434,6 +444,7 @@ describe('Expressions - API', () => {
operator: '??',
start: 0,
end: 30,
range: [0, 30],
loc: {
start: {
line: 1,
Expand All @@ -447,6 +458,7 @@ describe('Expressions - API', () => {
},
start: 0,
end: 30,
range: [0, 30],
loc: {
start: {
line: 1,
Expand All @@ -461,6 +473,7 @@ describe('Expressions - API', () => {
],
start: 0,
end: 30,
range: [0, 30],
loc: {
start: {
line: 1,
Expand Down

0 comments on commit f60ae26

Please sign in to comment.