diff --git a/packages/babel-core/src/index.js b/packages/babel-core/src/index.js index dbf2cff91b..7b30f15dbb 100644 --- a/packages/babel-core/src/index.js +++ b/packages/babel-core/src/index.js @@ -43,6 +43,8 @@ export { default as transformFileSync } from "./transform-file-sync"; export { default as transformFromAst } from "./transform-ast"; export { default as transformFromAstSync } from "./transform-ast-sync"; +export { default as parse } from "./parse"; + /** * Recommended set of compilable extensions. Not used in @babel/core directly, but meant as * as an easy source for tooling making use of @babel/core. diff --git a/packages/babel-core/src/parse.js b/packages/babel-core/src/parse.js new file mode 100644 index 0000000000..1a7e2231a3 --- /dev/null +++ b/packages/babel-core/src/parse.js @@ -0,0 +1,22 @@ +// @flow + +import loadConfig, { InputOptions } from "./config"; +import normalizeFile from "./transformation/normalize-file"; +import normalizeOptions from "./transformation/normalize-opts"; + +type AstRoot = BabelNodeFile | BabelNodeProgram; + +export default function parse( + code: string, + opts: InputOptions, +): AstRoot | null { + const config = loadConfig(opts); + + if (config === null) { + return null; + } + + const file = normalizeFile(config.passes, normalizeOptions(config), code); + + return file.ast; +} diff --git a/packages/babel-core/test/fixtures/parse/.babelignore b/packages/babel-core/test/fixtures/parse/.babelignore new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/babel-core/test/fixtures/parse/.babelrc b/packages/babel-core/test/fixtures/parse/.babelrc new file mode 100644 index 0000000000..da94220e79 --- /dev/null +++ b/packages/babel-core/test/fixtures/parse/.babelrc @@ -0,0 +1,3 @@ +{ + "plugins": ["@babel/syntax-decorators"] +} diff --git a/packages/babel-core/test/fixtures/parse/input.js b/packages/babel-core/test/fixtures/parse/input.js new file mode 100644 index 0000000000..e8d44fe090 --- /dev/null +++ b/packages/babel-core/test/fixtures/parse/input.js @@ -0,0 +1,6 @@ +@annotation +class MyClass { } + +function annotation(target) { + target.annotated = true; +} diff --git a/packages/babel-core/test/fixtures/parse/output.json b/packages/babel-core/test/fixtures/parse/output.json new file mode 100644 index 0000000000..7c9e1c3f4d --- /dev/null +++ b/packages/babel-core/test/fixtures/parse/output.json @@ -0,0 +1,286 @@ +{ + "type": "File", + "start": 0, + "end": 90, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 90, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "decorators": [ + { + "type": "Decorator", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "expression": { + "type": "Identifier", + "start": 1, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "annotation" + }, + "name": "annotation" + } + } + ], + "id": { + "type": "Identifier", + "start": 18, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 13 + }, + "identifierName": "MyClass" + }, + "name": "MyClass" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 26, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "body": [] + } + }, + { + "type": "FunctionDeclaration", + "start": 31, + "end": 90, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 40, + "end": 50, + "loc": { + "start": { + "line": 4, + "column": 9 + }, + "end": { + "line": 4, + "column": 19 + }, + "identifierName": "annotation" + }, + "name": "annotation" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 51, + "end": 57, + "loc": { + "start": { + "line": 4, + "column": 20 + }, + "end": { + "line": 4, + "column": 26 + }, + "identifierName": "target" + }, + "name": "target" + } + ], + "body": { + "type": "BlockStatement", + "start": 59, + "end": 90, + "loc": { + "start": { + "line": 4, + "column": 28 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 64, + "end": 88, + "loc": { + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 27 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 64, + "end": 87, + "loc": { + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 26 + } + }, + "operator": "=", + "left": { + "type": "MemberExpression", + "start": 64, + "end": 80, + "loc": { + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 19 + } + }, + "object": { + "type": "Identifier", + "start": 64, + "end": 70, + "loc": { + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 9 + }, + "identifierName": "target" + }, + "name": "target" + }, + "property": { + "type": "Identifier", + "start": 71, + "end": 80, + "loc": { + "start": { + "line": 5, + "column": 10 + }, + "end": { + "line": 5, + "column": 19 + }, + "identifierName": "annotated" + }, + "name": "annotated" + }, + "computed": false + }, + "right": { + "type": "BooleanLiteral", + "start": 83, + "end": 87, + "loc": { + "start": { + "line": 5, + "column": 22 + }, + "end": { + "line": 5, + "column": 26 + } + }, + "value": true + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + }, + "comments": [] +} diff --git a/packages/babel-core/test/parse.js b/packages/babel-core/test/parse.js new file mode 100644 index 0000000000..5663107f04 --- /dev/null +++ b/packages/babel-core/test/parse.js @@ -0,0 +1,25 @@ +import assert from "assert"; +import fs from "fs"; +import path from "path"; +import { parse } from "../lib"; + +function fixture(...args) { + return path.join(__dirname, "fixtures", "parse", ...args); +} + +describe("parse", function() { + it("should parse using configuration from .babelrc when a filename is provided", function() { + const input = fs.readFileSync(fixture("input.js"), "utf8"); + const output = fs.readFileSync(fixture("output.json"), "utf8"); + assert( + parse(input, { filename: fixture("input.js"), cwd: fixture() }), + output, + ); + }); + + it("should parse using passed in configuration", function() { + const input = fs.readFileSync(fixture("input.js"), "utf8"); + const output = fs.readFileSync(fixture("output.json"), "utf8"); + assert(parse(input, { parserOpts: { plugins: ["decorators"] } }), output); + }); +});