Skip to content

Commit

Permalink
Merge branch 'master' into cross-chunk-reexports
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Feb 1, 2023
2 parents ea7c185 + 4faf350 commit c1fdf07
Show file tree
Hide file tree
Showing 19 changed files with 308 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/Chunk.ts
Expand Up @@ -1168,7 +1168,8 @@ export default class Chunk {
indent,
namespaceToStringTag,
pluginDriver,
snippets
snippets,
useOriginalName: null
};

let usesTopLevelAwait = false;
Expand Down
6 changes: 5 additions & 1 deletion src/ast/nodes/ClassDeclaration.ts
Expand Up @@ -2,6 +2,7 @@ import type MagicString from 'magic-string';
import type { RenderOptions } from '../../utils/renderHelpers';
import { getSystemExportStatement } from '../../utils/systemJsRendering';
import type ChildScope from '../scopes/ChildScope';
import type Variable from '../variables/Variable';
import Identifier, { type IdentifierWithVariable } from './Identifier';
import type * as NodeType from './NodeType';
import ClassNode from './shared/ClassNode';
Expand Down Expand Up @@ -43,7 +44,10 @@ export default class ClassDeclaration extends ClassNode {
const renderedVariable = variable.getName(getPropertyAccess);
if (renderedVariable !== name) {
this.superClass?.render(code, options);
this.body.render(code, options);
this.body.render(code, {
...options,
useOriginalName: (_variable: Variable) => _variable === variable
});
code.prependRight(this.start, `let ${renderedVariable}${_}=${_}`);
code.prependLeft(this.end, ';');
return;
Expand Down
4 changes: 2 additions & 2 deletions src/ast/nodes/Identifier.ts
Expand Up @@ -244,11 +244,11 @@ export default class Identifier extends NodeBase implements PatternNode {

render(
code: MagicString,
{ snippets: { getPropertyAccess } }: RenderOptions,
{ snippets: { getPropertyAccess }, useOriginalName }: RenderOptions,
{ renderedParentType, isCalleeOfRenderedParent, isShorthandProperty }: NodeRenderOptions = BLANK
): void {
if (this.variable) {
const name = this.variable.getName(getPropertyAccess);
const name = this.variable.getName(getPropertyAccess, useOriginalName);

if (name !== this.name) {
code.overwrite(this.start, this.end, name, {
Expand Down
8 changes: 6 additions & 2 deletions src/ast/variables/Variable.ts
@@ -1,5 +1,6 @@
import type ExternalModule from '../../ExternalModule';
import type Module from '../../Module';
import type { RenderOptions } from '../../utils/renderHelpers';
import type { HasEffectsContext } from '../ExecutionContext';
import type { NodeInteraction } from '../NodeInteractions';
import { INTERACTION_ACCESSED } from '../NodeInteractions';
Expand Down Expand Up @@ -42,8 +43,11 @@ export default class Variable extends ExpressionEntity {
return this.renderBaseName || this.renderName || this.name;
}

getName(getPropertyAccess: (name: string) => string): string {
const name = this.renderName || this.name;
getName(
getPropertyAccess: (name: string) => string,
useOriginalName?: RenderOptions['useOriginalName']
): string {
const name = useOriginalName?.(this) ? this.name : this.renderName || this.name;
return this.renderBaseName ? `${this.renderBaseName}${getPropertyAccess(name)}` : name;
}

Expand Down
1 change: 1 addition & 0 deletions src/utils/renderHelpers.ts
Expand Up @@ -15,6 +15,7 @@ export interface RenderOptions {
namespaceToStringTag: boolean;
pluginDriver: PluginDriver;
snippets: GenerateCodeSnippets;
useOriginalName: ((variable: Variable) => boolean) | null;
}

export interface NodeRenderOptions {
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/use-class-name-in-static-block/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'use the original class name instead of renderName in class body'
};
36 changes: 36 additions & 0 deletions test/form/samples/use-class-name-in-static-block/_expected/amd.js
@@ -0,0 +1,36 @@
define((function () { 'use strict';

let Test$1 = class Test {
constructor() {
assert.ok(Test.test);
assert.ok(this.getText());
}
getText() {
return Test.test;
}
static test = 'Test1';
static {
assert.ok(Test.test);
new Test();
}
};

class Test {
constructor() {
assert.ok(Test.test);
assert.ok(this.getText());
}
getText() {
return Test.test;
}
static test = 'Test2';
static {
assert.ok(Test.test);
new Test();
}
}

assert.ok(Test$1.test);
assert.ok(Test.test);

}));
34 changes: 34 additions & 0 deletions test/form/samples/use-class-name-in-static-block/_expected/cjs.js
@@ -0,0 +1,34 @@
'use strict';

let Test$1 = class Test {
constructor() {
assert.ok(Test.test);
assert.ok(this.getText());
}
getText() {
return Test.test;
}
static test = 'Test1';
static {
assert.ok(Test.test);
new Test();
}
};

class Test {
constructor() {
assert.ok(Test.test);
assert.ok(this.getText());
}
getText() {
return Test.test;
}
static test = 'Test2';
static {
assert.ok(Test.test);
new Test();
}
}

assert.ok(Test$1.test);
assert.ok(Test.test);
32 changes: 32 additions & 0 deletions test/form/samples/use-class-name-in-static-block/_expected/es.js
@@ -0,0 +1,32 @@
let Test$1 = class Test {
constructor() {
assert.ok(Test.test);
assert.ok(this.getText());
}
getText() {
return Test.test;
}
static test = 'Test1';
static {
assert.ok(Test.test);
new Test();
}
};

class Test {
constructor() {
assert.ok(Test.test);
assert.ok(this.getText());
}
getText() {
return Test.test;
}
static test = 'Test2';
static {
assert.ok(Test.test);
new Test();
}
}

assert.ok(Test$1.test);
assert.ok(Test.test);
37 changes: 37 additions & 0 deletions test/form/samples/use-class-name-in-static-block/_expected/iife.js
@@ -0,0 +1,37 @@
(function () {
'use strict';

let Test$1 = class Test {
constructor() {
assert.ok(Test.test);
assert.ok(this.getText());
}
getText() {
return Test.test;
}
static test = 'Test1';
static {
assert.ok(Test.test);
new Test();
}
};

class Test {
constructor() {
assert.ok(Test.test);
assert.ok(this.getText());
}
getText() {
return Test.test;
}
static test = 'Test2';
static {
assert.ok(Test.test);
new Test();
}
}

assert.ok(Test$1.test);
assert.ok(Test.test);

})();
@@ -0,0 +1,41 @@
System.register([], (function () {
'use strict';
return {
execute: (function () {

let Test$1 = class Test {
constructor() {
assert.ok(Test.test);
assert.ok(this.getText());
}
getText() {
return Test.test;
}
static test = 'Test1';
static {
assert.ok(Test.test);
new Test();
}
};

class Test {
constructor() {
assert.ok(Test.test);
assert.ok(this.getText());
}
getText() {
return Test.test;
}
static test = 'Test2';
static {
assert.ok(Test.test);
new Test();
}
}

assert.ok(Test$1.test);
assert.ok(Test.test);

})
};
}));
39 changes: 39 additions & 0 deletions test/form/samples/use-class-name-in-static-block/_expected/umd.js
@@ -0,0 +1,39 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
})((function () { 'use strict';

let Test$1 = class Test {
constructor() {
assert.ok(Test.test);
assert.ok(this.getText());
}
getText() {
return Test.test;
}
static test = 'Test1';
static {
assert.ok(Test.test);
new Test();
}
};

class Test {
constructor() {
assert.ok(Test.test);
assert.ok(this.getText());
}
getText() {
return Test.test;
}
static test = 'Test2';
static {
assert.ok(Test.test);
new Test();
}
}

assert.ok(Test$1.test);
assert.ok(Test.test);

}));
5 changes: 5 additions & 0 deletions test/form/samples/use-class-name-in-static-block/main.js
@@ -0,0 +1,5 @@
import { Test } from './test1.js';
import { Test as Test2 } from './test2.js';

assert.ok(Test.test);
assert.ok(Test2.test);
14 changes: 14 additions & 0 deletions test/form/samples/use-class-name-in-static-block/test1.js
@@ -0,0 +1,14 @@
export class Test {
constructor() {
assert.ok(Test.test);
assert.ok(this.getText());
}
getText() {
return Test.test;
}
static test = 'Test1';
static {
assert.ok(Test.test);
new Test();
}
}
14 changes: 14 additions & 0 deletions test/form/samples/use-class-name-in-static-block/test2.js
@@ -0,0 +1,14 @@
export class Test {
constructor() {
assert.ok(Test.test);
assert.ok(this.getText());
}
getText() {
return Test.test;
}
static test = 'Test2';
static {
assert.ok(Test.test);
new Test();
}
}
@@ -0,0 +1,4 @@
module.exports = {
description: 'use the original class name instead of renderName in class body',
minNodeVersion: 16
};
5 changes: 5 additions & 0 deletions test/function/samples/use-class-name-in-static-block/main.js
@@ -0,0 +1,5 @@
import { Test } from './test1.js';
import { Test as Test2 } from './test2.js';

assert.ok(Test.test);
assert.ok(Test2.test);
14 changes: 14 additions & 0 deletions test/function/samples/use-class-name-in-static-block/test1.js
@@ -0,0 +1,14 @@
export class Test {
constructor() {
assert.ok(Test.test);
assert.ok(this.getText());
}
getText() {
return Test.test;
}
static test = 'Test1';
static {
assert.ok(Test.test);
new Test();
}
}
14 changes: 14 additions & 0 deletions test/function/samples/use-class-name-in-static-block/test2.js
@@ -0,0 +1,14 @@
export class Test {
constructor() {
assert.ok(Test.test);
assert.ok(this.getText());
}
getText() {
return Test.test;
}
static test = 'Test2';
static {
assert.ok(Test.test);
new Test();
}
}

0 comments on commit c1fdf07

Please sign in to comment.