Skip to content

Commit

Permalink
Support default exported classes that extend other classes (#4950)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Apr 21, 2023
1 parent bbce2b7 commit 2820962
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
37 changes: 12 additions & 25 deletions src/ast/nodes/ExportDefaultDeclaration.ts
Expand Up @@ -21,18 +21,10 @@ function getDeclarationStart(code: string, start: number): number {
return findNonWhiteSpace(code, findFirstOccurrenceOutsideComment(code, 'default', start) + 7);
}

function getIdInsertPosition(
code: string,
declarationKeyword: string,
endMarker: string,
start: number
): number {
function getFunctionIdInsertPosition(code: string, start: number): number {
const declarationEnd =
findFirstOccurrenceOutsideComment(code, declarationKeyword, start) + declarationKeyword.length;
code = code.slice(
declarationEnd,
findFirstOccurrenceOutsideComment(code, endMarker, declarationEnd)
);
findFirstOccurrenceOutsideComment(code, 'function', start) + 'function'.length;
code = code.slice(declarationEnd, findFirstOccurrenceOutsideComment(code, '(', declarationEnd));
const generatorStarPos = findFirstOccurrenceOutsideComment(code, '*');
if (generatorStarPos === -1) {
return declarationEnd;
Expand Down Expand Up @@ -76,18 +68,18 @@ export default class ExportDefaultDeclaration extends NodeBase {
this.renderNamedDeclaration(
code,
declarationStart,
'function',
'(',
this.declaration.id === null,
this.declaration.id === null
? getFunctionIdInsertPosition(code.original, declarationStart)
: null,
options
);
} else if (this.declaration instanceof ClassDeclaration) {
this.renderNamedDeclaration(
code,
declarationStart,
'class',
'{',
this.declaration.id === null,
this.declaration.id === null
? findFirstOccurrenceOutsideComment(code.original, 'class', start) + 'class'.length
: null,
options
);
} else if (this.variable.getOriginalVariable() !== this.variable) {
Expand All @@ -114,9 +106,7 @@ export default class ExportDefaultDeclaration extends NodeBase {
private renderNamedDeclaration(
code: MagicString,
declarationStart: number,
declarationKeyword: string,
endMarker: string,
needsId: boolean,
idInsertPosition: number | null,
options: RenderOptions
): void {
const {
Expand All @@ -128,11 +118,8 @@ export default class ExportDefaultDeclaration extends NodeBase {
// Remove `export default`
code.remove(this.start, declarationStart);

if (needsId) {
code.appendLeft(
getIdInsertPosition(code.original, declarationKeyword, endMarker, declarationStart),
` ${name}`
);
if (idInsertPosition !== null) {
code.appendLeft(idInsertPosition, ` ${name}`);
}
if (
format === 'system' &&
Expand Down
@@ -0,0 +1,3 @@
module.exports = {
description: 'handles default exported classes extending a regular expression argument (#4783)'
};
@@ -0,0 +1,7 @@
function foo(val) {
return val;
}

class main extends foo(/1/) {}

export { main as default };
@@ -0,0 +1,5 @@
function foo(val) {
return val;
}

export default class extends foo(/1/) {}

0 comments on commit 2820962

Please sign in to comment.