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

Support sourceType: "unambiguous" parsing #449

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,27 @@ plugins.jsx = jsxPlugin;
plugins.typescript = typescriptPlugin;

export function parse(input: string, options?: Options): File {
return getParser(options, input).parse();
if (options && options.sourceType === "unambiguous") {
options = Object.assign({}, options);
try {
options.sourceType = "module";
const ast = getParser(options, input).parse();

// Rather than try to parse as a script first, we opt to parse as a module and convert back
// to a script where possible to avoid having to do a full re-parse of the input content.
if (!hasModuleSyntax(ast)) ast.program.sourceType = "script";
return ast;
} catch (moduleError) {
try {
options.sourceType = "script";
return getParser(options, input).parse();
} catch (scriptError) {}

throw moduleError;
}
} else {
return getParser(options, input).parse();
}
}

export function parseExpression(input: string, options?: Options): Expression {
Expand Down Expand Up @@ -91,3 +111,16 @@ function getParserClass(
}
return cls;
}

function hasModuleSyntax(ast) {
return ast.program.body.some(
child =>
(child.type === "ImportDeclaration" &&
(!child.importKind || child.importKind === "value")) ||
(child.type === "ExportNamedDeclaration" &&
(!child.exportKind || child.exportKind === "value")) ||
(child.type === "ExportAllDeclaration" &&
(!child.exportKind || child.exportKind === "value")) ||
child.type === "ExportDefaultDeclaration",
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var foo = require("foo");
138 changes: 138 additions & 0 deletions test/fixtures/core/sourcetype-unambiguous/commonjs/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
{
"type": "File",
"start": 0,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 25
}
},
"program": {
"type": "Program",
"start": 0,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 25
}
},
"sourceType": "script",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 25
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 24
}
},
"id": {
"type": "Identifier",
"start": 4,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "foo"
},
"name": "foo"
},
"init": {
"type": "CallExpression",
"start": 10,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 24
}
},
"callee": {
"type": "Identifier",
"start": 10,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 17
},
"identifierName": "require"
},
"name": "require"
},
"arguments": [
{
"type": "StringLiteral",
"start": 18,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 18
},
"end": {
"line": 1,
"column": 23
}
},
"extra": {
"rawValue": "foo",
"raw": "\"foo\""
},
"value": "foo"
}
]
}
}
],
"kind": "var"
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sourceType": "unambiguous"
}
3 changes: 3 additions & 0 deletions test/fixtures/core/sourcetype-unambiguous/flow/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Foo } from "bar";
export type { Foo };
export type * from "bar";
Loading