babel-core: Add parse method (#7291)
This commit is contained in:
parent
023550c87b
commit
22c8f6376c
@ -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.
|
||||
|
||||
22
packages/babel-core/src/parse.js
Normal file
22
packages/babel-core/src/parse.js
Normal file
@ -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;
|
||||
}
|
||||
0
packages/babel-core/test/fixtures/parse/.babelignore
vendored
Normal file
0
packages/babel-core/test/fixtures/parse/.babelignore
vendored
Normal file
3
packages/babel-core/test/fixtures/parse/.babelrc
vendored
Normal file
3
packages/babel-core/test/fixtures/parse/.babelrc
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["@babel/syntax-decorators"]
|
||||
}
|
||||
6
packages/babel-core/test/fixtures/parse/input.js
vendored
Normal file
6
packages/babel-core/test/fixtures/parse/input.js
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
@annotation
|
||||
class MyClass { }
|
||||
|
||||
function annotation(target) {
|
||||
target.annotated = true;
|
||||
}
|
||||
286
packages/babel-core/test/fixtures/parse/output.json
vendored
Normal file
286
packages/babel-core/test/fixtures/parse/output.json
vendored
Normal file
@ -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": []
|
||||
}
|
||||
25
packages/babel-core/test/parse.js
Normal file
25
packages/babel-core/test/parse.js
Normal file
@ -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);
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user