|
2 | 2 | * unassertify
|
3 | 3 | * Browserify transform for unassert
|
4 | 4 | * Encourages programming with assertions by providing tools to compile them away.
|
5 |
| - * |
| 5 | + * |
6 | 6 | * https://github.com/unassert-js/unassertify
|
7 | 7 | *
|
8 | 8 | * Copyright (c) 2015-2018 Takuto Wada
|
|
11 | 11 | */
|
12 | 12 | 'use strict';
|
13 | 13 |
|
14 |
| -var path = require('path'); |
15 |
| -var through = require('through'); |
16 |
| -var acorn = require('acorn'); |
17 |
| -var escodegen = require('escodegen'); |
18 |
| -var convert = require('convert-source-map'); |
19 |
| -var transfer = require('multi-stage-sourcemap').transfer; |
20 |
| -var unassert = require('unassert'); |
| 14 | +const path = require('path'); |
| 15 | +const through = require('through'); |
| 16 | +const acorn = require('acorn'); |
| 17 | +const escodegen = require('escodegen'); |
| 18 | +const convert = require('convert-source-map'); |
| 19 | +const { transfer } = require('multi-stage-sourcemap'); |
| 20 | +const unassert = require('unassert'); |
| 21 | +const hasOwn = Object.prototype.hasOwnProperty; |
21 | 22 |
|
22 | 23 | function mergeSourceMap (incomingSourceMap, outgoingSourceMap) {
|
23 |
| - if (typeof outgoingSourceMap === 'string' || outgoingSourceMap instanceof String) { |
24 |
| - outgoingSourceMap = JSON.parse(outgoingSourceMap); |
25 |
| - } |
26 |
| - if (!incomingSourceMap) { |
27 |
| - return outgoingSourceMap; |
28 |
| - } |
29 |
| - return JSON.parse(transfer({fromSourceMap: outgoingSourceMap, toSourceMap: incomingSourceMap})); |
| 24 | + if (typeof outgoingSourceMap === 'string' || outgoingSourceMap instanceof String) { |
| 25 | + outgoingSourceMap = JSON.parse(outgoingSourceMap); |
| 26 | + } |
| 27 | + if (!incomingSourceMap) { |
| 28 | + return outgoingSourceMap; |
| 29 | + } |
| 30 | + return JSON.parse(transfer({ fromSourceMap: outgoingSourceMap, toSourceMap: incomingSourceMap })); |
30 | 31 | }
|
31 | 32 |
|
32 | 33 | function overwritePropertyIfExists (name, from, to) {
|
33 |
| - if (from.hasOwnProperty(name)) { |
34 |
| - to.setProperty(name, from[name]); |
35 |
| - } |
| 34 | + if (hasOwn.call(from, name)) { |
| 35 | + to.setProperty(name, from[name]); |
| 36 | + } |
36 | 37 | }
|
37 | 38 |
|
38 | 39 | function reconnectSourceMap (inMap, outMap) {
|
39 |
| - var mergedRawMap = mergeSourceMap(inMap, outMap.toObject()); |
40 |
| - var reMap = convert.fromObject(mergedRawMap); |
41 |
| - overwritePropertyIfExists('sources', inMap, reMap); |
42 |
| - overwritePropertyIfExists('sourceRoot', inMap, reMap); |
43 |
| - overwritePropertyIfExists('sourcesContent', inMap, reMap); |
44 |
| - return reMap; |
| 40 | + const mergedRawMap = mergeSourceMap(inMap, outMap.toObject()); |
| 41 | + const reMap = convert.fromObject(mergedRawMap); |
| 42 | + overwritePropertyIfExists('sources', inMap, reMap); |
| 43 | + overwritePropertyIfExists('sourceRoot', inMap, reMap); |
| 44 | + overwritePropertyIfExists('sourcesContent', inMap, reMap); |
| 45 | + return reMap; |
45 | 46 | }
|
46 | 47 |
|
47 | 48 | function handleIncomingSourceMap (originalCode) {
|
48 |
| - var commented = convert.fromSource(originalCode); |
49 |
| - if (commented) { |
50 |
| - return commented.toObject(); |
51 |
| - } |
52 |
| - return null; |
| 49 | + const commented = convert.fromSource(originalCode); |
| 50 | + if (commented) { |
| 51 | + return commented.toObject(); |
| 52 | + } |
| 53 | + return null; |
53 | 54 | }
|
54 | 55 |
|
55 | 56 | function applyUnassertWithSourceMap (code, filepath) {
|
56 |
| - var ast = acorn.parse(code, { |
57 |
| - sourceType: 'module', |
58 |
| - ecmaVersion: 2018, |
59 |
| - locations: true, |
60 |
| - allowHashBang: true |
61 |
| - }); |
62 |
| - var inMap = handleIncomingSourceMap(code); |
63 |
| - var instrumented = escodegen.generate(unassert(ast), { |
64 |
| - sourceMap: filepath, |
65 |
| - sourceContent: code, |
66 |
| - sourceMapWithCode: true |
67 |
| - }); |
68 |
| - var outMap = convert.fromJSON(instrumented.map.toString()); |
69 |
| - if (inMap) { |
70 |
| - var reMap = reconnectSourceMap(inMap, outMap); |
71 |
| - return instrumented.code + '\n' + reMap.toComment() + '\n'; |
72 |
| - } else { |
73 |
| - return instrumented.code + '\n' + outMap.toComment() + '\n'; |
74 |
| - } |
| 57 | + const ast = acorn.parse(code, { |
| 58 | + sourceType: 'module', |
| 59 | + ecmaVersion: 2018, |
| 60 | + locations: true, |
| 61 | + allowHashBang: true |
| 62 | + }); |
| 63 | + const inMap = handleIncomingSourceMap(code); |
| 64 | + const instrumented = escodegen.generate(unassert(ast), { |
| 65 | + sourceMap: filepath, |
| 66 | + sourceContent: code, |
| 67 | + sourceMapWithCode: true |
| 68 | + }); |
| 69 | + const outMap = convert.fromJSON(instrumented.map.toString()); |
| 70 | + if (inMap) { |
| 71 | + const reMap = reconnectSourceMap(inMap, outMap); |
| 72 | + return instrumented.code + '\n' + reMap.toComment() + '\n'; |
| 73 | + } else { |
| 74 | + return instrumented.code + '\n' + outMap.toComment() + '\n'; |
| 75 | + } |
75 | 76 | }
|
76 | 77 |
|
77 | 78 | function applyUnassertWithoutSourceMap (code) {
|
78 |
| - var ast = acorn.parse(code, { |
79 |
| - sourceType: 'module', |
80 |
| - ecmaVersion: 2018, |
81 |
| - allowHashBang: true |
82 |
| - }); |
83 |
| - return escodegen.generate(unassert(ast)); |
| 79 | + const ast = acorn.parse(code, { |
| 80 | + sourceType: 'module', |
| 81 | + ecmaVersion: 2018, |
| 82 | + allowHashBang: true |
| 83 | + }); |
| 84 | + return escodegen.generate(unassert(ast)); |
84 | 85 | }
|
85 | 86 |
|
86 | 87 | function shouldProduceSourceMap (options) {
|
87 |
| - return (options && options._flags && options._flags.debug); |
| 88 | + return (options && options._flags && options._flags.debug); |
88 | 89 | }
|
89 | 90 |
|
90 | 91 | function containsAssertions (src) {
|
91 |
| - // Matches both `assert` and `power-assert`. |
92 |
| - return src.indexOf('assert') !== -1; |
| 92 | + // Matches both `assert` and `power-assert`. |
| 93 | + return src.indexOf('assert') !== -1; |
93 | 94 | }
|
94 | 95 |
|
95 | 96 | module.exports = function unassertify (filepath, options) {
|
96 |
| - if (path.extname(filepath) === '.json') { |
97 |
| - return through(); |
98 |
| - } |
| 97 | + if (path.extname(filepath) === '.json') { |
| 98 | + return through(); |
| 99 | + } |
99 | 100 |
|
100 |
| - var data = '', |
101 |
| - stream = through(write, end); |
| 101 | + let data = ''; |
| 102 | + const stream = through(write, end); |
102 | 103 |
|
103 |
| - function write(buf) { |
104 |
| - data += buf; |
105 |
| - } |
| 104 | + function write (buf) { |
| 105 | + data += buf; |
| 106 | + } |
106 | 107 |
|
107 |
| - function end() { |
108 |
| - if (!containsAssertions(data)) { |
109 |
| - stream.queue(data); |
110 |
| - } else if (shouldProduceSourceMap(options)) { |
111 |
| - stream.queue(applyUnassertWithSourceMap(data, filepath)); |
112 |
| - } else { |
113 |
| - stream.queue(applyUnassertWithoutSourceMap(data)); |
114 |
| - } |
115 |
| - stream.queue(null); |
| 108 | + function end () { |
| 109 | + if (!containsAssertions(data)) { |
| 110 | + stream.queue(data); |
| 111 | + } else if (shouldProduceSourceMap(options)) { |
| 112 | + stream.queue(applyUnassertWithSourceMap(data, filepath)); |
| 113 | + } else { |
| 114 | + stream.queue(applyUnassertWithoutSourceMap(data)); |
116 | 115 | }
|
| 116 | + stream.queue(null); |
| 117 | + } |
117 | 118 |
|
118 |
| - return stream; |
| 119 | + return stream; |
119 | 120 | };
|
0 commit comments