hermes-parser 0.4.6 → 0.6.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.
@@ -1,26 +1,85 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
- * @flow
7
+ * @flow strict-local
8
8
  * @format
9
9
  */
10
10
 
11
11
  'use strict';
12
12
 
13
- declare type Options = {
14
- allowReturnOutsideFunction?: boolean,
15
- babel?: boolean,
16
- flow?: 'all' | 'detect',
17
- sourceFilename?: string,
18
- sourceType?: 'module' | 'script' | 'unambiguous',
19
- tokens?: boolean,
20
- };
13
+ import type {Program as ESTreeProgram} from 'hermes-estree';
14
+ import type {ParserOptions} from './ParserOptions';
21
15
 
22
- declare type Program = Object;
16
+ import * as HermesParser from './HermesParser';
17
+ import HermesToBabelAdapter from './HermesToBabelAdapter';
18
+ import HermesToESTreeAdapter from './HermesToESTreeAdapter';
23
19
 
24
- declare module.exports: {
25
- parse(code: string, opts: Options): Program,
20
+ const DEFAULTS = {
21
+ flow: 'detect',
26
22
  };
23
+
24
+ function getOptions(options?: ParserOptions = {...DEFAULTS}) {
25
+ // Default to detecting whether to parse Flow syntax by the presence
26
+ // of an @flow pragma.
27
+ if (options.flow == null) {
28
+ options.flow = DEFAULTS.flow;
29
+ } else if (options.flow !== 'all' && options.flow !== 'detect') {
30
+ throw new Error('flow option must be "all" or "detect"');
31
+ }
32
+
33
+ if (options.sourceType === 'unambiguous') {
34
+ // Clear source type so that it will be detected from the contents of the file
35
+ delete options.sourceType;
36
+ } else if (
37
+ options.sourceType != null &&
38
+ options.sourceType !== 'script' &&
39
+ options.sourceType !== 'module'
40
+ ) {
41
+ throw new Error(
42
+ 'sourceType option must be "script", "module", or "unambiguous" if set',
43
+ );
44
+ }
45
+
46
+ options.tokens = options.tokens === true;
47
+ options.allowReturnOutsideFunction =
48
+ options.allowReturnOutsideFunction === true;
49
+
50
+ return options;
51
+ }
52
+
53
+ function getAdapter(options: ParserOptions, code: string) {
54
+ return options.babel === true
55
+ ? new HermesToBabelAdapter(options)
56
+ : new HermesToESTreeAdapter(options, code);
57
+ }
58
+
59
+ // $FlowExpectedError[unclear-type]
60
+ type BabelProgram = Object;
61
+ declare function parse(
62
+ code: string,
63
+ opts: {...ParserOptions, babel: true},
64
+ ): BabelProgram;
65
+ // eslint-disable-next-line no-redeclare
66
+ declare function parse(
67
+ code: string,
68
+ opts?:
69
+ | {...ParserOptions, babel?: false | void}
70
+ | {...ParserOptions, babel: false},
71
+ ): ESTreeProgram;
72
+
73
+ // eslint-disable-next-line no-redeclare
74
+ export function parse(
75
+ code: string,
76
+ opts?: ParserOptions,
77
+ ): BabelProgram | ESTreeProgram {
78
+ const options = getOptions(opts);
79
+ const ast = HermesParser.parse(code, options);
80
+ const adapter = getAdapter(options, code);
81
+
82
+ return adapter.transform(ast);
83
+ }
84
+
85
+ export type {ParserOptions} from './ParserOptions';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermes-parser",
3
- "version": "0.4.6",
3
+ "version": "0.6.0",
4
4
  "description": "A JavaScript parser built from the Hermes engine",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
@@ -8,6 +8,12 @@
8
8
  "type": "git",
9
9
  "url": "git@github.com:facebook/hermes.git"
10
10
  },
11
+ "dependencies": {
12
+ "hermes-estree": "0.6.0"
13
+ },
14
+ "devDependencies": {
15
+ "hermes-transform": "0.6.0"
16
+ },
11
17
  "files": [
12
18
  "dist"
13
19
  ]