babel-plugin-syntax-hermes-parser 0.25.0 → 0.25.1

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/dist/index.js CHANGED
@@ -21,8 +21,11 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
21
21
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
22
22
 
23
23
  function BabelPluginSyntaxHermesParser( // $FlowExpectedError[unclear-type] We don't have types for this.
24
- api) {
24
+ api, options) {
25
25
  api.assertVersion('^7.0.0 || ^8.0.0-alpha.6');
26
+ const {
27
+ parseLangTypes = 'all'
28
+ } = options;
26
29
  let curParserOpts = {};
27
30
  let curFilename = null;
28
31
  return {
@@ -41,16 +44,20 @@ api) {
41
44
  return;
42
45
  }
43
46
 
44
- const opts = {};
47
+ const parserOpts = {};
45
48
 
46
49
  for (const [key, value] of Object.entries(curParserOpts)) {
47
50
  if (HermesParser.ParserOptionsKeys.has(key)) {
48
51
  // $FlowExpectedError[incompatible-type]
49
- opts[key] = value;
52
+ parserOpts[key] = value;
50
53
  }
51
54
  }
52
55
 
53
- return HermesParser.parse(code, { ...opts,
56
+ if (parseLangTypes === 'flow' && !/@flow/.test(code)) {
57
+ return;
58
+ }
59
+
60
+ return HermesParser.parse(code, { ...parserOpts,
54
61
  babel: true
55
62
  });
56
63
  },
@@ -14,12 +14,28 @@ import type {ParserOptions} from 'hermes-parser';
14
14
 
15
15
  import * as HermesParser from 'hermes-parser';
16
16
 
17
+ type Options = {
18
+ /**
19
+ * When set to 'flow', will check files for a `@flow` annotation to apply
20
+ * this plugin, otherwise falling back to Babel's parser.
21
+ *
22
+ * This is independent of `parserOpts.flow`, which may customise 'detect'
23
+ * behaviour within hermes-parser (downstream from this plugin).
24
+ *
25
+ * Defaults to 'all'.
26
+ */
27
+ parseLangTypes?: 'flow' | 'all',
28
+ };
29
+
17
30
  export default function BabelPluginSyntaxHermesParser(
18
31
  // $FlowExpectedError[unclear-type] We don't have types for this.
19
32
  api: any,
33
+ options: Options,
20
34
  ): $ReadOnly<{...}> {
21
35
  api.assertVersion('^7.0.0 || ^8.0.0-alpha.6');
22
36
 
37
+ const {parseLangTypes = 'all'} = options;
38
+
23
39
  let curParserOpts: ParserOptions = {};
24
40
  let curFilename: ?string = null;
25
41
 
@@ -42,14 +58,20 @@ export default function BabelPluginSyntaxHermesParser(
42
58
  ) {
43
59
  return;
44
60
  }
45
- const opts: ParserOptions = {};
61
+
62
+ const parserOpts: ParserOptions = {};
46
63
  for (const [key, value] of Object.entries(curParserOpts)) {
47
64
  if (HermesParser.ParserOptionsKeys.has(key)) {
48
65
  // $FlowExpectedError[incompatible-type]
49
- opts[key] = value;
66
+ parserOpts[key] = value;
50
67
  }
51
68
  }
52
- return HermesParser.parse(code, {...opts, babel: true});
69
+
70
+ if (parseLangTypes === 'flow' && !/@flow/.test(code)) {
71
+ return;
72
+ }
73
+
74
+ return HermesParser.parse(code, {...parserOpts, babel: true});
53
75
  },
54
76
 
55
77
  pre() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-plugin-syntax-hermes-parser",
3
- "version": "0.25.0",
3
+ "version": "0.25.1",
4
4
  "description": "Babel plugin which switches Babel to use the Hermes parser.",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
@@ -9,7 +9,7 @@
9
9
  "url": "git@github.com:facebook/hermes.git"
10
10
  },
11
11
  "dependencies": {
12
- "hermes-parser": "0.25.0"
12
+ "hermes-parser": "0.25.1"
13
13
  },
14
14
  "files": [
15
15
  "dist",