From 0b91de0138f5574524472887ca2e17e8cb60d3be Mon Sep 17 00:00:00 2001 From: Duncan Kimpton Date: Sun, 21 Jun 2020 20:47:00 +0200 Subject: [PATCH 1/8] FIX: Now supports specifying both functions and labels in the same config. FIX: Supports spaces around the . for functions and : for labels DOC: Updated documentation REFACTOR: Adjust tests so that the input and expected output can be seen next to each other. --- packages/strip/README.md | 57 +- packages/strip/src/index.js | 39 +- packages/strip/test/fixtures/assert/input.js | 6 - .../test/fixtures/console-custom/input.js | 4 - packages/strip/test/fixtures/console/input.js | 4 - packages/strip/test/fixtures/custom/input.js | 6 - .../test/fixtures/debugger-false/input.js | 5 - .../strip/test/fixtures/debugger/input.js | 5 - .../strip/test/fixtures/if-block/input.js | 3 - .../fixtures/inline-call-expressions/input.js | 1 - .../strip/test/fixtures/inline-if/input.js | 1 - .../strip/test/fixtures/inline-while/input.js | 1 - .../label-block-discriminate/input.js | 9 - .../fixtures/label-block-multiple/input.js | 9 - .../strip/test/fixtures/label-block/input.js | 7 - .../object-destructuring-default/input.js | 4 - .../strip/test/fixtures/super-method/input.js | 8 - .../strip/test/fixtures/switch-case/input.js | 4 - .../strip/test/fixtures/this-method/input.js | 4 - packages/strip/test/snapshots/test.js.md | 164 ----- packages/strip/test/snapshots/test.js.snap | Bin 846 -> 0 bytes packages/strip/test/test.js | 565 +++++++++++++++++- 22 files changed, 605 insertions(+), 301 deletions(-) delete mode 100755 packages/strip/test/fixtures/assert/input.js delete mode 100755 packages/strip/test/fixtures/console-custom/input.js delete mode 100755 packages/strip/test/fixtures/console/input.js delete mode 100755 packages/strip/test/fixtures/custom/input.js delete mode 100755 packages/strip/test/fixtures/debugger-false/input.js delete mode 100755 packages/strip/test/fixtures/debugger/input.js delete mode 100755 packages/strip/test/fixtures/if-block/input.js delete mode 100755 packages/strip/test/fixtures/inline-call-expressions/input.js delete mode 100755 packages/strip/test/fixtures/inline-if/input.js delete mode 100755 packages/strip/test/fixtures/inline-while/input.js delete mode 100755 packages/strip/test/fixtures/label-block-discriminate/input.js delete mode 100755 packages/strip/test/fixtures/label-block-multiple/input.js delete mode 100755 packages/strip/test/fixtures/label-block/input.js delete mode 100755 packages/strip/test/fixtures/object-destructuring-default/input.js delete mode 100755 packages/strip/test/fixtures/super-method/input.js delete mode 100755 packages/strip/test/fixtures/switch-case/input.js delete mode 100755 packages/strip/test/fixtures/this-method/input.js delete mode 100644 packages/strip/test/snapshots/test.js.md delete mode 100644 packages/strip/test/snapshots/test.js.snap diff --git a/packages/strip/README.md b/packages/strip/README.md index ef06a5cb9..d4e660680 100755 --- a/packages/strip/README.md +++ b/packages/strip/README.md @@ -36,59 +36,88 @@ export default { dir: 'output', format: 'cjs' }, - plugins: [ - strip({ - labels: ['unittest'] - }) - ] + plugins: [strip({})] }; ``` Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api). +### Default Options + +By default the **strip** plugin applies the following options, you can override by specifing options manually. + +> consider files matching **'\*\*/\*.js'** +> remove functions matching '**console.\***' and '**assert.\***' +> remove any **debugger** statements + +### Custom Options Example + +```js +plugins: [ + strip({ + include: '**/*.(mjs|js)', + exclude: 'tests/**/*', + debugger: true, + functions: ['console.log', 'assert.*'], + labels: ['unittest'], + sourceMap: true + }) +]; +``` + ## Options ### `include` Type: `String | RegExp | Array[...String|RegExp]`
-Default: `['**/*.js']` +Default: `['**/*.js']`
+Example: `include: '**/*.(mjs|js)',`
A pattern, or array of patterns, which specify the files in the build the plugin should operate on. ### `exclude` Type: `String | RegExp | Array[...String|RegExp]`
-Default: `[]` +Default: `[]`
+Example: `exlude: 'tests/**/*',`
A pattern, or array of patterns, which specify the files in the build the plugin should _ignore_. ### `debugger` Type: `Boolean`
-Default: `true` +Default: `true`
+Example: `debugger: false,`
-If `true`, instructs the plugin to remove debugger statements. +If `true` or unspecified, instructs the plugin to remove debugger statements. ### `functions` Type: `Array[...String]`
-Default: `[ 'console.*', 'assert.*' ]` +Default: `[ 'console.*', 'assert.*' ]`
+Example: `functions: [ 'console.log', 'MyClass.Test' ],`
Specifies the functions that the plugin will target and remove. +_Note: specifying functions that are used at the begining of a chain, such as 'a().b().c()', will result in '(void 0).b().c()' which will generate an error at runtime._ + ### `labels` Type: `Array[...String]`
-Default: `[]` +Default: `[]`
+Example: `labels: ['unittest'],`
+ +Specifies the [labeled blocks or statements](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label) that the plugin will target and remove. -Specifies the [labeled blocks](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label) that the plugin will target and remove. +_Note: the '**:**' is implied and should not be specified in the config._ ### `sourceMap` Type: `Boolean`
-Default: `true` +Default: `true`
+Example: `sourceMap: false,`
-If `true`, instructs the plugin to update source maps accordingly after removing configured targets from the bundle. +If `true` or unspecified, instructs the plugin to update source maps accordingly after removing configured targets from the bundle. ## Meta diff --git a/packages/strip/src/index.js b/packages/strip/src/index.js index 0e8d01833..f8f32a998 100755 --- a/packages/strip/src/index.js +++ b/packages/strip/src/index.js @@ -38,21 +38,34 @@ export default function strip(options = {}) { const sourceMap = options.sourceMap !== false; const removeDebuggerStatements = options.debugger !== false; - const functions = (options.functions || ['console.*', 'assert.*']).map((keypath) => - keypath.replace(/\./g, '\\.').replace(/\*/g, '\\w+') - ); + const functions = options.functions || ['console.*', 'assert.*']; const labels = options.labels || []; - const firstpass = new RegExp(`\\b(?:${functions.join('|')}|debugger)\\b`); - const pattern = new RegExp(`^(?:${functions.join('|')})$`); + const functionsPatterns = functions.map((f) => + f.replace(/\*/g, '\\w+').replace(/\./g, '\\.\\s*') + ); + const labelsPatterns = labels.map((l) => `${l}\\s*:`); + + const firstPassPatterns = removeDebuggerStatements + ? [...functionsPatterns, ...labelsPatterns, 'debugger\\b'] + : [...functionsPatterns, ...labelsPatterns]; + + const functionsRE = new RegExp(`^(?:${functionsPatterns.join('|')})$`); + const firstpassRE = new RegExp(`\\b(?:${firstPassPatterns.join('|')})`); + + const firstPassFilter = + firstPassPatterns.length > 0 ? (code) => firstpassRE.test(code) : () => false; + + const UNCHANGED = null; return { name: 'strip', transform(code, id) { - if (!filter(id)) return null; - if (functions.length > 0 && !firstpass.test(code)) return null; + if (!filter(id) || !firstPassFilter(code)) { + return UNCHANGED; + } let ast; @@ -81,7 +94,7 @@ export default function strip(options = {}) { if (parent.type === 'ExpressionStatement') { removeStatement(parent); } else { - magicString.overwrite(node.start, node.end, 'void 0'); + magicString.overwrite(node.start, node.end, '(void 0)'); } edited = true; @@ -93,7 +106,7 @@ export default function strip(options = {}) { if (isBlock(parent)) { remove(node.start, node.end); } else { - magicString.overwrite(node.start, node.end, 'void 0;'); + magicString.overwrite(node.start, node.end, '(void 0);'); } edited = true; @@ -114,13 +127,15 @@ export default function strip(options = {}) { if (removeDebuggerStatements && node.type === 'DebuggerStatement') { removeStatement(node); + this.skip(); } else if (node.type === 'LabeledStatement') { if (node.label && labels.includes(node.label.name)) { removeStatement(node); + this.skip(); } } else if (node.type === 'CallExpression') { const keypath = flatten(node.callee); - if (keypath && pattern.test(keypath)) { + if (keypath && functionsRE.test(keypath)) { removeExpression(node); this.skip(); } @@ -128,7 +143,9 @@ export default function strip(options = {}) { } }); - if (!edited) return null; + if (!edited) { + return UNCHANGED; + } code = magicString.toString(); const map = sourceMap ? magicString.generateMap() : null; diff --git a/packages/strip/test/fixtures/assert/input.js b/packages/strip/test/fixtures/assert/input.js deleted file mode 100755 index fddefeed9..000000000 --- a/packages/strip/test/fixtures/assert/input.js +++ /dev/null @@ -1,6 +0,0 @@ -/* eslint-disable */ -function foo(message) { - assert.equal(arguments.length, 1); - assert.equal(typeof arguments[0], 'string'); - bar(message); -} diff --git a/packages/strip/test/fixtures/console-custom/input.js b/packages/strip/test/fixtures/console-custom/input.js deleted file mode 100755 index 7c00b4db8..000000000 --- a/packages/strip/test/fixtures/console-custom/input.js +++ /dev/null @@ -1,4 +0,0 @@ -foo(123); -console.log('a'); -console.error('b'); -bar(789); diff --git a/packages/strip/test/fixtures/console/input.js b/packages/strip/test/fixtures/console/input.js deleted file mode 100755 index 7c00b4db8..000000000 --- a/packages/strip/test/fixtures/console/input.js +++ /dev/null @@ -1,4 +0,0 @@ -foo(123); -console.log('a'); -console.error('b'); -bar(789); diff --git a/packages/strip/test/fixtures/custom/input.js b/packages/strip/test/fixtures/custom/input.js deleted file mode 100755 index fc2599570..000000000 --- a/packages/strip/test/fixtures/custom/input.js +++ /dev/null @@ -1,6 +0,0 @@ -a(); -debug('hello'); -b(); -custom.foo('foo'); -custom.bar('bar'); -c(); diff --git a/packages/strip/test/fixtures/debugger-false/input.js b/packages/strip/test/fixtures/debugger-false/input.js deleted file mode 100755 index 458057512..000000000 --- a/packages/strip/test/fixtures/debugger-false/input.js +++ /dev/null @@ -1,5 +0,0 @@ -export default function foo() { - before(); - debugger; - after(); -} diff --git a/packages/strip/test/fixtures/debugger/input.js b/packages/strip/test/fixtures/debugger/input.js deleted file mode 100755 index 458057512..000000000 --- a/packages/strip/test/fixtures/debugger/input.js +++ /dev/null @@ -1,5 +0,0 @@ -export default function foo() { - before(); - debugger; - after(); -} diff --git a/packages/strip/test/fixtures/if-block/input.js b/packages/strip/test/fixtures/if-block/input.js deleted file mode 100755 index 96b5dbbd3..000000000 --- a/packages/strip/test/fixtures/if-block/input.js +++ /dev/null @@ -1,3 +0,0 @@ -if (DEBUG) { - console.log('debugging'); -} diff --git a/packages/strip/test/fixtures/inline-call-expressions/input.js b/packages/strip/test/fixtures/inline-call-expressions/input.js deleted file mode 100755 index f58db6763..000000000 --- a/packages/strip/test/fixtures/inline-call-expressions/input.js +++ /dev/null @@ -1 +0,0 @@ -DEBUG && console.log('debugging'); diff --git a/packages/strip/test/fixtures/inline-if/input.js b/packages/strip/test/fixtures/inline-if/input.js deleted file mode 100755 index 5087569fb..000000000 --- a/packages/strip/test/fixtures/inline-if/input.js +++ /dev/null @@ -1 +0,0 @@ -if (DEBUG) console.log('debugging'); diff --git a/packages/strip/test/fixtures/inline-while/input.js b/packages/strip/test/fixtures/inline-while/input.js deleted file mode 100755 index 62b0f6003..000000000 --- a/packages/strip/test/fixtures/inline-while/input.js +++ /dev/null @@ -1 +0,0 @@ -while (test()) console.log('still true!'); diff --git a/packages/strip/test/fixtures/label-block-discriminate/input.js b/packages/strip/test/fixtures/label-block-discriminate/input.js deleted file mode 100755 index d1d03a29d..000000000 --- a/packages/strip/test/fixtures/label-block-discriminate/input.js +++ /dev/null @@ -1,9 +0,0 @@ -before(); -first: { - things(); -} -after(); -second: { - assert.things(); -} -again(); diff --git a/packages/strip/test/fixtures/label-block-multiple/input.js b/packages/strip/test/fixtures/label-block-multiple/input.js deleted file mode 100755 index d1d03a29d..000000000 --- a/packages/strip/test/fixtures/label-block-multiple/input.js +++ /dev/null @@ -1,9 +0,0 @@ -before(); -first: { - things(); -} -after(); -second: { - assert.things(); -} -again(); diff --git a/packages/strip/test/fixtures/label-block/input.js b/packages/strip/test/fixtures/label-block/input.js deleted file mode 100755 index 7c805e051..000000000 --- a/packages/strip/test/fixtures/label-block/input.js +++ /dev/null @@ -1,7 +0,0 @@ -before(); -unittest: { - test('some test', (assert) => { - assert.true(true); - }); -} -after(); diff --git a/packages/strip/test/fixtures/object-destructuring-default/input.js b/packages/strip/test/fixtures/object-destructuring-default/input.js deleted file mode 100755 index 700b8e771..000000000 --- a/packages/strip/test/fixtures/object-destructuring-default/input.js +++ /dev/null @@ -1,4 +0,0 @@ -export function fn({ foo = console.log(), bar } = {}) { - const { baz = console.log() } = bar; - console.log(foo, bar, baz); -} diff --git a/packages/strip/test/fixtures/super-method/input.js b/packages/strip/test/fixtures/super-method/input.js deleted file mode 100755 index 2e4b21172..000000000 --- a/packages/strip/test/fixtures/super-method/input.js +++ /dev/null @@ -1,8 +0,0 @@ -/* eslint-disable */ -class Foo { - bar() { - a(); - super.log('hello'); - b(); - } -} diff --git a/packages/strip/test/fixtures/switch-case/input.js b/packages/strip/test/fixtures/switch-case/input.js deleted file mode 100755 index 79815fa9b..000000000 --- a/packages/strip/test/fixtures/switch-case/input.js +++ /dev/null @@ -1,4 +0,0 @@ -switch (a) { - case 1: - debugger; -} diff --git a/packages/strip/test/fixtures/this-method/input.js b/packages/strip/test/fixtures/this-method/input.js deleted file mode 100755 index e927c7e8f..000000000 --- a/packages/strip/test/fixtures/this-method/input.js +++ /dev/null @@ -1,4 +0,0 @@ -a(); -this.foo('foo'); -this.bar('bar'); -b(); diff --git a/packages/strip/test/snapshots/test.js.md b/packages/strip/test/snapshots/test.js.md deleted file mode 100644 index 76bed0fd5..000000000 --- a/packages/strip/test/snapshots/test.js.md +++ /dev/null @@ -1,164 +0,0 @@ -# Snapshot report for `test/test.js` - -The actual snapshot is saved in `test.js.snap`. - -Generated by [AVA](https://ava.li). - -## does not remove debugger statements with debugger: false - -> Snapshot 1 - - `export default function foo() {␊ - before();␊ - debugger;␊ - after();␊ - }␊ - ` - -## leaves console statements if custom functions are provided - -> Snapshot 1 - - `foo(123);␊ - console.error('b');␊ - bar(789);␊ - ` - -## only removes specified blocks - -> Snapshot 1 - - `before();␊ - first: {␊ - things();␊ - }␊ - after();␊ - again();␊ - ` - -## removes assert statements - -> Snapshot 1 - - `/* eslint-disable */␊ - function foo(message) {␊ - bar(message);␊ - }␊ - ` - -## removes console statements - -> Snapshot 1 - - `foo(123);␊ - bar(789);␊ - ` - -## removes custom functions - -> Snapshot 1 - - `a();␊ - b();␊ - c();␊ - ` - -## removes debugger statements - -> Snapshot 1 - - `export default function foo() {␊ - before();␊ - after();␊ - }␊ - ` - -## removes expressions in if blocks - -> Snapshot 1 - - `if (DEBUG) {␊ - }␊ - ` - -## removes labeled blocks - -> Snapshot 1 - - `before();␊ - unittest: {␊ - test('some test', (assert) => {␊ - });␊ - }␊ - after();␊ - ` - -## removes methods of this - -> Snapshot 1 - - `a();␊ - b();␊ - ` - -## removes multiple labeled blocks - -> Snapshot 1 - - `before();␊ - after();␊ - again();␊ - ` - -## removes super calls - -> Snapshot 1 - - `/* eslint-disable */␊ - class Foo {␊ - bar() {␊ - a();␊ - b();␊ - }␊ - }␊ - ` - -## replaces case body with void 0 - -> Snapshot 1 - - `switch (a) {␊ - case 1:␊ - void 0;␊ - }␊ - ` - -## rewrtestes inline call expressions (not expression statements) as void 0 - -> Snapshot 1 - - `DEBUG && void 0;␊ - ` - -## rewrtestes inline if expessions as void 0 - -> Snapshot 1 - - `if (DEBUG) void 0;␊ - ` - -## rewrtestes inline while expressions as void 0 - -> Snapshot 1 - - `while (test()) void 0;␊ - ` - -## supports object destructuring assignments with default values - -> Snapshot 1 - - `export function fn({ foo = void 0, bar } = {}) {␊ - const { baz = void 0 } = bar;␊ - }␊ - ` diff --git a/packages/strip/test/snapshots/test.js.snap b/packages/strip/test/snapshots/test.js.snap deleted file mode 100644 index 5c1e641a460b0c5b8e48f2e729a744566f5f92f4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 846 zcmV-U1F`%;RzV*Af0b(QxD!F`_!Ru#?V9^*LzE<(}$b@F4{=a`Ct_kVw z`ojnotp;K(*N+9^CcaNDuX?=e^EY=tMzH7{AdZj_TTvflBQbxAt-hg-y9*;&bSDt6 z-8t{In2Y;|D-xU6+I_immJuv^5s26ANIdEiyol+_)Yt8rcJb|uU{NI|28JnntO~L( zA6~Pt`E20KmUM4MuxKa{|24n4kjt`H;6=IpIku3lPmEyEDj<%q*fXn8B!(k??*6@N z7yYVb1dHAV;_0m}g6lWDcv1R@;r<@GlL3riQ6pvs23;n*#aCb29ACg68><>1v78Ys z8Uw^rkNh{Wl<5+jsJiK9Sx>`4MzH8yAZBJ@2gem7D}x}TWoku1eo=`+N@`kSX-FKFO z_>2?*X*D!52FfHQ7HODUSOQrnD(v;O6jFE>28O zg}MM}3`_vSIt_$%$@zK3`8lb2sYONkMH=c!>bRZ2p9pe!5{O9FK$jGBadirHS5Q+^ zD9g`GQ7}N07S2pl&;TpZ#IArJNkKKbd$|xcqli0W4V2`Z#NuKFxBPr~KtO^782peJ z0SZDGwdfY97MEw1BxfjSBto<#Cl;qF7+Qh#z?_YyMZ7#C6KJnSNosM4h9(Z*1w)ez zBEIr8szH&aU<=it19GTBEs$Sb3$+^BSnTHoy%40Ou_KdR+wo0GZTz-v9sr diff --git a/packages/strip/test/test.js b/packages/strip/test/test.js index 27fa54b87..d0a1f68ed 100755 --- a/packages/strip/test/test.js +++ b/packages/strip/test/test.js @@ -1,4 +1,3 @@ -const fs = require('fs'); const path = require('path'); const acorn = require('acorn'); @@ -7,10 +6,12 @@ const test = require('ava'); // eslint-disable-next-line import/no-unresolved const strip = require('..'); -const compare = (t, fixture, options) => { - const filename = path.resolve(`test/fixtures/${fixture}/input.js`); - const input = fs.readFileSync(filename, 'utf-8'); - const output = strip(options).transform.call( +const UNCHANGED = null; +const testFileName = 'virtual-test-case.js'; + +const stripped = (input, options) => { + const virtualFilename = path.resolve(`test/fixtures/${testFileName}`); + const result = strip(options).transform.call( { parse: (code) => acorn.parse(code, { @@ -19,76 +20,578 @@ const compare = (t, fixture, options) => { }) }, input, - filename + virtualFilename ); - t.snapshot(output ? output.code : input); + return result === null ? UNCHANGED : result.code; }; -test('removes debugger statements', (t) => { - compare(t, 'debugger'); +// --------------- TESTS --------------- + +test('can be configured to make no changes', (t) => { + const input = ` + export default function foo() { + before(); + debugger; + logging: + console.log('a'); + console.error('b'); + after(); + } + `; + + const options = { + labels: [], + functions: [], + debugger: false + }; + + t.is(stripped(input, options), UNCHANGED); +}); + +test('excluded files do not get changed', (t) => { + const input = ` + export default function foo() { + before(); + debugger; + logging: + console.log('a'); + console.error('b'); + after(); + } + `; + + const options = { + exclude: `**/${testFileName}` + }; + + t.is(stripped(input, options), UNCHANGED); +}); + +test('removes debugger statements by default', (t) => { + const input = ` + export default function foo() { + before(); + debugger; + after(); + } + `; + + const expected = ` + export default function foo() { + before(); + after(); + } + `; + + t.is(stripped(input), expected); }); test('does not remove debugger statements with debugger: false', (t) => { - compare(t, 'debugger-false', { debugger: false }); + const input = ` + export default function foo() { + before(); + debugger; + after(); + } + `; + + const options = { debugger: false }; + + t.is(stripped(input, options), UNCHANGED); }); -test('removes console statements', (t) => { - compare(t, 'console'); +test('empty functions list leaves console statements', (t) => { + const input = ` + foo(123); + console.log('a'); + console.error('b'); + bar(789); + `; + + const options = { functions: [] }; + + t.is(stripped(input, options), UNCHANGED); }); -test('removes assert statements', (t) => { - compare(t, 'assert'); +test('removes console statements by default', (t) => { + const input = ` + foo(123); + console.log('a'); + console.error('b'); + bar(789); + `; + + const expected = ` + foo(123); + bar(789); + `; + + t.is(stripped(input), expected); }); -test('leaves console statements if custom functions are provided', (t) => { - compare(t, 'console-custom', { functions: ['console.log'] }); +test('empty functions list leaves assert statements', (t) => { + const input = ` + /* eslint-disable */ + function foo(message) { + assert.equal(arguments.length, 1); + assert.equal(typeof arguments[0], 'string'); + bar(message); + } + `; + + const options = { functions: [] }; + + t.is(stripped(input, options), UNCHANGED); +}); + +test('removes assert statements by default', (t) => { + const input = ` + /* eslint-disable */ + function foo(message) { + assert.equal(arguments.length, 1); + assert.equal(typeof arguments[0], 'string'); + bar(message); + } + `; + + const expected = ` + /* eslint-disable */ + function foo(message) { + bar(message); + } + `; + + t.is(stripped(input), expected); +}); + +test('leaves other console statements if custom functions are provided', (t) => { + const input = ` + foo(123); + console.log('a'); + console.error('b'); + bar(789); + `; + + const options = { functions: ['console.log'] }; + + const expected = ` + foo(123); + console.error('b'); + bar(789); + `; + + t.is(stripped(input, options), expected); }); test('removes custom functions', (t) => { - compare(t, 'custom', { functions: ['debug', 'custom.*'] }); + const input = ` + a(); + debug('hello'); + b(); + custom.foo('foo'); + custom.bar('bar'); + c(); + `; + + const options = { functions: ['debug', 'custom.*'] }; + + const expected = ` + a(); + b(); + c(); + `; + + t.is(stripped(input, options), expected); }); -test('rewrtestes inline call expressions (not expression statements) as void 0', (t) => { - compare(t, 'inline-call-expressions'); +test('rewrites inline call expressions (not expression statements) as void 0', (t) => { + const input = `DEBUG && console.log('debugging');`; + const expected = `DEBUG && (void 0);`; + + t.is(stripped(input), expected); }); -test('rewrtestes inline if expessions as void 0', (t) => { - compare(t, 'inline-if'); +test('rewrites inline if expessions as void 0', (t) => { + const input = `if (DEBUG) console.log('debugging');`; + const expected = `if (DEBUG) (void 0);`; + + t.is(stripped(input), expected); +}); + +test('rewrites expressions as void 0 in lambdas', (t) => { + const input = ` + console.log(['h', 'e', 'y'].forEach((letter) => console.warn(letter))) + `; + + const options = { functions: ['console.warn'] }; + + const expected = ` + console.log(['h', 'e', 'y'].forEach((letter) => (void 0))) + `; + + t.is(stripped(input, options), expected); }); test('removes expressions in if blocks', (t) => { - compare(t, 'if-block'); + const input = ` + if (DEBUG) { + console.log('debugging'); + } + `; + + const expected = ` + if (DEBUG) { + } + `; + + t.is(stripped(input), expected); }); test('removes methods of this', (t) => { - compare(t, 'this-method', { functions: ['this.*'] }); + const input = ` + a(); + this.foo('foo'); + this.bar('bar'); + b(); + `; + + const options = { functions: ['this.*'] }; + + const expected = ` + a(); + b(); + `; + + t.is(stripped(input, options), expected); }); test('removes super calls', (t) => { - compare(t, 'super-method', { functions: ['super.log'] }); + const input = ` + /* eslint-disable */ + class Foo { + bar() { + a(); + super.log('hello'); + b(); + } + } + `; + + const options = { functions: ['super.log'] }; + + const expected = ` + /* eslint-disable */ + class Foo { + bar() { + a(); + b(); + } + } + `; + + t.is(stripped(input, options), expected); }); test('replaces case body with void 0', (t) => { - compare(t, 'switch-case'); + const input = ` + switch (a) { + case 1: + debugger; + } + `; + + const expected = ` + switch (a) { + case 1: + (void 0); + } + `; + + t.is(stripped(input), expected); }); -test('rewrtestes inline while expressions as void 0', (t) => { - compare(t, 'inline-while'); +test('rewrites inline while expressions as void 0', (t) => { + const input = `while (test()) console.log('still true!');`; + const expected = `while (test()) (void 0);`; + + t.is(stripped(input), expected); }); test('supports object destructuring assignments with default values', (t) => { - compare(t, 'object-destructuring-default'); + const input = ` + export function fn({ foo = console.log(), bar } = {}) { + const { baz = console.log() } = bar; + console.log(foo, bar, baz); + } + `; + + const expected = ` + export function fn({ foo = (void 0), bar } = {}) { + const { baz = (void 0) } = bar; + } + `; + + t.is(stripped(input), expected); }); test('removes labeled blocks', (t) => { - compare(t, 'label-block', { labels: ['untesttest'] }); + const input = ` + before(); + unittest: { + test('some test', (assert) => { + assert.true(true); + }); + } + after(); + `; + + const options = { labels: ['unittest'] }; + + const expected = ` + before(); + after(); + `; + + t.is(stripped(input, options), expected); }); test('removes multiple labeled blocks', (t) => { - compare(t, 'label-block-multiple', { labels: ['first', 'second'] }); + const input = ` + before(); + first: { + things(); + } + after(); + second: { + assert.things(); + } + again(); + `; + + const options = { labels: ['first', 'second'] }; + + const expected = ` + before(); + after(); + again(); + `; + + t.is(stripped(input, options), expected); }); test('only removes specified blocks', (t) => { - compare(t, 'label-block-discriminate', { labels: ['second'] }); + const input = ` + before(); + first: { + things(); + } + after(); + second: { + assert.things(); + } + again(); + `; + + const options = { labels: ['second'] }; + + const expected = ` + before(); + first: { + things(); + } + after(); + again(); + `; + + t.is(stripped(input, options), expected); +}); + +test('removes labeled blocks when filtered function is present', (t) => { + const input = ` + before(); + unittest: { + test('some test', (assert) => { + }); + } + after(); + `; + + const options = { labels: ['unittest'], functions: ['console.*'] }; + + const expected = ` + before(); + after(); + `; + + t.is(stripped(input, options), expected); +}); + +test('removes labeled blocks when functions list is empty', (t) => { + const input = ` + before(); + unittest: { + test('some test', (assert) => { + }); + } + after(); + `; + + const options = { labels: ['unittest'], functions: [] }; + + const expected = ` + before(); + after(); + `; + + t.is(stripped(input, options), expected); +}); + +test('whitespace between label and colon is accepted', (t) => { + const input = ` + before(); + unittest + : { + test('some test', (assert) => { + }); + } + after(); + `; + + const options = { labels: ['unittest'], functions: ['console.*'] }; + + const expected = ` + before(); + after(); + `; + + t.is(stripped(input, options), expected); +}); + +test('removing a lable also works for expressions', (t) => { + const input = ` + before();unittest:console.log();after(); + `; + + const options = { labels: ['unittest'] }; + + const expected = ` + before();after(); + `; + + t.is(stripped(input, options), expected); +}); + +test('the same label can occur multiple times and all are removed', (t) => { + const input = ` + before(); + unittest + : { + test('some test', (assert) => { + }); + } + again(); + unittest:console.log(); + after(); + `; + + const options = { labels: ['unittest'], functions: ['console.*'] }; + + const expected = ` + before(); + again(); + after(); + `; + + t.is(stripped(input, options), expected); +}); + +test('removes labeled even with awkward spacing', (t) => { + const input = ` + before(); + unittest + : { + test('some test', (assert) => { + }); + } + again(); + unittest:console.log(); + after(); + `; + + const options = { labels: ['unittest'], functions: ['console.*'] }; + + const expected = ` + before(); + again(); + after(); + `; + + t.is(stripped(input, options), expected); +}); + +test('spaces around . in function calls are accepted', (t) => { + const input = ` + before(); + function f() { + return { + g: function () { + return { + hello: function () { + console.log('hello'); + } + }; + } + }; + } + + Test + . + f() + . g() . + hello(); + after(); + `; + + const options = { functions: ['Test.f', 'g', 'hello'] }; + + const expected = ` + before(); + function f() { + return { + g: function () { + return { + hello: function () { + console.log('hello'); + } + }; + } + }; + } + + (void 0) + . g() . + hello(); + after(); + `; + + t.is(stripped(input, options), expected); +}); + +test('function calls without . are replaced with (void 0)', (t) => { + const input = ` + before(); + f() . g() . hello(); + after(); + `; + + const options = { functions: ['f', 'g', 'hello'] }; + + const expected = ` + before(); + (void 0) . g() . hello(); + after(); + `; + + t.is(stripped(input, options), expected); }); From 37df374db4849776759eb8697f4885e56b20d1a9 Mon Sep 17 00:00:00 2001 From: Duncan Kimpton Date: Wed, 8 Jul 2020 15:19:08 +0200 Subject: [PATCH 2/8] restoring test fixtures --- packages/strip/test/fixtures/assert/input.js | 6 + .../test/fixtures/console-custom/input.js | 4 + packages/strip/test/fixtures/console/input.js | 4 + packages/strip/test/fixtures/custom/input.js | 6 + .../test/fixtures/debugger-false/input.js | 5 + .../strip/test/fixtures/debugger/input.js | 5 + .../fixtures/excluded-not-changed/input.js | 9 + .../strip/test/fixtures/if-block/input.js | 3 + .../fixtures/inline-call-expressions/input.js | 1 + .../strip/test/fixtures/inline-if/input.js | 1 + .../strip/test/fixtures/inline-while/input.js | 1 + .../label-block-discriminate/input.js | 9 + .../fixtures/label-block-multiple/input.js | 9 + .../strip/test/fixtures/label-block/input.js | 7 + .../strip/test/fixtures/no-changes/input.js | 9 + .../object-destructuring-default/input.js | 4 + .../strip/test/fixtures/super-method/input.js | 8 + .../strip/test/fixtures/switch-case/input.js | 4 + .../strip/test/fixtures/this-method/input.js | 4 + packages/strip/test/snapshots/test.js.md | 164 +++++ packages/strip/test/snapshots/test.js.snap | Bin 0 -> 846 bytes packages/strip/test/test.js | 565 +----------------- 22 files changed, 294 insertions(+), 534 deletions(-) create mode 100644 packages/strip/test/fixtures/assert/input.js create mode 100644 packages/strip/test/fixtures/console-custom/input.js create mode 100644 packages/strip/test/fixtures/console/input.js create mode 100644 packages/strip/test/fixtures/custom/input.js create mode 100644 packages/strip/test/fixtures/debugger-false/input.js create mode 100644 packages/strip/test/fixtures/debugger/input.js create mode 100644 packages/strip/test/fixtures/excluded-not-changed/input.js create mode 100644 packages/strip/test/fixtures/if-block/input.js create mode 100644 packages/strip/test/fixtures/inline-call-expressions/input.js create mode 100644 packages/strip/test/fixtures/inline-if/input.js create mode 100644 packages/strip/test/fixtures/inline-while/input.js create mode 100644 packages/strip/test/fixtures/label-block-discriminate/input.js create mode 100644 packages/strip/test/fixtures/label-block-multiple/input.js create mode 100644 packages/strip/test/fixtures/label-block/input.js create mode 100644 packages/strip/test/fixtures/no-changes/input.js create mode 100644 packages/strip/test/fixtures/object-destructuring-default/input.js create mode 100644 packages/strip/test/fixtures/super-method/input.js create mode 100644 packages/strip/test/fixtures/switch-case/input.js create mode 100644 packages/strip/test/fixtures/this-method/input.js create mode 100644 packages/strip/test/snapshots/test.js.md create mode 100644 packages/strip/test/snapshots/test.js.snap diff --git a/packages/strip/test/fixtures/assert/input.js b/packages/strip/test/fixtures/assert/input.js new file mode 100644 index 000000000..fddefeed9 --- /dev/null +++ b/packages/strip/test/fixtures/assert/input.js @@ -0,0 +1,6 @@ +/* eslint-disable */ +function foo(message) { + assert.equal(arguments.length, 1); + assert.equal(typeof arguments[0], 'string'); + bar(message); +} diff --git a/packages/strip/test/fixtures/console-custom/input.js b/packages/strip/test/fixtures/console-custom/input.js new file mode 100644 index 000000000..7c00b4db8 --- /dev/null +++ b/packages/strip/test/fixtures/console-custom/input.js @@ -0,0 +1,4 @@ +foo(123); +console.log('a'); +console.error('b'); +bar(789); diff --git a/packages/strip/test/fixtures/console/input.js b/packages/strip/test/fixtures/console/input.js new file mode 100644 index 000000000..7c00b4db8 --- /dev/null +++ b/packages/strip/test/fixtures/console/input.js @@ -0,0 +1,4 @@ +foo(123); +console.log('a'); +console.error('b'); +bar(789); diff --git a/packages/strip/test/fixtures/custom/input.js b/packages/strip/test/fixtures/custom/input.js new file mode 100644 index 000000000..fc2599570 --- /dev/null +++ b/packages/strip/test/fixtures/custom/input.js @@ -0,0 +1,6 @@ +a(); +debug('hello'); +b(); +custom.foo('foo'); +custom.bar('bar'); +c(); diff --git a/packages/strip/test/fixtures/debugger-false/input.js b/packages/strip/test/fixtures/debugger-false/input.js new file mode 100644 index 000000000..458057512 --- /dev/null +++ b/packages/strip/test/fixtures/debugger-false/input.js @@ -0,0 +1,5 @@ +export default function foo() { + before(); + debugger; + after(); +} diff --git a/packages/strip/test/fixtures/debugger/input.js b/packages/strip/test/fixtures/debugger/input.js new file mode 100644 index 000000000..458057512 --- /dev/null +++ b/packages/strip/test/fixtures/debugger/input.js @@ -0,0 +1,5 @@ +export default function foo() { + before(); + debugger; + after(); +} diff --git a/packages/strip/test/fixtures/excluded-not-changed/input.js b/packages/strip/test/fixtures/excluded-not-changed/input.js new file mode 100644 index 000000000..bd4e4ddba --- /dev/null +++ b/packages/strip/test/fixtures/excluded-not-changed/input.js @@ -0,0 +1,9 @@ +/* eslint-disable */ +export default function foo() { + before(); + debugger; + logging: + console.log('a'); + console.error('b'); + after(); +} diff --git a/packages/strip/test/fixtures/if-block/input.js b/packages/strip/test/fixtures/if-block/input.js new file mode 100644 index 000000000..96b5dbbd3 --- /dev/null +++ b/packages/strip/test/fixtures/if-block/input.js @@ -0,0 +1,3 @@ +if (DEBUG) { + console.log('debugging'); +} diff --git a/packages/strip/test/fixtures/inline-call-expressions/input.js b/packages/strip/test/fixtures/inline-call-expressions/input.js new file mode 100644 index 000000000..f58db6763 --- /dev/null +++ b/packages/strip/test/fixtures/inline-call-expressions/input.js @@ -0,0 +1 @@ +DEBUG && console.log('debugging'); diff --git a/packages/strip/test/fixtures/inline-if/input.js b/packages/strip/test/fixtures/inline-if/input.js new file mode 100644 index 000000000..5087569fb --- /dev/null +++ b/packages/strip/test/fixtures/inline-if/input.js @@ -0,0 +1 @@ +if (DEBUG) console.log('debugging'); diff --git a/packages/strip/test/fixtures/inline-while/input.js b/packages/strip/test/fixtures/inline-while/input.js new file mode 100644 index 000000000..62b0f6003 --- /dev/null +++ b/packages/strip/test/fixtures/inline-while/input.js @@ -0,0 +1 @@ +while (test()) console.log('still true!'); diff --git a/packages/strip/test/fixtures/label-block-discriminate/input.js b/packages/strip/test/fixtures/label-block-discriminate/input.js new file mode 100644 index 000000000..d1d03a29d --- /dev/null +++ b/packages/strip/test/fixtures/label-block-discriminate/input.js @@ -0,0 +1,9 @@ +before(); +first: { + things(); +} +after(); +second: { + assert.things(); +} +again(); diff --git a/packages/strip/test/fixtures/label-block-multiple/input.js b/packages/strip/test/fixtures/label-block-multiple/input.js new file mode 100644 index 000000000..d1d03a29d --- /dev/null +++ b/packages/strip/test/fixtures/label-block-multiple/input.js @@ -0,0 +1,9 @@ +before(); +first: { + things(); +} +after(); +second: { + assert.things(); +} +again(); diff --git a/packages/strip/test/fixtures/label-block/input.js b/packages/strip/test/fixtures/label-block/input.js new file mode 100644 index 000000000..7c805e051 --- /dev/null +++ b/packages/strip/test/fixtures/label-block/input.js @@ -0,0 +1,7 @@ +before(); +unittest: { + test('some test', (assert) => { + assert.true(true); + }); +} +after(); diff --git a/packages/strip/test/fixtures/no-changes/input.js b/packages/strip/test/fixtures/no-changes/input.js new file mode 100644 index 000000000..bd4e4ddba --- /dev/null +++ b/packages/strip/test/fixtures/no-changes/input.js @@ -0,0 +1,9 @@ +/* eslint-disable */ +export default function foo() { + before(); + debugger; + logging: + console.log('a'); + console.error('b'); + after(); +} diff --git a/packages/strip/test/fixtures/object-destructuring-default/input.js b/packages/strip/test/fixtures/object-destructuring-default/input.js new file mode 100644 index 000000000..700b8e771 --- /dev/null +++ b/packages/strip/test/fixtures/object-destructuring-default/input.js @@ -0,0 +1,4 @@ +export function fn({ foo = console.log(), bar } = {}) { + const { baz = console.log() } = bar; + console.log(foo, bar, baz); +} diff --git a/packages/strip/test/fixtures/super-method/input.js b/packages/strip/test/fixtures/super-method/input.js new file mode 100644 index 000000000..2e4b21172 --- /dev/null +++ b/packages/strip/test/fixtures/super-method/input.js @@ -0,0 +1,8 @@ +/* eslint-disable */ +class Foo { + bar() { + a(); + super.log('hello'); + b(); + } +} diff --git a/packages/strip/test/fixtures/switch-case/input.js b/packages/strip/test/fixtures/switch-case/input.js new file mode 100644 index 000000000..79815fa9b --- /dev/null +++ b/packages/strip/test/fixtures/switch-case/input.js @@ -0,0 +1,4 @@ +switch (a) { + case 1: + debugger; +} diff --git a/packages/strip/test/fixtures/this-method/input.js b/packages/strip/test/fixtures/this-method/input.js new file mode 100644 index 000000000..e927c7e8f --- /dev/null +++ b/packages/strip/test/fixtures/this-method/input.js @@ -0,0 +1,4 @@ +a(); +this.foo('foo'); +this.bar('bar'); +b(); diff --git a/packages/strip/test/snapshots/test.js.md b/packages/strip/test/snapshots/test.js.md new file mode 100644 index 000000000..76bed0fd5 --- /dev/null +++ b/packages/strip/test/snapshots/test.js.md @@ -0,0 +1,164 @@ +# Snapshot report for `test/test.js` + +The actual snapshot is saved in `test.js.snap`. + +Generated by [AVA](https://ava.li). + +## does not remove debugger statements with debugger: false + +> Snapshot 1 + + `export default function foo() {␊ + before();␊ + debugger;␊ + after();␊ + }␊ + ` + +## leaves console statements if custom functions are provided + +> Snapshot 1 + + `foo(123);␊ + console.error('b');␊ + bar(789);␊ + ` + +## only removes specified blocks + +> Snapshot 1 + + `before();␊ + first: {␊ + things();␊ + }␊ + after();␊ + again();␊ + ` + +## removes assert statements + +> Snapshot 1 + + `/* eslint-disable */␊ + function foo(message) {␊ + bar(message);␊ + }␊ + ` + +## removes console statements + +> Snapshot 1 + + `foo(123);␊ + bar(789);␊ + ` + +## removes custom functions + +> Snapshot 1 + + `a();␊ + b();␊ + c();␊ + ` + +## removes debugger statements + +> Snapshot 1 + + `export default function foo() {␊ + before();␊ + after();␊ + }␊ + ` + +## removes expressions in if blocks + +> Snapshot 1 + + `if (DEBUG) {␊ + }␊ + ` + +## removes labeled blocks + +> Snapshot 1 + + `before();␊ + unittest: {␊ + test('some test', (assert) => {␊ + });␊ + }␊ + after();␊ + ` + +## removes methods of this + +> Snapshot 1 + + `a();␊ + b();␊ + ` + +## removes multiple labeled blocks + +> Snapshot 1 + + `before();␊ + after();␊ + again();␊ + ` + +## removes super calls + +> Snapshot 1 + + `/* eslint-disable */␊ + class Foo {␊ + bar() {␊ + a();␊ + b();␊ + }␊ + }␊ + ` + +## replaces case body with void 0 + +> Snapshot 1 + + `switch (a) {␊ + case 1:␊ + void 0;␊ + }␊ + ` + +## rewrtestes inline call expressions (not expression statements) as void 0 + +> Snapshot 1 + + `DEBUG && void 0;␊ + ` + +## rewrtestes inline if expessions as void 0 + +> Snapshot 1 + + `if (DEBUG) void 0;␊ + ` + +## rewrtestes inline while expressions as void 0 + +> Snapshot 1 + + `while (test()) void 0;␊ + ` + +## supports object destructuring assignments with default values + +> Snapshot 1 + + `export function fn({ foo = void 0, bar } = {}) {␊ + const { baz = void 0 } = bar;␊ + }␊ + ` diff --git a/packages/strip/test/snapshots/test.js.snap b/packages/strip/test/snapshots/test.js.snap new file mode 100644 index 0000000000000000000000000000000000000000..5c1e641a460b0c5b8e48f2e729a744566f5f92f4 GIT binary patch literal 846 zcmV-U1F`%;RzV*Af0b(QxD!F`_!Ru#?V9^*LzE<(}$b@F4{=a`Ct_kVw z`ojnotp;K(*N+9^CcaNDuX?=e^EY=tMzH7{AdZj_TTvflBQbxAt-hg-y9*;&bSDt6 z-8t{In2Y;|D-xU6+I_immJuv^5s26ANIdEiyol+_)Yt8rcJb|uU{NI|28JnntO~L( zA6~Pt`E20KmUM4MuxKa{|24n4kjt`H;6=IpIku3lPmEyEDj<%q*fXn8B!(k??*6@N z7yYVb1dHAV;_0m}g6lWDcv1R@;r<@GlL3riQ6pvs23;n*#aCb29ACg68><>1v78Ys z8Uw^rkNh{Wl<5+jsJiK9Sx>`4MzH8yAZBJ@2gem7D}x}TWoku1eo=`+N@`kSX-FKFO z_>2?*X*D!52FfHQ7HODUSOQrnD(v;O6jFE>28O zg}MM}3`_vSIt_$%$@zK3`8lb2sYONkMH=c!>bRZ2p9pe!5{O9FK$jGBadirHS5Q+^ zD9g`GQ7}N07S2pl&;TpZ#IArJNkKKbd$|xcqli0W4V2`Z#NuKFxBPr~KtO^782peJ z0SZDGwdfY97MEw1BxfjSBto<#Cl;qF7+Qh#z?_YyMZ7#C6KJnSNosM4h9(Z*1w)ez zBEIr8szH&aU<=it19GTBEs$Sb3$+^BSnTHoy%40Ou_KdR+wo0GZTz-v9sr literal 0 HcmV?d00001 diff --git a/packages/strip/test/test.js b/packages/strip/test/test.js index d0a1f68ed..27fa54b87 100755 --- a/packages/strip/test/test.js +++ b/packages/strip/test/test.js @@ -1,3 +1,4 @@ +const fs = require('fs'); const path = require('path'); const acorn = require('acorn'); @@ -6,12 +7,10 @@ const test = require('ava'); // eslint-disable-next-line import/no-unresolved const strip = require('..'); -const UNCHANGED = null; -const testFileName = 'virtual-test-case.js'; - -const stripped = (input, options) => { - const virtualFilename = path.resolve(`test/fixtures/${testFileName}`); - const result = strip(options).transform.call( +const compare = (t, fixture, options) => { + const filename = path.resolve(`test/fixtures/${fixture}/input.js`); + const input = fs.readFileSync(filename, 'utf-8'); + const output = strip(options).transform.call( { parse: (code) => acorn.parse(code, { @@ -20,578 +19,76 @@ const stripped = (input, options) => { }) }, input, - virtualFilename + filename ); - return result === null ? UNCHANGED : result.code; + t.snapshot(output ? output.code : input); }; -// --------------- TESTS --------------- - -test('can be configured to make no changes', (t) => { - const input = ` - export default function foo() { - before(); - debugger; - logging: - console.log('a'); - console.error('b'); - after(); - } - `; - - const options = { - labels: [], - functions: [], - debugger: false - }; - - t.is(stripped(input, options), UNCHANGED); -}); - -test('excluded files do not get changed', (t) => { - const input = ` - export default function foo() { - before(); - debugger; - logging: - console.log('a'); - console.error('b'); - after(); - } - `; - - const options = { - exclude: `**/${testFileName}` - }; - - t.is(stripped(input, options), UNCHANGED); -}); - -test('removes debugger statements by default', (t) => { - const input = ` - export default function foo() { - before(); - debugger; - after(); - } - `; - - const expected = ` - export default function foo() { - before(); - after(); - } - `; - - t.is(stripped(input), expected); +test('removes debugger statements', (t) => { + compare(t, 'debugger'); }); test('does not remove debugger statements with debugger: false', (t) => { - const input = ` - export default function foo() { - before(); - debugger; - after(); - } - `; - - const options = { debugger: false }; - - t.is(stripped(input, options), UNCHANGED); + compare(t, 'debugger-false', { debugger: false }); }); -test('empty functions list leaves console statements', (t) => { - const input = ` - foo(123); - console.log('a'); - console.error('b'); - bar(789); - `; - - const options = { functions: [] }; - - t.is(stripped(input, options), UNCHANGED); +test('removes console statements', (t) => { + compare(t, 'console'); }); -test('removes console statements by default', (t) => { - const input = ` - foo(123); - console.log('a'); - console.error('b'); - bar(789); - `; - - const expected = ` - foo(123); - bar(789); - `; - - t.is(stripped(input), expected); +test('removes assert statements', (t) => { + compare(t, 'assert'); }); -test('empty functions list leaves assert statements', (t) => { - const input = ` - /* eslint-disable */ - function foo(message) { - assert.equal(arguments.length, 1); - assert.equal(typeof arguments[0], 'string'); - bar(message); - } - `; - - const options = { functions: [] }; - - t.is(stripped(input, options), UNCHANGED); -}); - -test('removes assert statements by default', (t) => { - const input = ` - /* eslint-disable */ - function foo(message) { - assert.equal(arguments.length, 1); - assert.equal(typeof arguments[0], 'string'); - bar(message); - } - `; - - const expected = ` - /* eslint-disable */ - function foo(message) { - bar(message); - } - `; - - t.is(stripped(input), expected); -}); - -test('leaves other console statements if custom functions are provided', (t) => { - const input = ` - foo(123); - console.log('a'); - console.error('b'); - bar(789); - `; - - const options = { functions: ['console.log'] }; - - const expected = ` - foo(123); - console.error('b'); - bar(789); - `; - - t.is(stripped(input, options), expected); +test('leaves console statements if custom functions are provided', (t) => { + compare(t, 'console-custom', { functions: ['console.log'] }); }); test('removes custom functions', (t) => { - const input = ` - a(); - debug('hello'); - b(); - custom.foo('foo'); - custom.bar('bar'); - c(); - `; - - const options = { functions: ['debug', 'custom.*'] }; - - const expected = ` - a(); - b(); - c(); - `; - - t.is(stripped(input, options), expected); + compare(t, 'custom', { functions: ['debug', 'custom.*'] }); }); -test('rewrites inline call expressions (not expression statements) as void 0', (t) => { - const input = `DEBUG && console.log('debugging');`; - const expected = `DEBUG && (void 0);`; - - t.is(stripped(input), expected); +test('rewrtestes inline call expressions (not expression statements) as void 0', (t) => { + compare(t, 'inline-call-expressions'); }); -test('rewrites inline if expessions as void 0', (t) => { - const input = `if (DEBUG) console.log('debugging');`; - const expected = `if (DEBUG) (void 0);`; - - t.is(stripped(input), expected); -}); - -test('rewrites expressions as void 0 in lambdas', (t) => { - const input = ` - console.log(['h', 'e', 'y'].forEach((letter) => console.warn(letter))) - `; - - const options = { functions: ['console.warn'] }; - - const expected = ` - console.log(['h', 'e', 'y'].forEach((letter) => (void 0))) - `; - - t.is(stripped(input, options), expected); +test('rewrtestes inline if expessions as void 0', (t) => { + compare(t, 'inline-if'); }); test('removes expressions in if blocks', (t) => { - const input = ` - if (DEBUG) { - console.log('debugging'); - } - `; - - const expected = ` - if (DEBUG) { - } - `; - - t.is(stripped(input), expected); + compare(t, 'if-block'); }); test('removes methods of this', (t) => { - const input = ` - a(); - this.foo('foo'); - this.bar('bar'); - b(); - `; - - const options = { functions: ['this.*'] }; - - const expected = ` - a(); - b(); - `; - - t.is(stripped(input, options), expected); + compare(t, 'this-method', { functions: ['this.*'] }); }); test('removes super calls', (t) => { - const input = ` - /* eslint-disable */ - class Foo { - bar() { - a(); - super.log('hello'); - b(); - } - } - `; - - const options = { functions: ['super.log'] }; - - const expected = ` - /* eslint-disable */ - class Foo { - bar() { - a(); - b(); - } - } - `; - - t.is(stripped(input, options), expected); + compare(t, 'super-method', { functions: ['super.log'] }); }); test('replaces case body with void 0', (t) => { - const input = ` - switch (a) { - case 1: - debugger; - } - `; - - const expected = ` - switch (a) { - case 1: - (void 0); - } - `; - - t.is(stripped(input), expected); + compare(t, 'switch-case'); }); -test('rewrites inline while expressions as void 0', (t) => { - const input = `while (test()) console.log('still true!');`; - const expected = `while (test()) (void 0);`; - - t.is(stripped(input), expected); +test('rewrtestes inline while expressions as void 0', (t) => { + compare(t, 'inline-while'); }); test('supports object destructuring assignments with default values', (t) => { - const input = ` - export function fn({ foo = console.log(), bar } = {}) { - const { baz = console.log() } = bar; - console.log(foo, bar, baz); - } - `; - - const expected = ` - export function fn({ foo = (void 0), bar } = {}) { - const { baz = (void 0) } = bar; - } - `; - - t.is(stripped(input), expected); + compare(t, 'object-destructuring-default'); }); test('removes labeled blocks', (t) => { - const input = ` - before(); - unittest: { - test('some test', (assert) => { - assert.true(true); - }); - } - after(); - `; - - const options = { labels: ['unittest'] }; - - const expected = ` - before(); - after(); - `; - - t.is(stripped(input, options), expected); + compare(t, 'label-block', { labels: ['untesttest'] }); }); test('removes multiple labeled blocks', (t) => { - const input = ` - before(); - first: { - things(); - } - after(); - second: { - assert.things(); - } - again(); - `; - - const options = { labels: ['first', 'second'] }; - - const expected = ` - before(); - after(); - again(); - `; - - t.is(stripped(input, options), expected); + compare(t, 'label-block-multiple', { labels: ['first', 'second'] }); }); test('only removes specified blocks', (t) => { - const input = ` - before(); - first: { - things(); - } - after(); - second: { - assert.things(); - } - again(); - `; - - const options = { labels: ['second'] }; - - const expected = ` - before(); - first: { - things(); - } - after(); - again(); - `; - - t.is(stripped(input, options), expected); -}); - -test('removes labeled blocks when filtered function is present', (t) => { - const input = ` - before(); - unittest: { - test('some test', (assert) => { - }); - } - after(); - `; - - const options = { labels: ['unittest'], functions: ['console.*'] }; - - const expected = ` - before(); - after(); - `; - - t.is(stripped(input, options), expected); -}); - -test('removes labeled blocks when functions list is empty', (t) => { - const input = ` - before(); - unittest: { - test('some test', (assert) => { - }); - } - after(); - `; - - const options = { labels: ['unittest'], functions: [] }; - - const expected = ` - before(); - after(); - `; - - t.is(stripped(input, options), expected); -}); - -test('whitespace between label and colon is accepted', (t) => { - const input = ` - before(); - unittest - : { - test('some test', (assert) => { - }); - } - after(); - `; - - const options = { labels: ['unittest'], functions: ['console.*'] }; - - const expected = ` - before(); - after(); - `; - - t.is(stripped(input, options), expected); -}); - -test('removing a lable also works for expressions', (t) => { - const input = ` - before();unittest:console.log();after(); - `; - - const options = { labels: ['unittest'] }; - - const expected = ` - before();after(); - `; - - t.is(stripped(input, options), expected); -}); - -test('the same label can occur multiple times and all are removed', (t) => { - const input = ` - before(); - unittest - : { - test('some test', (assert) => { - }); - } - again(); - unittest:console.log(); - after(); - `; - - const options = { labels: ['unittest'], functions: ['console.*'] }; - - const expected = ` - before(); - again(); - after(); - `; - - t.is(stripped(input, options), expected); -}); - -test('removes labeled even with awkward spacing', (t) => { - const input = ` - before(); - unittest - : { - test('some test', (assert) => { - }); - } - again(); - unittest:console.log(); - after(); - `; - - const options = { labels: ['unittest'], functions: ['console.*'] }; - - const expected = ` - before(); - again(); - after(); - `; - - t.is(stripped(input, options), expected); -}); - -test('spaces around . in function calls are accepted', (t) => { - const input = ` - before(); - function f() { - return { - g: function () { - return { - hello: function () { - console.log('hello'); - } - }; - } - }; - } - - Test - . - f() - . g() . - hello(); - after(); - `; - - const options = { functions: ['Test.f', 'g', 'hello'] }; - - const expected = ` - before(); - function f() { - return { - g: function () { - return { - hello: function () { - console.log('hello'); - } - }; - } - }; - } - - (void 0) - . g() . - hello(); - after(); - `; - - t.is(stripped(input, options), expected); -}); - -test('function calls without . are replaced with (void 0)', (t) => { - const input = ` - before(); - f() . g() . hello(); - after(); - `; - - const options = { functions: ['f', 'g', 'hello'] }; - - const expected = ` - before(); - (void 0) . g() . hello(); - after(); - `; - - t.is(stripped(input, options), expected); + compare(t, 'label-block-discriminate', { labels: ['second'] }); }); From 06871ec06f76b68c82e9d42bfa09541614783f20 Mon Sep 17 00:00:00 2001 From: Duncan Kimpton Date: Wed, 8 Jul 2020 15:19:59 +0200 Subject: [PATCH 3/8] code review changes --- packages/strip/README.md | 33 +++++++-------------------------- packages/strip/src/index.js | 24 ++++++++++++------------ 2 files changed, 19 insertions(+), 38 deletions(-) diff --git a/packages/strip/README.md b/packages/strip/README.md index d4e660680..44bac09dd 100755 --- a/packages/strip/README.md +++ b/packages/strip/README.md @@ -36,35 +36,16 @@ export default { dir: 'output', format: 'cjs' }, - plugins: [strip({})] + plugins: [ + strip({ + labels: ['unittest'] + }) + ] }; ``` Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api). -### Default Options - -By default the **strip** plugin applies the following options, you can override by specifing options manually. - -> consider files matching **'\*\*/\*.js'** -> remove functions matching '**console.\***' and '**assert.\***' -> remove any **debugger** statements - -### Custom Options Example - -```js -plugins: [ - strip({ - include: '**/*.(mjs|js)', - exclude: 'tests/**/*', - debugger: true, - functions: ['console.log', 'assert.*'], - labels: ['unittest'], - sourceMap: true - }) -]; -``` - ## Options ### `include` @@ -89,7 +70,7 @@ Type: `Boolean`
Default: `true`
Example: `debugger: false,`
-If `true` or unspecified, instructs the plugin to remove debugger statements. +If `true` instructs the plugin to remove debugger statements. ### `functions` @@ -117,7 +98,7 @@ Type: `Boolean`
Default: `true`
Example: `sourceMap: false,`
-If `true` or unspecified, instructs the plugin to update source maps accordingly after removing configured targets from the bundle. +If `true` instructs the plugin to update source maps accordingly after removing configured targets from the bundle. ## Meta diff --git a/packages/strip/src/index.js b/packages/strip/src/index.js index f8f32a998..551fae60a 100755 --- a/packages/strip/src/index.js +++ b/packages/strip/src/index.js @@ -39,23 +39,23 @@ export default function strip(options = {}) { const removeDebuggerStatements = options.debugger !== false; - const functions = options.functions || ['console.*', 'assert.*']; + const functions = (options.functions || ['console.*', 'assert.*']).map((keypath) => + keypath.replace(/\./g, '\\.').replace(/\*/g, '\\w+') + ); + const labels = options.labels || []; - const functionsPatterns = functions.map((f) => - f.replace(/\*/g, '\\w+').replace(/\./g, '\\.\\s*') - ); const labelsPatterns = labels.map((l) => `${l}\\s*:`); - const firstPassPatterns = removeDebuggerStatements - ? [...functionsPatterns, ...labelsPatterns, 'debugger\\b'] - : [...functionsPatterns, ...labelsPatterns]; + const firstPass = [...functions, ...labelsPatterns]; + if (removeDebuggerStatements) { + firstPass.push('debugger\\b'); + } - const functionsRE = new RegExp(`^(?:${functionsPatterns.join('|')})$`); - const firstpassRE = new RegExp(`\\b(?:${firstPassPatterns.join('|')})`); + const reFunctions = new RegExp(`^(?:${functions.join('|')})$`); + const reFirstpass = new RegExp(`\\b(?:${firstPass.join('|')})`); - const firstPassFilter = - firstPassPatterns.length > 0 ? (code) => firstpassRE.test(code) : () => false; + const firstPassFilter = firstPass.length > 0 ? (code) => reFirstpass.test(code) : () => false; const UNCHANGED = null; @@ -135,7 +135,7 @@ export default function strip(options = {}) { } } else if (node.type === 'CallExpression') { const keypath = flatten(node.callee); - if (keypath && functionsRE.test(keypath)) { + if (keypath && reFunctions.test(keypath)) { removeExpression(node); this.skip(); } From ab96d8ba9357811dd8a3c26cb1bc153c78c22861 Mon Sep 17 00:00:00 2001 From: Duncan Kimpton Date: Wed, 8 Jul 2020 16:02:46 +0200 Subject: [PATCH 4/8] additional tests --- packages/strip/src/index.js | 2 +- .../test/fixtures/functions-direct/input.js | 4 + .../test/fixtures/functions-spaced/input.js | 20 +++ .../fixtures/label-awkward-spacing/input.js | 10 ++ .../test/fixtures/label-expression/input.js | 2 + .../fixtures/label-multiple-times/input.js | 10 ++ .../test/fixtures/label-whitespace/input.js | 7 + .../strip/test/fixtures/lambda-void/input.js | 2 + packages/strip/test/snapshots/test.js.md | 163 +++++++++++++++++- packages/strip/test/snapshots/test.js.snap | Bin 846 -> 1395 bytes packages/strip/test/test.js | 56 ++++++ 11 files changed, 269 insertions(+), 7 deletions(-) create mode 100644 packages/strip/test/fixtures/functions-direct/input.js create mode 100644 packages/strip/test/fixtures/functions-spaced/input.js create mode 100644 packages/strip/test/fixtures/label-awkward-spacing/input.js create mode 100644 packages/strip/test/fixtures/label-expression/input.js create mode 100644 packages/strip/test/fixtures/label-multiple-times/input.js create mode 100644 packages/strip/test/fixtures/label-whitespace/input.js create mode 100644 packages/strip/test/fixtures/lambda-void/input.js diff --git a/packages/strip/src/index.js b/packages/strip/src/index.js index 551fae60a..6b9697192 100755 --- a/packages/strip/src/index.js +++ b/packages/strip/src/index.js @@ -40,7 +40,7 @@ export default function strip(options = {}) { const removeDebuggerStatements = options.debugger !== false; const functions = (options.functions || ['console.*', 'assert.*']).map((keypath) => - keypath.replace(/\./g, '\\.').replace(/\*/g, '\\w+') + keypath.replace(/\*/g, '\\w+').replace(/\./g, '\\s*\\.\\s*') ); const labels = options.labels || []; diff --git a/packages/strip/test/fixtures/functions-direct/input.js b/packages/strip/test/fixtures/functions-direct/input.js new file mode 100644 index 000000000..d60875bdc --- /dev/null +++ b/packages/strip/test/fixtures/functions-direct/input.js @@ -0,0 +1,4 @@ +/* eslint-disable */ + before(); + f().t(); + after(); diff --git a/packages/strip/test/fixtures/functions-spaced/input.js b/packages/strip/test/fixtures/functions-spaced/input.js new file mode 100644 index 000000000..d5e4655ba --- /dev/null +++ b/packages/strip/test/fixtures/functions-spaced/input.js @@ -0,0 +1,20 @@ +/* eslint-disable */ + before(); + function f() { + return { + g: function () { + return { + hello: function () { + console.log('hello'); + } + }; + } + }; + } + + Test + . + f() + . g() . + hello(); + after(); diff --git a/packages/strip/test/fixtures/label-awkward-spacing/input.js b/packages/strip/test/fixtures/label-awkward-spacing/input.js new file mode 100644 index 000000000..70af38461 --- /dev/null +++ b/packages/strip/test/fixtures/label-awkward-spacing/input.js @@ -0,0 +1,10 @@ +/* eslint-disable */ + before(); + unittest + : { + test('some test', (assert) => { + }); + } + again(); + unittest:console.log(); + after(); diff --git a/packages/strip/test/fixtures/label-expression/input.js b/packages/strip/test/fixtures/label-expression/input.js new file mode 100644 index 000000000..a8a10d760 --- /dev/null +++ b/packages/strip/test/fixtures/label-expression/input.js @@ -0,0 +1,2 @@ +/* eslint-disable */ + before();unittest:console.log();after(); diff --git a/packages/strip/test/fixtures/label-multiple-times/input.js b/packages/strip/test/fixtures/label-multiple-times/input.js new file mode 100644 index 000000000..70af38461 --- /dev/null +++ b/packages/strip/test/fixtures/label-multiple-times/input.js @@ -0,0 +1,10 @@ +/* eslint-disable */ + before(); + unittest + : { + test('some test', (assert) => { + }); + } + again(); + unittest:console.log(); + after(); diff --git a/packages/strip/test/fixtures/label-whitespace/input.js b/packages/strip/test/fixtures/label-whitespace/input.js new file mode 100644 index 000000000..5e95eca7b --- /dev/null +++ b/packages/strip/test/fixtures/label-whitespace/input.js @@ -0,0 +1,7 @@ +/* eslint-disable */ + before(); + unittest : { + test('some test', (assert) => { + }); + } + after(); diff --git a/packages/strip/test/fixtures/lambda-void/input.js b/packages/strip/test/fixtures/lambda-void/input.js new file mode 100644 index 000000000..f370859ff --- /dev/null +++ b/packages/strip/test/fixtures/lambda-void/input.js @@ -0,0 +1,2 @@ +/* eslint-disable */ +console.log(['h', 'e', 'y'].forEach((letter) => console.warn(letter))) diff --git a/packages/strip/test/snapshots/test.js.md b/packages/strip/test/snapshots/test.js.md index 76bed0fd5..0fde251ad 100644 --- a/packages/strip/test/snapshots/test.js.md +++ b/packages/strip/test/snapshots/test.js.md @@ -4,6 +4,21 @@ The actual snapshot is saved in `test.js.snap`. Generated by [AVA](https://ava.li). +## can be configured to make no changes + +> Snapshot 1 + + `/* eslint-disable */␊ + export default function foo() {␊ + before();␊ + debugger;␊ + logging:␊ + console.log('a');␊ + console.error('b');␊ + after();␊ + }␊ + ` + ## does not remove debugger statements with debugger: false > Snapshot 1 @@ -15,6 +30,57 @@ Generated by [AVA](https://ava.li). }␊ ` +## empty functions list leaves assert statements + +> Snapshot 1 + + `/* eslint-disable */␊ + function foo(message) {␊ + assert.equal(arguments.length, 1);␊ + assert.equal(typeof arguments[0], 'string');␊ + bar(message);␊ + }␊ + ` + +## empty functions list leaves console statements + +> Snapshot 1 + + `/* eslint-disable */␊ + export default function foo() {␊ + before();␊ + logging:␊ + console.log('a');␊ + console.error('b');␊ + after();␊ + }␊ + ` + +## excluded files do not get changed + +> Snapshot 1 + + `/* eslint-disable */␊ + export default function foo() {␊ + before();␊ + debugger;␊ + logging:␊ + console.log('a');␊ + console.error('b');␊ + after();␊ + }␊ + ` + +## function calls without object are replaced with (void 0) + +> Snapshot 1 + + `/* eslint-disable */␊ + before();␊ + (void 0).t();␊ + after();␊ + ` + ## leaves console statements if custom functions are provided > Snapshot 1 @@ -93,6 +159,32 @@ Generated by [AVA](https://ava.li). after();␊ ` +## removes labeled blocks when filtered function is present + +> Snapshot 1 + + `before();␊ + after();␊ + ` + +## removes labeled blocks when functions list is empty + +> Snapshot 1 + + `before();␊ + after();␊ + ` + +## removes labeled even with awkward spacing + +> Snapshot 1 + + `/* eslint-disable */␊ + before();␊ + again();␊ + after();␊ + ` + ## removes methods of this > Snapshot 1 @@ -123,42 +215,101 @@ Generated by [AVA](https://ava.li). }␊ ` +## removing a lable also works for expressions + +> Snapshot 1 + + `/* eslint-disable */␊ + before();after();␊ + ` + ## replaces case body with void 0 > Snapshot 1 `switch (a) {␊ case 1:␊ - void 0;␊ + (void 0);␊ }␊ ` +## rewrites expressions as void 0 in lambdas + +> Snapshot 1 + + `/* eslint-disable */␊ + console.log(['h', 'e', 'y'].forEach((letter) => (void 0)))␊ + ` + ## rewrtestes inline call expressions (not expression statements) as void 0 > Snapshot 1 - `DEBUG && void 0;␊ + `DEBUG && (void 0);␊ ` ## rewrtestes inline if expessions as void 0 > Snapshot 1 - `if (DEBUG) void 0;␊ + `if (DEBUG) (void 0);␊ ` ## rewrtestes inline while expressions as void 0 > Snapshot 1 - `while (test()) void 0;␊ + `while (test()) (void 0);␊ + ` + +## spaces around . in function calls are accepted + +> Snapshot 1 + + `/* eslint-disable */␊ + before();␊ + function f() {␊ + return {␊ + g: function () {␊ + return {␊ + hello: function () {␊ + console.log('hello');␊ + }␊ + };␊ + }␊ + };␊ + }␊ + ␊ + (void 0)␊ + . g() .␊ + hello();␊ + after();␊ ` ## supports object destructuring assignments with default values > Snapshot 1 - `export function fn({ foo = void 0, bar } = {}) {␊ - const { baz = void 0 } = bar;␊ + `export function fn({ foo = (void 0), bar } = {}) {␊ + const { baz = (void 0) } = bar;␊ }␊ ` + +## the same label can occur multiple times and all are removed + +> Snapshot 1 + + `/* eslint-disable */␊ + before();␊ + again();␊ + after();␊ + ` + +## whitespace between label and colon is accepted + +> Snapshot 1 + + `/* eslint-disable */␊ + before();␊ + after();␊ + ` diff --git a/packages/strip/test/snapshots/test.js.snap b/packages/strip/test/snapshots/test.js.snap index 5c1e641a460b0c5b8e48f2e729a744566f5f92f4..e828bef8131dc140b15a02d6601f1f6c0158d6ea 100644 GIT binary patch literal 1395 zcmV-(1&sPZRzVfDRT#$4K~Y<$L?CA^v@~1k@t^L@vvc;h&w0;# z-t(Tlfr9`T3v}7~obKDr1$(#HR+oNJbQ%Kr*+rD~pzD>a^4tp{9it-F`!|I^N)w3T zeoEtlYy0Ykw2qk5X*-d-3{tw5=;eevYooPq*;k#bE?V_YWd@|QndrdOSxNeh^Ty2O zwtsr^jnin`b7vc*$cG!OVwG2{v zlPJEmy&!Z`z`>&64}r3zQV%IDAUoPS=6c56yH$Y;7eqv{1+f*7(!)eYXC;r_6Y=ft z`Z+h|CEMCtA*EM{E@%I8sx*IG$-{@KmjktRe?dy`5rrjo+7?I8z13KKv+j?sqgcYYZk9;rzprIG1YqMAcH+D9cM zbzD*&sL|cIS`R6$CEE8DdphCGU4EA~w->5)X@!td;159AkyzWZ#*=%i^6Tf9uQ4S< zN?#>VrQJlcU)P#j%yr+LjXrbZ@cF-QLrNzN1t92!>iCW07KNCe-5sgC z^TTmSX&zBG2QGH*Uw>-dwp>j?%$bFd(&I$elS?yagmT<-M->-8YI&^|Qra*KfU_@! zHWZDXzw$;|+v-WHf+``UFFpgnATX46KlBG9{WgK{FoZ>`g_9;^SVY!f#V9OXfwOJA zAfXIwWbIZ78SPxUWZ^kvRRSr%}77Uaw$z zoydt%)h5+#Au%~^}bWe6E}LA+JcN0COba%~Or zM>^yAV^Bik^hHT%{CLD<@|Fw~;c0l@VnhrzseM^eywV}3wWMhh z*>p3*Sg}OTUX9{)uHb65NBVsd$n&g-!o9bCe1`XwtmHJ%OrCc&gYzKCDKR}AYH$u5 zo(rkOEQ^$GMhxqmlFo`4MLK)kT^5f?qqEEwvN9uKQDW3S>$kY)p1C`XWAfy7L-DRD z8aZ9$ASBPxlL4u(gz^Z_?c<{sVtpi++3u@q=PZ((QY?)_lT(TOa!iNhh!|(j(o>Ng z4!SefE$tUJ#2waUU`?!rd&C*#b{Z{$YjrbeeUa(Eg7*MHUY2??gT`xD`1sfDH&gv@ zGrdonJdg!@H|uNdrxC=@4-O!Ty8&GnL%@<<;Cj9wWQuj0Tplkz`7vXwmG|X;*Af0b(QxD!F`_!Ru#?V9^*LzE<(}$b@F4{=a`Ct_kVw z`ojnotp;K(*N+9^CcaNDuX?=e^EY=tMzH7{AdZj_TTvflBQbxAt-hg-y9*;&bSDt6 z-8t{In2Y;|D-xU6+I_immJuv^5s26ANIdEiyol+_)Yt8rcJb|uU{NI|28JnntO~L( zA6~Pt`E20KmUM4MuxKa{|24n4kjt`H;6=IpIku3lPmEyEDj<%q*fXn8B!(k??*6@N z7yYVb1dHAV;_0m}g6lWDcv1R@;r<@GlL3riQ6pvs23;n*#aCb29ACg68><>1v78Ys z8Uw^rkNh{Wl<5+jsJiK9Sx>`4MzH8yAZBJ@2gem7D}x}TWoku1eo=`+N@`kSX-FKFO z_>2?*X*D!52FfHQ7HODUSOQrnD(v;O6jFE>28O zg}MM}3`_vSIt_$%$@zK3`8lb2sYONkMH=c!>bRZ2p9pe!5{O9FK$jGBadirHS5Q+^ zD9g`GQ7}N07S2pl&;TpZ#IArJNkKKbd$|xcqli0W4V2`Z#NuKFxBPr~KtO^782peJ z0SZDGwdfY97MEw1BxfjSBto<#Cl;qF7+Qh#z?_YyMZ7#C6KJnSNosM4h9(Z*1w)ez zBEIr8szH&aU<=it19GTBEs$Sb3$+^BSnTHoy%40Ou_KdR+wo0GZTz-v9sr diff --git a/packages/strip/test/test.js b/packages/strip/test/test.js index 27fa54b87..c279a14a7 100755 --- a/packages/strip/test/test.js +++ b/packages/strip/test/test.js @@ -25,6 +25,18 @@ const compare = (t, fixture, options) => { t.snapshot(output ? output.code : input); }; +test('can be configured to make no changes', (t) => { + compare(t, 'no-changes', { + labels: [], + functions: [], + debugger: false + }); +}); + +test('excluded files do not get changed', (t) => { + compare(t, 'excluded-not-changed', { exclude: `**/excluded-not-changed/input.js` }); +}); + test('removes debugger statements', (t) => { compare(t, 'debugger'); }); @@ -33,6 +45,10 @@ test('does not remove debugger statements with debugger: false', (t) => { compare(t, 'debugger-false', { debugger: false }); }); +test('empty functions list leaves console statements', (t) => { + compare(t, 'no-changes', { functions: [] }); +}); + test('removes console statements', (t) => { compare(t, 'console'); }); @@ -41,6 +57,10 @@ test('removes assert statements', (t) => { compare(t, 'assert'); }); +test('empty functions list leaves assert statements', (t) => { + compare(t, 'assert', { functions: [] }); +}); + test('leaves console statements if custom functions are provided', (t) => { compare(t, 'console-custom', { functions: ['console.log'] }); }); @@ -57,6 +77,10 @@ test('rewrtestes inline if expessions as void 0', (t) => { compare(t, 'inline-if'); }); +test('rewrites expressions as void 0 in lambdas', (t) => { + compare(t, 'lambda-void', { functions: ['console.warn'] }); +}); + test('removes expressions in if blocks', (t) => { compare(t, 'if-block'); }); @@ -92,3 +116,35 @@ test('removes multiple labeled blocks', (t) => { test('only removes specified blocks', (t) => { compare(t, 'label-block-discriminate', { labels: ['second'] }); }); + +test('removes labeled blocks when filtered function is present', (t) => { + compare(t, 'label-block', { labels: ['unittest'], functions: ['console.*'] }); +}); + +test('removes labeled blocks when functions list is empty', (t) => { + compare(t, 'label-block', { labels: ['unittest'], functions: [] }); +}); + +test('whitespace between label and colon is accepted', (t) => { + compare(t, 'label-whitespace', { labels: ['unittest'], functions: ['console.*'] }); +}); + +test('removing a lable also works for expressions', (t) => { + compare(t, 'label-expression', { labels: ['unittest'] }); +}); + +test('the same label can occur multiple times and all are removed', (t) => { + compare(t, 'label-multiple-times', { labels: ['unittest'] }); +}); + +test('removes labeled even with awkward spacing', (t) => { + compare(t, 'label-awkward-spacing', { labels: ['unittest'], functions: ['console.*'] }); +}); + +test('spaces around . in function calls are accepted', (t) => { + compare(t, 'functions-spaced', { labels: ['unittest'], functions: ['Test.f'] }); +}); + +test('function calls without object are replaced with (void 0)', (t) => { + compare(t, 'functions-direct', { labels: ['unittest'], functions: ['f'] }); +}); From 8dfaa84377d4d8c0f71b82c67b64c0ccb2c654ea Mon Sep 17 00:00:00 2001 From: Duncan Kimpton Date: Thu, 9 Jul 2020 13:58:54 +0200 Subject: [PATCH 5/8] attempt to clean up filemodes --- packages/strip/test/fixtures/assert/input.js | 0 packages/strip/test/fixtures/console-custom/input.js | 0 packages/strip/test/fixtures/console/input.js | 0 packages/strip/test/fixtures/custom/input.js | 0 packages/strip/test/fixtures/debugger-false/input.js | 0 packages/strip/test/fixtures/debugger/input.js | 0 packages/strip/test/fixtures/if-block/input.js | 0 packages/strip/test/fixtures/inline-call-expressions/input.js | 0 packages/strip/test/fixtures/inline-if/input.js | 0 packages/strip/test/fixtures/inline-while/input.js | 0 packages/strip/test/fixtures/label-block-discriminate/input.js | 0 packages/strip/test/fixtures/label-block-multiple/input.js | 0 packages/strip/test/fixtures/label-block/input.js | 0 .../strip/test/fixtures/object-destructuring-default/input.js | 0 packages/strip/test/fixtures/super-method/input.js | 0 packages/strip/test/fixtures/switch-case/input.js | 0 packages/strip/test/fixtures/this-method/input.js | 0 17 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 packages/strip/test/fixtures/assert/input.js mode change 100644 => 100755 packages/strip/test/fixtures/console-custom/input.js mode change 100644 => 100755 packages/strip/test/fixtures/console/input.js mode change 100644 => 100755 packages/strip/test/fixtures/custom/input.js mode change 100644 => 100755 packages/strip/test/fixtures/debugger-false/input.js mode change 100644 => 100755 packages/strip/test/fixtures/debugger/input.js mode change 100644 => 100755 packages/strip/test/fixtures/if-block/input.js mode change 100644 => 100755 packages/strip/test/fixtures/inline-call-expressions/input.js mode change 100644 => 100755 packages/strip/test/fixtures/inline-if/input.js mode change 100644 => 100755 packages/strip/test/fixtures/inline-while/input.js mode change 100644 => 100755 packages/strip/test/fixtures/label-block-discriminate/input.js mode change 100644 => 100755 packages/strip/test/fixtures/label-block-multiple/input.js mode change 100644 => 100755 packages/strip/test/fixtures/label-block/input.js mode change 100644 => 100755 packages/strip/test/fixtures/object-destructuring-default/input.js mode change 100644 => 100755 packages/strip/test/fixtures/super-method/input.js mode change 100644 => 100755 packages/strip/test/fixtures/switch-case/input.js mode change 100644 => 100755 packages/strip/test/fixtures/this-method/input.js diff --git a/packages/strip/test/fixtures/assert/input.js b/packages/strip/test/fixtures/assert/input.js old mode 100644 new mode 100755 diff --git a/packages/strip/test/fixtures/console-custom/input.js b/packages/strip/test/fixtures/console-custom/input.js old mode 100644 new mode 100755 diff --git a/packages/strip/test/fixtures/console/input.js b/packages/strip/test/fixtures/console/input.js old mode 100644 new mode 100755 diff --git a/packages/strip/test/fixtures/custom/input.js b/packages/strip/test/fixtures/custom/input.js old mode 100644 new mode 100755 diff --git a/packages/strip/test/fixtures/debugger-false/input.js b/packages/strip/test/fixtures/debugger-false/input.js old mode 100644 new mode 100755 diff --git a/packages/strip/test/fixtures/debugger/input.js b/packages/strip/test/fixtures/debugger/input.js old mode 100644 new mode 100755 diff --git a/packages/strip/test/fixtures/if-block/input.js b/packages/strip/test/fixtures/if-block/input.js old mode 100644 new mode 100755 diff --git a/packages/strip/test/fixtures/inline-call-expressions/input.js b/packages/strip/test/fixtures/inline-call-expressions/input.js old mode 100644 new mode 100755 diff --git a/packages/strip/test/fixtures/inline-if/input.js b/packages/strip/test/fixtures/inline-if/input.js old mode 100644 new mode 100755 diff --git a/packages/strip/test/fixtures/inline-while/input.js b/packages/strip/test/fixtures/inline-while/input.js old mode 100644 new mode 100755 diff --git a/packages/strip/test/fixtures/label-block-discriminate/input.js b/packages/strip/test/fixtures/label-block-discriminate/input.js old mode 100644 new mode 100755 diff --git a/packages/strip/test/fixtures/label-block-multiple/input.js b/packages/strip/test/fixtures/label-block-multiple/input.js old mode 100644 new mode 100755 diff --git a/packages/strip/test/fixtures/label-block/input.js b/packages/strip/test/fixtures/label-block/input.js old mode 100644 new mode 100755 diff --git a/packages/strip/test/fixtures/object-destructuring-default/input.js b/packages/strip/test/fixtures/object-destructuring-default/input.js old mode 100644 new mode 100755 diff --git a/packages/strip/test/fixtures/super-method/input.js b/packages/strip/test/fixtures/super-method/input.js old mode 100644 new mode 100755 diff --git a/packages/strip/test/fixtures/switch-case/input.js b/packages/strip/test/fixtures/switch-case/input.js old mode 100644 new mode 100755 diff --git a/packages/strip/test/fixtures/this-method/input.js b/packages/strip/test/fixtures/this-method/input.js old mode 100644 new mode 100755 From 30ae82f281ba51d3051a090b2cf6cb58ae9f3f7a Mon Sep 17 00:00:00 2001 From: Andrew Powell Date: Thu, 9 Jul 2020 09:30:02 -0400 Subject: [PATCH 6/8] revert subjective style change --- packages/strip/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/strip/README.md b/packages/strip/README.md index 44bac09dd..43e8dd4e0 100755 --- a/packages/strip/README.md +++ b/packages/strip/README.md @@ -98,7 +98,7 @@ Type: `Boolean`
Default: `true`
Example: `sourceMap: false,`
-If `true` instructs the plugin to update source maps accordingly after removing configured targets from the bundle. +If `true`, instructs the plugin to update source maps accordingly after removing configured targets from the bundle. ## Meta From eec0465450b67ba552e6100b6b275aee40012554 Mon Sep 17 00:00:00 2001 From: Andrew Powell Date: Thu, 9 Jul 2020 09:31:01 -0400 Subject: [PATCH 7/8] revert extra newline --- packages/strip/src/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/strip/src/index.js b/packages/strip/src/index.js index 6b9697192..8baeb25e5 100755 --- a/packages/strip/src/index.js +++ b/packages/strip/src/index.js @@ -38,7 +38,6 @@ export default function strip(options = {}) { const sourceMap = options.sourceMap !== false; const removeDebuggerStatements = options.debugger !== false; - const functions = (options.functions || ['console.*', 'assert.*']).map((keypath) => keypath.replace(/\*/g, '\\w+').replace(/\./g, '\\s*\\.\\s*') ); From 4524da9ad7695f2a0691b7036bf65f84e578689a Mon Sep 17 00:00:00 2001 From: Andrew Powell Date: Thu, 9 Jul 2020 09:32:02 -0400 Subject: [PATCH 8/8] revert extra newlines --- packages/strip/src/index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/strip/src/index.js b/packages/strip/src/index.js index 8baeb25e5..0759a7491 100755 --- a/packages/strip/src/index.js +++ b/packages/strip/src/index.js @@ -53,9 +53,7 @@ export default function strip(options = {}) { const reFunctions = new RegExp(`^(?:${functions.join('|')})$`); const reFirstpass = new RegExp(`\\b(?:${firstPass.join('|')})`); - const firstPassFilter = firstPass.length > 0 ? (code) => reFirstpass.test(code) : () => false; - const UNCHANGED = null; return {