Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Enable type-checking in src #393

Closed
wants to merge 17 commits into from
Closed
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"extends": "babel",
"env": {
"node": true
},
"rules": {
"indent": 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you disable this rule? Where there problems with it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I indent things correctly, this PR will have an unreadable diff because the indent of most lines will change. Don't know a good solution for that.

Copy link
Member

@danez danez Mar 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh? Wasn't it properly indented before?

Edit: Ah I see the mixin part. Nice catch. Yes we can enable and autofix later

👍

}
}
8 changes: 8 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
.*/lib/.*
.*/test/.*
.*/build/.*
.*/node_modules/.*
.*/rollup.config.js
.*/bin/.*
.*/scripts/.*

[include]

[libs]

[options]
all=true
strip_root=true
suppress_comment= \\(.\\|\n\\)*\\$FlowFixMe
suppress_comment= \\(.\\|\n\\)*\\$FlowIssue
suppress_comment= \\(.\\|\n\\)*\\$FlowIgnore
suppress_comment= \\(.\\|\n\\)*\\$FlowNonNull
suppress_comment= \\(.\\|\n\\)*\\$FlowCast
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Options } from "./options";
import Parser, { plugins } from "./parser";
import "./parser/util";
import "./parser/statement";
Expand All @@ -11,18 +12,20 @@ import { types as tokTypes } from "./tokenizer/types";
import "./tokenizer";
import "./tokenizer/context";

import * as N from "./types";

import estreePlugin from "./plugins/estree";
import flowPlugin from "./plugins/flow";
import jsxPlugin from "./plugins/jsx";
plugins.estree = estreePlugin;
plugins.flow = flowPlugin;
plugins.jsx = jsxPlugin;

export function parse(input, options) {
export function parse(input: string, options: Options): N.File {
return new Parser(options, input).parse();
}

export function parseExpression(input, options) {
export function parseExpression(input: string, options: Options): N.Expression {
const parser = new Parser(options, input);
if (parser.options.strictMode) {
parser.state.strict = true;
Expand Down
27 changes: 15 additions & 12 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// A second optional argument can be given to further configure
// the parser process. These options are recognized:

export const defaultOptions: {
sourceType: string,
sourceFilename: any,
startLine: number,
allowReturnOutsideFunction: boolean,
allowImportExportEverywhere: boolean,
allowSuperOutsideMethod: boolean,
plugins: Array<string>,
strictMode: any,
ranges: boolean,
} = {
export type Options = {
sourceType: "script" | "module";
sourceFilename?: string;
startLine: number;
allowReturnOutsideFunction: boolean;
allowImportExportEverywhere: boolean;
allowSuperOutsideMethod: boolean;
plugins: $ReadOnlyArray<string>;
strictMode: ?boolean;
ranges: boolean;
};

export const defaultOptions: Options = {
// Source type ("script" or "module") for different semantics
sourceType: "script",
// Source filename.
Expand Down Expand Up @@ -44,10 +46,11 @@ export const defaultOptions: {

// Interpret and default an options object

export function getOptions(opts?: Object): Object {
export function getOptions(opts?: Options): Options {
const options = {};
for (const key in defaultOptions) {
options[key] = opts && key in opts ? opts[key] : defaultOptions[key];
}
// $FlowIgnore
return options;
}
21 changes: 10 additions & 11 deletions src/parser/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@
*/

import Parser from "./index";
import * as N from "../types";

function last(stack) {
function last<T>(stack: $ReadOnlyArray<T>): T {
return stack[stack.length - 1];
}

const pp = Parser.prototype;

pp.addComment = function (comment) {
Parser.mixin(class extends Parser {
addComment(comment: N.Comment): void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can omit annotations if you don't export class or function

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of them could be omitted, but I've tested around and some of them can't without breaking compilation. I also think it's nice to have the type annotations locally so I don't have to look them up in parserTypes.js. The code worked fine without any type annotations, the point was to make it easier to read.

if (this.filename) comment.loc.filename = this.filename;
this.state.trailingComments.push(comment);
this.state.leadingComments.push(comment);
};
}

pp.processComment = function (node) {
processComment(node: N.Node): void {
if (node.type === "Program" && node.body.length > 0) return;

const stack = this.state.commentStack;
Expand Down Expand Up @@ -128,10 +128,8 @@ pp.processComment = function (node) {
// that comes after the node. Keep in mind that this could
// result in an empty array, and if so, the array must be
// deleted.
node.leadingComments = this.state.leadingComments.slice(0, i);
if ((node.leadingComments: Array<any>).length === 0) {
node.leadingComments = null;
}
const leadingComments = this.state.leadingComments.slice(0, i);
node.leadingComments = leadingComments.length === 0 ? null : leadingComments;

// Similarly, trailing comments are attached later. The variable
// must be reset to null if there are no trailing comments.
Expand All @@ -153,4 +151,5 @@ pp.processComment = function (node) {
}

stack.push(node);
};
}
});
Loading