Add support for an InterpreterDirective AST node.

This commit is contained in:
Logan Smyth 2018-05-12 10:50:33 -07:00
parent c0013264b7
commit 2058e0686e
2113 changed files with 2281 additions and 162 deletions

View File

@ -26,6 +26,7 @@
"column": 0
}
},
"interpreter": null,
"sourceType": "module",
"body": [
{

View File

@ -1,4 +1,10 @@
export function File(node: Object) {
if (node.program) {
// Print this here to ensure that Program node 'leadingComments' still
// get printed after the hashbang.
this.print(node.program.interpreter, node);
}
this.print(node.program, node);
}
@ -44,4 +50,8 @@ export function Directive(node: Object) {
this.semicolon();
}
export function InterpreterDirective(node: Object) {
this.token(`#!${node.value}\n`);
}
export { StringLiteral as DirectiveLiteral } from "./types";

View File

@ -0,0 +1,5 @@
#!env node
"use strict";
console.log("test");

View File

@ -0,0 +1,4 @@
#!env node
"use strict";
console.log("test");

View File

@ -19,15 +19,6 @@ export default class Parser extends StatementParser {
this.input = input;
this.plugins = pluginsMap(this.options.plugins);
this.filename = options.sourceFilename;
// If enabled, skip leading hashbang line.
if (
this.state.pos === 0 &&
this.input[0] === "#" &&
this.input[1] === "!"
) {
this.skipLineComment(2);
}
}
parse(): File {

View File

@ -23,6 +23,8 @@ export default class StatementParser extends ExpressionParser {
parseTopLevel(file: N.File, program: N.Program): N.File {
program.sourceType = this.options.sourceType;
program.interpreter = this.parseInterpreterDirective();
this.parseBlockBody(program, true, true, tt.eof);
file.program = this.finishNode(program, "Program");
@ -57,6 +59,17 @@ export default class StatementParser extends ExpressionParser {
return this.finishNodeAt(directive, "Directive", stmt.end, stmt.loc.end);
}
parseInterpreterDirective(): N.InterpreterDirective | null {
if (!this.match(tt.interpreterDirective)) {
return null;
}
const node = this.startNode();
node.value = this.state.value;
this.next();
return this.finishNode(node, "InterpreterDirective");
}
// Parse a single statement.
//
// If expecting a statement and finding a slash operator, parse a

View File

@ -437,6 +437,32 @@ export default class Tokenizer extends LocationParser {
}
}
readToken_interpreter(): boolean {
if (this.state.pos !== 0 || this.state.input.length < 2) return false;
const start = this.state.pos;
this.state.pos += 1;
let ch = this.input.charCodeAt(this.state.pos);
if (ch !== charCodes.exclamationMark) return false;
while (
ch !== charCodes.lineFeed &&
ch !== charCodes.carriageReturn &&
ch !== charCodes.lineSeparator &&
ch !== charCodes.paragraphSeparator &&
++this.state.pos < this.input.length
) {
ch = this.input.charCodeAt(this.state.pos);
}
const value = this.input.slice(start + 2, this.state.pos);
this.finishToken(tt.interpreterDirective, value);
return true;
}
readToken_mult_modulo(code: number): void {
// '%*'
let type = code === charCodes.asterisk ? tt.star : tt.modulo;
@ -626,6 +652,10 @@ export default class Tokenizer extends LocationParser {
getTokenFromCode(code: number): void {
switch (code) {
case charCodes.numberSign:
if (this.state.pos === 0 && this.readToken_interpreter()) {
return;
}
if (
(this.hasPlugin("classPrivateProperties") ||
this.hasPlugin("classPrivateMethods")) &&

View File

@ -112,6 +112,9 @@ export const types: { [name: string]: TokenType } = {
at: new TokenType("@"),
hash: new TokenType("#"),
// Special hashbang token.
interpreterDirective: new TokenType("#!..."),
// Operators. These carry several kinds of properties to help the
// parser use them properly (the presence of these properties is
// what categorizes them as operators).

View File

@ -63,6 +63,11 @@ export type HasDecorators = NodeBase & {
decorators?: $ReadOnlyArray<Decorator>,
};
export type InterpreterDirective = NodeBase & {
type: "InterpreterDirective",
value: string,
};
export type Identifier = PatternBase & {
type: "Identifier",
name: string,
@ -133,6 +138,7 @@ export type Program = NodeBase & {
sourceType: "script" | "module",
body: Array<Statement | ModuleDeclaration>, // TODO: $ReadOnlyArray
directives: $ReadOnlyArray<Directive>, // TODO: Not in spec
interpreter: InterpreterDirective | null,
};
// Functions

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "BlockStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "IfStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportDefaultDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",

View File

@ -27,6 +27,22 @@
}
},
"sourceType": "module",
"interpreter": {
"type": "InterpreterDirective",
"start": 0,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 25
}
},
"value": "/usr/bin/env babel-node"
},
"body": [
{
"type": "ImportDeclaration",
@ -112,45 +128,9 @@
"raw": "'foobar'"
},
"value": "foobar"
},
"leadingComments": [
{
"type": "CommentLine",
"value": "/usr/bin/env babel-node",
"start": 0,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 25
}
}
}
]
}
}
],
"directives": []
},
"comments": [
{
"type": "CommentLine",
"value": "/usr/bin/env babel-node",
"start": 0,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 25
}
}
}
]
}
}

View File

@ -27,6 +27,22 @@
}
},
"sourceType": "script",
"interpreter": {
"type": "InterpreterDirective",
"start": 0,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 25
}
},
"value": "/usr/bin/env babel-node"
},
"body": [
{
"type": "VariableDeclaration",
@ -148,45 +164,9 @@
}
}
],
"kind": "var",
"leadingComments": [
{
"type": "CommentLine",
"value": "/usr/bin/env babel-node",
"start": 0,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 25
}
}
}
]
"kind": "var"
}
],
"directives": []
},
"comments": [
{
"type": "CommentLine",
"value": "/usr/bin/env babel-node",
"start": 0,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 25
}
}
}
]
}
}

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "SwitchStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "SwitchStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "SwitchStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "SwitchStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",

View File

@ -29,6 +29,7 @@
"filename": "path/to/input-file.js"
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "BlockStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "IfStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",

View File

@ -35,6 +35,7 @@
26
],
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportAllDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportDefaultDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportNamedDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [],
"directives": [
{

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [],
"directives": [
{

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

View File

@ -27,6 +27,7 @@
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",

Some files were not shown because too many files have changed in this diff Show More