forked from prettier/prettier
/
flow.js
66 lines (59 loc) · 2.52 KB
/
flow.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
57
58
59
60
61
62
63
64
65
66
"use strict";
const createError = require("../../common/parser-create-error.js");
const createParser = require("./utils/create-parser.js");
const replaceHashbang = require("./utils/replace-hashbang.js");
const postprocess = require("./postprocess/index.js");
// https://github.com/facebook/flow/tree/master/packages/flow-parser#options
const parseOptions = {
// `all_comments` (boolean, default `true`) - include a list of all comments from the whole program
// all_comments: true,
// `comments` (boolean, default `true`) - attach comments to AST nodes (`leadingComments` and `trailingComments`)
comments: false,
// `enums` (boolean, default `false`) - enable parsing of Flow enums
enums: true,
// `esproposal_class_instance_fields` (boolean, default `false`) - enable parsing of class instance fields
esproposal_class_instance_fields: true,
// `esproposal_class_static_fields` (boolean, default `false`) - enable parsing of class static fields
esproposal_class_static_fields: true,
// `esproposal_decorators` (boolean, default `false`) - enable parsing of decorators
esproposal_decorators: true,
// `esproposal_export_star_as` (boolean, default `false`) - enable parsing of `export * as` syntax
esproposal_export_star_as: true,
// `esproposal_nullish_coalescing` (boolean, default `false`) - enable parsing of nullish coalescing (`??`)
esproposal_nullish_coalescing: true,
// `esproposal_optional_chaining` (boolean, default `false`) - enable parsing of optional chaining (`?.`)
esproposal_optional_chaining: true,
// `types` (boolean, default `true`) - enable parsing of Flow types
// types: true,
// `use_strict` (boolean, default `false`) - treat the file as strict, without needing a "use strict" directive
// use_strict: false,
// Not documented
tokens: true,
};
function createParseError(error) {
const {
message,
loc: { start, end },
} = error;
return createError(message, {
start: { line: start.line, column: start.column + 1 },
end: { line: end.line, column: end.column + 1 },
});
}
function parse(text, parsers, options = {}) {
// Inline the require to avoid loading all the JS if we don't use it
const { parse } = require("flow-parser");
const ast = parse(replaceHashbang(text), parseOptions);
const [error] = ast.errors;
if (error) {
throw createParseError(error);
}
options.originalText = text;
return postprocess(ast, options);
}
// Export as a plugin so we can reuse the same bundle for UMD loading
module.exports = {
parsers: {
flow: createParser(parse),
},
};