Skip to content

Commit

Permalink
refactor: override useOriginalName in ClassDeclaration
Browse files Browse the repository at this point in the history
  • Loading branch information
TrickyPi committed Jan 30, 2023
1 parent f5682eb commit 9f5dc2b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,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
Original file line number Diff line number Diff line change
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
9 changes: 0 additions & 9 deletions src/ast/nodes/StaticBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ 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 @@ -33,12 +30,6 @@ 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
2 changes: 1 addition & 1 deletion src/utils/renderHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface RenderOptions {
namespaceToStringTag: boolean;
pluginDriver: PluginDriver;
snippets: GenerateCodeSnippets;
useOriginalName?: (variable: Variable) => boolean;
useOriginalName: ((variable: Variable) => boolean) | null;
}

export interface NodeRenderOptions {
Expand Down

0 comments on commit 9f5dc2b

Please sign in to comment.