flow-parser
Advanced tools
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| * @flow strict-local | ||
| * @format | ||
| */ | ||
| 'use strict'; | ||
| // This file ships as-is (it is not run through the `dist` build), so it must be | ||
| // runtime-valid CommonJS. Flow types are written in comment syntax accordingly. | ||
| /*:: import type {ParserOptions} from 'flow-parser'; */ | ||
| const FlowParser = require('flow-parser'); | ||
| /*:: | ||
| type Options = { | ||
| // When set to 'flow', will check files for a `@flow` annotation to apply | ||
| // this plugin, otherwise falling back to Babel's parser. | ||
| // | ||
| // This is independent of `parserOpts.flow`, which may customise 'detect' | ||
| // behaviour within hermes-parser (downstream from this plugin). | ||
| // | ||
| // Defaults to 'all'. | ||
| parseLangTypes?: 'flow' | 'all', | ||
| }; | ||
| */ | ||
| module.exports = function BabelPluginSyntaxFlowParser( | ||
| // $FlowExpectedError[unclear-type] We don't have types for this. | ||
| api /*: any */, | ||
| options /*: Options */, | ||
| ) /*: Readonly<{...}> */ { | ||
| api.assertVersion('^7.0.0 || ^8.0.0-alpha.6'); | ||
| const {parseLangTypes = 'all'} = options; | ||
| let curParserOpts /*: ParserOptions */ = {}; | ||
| let curFilename /*: ?string */ = null; | ||
| return { | ||
| name: 'syntax-flow-parser', | ||
| manipulateOptions( | ||
| opts /*: Readonly<{parserOpts: ParserOptions, filename?: ?string}> */, | ||
| ) { | ||
| curParserOpts = opts.parserOpts; | ||
| curFilename = opts.filename; | ||
| }, | ||
| // API suggested via https://babeljs.io/docs/babel-parser#will-the-babel-parser-support-a-plugin-system | ||
| parserOverride(code /*: string */) { | ||
| const filename = curFilename; | ||
| if ( | ||
| filename != null && | ||
| (filename.endsWith('.ts') || filename.endsWith('.tsx')) | ||
| ) { | ||
| return; | ||
| } | ||
| const parserOpts /*: ParserOptions */ = {}; | ||
| for (const [key, value] of Object.entries(curParserOpts)) { | ||
| if (FlowParser.ParserOptionsKeys.has(key)) { | ||
| // $FlowExpectedError[incompatible-type] | ||
| parserOpts[key] = value; | ||
| } | ||
| } | ||
| if (parseLangTypes === 'flow' && !/@flow/.test(code)) { | ||
| return; | ||
| } | ||
| if (parseLangTypes === 'all' && parserOpts.flow == null) { | ||
| parserOpts.flow = 'all'; | ||
| } | ||
| return FlowParser.parse(code, { | ||
| ...parserOpts, | ||
| babel: true, | ||
| }); | ||
| }, | ||
| pre() { | ||
| curParserOpts = {}; | ||
| }, | ||
| }; | ||
| }; |
+3
-2
| { | ||
| "name": "flow-parser", | ||
| "version": "0.324.0", | ||
| "version": "0.325.0", | ||
| "description": "JavaScript parser written in OCaml. Produces ESTree AST", | ||
@@ -12,2 +12,3 @@ "homepage": "https://flow.org", | ||
| "files": [ | ||
| "babel-plugin.js", | ||
| "dist", | ||
@@ -26,3 +27,3 @@ "README.md" | ||
| "dependencies": { | ||
| "flow-estree": "0.324.0" | ||
| "flow-estree": "0.325.0" | ||
| }, | ||
@@ -29,0 +30,0 @@ "devDependencies": { |
1310968
0.19%56
1.82%6441
1.18%+ Added
- Removed
Updated