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 rewrite new.target #4679

Merged
merged 2 commits into from Oct 18, 2022
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
12 changes: 9 additions & 3 deletions src/ast/nodes/MetaProperty.ts
Expand Up @@ -14,6 +14,7 @@ import type * as NodeType from './NodeType';
import { NodeBase } from './shared/Node';

const FILE_PREFIX = 'ROLLUP_FILE_URL_';
const IMPORT = 'import';

export default class MetaProperty extends NodeBase {
declare meta: Identifier;
Expand All @@ -25,8 +26,11 @@ export default class MetaProperty extends NodeBase {
private referenceId: string | null = null;

getReferencedFileName(outputPluginDriver: PluginDriver): string | null {
const { metaProperty } = this;
if (metaProperty?.startsWith(FILE_PREFIX)) {
const {
meta: { name },
metaProperty
} = this;
if (name === IMPORT && metaProperty?.startsWith(FILE_PREFIX)) {
return outputPluginDriver.getFileName(metaProperty.slice(FILE_PREFIX.length));
}
return null;
Expand All @@ -43,7 +47,7 @@ export default class MetaProperty extends NodeBase {
include(): void {
if (!this.included) {
this.included = true;
if (this.meta.name === 'import') {
if (this.meta.name === IMPORT) {
this.context.addImportMeta(this);
const parent = this.parent;
const metaProperty = (this.metaProperty =
Expand All @@ -62,13 +66,15 @@ export default class MetaProperty extends NodeBase {
context: {
module: { id: moduleId }
},
meta: { name },
metaProperty,
parent,
preliminaryChunkId,
referenceId,
start,
end
} = this;
if (name !== IMPORT) return;
const chunkId = preliminaryChunkId!;

if (referenceId) {
Expand Down
11 changes: 11 additions & 0 deletions test/form/samples/new-target-meta-property/_expected/amd.js
@@ -0,0 +1,11 @@
define((function () { 'use strict';

class Foo {
constructor() {
console.log(new.target.name);
}
}

new Foo();

}));
9 changes: 9 additions & 0 deletions test/form/samples/new-target-meta-property/_expected/cjs.js
@@ -0,0 +1,9 @@
'use strict';

class Foo {
constructor() {
console.log(new.target.name);
}
}

new Foo();
12 changes: 12 additions & 0 deletions test/form/samples/new-target-meta-property/_expected/iife.js
@@ -0,0 +1,12 @@
(function () {
'use strict';

class Foo {
constructor() {
console.log(new.target.name);
}
}

new Foo();

})();
16 changes: 16 additions & 0 deletions test/form/samples/new-target-meta-property/_expected/system.js
@@ -0,0 +1,16 @@
System.register([], (function () {
'use strict';
return {
execute: (function () {

class Foo {
constructor() {
console.log(new.target.name);
}
}

new Foo();

})
};
}));
14 changes: 14 additions & 0 deletions test/form/samples/new-target-meta-property/_expected/umd.js
@@ -0,0 +1,14 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
})((function () { 'use strict';

class Foo {
constructor() {
console.log(new.target.name);
}
}

new Foo();

}));
3 changes: 3 additions & 0 deletions test/function/samples/new-target-meta/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'does not change new.target usages'
};
5 changes: 5 additions & 0 deletions test/function/samples/new-target-meta/main.js
@@ -0,0 +1,5 @@
function Foo() {
assert.strictEqual(new.target, Foo);
}

new Foo();