flow-parser 0.324.0 → 0.325.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/babel-plugin.js +91 -0
- package/package.json +3 -2
package/babel-plugin.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
* @flow strict-local
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
// This file ships as-is (it is not run through the `dist` build), so it must be
|
|
14
|
+
// runtime-valid CommonJS. Flow types are written in comment syntax accordingly.
|
|
15
|
+
|
|
16
|
+
/*:: import type {ParserOptions} from 'flow-parser'; */
|
|
17
|
+
|
|
18
|
+
const FlowParser = require('flow-parser');
|
|
19
|
+
|
|
20
|
+
/*::
|
|
21
|
+
type Options = {
|
|
22
|
+
// When set to 'flow', will check files for a `@flow` annotation to apply
|
|
23
|
+
// this plugin, otherwise falling back to Babel's parser.
|
|
24
|
+
//
|
|
25
|
+
// This is independent of `parserOpts.flow`, which may customise 'detect'
|
|
26
|
+
// behaviour within hermes-parser (downstream from this plugin).
|
|
27
|
+
//
|
|
28
|
+
// Defaults to 'all'.
|
|
29
|
+
parseLangTypes?: 'flow' | 'all',
|
|
30
|
+
};
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
module.exports = function BabelPluginSyntaxFlowParser(
|
|
34
|
+
// $FlowExpectedError[unclear-type] We don't have types for this.
|
|
35
|
+
api /*: any */,
|
|
36
|
+
options /*: Options */,
|
|
37
|
+
) /*: Readonly<{...}> */ {
|
|
38
|
+
api.assertVersion('^7.0.0 || ^8.0.0-alpha.6');
|
|
39
|
+
|
|
40
|
+
const {parseLangTypes = 'all'} = options;
|
|
41
|
+
|
|
42
|
+
let curParserOpts /*: ParserOptions */ = {};
|
|
43
|
+
let curFilename /*: ?string */ = null;
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
name: 'syntax-flow-parser',
|
|
47
|
+
|
|
48
|
+
manipulateOptions(
|
|
49
|
+
opts /*: Readonly<{parserOpts: ParserOptions, filename?: ?string}> */,
|
|
50
|
+
) {
|
|
51
|
+
curParserOpts = opts.parserOpts;
|
|
52
|
+
curFilename = opts.filename;
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
// API suggested via https://babeljs.io/docs/babel-parser#will-the-babel-parser-support-a-plugin-system
|
|
56
|
+
parserOverride(code /*: string */) {
|
|
57
|
+
const filename = curFilename;
|
|
58
|
+
if (
|
|
59
|
+
filename != null &&
|
|
60
|
+
(filename.endsWith('.ts') || filename.endsWith('.tsx'))
|
|
61
|
+
) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const parserOpts /*: ParserOptions */ = {};
|
|
66
|
+
for (const [key, value] of Object.entries(curParserOpts)) {
|
|
67
|
+
if (FlowParser.ParserOptionsKeys.has(key)) {
|
|
68
|
+
// $FlowExpectedError[incompatible-type]
|
|
69
|
+
parserOpts[key] = value;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (parseLangTypes === 'flow' && !/@flow/.test(code)) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (parseLangTypes === 'all' && parserOpts.flow == null) {
|
|
78
|
+
parserOpts.flow = 'all';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return FlowParser.parse(code, {
|
|
82
|
+
...parserOpts,
|
|
83
|
+
babel: true,
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
pre() {
|
|
88
|
+
curParserOpts = {};
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "flow-parser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.325.0",
|
|
4
4
|
"description": "JavaScript parser written in OCaml. Produces ESTree AST",
|
|
5
5
|
"homepage": "https://flow.org",
|
|
6
6
|
"license": "MIT",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"email": "flow@fb.com"
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
|
+
"babel-plugin.js",
|
|
12
13
|
"dist",
|
|
13
14
|
"README.md"
|
|
14
15
|
],
|
|
@@ -22,7 +23,7 @@
|
|
|
22
23
|
"test": "node test/run_tests.js"
|
|
23
24
|
},
|
|
24
25
|
"dependencies": {
|
|
25
|
-
"flow-estree": "0.
|
|
26
|
+
"flow-estree": "0.325.0"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"@babel/generator": "7.7.4",
|