hermes-parser 0.5.0 → 0.8.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.
@@ -0,0 +1,111 @@
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
+ *
8
+ * @format
9
+ */
10
+ 'use strict';
11
+
12
+ Object.defineProperty(exports, "__esModule", {
13
+ value: true
14
+ });
15
+ exports.getModuleDocblock = getModuleDocblock;
16
+ exports.parseDocblockString = parseDocblockString;
17
+ const DIRECTIVE_REGEX = /^\s*@([a-zA-Z0-9_-]+)( +.+)?$/;
18
+
19
+ function parseDocblockString(docblock) {
20
+ const directiveLines = docblock.split('\n') // remove the leading " *" from each line
21
+ .map(line => line.trimStart().replace(/^\* ?/, '').trim()).filter(line => line.startsWith('@'));
22
+ const directives = {};
23
+
24
+ for (const line of directiveLines) {
25
+ var _match$;
26
+
27
+ const match = DIRECTIVE_REGEX.exec(line);
28
+
29
+ if (match == null) {
30
+ continue;
31
+ }
32
+
33
+ const name = match[1]; // explicitly use an empty string if there's no value
34
+ // this way the array length tracks how many instances of the directive there was
35
+
36
+ const value = ((_match$ = match[2]) != null ? _match$ : '').trim();
37
+
38
+ if (directives[name]) {
39
+ directives[name].push(value);
40
+ } else {
41
+ directives[name] = [value];
42
+ }
43
+ }
44
+
45
+ return directives;
46
+ }
47
+
48
+ function getModuleDocblock(hermesProgram) {
49
+ const docblockNode = (() => {
50
+ if (hermesProgram.type !== 'Program') {
51
+ return null;
52
+ } // $FlowExpectedError[incompatible-type] - escape out of the unsafe hermes types
53
+
54
+
55
+ const program = hermesProgram;
56
+
57
+ if (program.comments.length === 0) {
58
+ return null;
59
+ }
60
+
61
+ const firstComment = (() => {
62
+ const first = program.comments[0];
63
+
64
+ if (first.type === 'Block') {
65
+ return first;
66
+ }
67
+
68
+ if (program.comments.length === 1) {
69
+ return null;
70
+ } // ESLint will always strip out the shebang comment from the code before passing it to the parser
71
+ // https://github.com/eslint/eslint/blob/21d647904dc30f9484b22acdd9243a6d0ecfba38/lib/linter/linter.js#L779
72
+ // this means that we're forced to parse it as a line comment :(
73
+ // this hacks around it by selecting the second comment in this case
74
+
75
+
76
+ const second = program.comments[1];
77
+
78
+ if (first.type === 'Line' && first.range[0] === 0 && second.type === 'Block') {
79
+ return second;
80
+ }
81
+
82
+ return null;
83
+ })();
84
+
85
+ if (firstComment == null) {
86
+ return null;
87
+ }
88
+ /*
89
+ Handle cases like this where the comment isn't actually the first thing in the code:
90
+ ```
91
+ const x = 1; /* docblock *./
92
+ ```
93
+ */
94
+
95
+
96
+ if (program.body.length > 0 && program.body[0].range[0] < firstComment.range[0]) {
97
+ return null;
98
+ }
99
+
100
+ return firstComment;
101
+ })();
102
+
103
+ if (docblockNode == null) {
104
+ return null;
105
+ }
106
+
107
+ return {
108
+ directives: parseDocblockString(docblockNode.value),
109
+ comment: docblockNode
110
+ };
111
+ }
@@ -0,0 +1,116 @@
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
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ import type {HermesNode} from './HermesAST';
14
+ import type {DocblockDirectives, Program} from 'hermes-estree';
15
+
16
+ const DIRECTIVE_REGEX = /^\s*@([a-zA-Z0-9_-]+)( +.+)?$/;
17
+
18
+ export function parseDocblockString(docblock: string): DocblockDirectives {
19
+ const directiveLines = docblock
20
+ .split('\n')
21
+ // remove the leading " *" from each line
22
+ .map(line => line.trimStart().replace(/^\* ?/, '').trim())
23
+ .filter(line => line.startsWith('@'));
24
+
25
+ const directives: {
26
+ [string]: Array<string>,
27
+ } = {};
28
+
29
+ for (const line of directiveLines) {
30
+ const match = DIRECTIVE_REGEX.exec(line);
31
+ if (match == null) {
32
+ continue;
33
+ }
34
+ const name = match[1];
35
+ // explicitly use an empty string if there's no value
36
+ // this way the array length tracks how many instances of the directive there was
37
+ const value = (match[2] ?? '').trim();
38
+ if (directives[name]) {
39
+ directives[name].push(value);
40
+ } else {
41
+ directives[name] = [value];
42
+ }
43
+ }
44
+
45
+ return directives;
46
+ }
47
+
48
+ export function getModuleDocblock(
49
+ hermesProgram: HermesNode,
50
+ ): Program['docblock'] {
51
+ const docblockNode = (() => {
52
+ if (hermesProgram.type !== 'Program') {
53
+ return null;
54
+ }
55
+
56
+ // $FlowExpectedError[incompatible-type] - escape out of the unsafe hermes types
57
+ const program: Program = hermesProgram;
58
+
59
+ if (program.comments.length === 0) {
60
+ return null;
61
+ }
62
+
63
+ const firstComment = (() => {
64
+ const first = program.comments[0];
65
+ if (first.type === 'Block') {
66
+ return first;
67
+ }
68
+
69
+ if (program.comments.length === 1) {
70
+ return null;
71
+ }
72
+
73
+ // ESLint will always strip out the shebang comment from the code before passing it to the parser
74
+ // https://github.com/eslint/eslint/blob/21d647904dc30f9484b22acdd9243a6d0ecfba38/lib/linter/linter.js#L779
75
+ // this means that we're forced to parse it as a line comment :(
76
+ // this hacks around it by selecting the second comment in this case
77
+ const second = program.comments[1];
78
+ if (
79
+ first.type === 'Line' &&
80
+ first.range[0] === 0 &&
81
+ second.type === 'Block'
82
+ ) {
83
+ return second;
84
+ }
85
+
86
+ return null;
87
+ })();
88
+ if (firstComment == null) {
89
+ return null;
90
+ }
91
+
92
+ /*
93
+ Handle cases like this where the comment isn't actually the first thing in the code:
94
+ ```
95
+ const x = 1; /* docblock *./
96
+ ```
97
+ */
98
+ if (
99
+ program.body.length > 0 &&
100
+ program.body[0].range[0] < firstComment.range[0]
101
+ ) {
102
+ return null;
103
+ }
104
+
105
+ return firstComment;
106
+ })();
107
+
108
+ if (docblockNode == null) {
109
+ return null;
110
+ }
111
+
112
+ return {
113
+ directives: parseDocblockString(docblockNode.value),
114
+ comment: docblockNode,
115
+ };
116
+ }
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
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.
@@ -9,8 +9,6 @@
9
9
  */
