Skip to content

Instantly share code, notes, and snippets.

@KFlash
Created December 15, 2019 19:51
Show Gist options
  • Save KFlash/0a8781823d79532eaa39320aa7983b90 to your computer and use it in GitHub Desktop.
Save KFlash/0a8781823d79532eaa39320aa7983b90 to your computer and use it in GitHub Desktop.
startup
export function parseSource(source: string, options: Options | void, context: Context): ESTree.Program {
if (options != null) {
if (options.module) context |= Context.Module | Context.Strict;
if (options.next) context |= Context.OptionsNext;
if (options.loc) context |= Context.OptionsLoc;
if (options.disableWebCompat) context |= Context.OptionsDisableWebCompat;
if (options.directives) context |= Context.OptionsDirectives | Context.OptionsRaw;
if (options.raw) context |= Context.OptionsRaw;
if (options.impliedStrict) context |= Context.Strict;
}
const parser = create(source);
skipHashBang(parser, source);
nextToken(parser, context, /* allowRegExp */ 1);
if (context & Context.Module) {
const body = parseModuleItemListAndDirectives(parser, context | Context.InGlobal, {
parent: void 0,
type: ScopeKind.Block
});
return context & Context.OptionsLoc
? {
type: 'Program',
sourceType: 'module',
body,
start: 0,
end: source.length,
loc: {
start: {
line: 1,
column: 0
},
end: {
line: parser.lineBase,
column: parser.index - parser.offset
}
}
}
: {
type: 'Program',
sourceType: 'module',
body
};
}
const body = parseStatementList(parser, context | Context.InGlobal, {
parent: void 0,
type: ScopeKind.Block
});
return context & Context.OptionsLoc
? {
type: 'Program',
sourceType: 'script',
body,
start: 0,
end: source.length,
loc: {
start: {
line: 1,
column: 0
},
end: {
line: parser.lineBase,
column: parser.index - parser.offset
}
}
}
: {
type: 'Program',
sourceType: 'script',
body
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment