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

Do not count accessing members of an external namespace as side-effects. #4001

Merged
merged 1 commit into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ast/variables/ExternalVariable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ExternalModule from '../../ExternalModule';
import Identifier from '../nodes/Identifier';
import { ObjectPath } from '../utils/PathTracker';
import Variable from './Variable';

export default class ExternalVariable extends Variable {
Expand All @@ -21,6 +22,10 @@ export default class ExternalVariable extends Variable {
}
}

hasEffectsWhenAccessedAtPath(path: ObjectPath) {
return path.length > (this.isNamespace ? 1 : 0);
}

include() {
if (!this.included) {
this.included = true;
Expand Down
4 changes: 4 additions & 0 deletions test/form/samples/treeshake-namespace-access/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
description: 'does not count namespace property access as side-effect',
options: { external: 'external' }
};
5 changes: 5 additions & 0 deletions test/form/samples/treeshake-namespace-access/_expected/amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
define(['external'], function (external) { 'use strict';

console.log('main');

});
5 changes: 5 additions & 0 deletions test/form/samples/treeshake-namespace-access/_expected/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

require('external');

console.log('main');
3 changes: 3 additions & 0 deletions test/form/samples/treeshake-namespace-access/_expected/es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'external';

console.log('main');
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(function () {
'use strict';

console.log('main');

}());
11 changes: 11 additions & 0 deletions test/form/samples/treeshake-namespace-access/_expected/system.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
System.register(['external'], function () {
'use strict';
return {
setters: [function () {}],
execute: function () {

console.log('main');

}
};
});
8 changes: 8 additions & 0 deletions test/form/samples/treeshake-namespace-access/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(['external'], factory) :
factory();
}((function () { 'use strict';

console.log('main');

})));
4 changes: 4 additions & 0 deletions test/form/samples/treeshake-namespace-access/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as external from 'external';

const unused = external.foo;
console.log('main');