10
10
  'use strict';
11
11
 
12
- function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
13
-
14
12
  Object.defineProperty(exports, "__esModule", {
15
13
  value: true
16
14
  });
@@ -22,30 +20,23 @@ var _HermesToBabelAdapter = _interopRequireDefault(require("./HermesToBabelAdapt
22
20
 
23
21
  var _HermesToESTreeAdapter = _interopRequireDefault(require("./HermesToESTreeAdapter"));
24
22
 
25
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
26
-
27
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
28
-
29
- 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; }
23
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
24
 
31
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
25
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
32
26
 
33
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
27
+ 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; }
34
28
 
35
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
36
-
37
- var DEFAULTS = {
29
+ const DEFAULTS = {
38
30
  flow: 'detect'
39
31
  };
40
32
 
41
- function getOptions() {
42
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _objectSpread({}, DEFAULTS);
43
-
33
+ function getOptions(options = { ...DEFAULTS
34
+ }) {
44
35
  // Default to detecting whether to parse Flow syntax by the presence
45
36
  // of an pragma.
46
37
  if (options.flow == null) {
47
38
  options.flow = DEFAULTS.flow;
48
- } else if (options.flow != 'all' && options.flow != 'detect') {
39
+ } else if (options.flow !== 'all' && options.flow !== 'detect') {
49
40
  throw new Error('flow option must be "all" or "detect"');
50
41
  }
51
42
 
@@ -62,14 +53,14 @@ function getOptions() {
62
53
  }
63
54
 
64
55
  function getAdapter(options, code) {
65
- return options.babel === true ? new _HermesToBabelAdapter["default"](options) : new _HermesToESTreeAdapter["default"](options, code);
56
+ return options.babel === true ? new _HermesToBabelAdapter.default(options) : new _HermesToESTreeAdapter.default(options, code);
66
57
  } // $FlowExpectedError[unclear-type]
67
58
 
68
59
 
69
60
  // eslint-disable-next-line no-redeclare
70
61
  function parse(code, opts) {
71
- var options = getOptions(opts);
72
- var ast = HermesParser.parse(code, options);
73
- var adapter = getAdapter(options, code);
62
+ const options = getOptions(opts);
63
+ const ast = HermesParser.parse(code, options);
64
+ const adapter = getAdapter(options, code);
74
65
  return adapter.transform(ast);
75
66
  }
@@ -1,5 +1,5 @@
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.
@@ -26,7 +26,7 @@ function getOptions(options?: ParserOptions = {...DEFAULTS}) {
26
26
  // of an @flow pragma.
27
27
  if (options.flow == null) {
28
28
  options.flow = DEFAULTS.flow;
29
- } else if (options.flow != 'all' && options.flow != 'detect') {
29
+ } else if (options.flow !== 'all' && options.flow !== 'detect') {
30
30
  throw new Error('flow option must be "all" or "detect"');
31
31
  }
32
32
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermes-parser",
3
- "version": "0.5.0",
3
+ "version": "0.8.0",
4
4
  "description": "A JavaScript parser built from the Hermes engine",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
@@ -9,7 +9,12 @@
9
9
  "url": "git@github.com:facebook/hermes.git"
10
10
  },
11
11
  "dependencies": {
12
- "hermes-estree": "0.5.0"
12
+ "hermes-estree": "0.8.0"
13
+ },
14
+ "devDependencies": {
15
+ "@babel/parser": "7.7.4",
16
+ "espree": "9.3.2",
17
+ "hermes-transform": "0.8.0"
13
18
  },
14
19
  "files": [
15
20
  "dist"