@putout/engine-parser 4.9.1 → 4.10.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.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [NPMIMGURL]: https://img.shields.io/npm/v/@putout/engine-parser.svg?style=flat&longCache=true
4
4
  [NPMURL]: https://npmjs.org/package/@putout/engine-parser"npm"
5
5
 
6
- `putout` engine that parses input.
6
+ 🐊[`Putout`](https://github.com/coderaiser/putout) engine that parses input.
7
7
 
8
8
  ## Install
9
9
 
@@ -43,6 +43,46 @@ create node using `memoization`.
43
43
 
44
44
  create node without `memoization`.
45
45
 
46
+ ### Sourcemaps
47
+
48
+ You have two ways to benefit from source map generation:
49
+
50
+ - using `Recast` print;
51
+ - using `Babel` generator;
52
+
53
+ #### Generate sourcemaps usign Recast
54
+
55
+ ```js
56
+ const source = `const hello = 'world';`;
57
+ const ast = parse(source, {
58
+ sourceFileName: 'hello.js',
59
+ });
60
+ print(ast, {sourceMapName: 'hello.map'});
61
+ // returns
62
+ `const hello = 'world';
63
+ {"version":3,"sources":["hello.js"],"names":[],"mappings":"AAAA...","file":"hello.map","sourcesContent":["const hello = 'world';"]}`;
64
+ ```
65
+
66
+ #### Generate sourcemaps usign Bebel
67
+
68
+ To generate sourcemap usig babel generator, you should use babel parser before.
69
+ This is low level transformation, because `Babel` doesn't preserve any formatting.
70
+
71
+ ```js
72
+ const {generate} = require('@putout/engine-parser');
73
+ const babel = require('@putout/engine-parser/babel');
74
+
75
+ const ast = babel.parse(source, {
76
+ sourceFilename: 'hello.js',
77
+ });
78
+
79
+ generate(ast, {sourceMaps: true}, {
80
+ 'hello.js': source,
81
+ });
82
+ // returns
83
+ ({code, map});
84
+ ```
85
+
46
86
  ## Example
47
87
 
48
88
  ```js
package/lib/generate.js CHANGED
@@ -2,10 +2,11 @@
2
2
 
3
3
  const generate = require('@babel/generator').default;
4
4
 
5
- module.exports = (node) => {
5
+ module.exports = (node, options, sourceMaps) => {
6
6
  return generate(node, {
7
7
  comments: false,
8
8
  recordAndTupleSyntaxType: 'hash',
9
- });
9
+ ...options,
10
+ }, sourceMaps);
10
11
  };
11
12
 
package/lib/parse.js CHANGED
@@ -13,14 +13,17 @@ function parse(source, options) {
13
13
  isTS,
14
14
  isFlow,
15
15
  isJSX,
16
+ sourceFileName,
16
17
  } = options || {};
17
18
 
18
19
  const ast = recast.parse(source, {
20
+ sourceFileName,
19
21
  parser: getParser({
20
22
  parser,
21
23
  isTS,
22
24
  isFlow,
23
25
  isJSX,
26
+ sourceFileName,
24
27
  }),
25
28
  });
26
29
 
@@ -18,12 +18,16 @@ const options = require('./options');
18
18
 
19
19
  const moveOutDirectives = require('./move-out-directives');
20
20
 
21
- module.exports.parse = function babelParse(source, {isTS, isJSX = true, isFlow = getFlow(source)}) {
21
+ // There is a difference in options naming for babel and recast
22
+ // recast -> sourceFileName
23
+ // babel, putout: sourceFilename
24
+ module.exports.parse = function babelParse(source, {sourceFilename, isTS, isJSX = true, isFlow = getFlow(source)}) {
22
25
  const {parse} = initBabel();
23
26
 
24
27
  const ast = parse(source, {
25
28
  sourceType: 'module',
26
29
  tokens: true,
30
+ sourceFilename,
27
31
  ...options,
28
32
  plugins: clean([
29
33
  ...plugins,
package/lib/print.js CHANGED
@@ -3,17 +3,26 @@
3
3
  const {print} = require('@putout/recast');
4
4
 
5
5
  const fixStrictMode = (a) => a.replace(`\n\n\n'use strict'`, `\n\n'use strict'`);
6
+ const addSourceMap = (sourceMapName, {code, map}) => !sourceMapName ? code : `${code}\n//${stringify(map)}\n`;
6
7
 
7
- module.exports = (ast) => {
8
+ const {stringify} = JSON;
9
+
10
+ module.exports = (ast, options = {}) => {
11
+ const {sourceMapName} = options;
8
12
  const printOptions = {
9
13
  quote: 'single',
10
14
  objectCurlySpacing: false,
11
15
  wrapColumn: Infinity,
16
+ ...options,
12
17
  };
13
18
 
14
- const printed = print(ast, printOptions).code;
15
- const code = fixStrictMode(printed);
19
+ const printed = print(ast, printOptions);
20
+ const {map} = printed;
21
+ const code = fixStrictMode(printed.code);
16
22
 
17
- return code;
23
+ return addSourceMap(sourceMapName, {
24
+ code,
25
+ map,
26
+ });
18
27
  };
19
28
 
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@putout/engine-parser",
3
- "version": "4.9.1",
3
+ "version": "4.10.0",
4
+ "type": "commonjs",
4
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
5
6
  "description": "putout parser",
6
- "homepage": "https://github.com/coderaiser/putout/tree/master/packages/engine-parser",
7
+ "homepage": "https://github.com/coderaiser/putout/tree/master/packages/engine-parser#readme",
7
8
  "main": "./lib/parser.js",
8
9
  "release": false,
9
10
  "tag": false,