Skip to content

Commit

Permalink
Improve coverage and remove trailing annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 26, 2021
1 parent cf001fb commit 12d5b03
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 12 deletions.
2 changes: 0 additions & 2 deletions src/ast/nodes/IfStatement.ts
@@ -1,6 +1,5 @@
import MagicString from 'magic-string';
import { RenderOptions } from '../../utils/renderHelpers';
import { removeAnnotations } from '../../utils/treeshakeNode';
import { DeoptimizableEntity } from '../DeoptimizableEntity';
import { BROKEN_FLOW_NONE, HasEffectsContext, InclusionContext } from '../ExecutionContext';
import TrackingScope from '../scopes/TrackingScope';
Expand Down Expand Up @@ -89,7 +88,6 @@ export default class IfStatement extends StatementBase implements DeoptimizableE
if (includesIfElse) {
this.test.render(code, options);
} else {
removeAnnotations(this, code);
code.remove(this.start, this.consequent.start);
}
if (this.consequent.included && (noTreeshake || testValue === UnknownValue || testValue)) {
Expand Down
15 changes: 10 additions & 5 deletions src/utils/pureComments.ts
Expand Up @@ -41,13 +41,18 @@ function handlePureAnnotationsOfNode(
state: CommentState,
type: string = node.type
) {
let commentNode = state.annotations[state.annotationIndex];
while (commentNode && node.start >= commentNode.end) {
markPureNode(node, commentNode, state.code);
commentNode = state.annotations[++state.annotationIndex];
const { annotations } = state;
let comment = annotations[state.annotationIndex];
while (comment && node.start >= comment.end) {
markPureNode(node, comment, state.code);
comment = annotations[++state.annotationIndex];
}
if (commentNode && commentNode.end <= node.end) {
if (comment && comment.end <= node.end) {
(basicWalker as BaseWalker<CommentState>)[type](node, state, handlePureAnnotationsOfNode);
while ((comment = annotations[state.annotationIndex]) && comment.end <= node.end) {
++state.annotationIndex;
annotateNode(node, comment, false);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/form/samples/nested-pure-comments/_expected.js
Expand Up @@ -12,7 +12,7 @@ keep();
keep();

// Binary expression
(keep() + 1);
1 + keep();
(keep() / 1);
1 / keep();

export { foo };
6 changes: 3 additions & 3 deletions test/form/samples/nested-pure-comments/main.js
Expand Up @@ -15,9 +15,9 @@ false ? 1 /*@__PURE__*/ : keep();
false /*@__PURE__*/ || keep();

// Binary expression
/* @__PURE__ */(keep() + 1);
/* @__PURE__ */remove() + /* @__PURE__ */remove();
1 /* @__PURE__ */ + keep();
/* @__PURE__ */(keep() / 1);
/* @__PURE__ */remove() / /* @__PURE__ */remove();
1 /* @__PURE__ */ / keep();

// Calls with parentheses
/*@__PURE__*/(remove());
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/remove-tree-shaken-pure-comments/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'removes pure comments of tree-shaken nodes'
};
@@ -0,0 +1,3 @@
kept() ;

kept() ;
3 changes: 3 additions & 0 deletions test/form/samples/remove-tree-shaken-pure-comments/main.js
@@ -0,0 +1,3 @@
/*@__PURE__*/(() => false)() /*@__PURE__*/ ? removed() /*@__PURE__*/ : kept() /*@__PURE__*/;

/*@__PURE__*/(() => true)() /*@__PURE__*/ && kept() /*@__PURE__*/;
1 change: 1 addition & 0 deletions test/function/samples/uses-supplied-ast/_config.js
Expand Up @@ -18,6 +18,7 @@ const modules = {

baz: 'export default 42;'
};
modules.foo.ast._ignoredProp = {};

module.exports = {
description: 'uses supplied AST',
Expand Down

0 comments on commit 12d5b03

Please sign in to comment.