Skip to content

Commit

Permalink
Do not rewrite new.target (#4679)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Oct 18, 2022
1 parent 088c877 commit 63ed4fa
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 3 deletions.
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();

0 comments on commit 63ed4fa

Please sign in to comment.