forked from prettier/prettier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
56 lines (51 loc) · 1.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"use strict";
const babylon = require("babylon");
const Printer = require("./src/printer").Printer;
const flowParser = require("flow-parser");
const comments = require("./src/comments");
var babylonOptions = {
sourceType: 'module',
allowImportExportEverywhere: false,
allowReturnOutsideFunction: false,
plugins: [
'jsx',
'flow',
'doExpressions',
'objectRestSpread',
'decorators',
'classProperties',
'exportExtensions',
'asyncGenerators',
'functionBind',
'functionSent',
'dynamicImport'
]
};
module.exports = {
format: function(text, opts) {
opts = opts || {};
let ast;
if(opts.useFlowParser) {
ast = flowParser.parse(text);
if(ast.errors.length > 0) {
let msg = ast.errors[0].message + " on line " + ast.errors[0].loc.start.line
if(opts.filename) {
msg += " in file " + opts.filename;
}
throw new Error(msg);
}
}
else {
ast = babylon.parse(text, babylonOptions);
}
// Interleave comment nodes
if(ast.comments) {
comments.attach(ast.comments, ast, text);
ast.comments = [];
}
ast.tokens = [];
opts.originalText = text;
const printer = new Printer(opts);
return printer.printGenerically(ast).code;
}
};