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

fix: use the original class name in the class body #4827

Merged
merged 7 commits into from Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
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
9 changes: 9 additions & 0 deletions src/ast/nodes/StaticBlock.ts
Expand Up @@ -3,7 +3,10 @@ import { type RenderOptions, renderStatementList } from '../../utils/renderHelpe
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import BlockScope from '../scopes/BlockScope';
import type Scope from '../scopes/Scope';
import type Variable from '../variables/Variable';
import type ClassBody from './ClassBody';
import type * as NodeType from './NodeType';
import type ClassNode from './shared/ClassNode';
import { type IncludeChildren, StatementBase, type StatementNode } from './shared/Node';

export default class StaticBlock extends StatementBase {
Expand All @@ -30,6 +33,12 @@ export default class StaticBlock extends StatementBase {
}

render(code: MagicString, options: RenderOptions): void {
const classVariable = ((this.parent as ClassBody).parent as ClassNode).id!.variable;
const useOriginalName = (variable: Variable) => variable === classVariable;
options = {
...options,
useOriginalName
};
if (this.body.length > 0) {
renderStatementList(this.body, code, this.start + 1, this.end - 1, options);
} else {
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;
TrickyPi marked this conversation as resolved.
Show resolved Hide resolved
}

export interface NodeRenderOptions {
Expand Down
4 changes: 4 additions & 0 deletions test/form/samples/use-class-name-in-static-block/_config.js
@@ -0,0 +1,4 @@
module.exports = {
description: 'use class name instead of renderName in static block',
solo: true
TrickyPi marked this conversation as resolved.
Show resolved Hide resolved
};
20 changes: 20 additions & 0 deletions test/form/samples/use-class-name-in-static-block/_expected/amd.js
@@ -0,0 +1,20 @@
define((function () { 'use strict';

let Test$1 = class Test {
static test = 'Test1';
static {
assert.ok(Test.test);
}
};

class Test {
static test = 'Test2';
static {
assert.ok(Test.test);
}
}

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

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

let Test$1 = class Test {
static test = 'Test1';
static {
assert.ok(Test.test);
}
};

class Test {
static test = 'Test2';
static {
assert.ok(Test.test);
}
}

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

class Test {
static test = 'Test2';
static {
assert.ok(Test.test);
}
}

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

let Test$1 = class Test {
static test = 'Test1';
static {
assert.ok(Test.test);
}
};

class Test {
static test = 'Test2';
static {
assert.ok(Test.test);
}
}

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

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

let Test$1 = class Test {
static test = 'Test1';
static {
assert.ok(Test.test);
}
};

class Test {
static test = 'Test2';
static {
assert.ok(Test.test);
}
}

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

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

let Test$1 = class Test {
static test = 'Test1';
static {
assert.ok(Test.test);
}
};

class Test {
static test = 'Test2';
static {
assert.ok(Test.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);
6 changes: 6 additions & 0 deletions test/form/samples/use-class-name-in-static-block/test1.js
@@ -0,0 +1,6 @@
export class Test {
static test = 'Test1';
static {
assert.ok(Test.test);
}
}
6 changes: 6 additions & 0 deletions test/form/samples/use-class-name-in-static-block/test2.js
@@ -0,0 +1,6 @@
export class Test {
static test = 'Test2';
static {
assert.ok(Test.test);
}
}
@@ -0,0 +1,3 @@
module.exports = {
description: 'use class name instead of renderName in static block'
};
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);
6 changes: 6 additions & 0 deletions test/function/samples/use-class-name-in-static-block/test1.js
@@ -0,0 +1,6 @@
export class Test {
static test = 'Test1';
static {
assert.ok(Test.test);
}
}
6 changes: 6 additions & 0 deletions test/function/samples/use-class-name-in-static-block/test2.js
@@ -0,0 +1,6 @@
export class Test {
static test = 'Test2';
static {
assert.ok(Test.test);
}
}