Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(strip): support specifying both functions and labels #471

Merged

Conversation

dgkimpton
Copy link
Contributor

@dgkimpton dgkimpton commented Jun 23, 2020

Rollup Plugin Name: strip

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes
  • no

Breaking Changes?

  • yes
  • no

List any relevant issue numbers:
#167

Description

When trying to use the strip plugin, I was for ages unable to get it to remove my labelled code. After some investigation, it became clear that the 'first pass filter' was rejecting labels when functions were specified.

This pull request fixes the exclusive nature of labels and functions. It is now possible to specify both, either, or neither.

I later found this 'closed' issue that reports the same problem, #167, it appears to have been started and then abandoned - hope I'm not treading on toes by fixing it.

Along the way, I had some trouble with the unit tests and got annoyed that I couldn't see the input and expected output together, so I refactored them and then added some more.

During testing, I further discovered that labels and functions were being ignored when spaces/newlines were included around the punctuation, so I fixed that too. Probably that should have been a different PR, but since my use-case had spaces in it, I just kept going until it worked. Sorry about that!

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.
@shellscape shellscape changed the title FIX: Now supports specifying both functions and labels at same time fix(strip): support specifying both functions and labels Jun 23, 2020
Copy link
Contributor

@Kocal Kocal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I was only able to understand the tests, and they look good to me.

Thanks for your work!

Copy link
Contributor

@pnevares pnevares left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like some big changes here, but the work is appreciated! The only thing I'd challenge is that there are breaking changes, if labels were passing by the filter when functions were specified. It's probably up to personal opinion, but to me that difference means that a user may be inadvertently relying on that implementation difference and this fix would break their use case.

Otherwise, LGTM.

@dgkimpton
Copy link
Contributor Author

I see your point. That would be a bit of a hairy build process, but I suppose it is conceivable. The only way I can think to resolve that is to add a new option with a different name.
E.g. labels:[] has the current functionality and labeled:[] works in the none exclusive mode.
That can definitely be done but explodes the complexity quite a bit... is that your preferred option, or can you think of an alternative?

@pnevares
Copy link
Contributor

pnevares commented Jul 5, 2020

I would probably prefer marking it as a breaking change and a major version bump for the package, instead of adding the technical debt of the newly-named option. Somebody would have an item backlogged to clean that up for the next version anyway.

Copy link
Collaborator

@shellscape shellscape left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to the changes requested in the source, you'll need to revert your deletions of the fixtures and modifications of the tests. We've established a pattern for testing in this plugin and contributors should follow that. Completely refactoring the tests in this plugin (or any project) is going too far without previous discussing that action in an issue.

As it is, we cannot merge this PR.

labels: ['unittest']
})
]
plugins: [strip({})]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert this change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

};
```

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this section. the defaults are spelled out in the options below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this because I wished it had been here when I started trying to use this plugin, but ok.

> remove functions matching '**console.\***' and '**assert.\***'
> remove any **debugger** statements

### Custom Options Example
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this section. you've added examples in the options below and that's sufficient

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


If `true`, instructs the plugin to remove debugger statements.
If `true` or unspecified, instructs the plugin to remove debugger statements.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or unspecified is not not necessary and doesn't add any value here, please revert.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

packages/strip/src/index.js Show resolved Hide resolved
);
const labelsPatterns = labels.map((l) => `${l}\\s*:`);

const firstPassPatterns = removeDebuggerStatements
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this ternary and the following array assignment within is unnecessary.

const firstPass = [...functionsPatterns, ...labelsPatterns];
if (removeDebuggerStatements) {
  firstPass.push('debugger\\b');
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops, that would be my C++ background where const is deep. I'd forgotten in JS const is shallow and still allows the object to be mutated. Done.

? [...functionsPatterns, ...labelsPatterns, 'debugger\\b']
: [...functionsPatterns, ...labelsPatterns];

const functionsRE = new RegExp(`^(?:${functionsPatterns.join('|')})$`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while we're in here, please use the naming pattern reFunctions and reFirstpass

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@dgkimpton
Copy link
Contributor Author

OK, I figured that would probably be the case. I modified the tests to make it easier for me to verify the fix and then decided just to offer them up as-is in case you preferred them (no point doing extra work if not needed). Changed.

@shellscape
Copy link
Collaborator

Thanks. Please clean up those file mode changes so they're not part of the PR and we should be good to merge.

packages/strip/README.md Outdated Show resolved Hide resolved
@shellscape shellscape merged commit a014c66 into rollup:master Jul 9, 2020
@shellscape
Copy link
Collaborator

thanks!

LarsDenBakker pushed a commit to LarsDenBakker/plugins that referenced this pull request Sep 12, 2020
* 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.

* restoring test fixtures

* code review changes

* additional tests

* attempt to clean up filemodes

* revert subjective style change

* revert extra newline

* revert extra newlines

Co-authored-by: Andrew Powell <shellscape@users.noreply.github.com>
@dasa
Copy link
Contributor

dasa commented May 6, 2021

It looks like there was a publishing problem and this PR wasn't included in the 2.0.0 release: https://unpkg.com/@rollup/plugin-strip@2.0.0/dist/index.es.js

For example, the published file is still using if (!edited) return null;.

@dasa
Copy link
Contributor

dasa commented May 7, 2021

Fixed in 2.0.1: https://unpkg.com/@rollup/plugin-strip@2.0.1/dist/index.es.js

👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants