@putout/engine-parser 10.3.0 → 10.5.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.
@@ -9,6 +9,10 @@ const hermes = require('./parsers/hermes');
9
9
  const secondChance = require('./second-chance');
10
10
  const isObject = (a) => typeof a === 'object';
11
11
 
12
+ const MESSAGES = [
13
+ 'has already been declared',
14
+ ];
15
+
12
16
  module.exports = (source, parser, {isTS, isFlow, isJSX}) => {
13
17
  const options = {
14
18
  parser,
@@ -17,18 +21,30 @@ module.exports = (source, parser, {isTS, isFlow, isJSX}) => {
17
21
  isJSX,
18
22
  };
19
23
 
20
- return secondChance(customParse, source, options, {
24
+ const optionsB = {
21
25
  ...options,
22
26
  isJSX: false,
23
- });
27
+ };
28
+
29
+ const optionsC = {
30
+ ...options,
31
+ isRecovery: true,
32
+ };
33
+
34
+ return secondChance(customParse, source, MESSAGES, [
35
+ options,
36
+ optionsB,
37
+ optionsC,
38
+ ]);
24
39
  };
25
40
 
26
- function customParse(source, {parser, isTS, isFlow, isJSX}) {
41
+ function customParse(source, {parser, isTS, isFlow, isJSX, isRecovery}) {
27
42
  if (parser === 'babel')
28
43
  return babel.parse(source, {
29
44
  isTS,
30
45
  isFlow,
31
46
  isJSX,
47
+ isRecovery,
32
48
  });
33
49
 
34
50
  if (isObject(parser))
@@ -1,9 +1,13 @@
1
1
  'use strict';
2
2
 
3
- const process = require('process');
3
+ const process = require('node:process');
4
4
  const once = require('once');
5
5
 
6
- // stricter validation to prevent even more invalid ASTs: not only// from a tree shape point of view but also ensuring that nodes in// the correct position carry valid information. For example, starting// from Babel 8 t.identifier("123") will be disallowed, because 123 is// not a valid identifier.
6
+ // stricter validation to prevent even more invalid ASTs: not only
7
+ // from a tree shape point of view but also ensuring that nodes in
8
+ // the correct position carry valid information. For example, starting
9
+ // from Babel 8 t.identifier("123") will be disallowed, because 123 is
10
+ // not a valid identifier.
7
11
  process.env.BABEL_TYPES_8_BREAKING = true;
8
12
 
9
13
  const plugins = require('./plugins');
@@ -13,14 +17,16 @@ const clean = (a) => a.filter(Boolean);
13
17
  const initBabel = once(() => require('@putout/babel'));
14
18
  const {assign} = Object;
15
19
 
16
- // There is a difference in options naming for babel and recast// recast -> sourceFileName// babel, putout: sourceFilename
17
- module.exports.parse = function babelParse(source, {sourceFilename, isTS, isJSX = true, isFlow = getFlow(source)}) {
20
+ // There is a difference in options naming for babel and recast
21
+ // recast -> sourceFileName
22
+ // babel, putout: sourceFilename
23
+ module.exports.parse = function babelParse(source, {sourceFilename, isTS, isJSX = true, isFlow = getFlow(source), isRecovery}) {
18
24
  const {parse} = initBabel();
19
-
20
25
  const parserOptions = {
21
26
  sourceType: 'module',
22
27
  tokens: true,
23
28
  ...options,
29
+ errorRecovery: isRecovery,
24
30
  plugins: clean([
25
31
  ...plugins,
26
32
  ...getBabelLangExts({
@@ -2,16 +2,31 @@
2
2
 
3
3
  const tryCatch = require('try-catch');
4
4
 
5
- module.exports = (fn, source, a, b) => {
5
+ module.exports = (fn, source, messages, args) => {
6
+ const [a, ...others] = args;
6
7
  const [errorA, resultA] = tryCatch(fn, source, a);
7
8
 
8
9
  if (!errorA)
9
10
  return resultA;
10
11
 
11
- const [errorB, resultB] = tryCatch(fn, source, b);
12
+ if (checkError(errorA, messages))
13
+ throw errorA;
12
14
 
13
- if (!errorB)
14
- return resultB;
15
+ for (const b of others) {
16
+ const [errorB, resultB] = tryCatch(fn, source, b);
17
+
18
+ if (!errorB)
19
+ return resultB;
20
+ }
15
21
 
16
22
  throw errorA;
17
23
  };
24
+
25
+ function checkError(error, messages) {
26
+ for (const message of messages) {
27
+ if (error.message.includes(message))
28
+ return true;
29
+ }
30
+
31
+ return false;
32
+ }
package/lib/template.js CHANGED
@@ -14,8 +14,20 @@ const defaults = {
14
14
  ],
15
15
  };
16
16
 
17
- const {isExpressionStatement} = types;
18
- const extractExpression = (a) => isExpressionStatement(a) ? a.expression : a;
17
+ const {
18
+ isExpressionStatement,
19
+ isTSExternalModuleReference,
20
+ } = types;
21
+
22
+ const extractExpression = (a) => {
23
+ if (isExpressionStatement(a))
24
+ return a.expression;
25
+
26
+ if (isTSExternalModuleReference(a))
27
+ return a.expression;
28
+
29
+ return a;
30
+ };
19
31
 
20
32
  module.exports = nanomemoize((value, options) => {
21
33
  const fn = template(value, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/engine-parser",
3
- "version": "10.3.0",
3
+ "version": "10.5.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout parser",
@@ -50,8 +50,8 @@
50
50
  "acorn-jsx": "^5.0.2",
51
51
  "acorn-stage3": "^4.0.0",
52
52
  "c8": "^9.0.0",
53
- "eslint": "^9.0.0-alpha.0",
54
- "eslint-plugin-n": "^17.0.0-0",
53
+ "eslint": "^9.0.0",
54
+ "eslint-plugin-n": "^17.0.0",
55
55
  "eslint-plugin-putout": "^22.0.0",
56
56
  "espree": "^10.0.0",
57
57
  "esprima": "^4.0.1",