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

feat(un-es6-class): support extending super class #58

Merged
merged 3 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions packages/unminify/src/transformations/__tests__/un-es6-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,116 @@ class C {
}
`)

inlineTest('extend super class',
`
import babelInherits from "@babel/runtime/helpers/inherits";

var BabelSuperClass = /*#__PURE__*/_createClass(function BabelSuperClass() {
_classCallCheck(this, BabelSuperClass);
});
var BabelSubClass = /*#__PURE__*/function (_BabelSuperClass) {
_inherits(BabelSubClass, _BabelSuperClass);
var _super = _createSuper(BabelSubClass);
function BabelSubClass() {
var _this;
_classCallCheck(this, BabelSubClass);
return _possibleConstructorReturn(_this);
}
return _createClass(BabelSubClass);
}(BabelSuperClass);

var BabelSuperClass2 = /*#__PURE__*/_createClass(function BabelSuperClass2() {
_classCallCheck(this, BabelSuperClass2);
});
var BabelSubClass2 = /*#__PURE__*/function (_BabelSuperClass2) {
babelInherits(BabelSubClass2, _BabelSuperClass2);
var _super = _createSuper(BabelSubClass2);
function BabelSubClass2() {
var _this;
_classCallCheck(this, BabelSubClass2);
return _possibleConstructorReturn(_this);
}
return _createClass(BabelSubClass2);
}(BabelSuperClass2);

var SwcSuperClass = function SwcSuperClass() {
"use strict";
_class_call_check(this, SwcSuperClass);
};
var SwcSubClass = /*#__PURE__*/ function(SwcSuperClass) {
"use strict";
_inherits(SwcSubClass, SwcSuperClass);
var _super = _create_super(SwcSubClass);
function SwcSubClass() {
_class_call_check(this, SwcSubClass);
var _this;
return _possible_constructor_return(_this);
}
return SwcSubClass;
}(SwcSuperClass);

var TsSuperClass = /** @class */ (function () {
function TsSuperClass() {
}
return TsSuperClass;
}());
var TsSubClass = /** @class */ (function (_super) {
__extends(TsSubClass, _super);
function TsSubClass() {
var _this = this;
return _this;
}
return TsSubClass;
}(TsSuperClass));
`,
`
var BabelSuperClass = /*#__PURE__*/_createClass(function BabelSuperClass() {
_classCallCheck(this, BabelSuperClass);
});

class BabelSubClass extends BabelSuperClass {
constructor() {
var _this;
_classCallCheck(this, BabelSubClass);
return _possibleConstructorReturn(_this);
}
}

var BabelSuperClass2 = /*#__PURE__*/_createClass(function BabelSuperClass2() {
_classCallCheck(this, BabelSuperClass2);
});

class BabelSubClass2 extends BabelSuperClass2 {
constructor() {
var _this;
_classCallCheck(this, BabelSubClass2);
return _possibleConstructorReturn(_this);
}
}

var SwcSuperClass = function SwcSuperClass() {
"use strict";
_class_call_check(this, SwcSuperClass);
};

class SwcSubClass extends SwcSuperClass {
constructor() {
_class_call_check(this, SwcSubClass);
var _this;
return _possible_constructor_return(_this);
}
}

class TsSuperClass {}

class TsSubClass extends TsSuperClass {
constructor() {
var _this = this;
return _this;
}
}
`)

inlineTest.todo('ultimate class declaration',
`
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
Expand Down
99 changes: 86 additions & 13 deletions packages/unminify/src/transformations/un-es6-class.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { findReferences } from '@wakaru/ast-utils'
import { findHelperLocals, removeHelperImport } from '../utils/import'
import wrap from '../wrapAstTransformation'
import type { ASTTransformation } from '../wrapAstTransformation'
import type { ExpressionKind } from 'ast-types/lib/gen/kinds'
import type { Scope } from 'ast-types/lib/scope'
import type { AssignmentExpression, CallExpression, ExpressionStatement, FunctionExpression, Identifier, MemberExpression, VariableDeclarator } from 'jscodeshift'

const inheritsModuleName = '@babel/runtime/helpers/inherits'
const inheritsModuleEsmName = '@babel/runtime/helpers/esm/inherits'
pionxzh marked this conversation as resolved.
Show resolved Hide resolved

/**
* Restore `Class` definition from the constructor and the prototype.
* Currently, this transformation only supports output from TypeScript.
Expand Down Expand Up @@ -47,9 +53,14 @@
* @see https://babeljs.io/docs/babel-plugin-transform-classes
* @see https://www.typescriptlang.org/play?target=1#code/MYGwhgzhAEBiD29oG8BQ1oDswFsCmAXNBAC4BOAlpgObrRjWFYCuOARnmXXcPJqWWbAS8MgAps+IgKrUAlCjoYSACwoQAdJLzQAvFlx4l0Veo0Md+gIwAOOgF86IeNUbiFaDBl794IPBrO1GIARAASeCDOIQA0Jmqa2nIA3A50pGAkFMDEJJnZALJ4qvAAJmIexj4QfgFBYgDkGVk5+CWlDXJpGM3Z0FQZmMCWWHgA7nCIoQBmiCFd9kA
*/
export const transformAST: ASTTransformation = (context) => {
export const transformAST: ASTTransformation = (context, params) => {
const { root, j } = context

const rootScope = root.find(j.Program).get().scope as Scope | null
if (!rootScope) return

const inheritsHelpers = findHelperLocals(context, params, inheritsModuleName, inheritsModuleEsmName)

root
.find(j.VariableDeclaration, {
declarations: [
Expand All @@ -64,17 +75,9 @@
type: 'FunctionExpression',
body: {
type: 'BlockStatement',
body: [
{
type: 'FunctionDeclaration',
id: {
type: 'Identifier',
},
},
],
},
},
arguments: args => args.length === 0,
arguments: args => args.length <= 1,
},
},
],
Expand All @@ -86,12 +89,29 @@
const callee = init.callee as FunctionExpression
const bodyBody = callee.body.body
if (bodyBody.length < 2) return
if (init.arguments.length === 0 && !j.FunctionDeclaration.check(bodyBody[0])) return
if (init.arguments.length === 1 && bodyBody.findIndex(node => j.FunctionDeclaration.check(node)) < 1) return

const lastBodyNode = bodyBody[bodyBody.length - 1]
if (!j.ReturnStatement.check(lastBodyNode)) return
if (!j.Identifier.check(lastBodyNode.argument)) return
let internalName: string
if (j.Identifier.check(lastBodyNode.argument)) {
internalName = lastBodyNode.argument.name
}
else if (/* Babel */
j.CallExpression.check(lastBodyNode.argument)
&& j.Identifier.check(lastBodyNode.argument.callee)
&& lastBodyNode.argument.callee.name === '_createClass'
&& j.Identifier.check(lastBodyNode.argument.arguments[0])
) {
internalName = lastBodyNode.argument.arguments[0].name
}
else {
return

Check warning on line 110 in packages/unminify/src/transformations/un-es6-class.ts

View check run for this annotation

Codecov / codecov/patch

packages/unminify/src/transformations/un-es6-class.ts#L109-L110

Added lines #L109 - L110 were not covered by tests
}

let superClass: ExpressionKind | null = null

const internalName = lastBodyNode.argument.name
let prototypeNode: ExpressionKind = {
type: 'MemberExpression',
object: {
Expand Down Expand Up @@ -228,7 +248,31 @@
* configurable: !0
* })
*/
if (j.ExpressionStatement.check(bodyNode) && j.CallExpression.check(bodyNode.expression)) {
if (
j.ExpressionStatement.check(bodyNode)
&& j.CallExpression.check(bodyNode.expression)
&& (
j.match(bodyNode.expression.callee, {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'Object',
},
property: {
type: 'Identifier',
name: 'defineProperty',
},
})
|| j.match(bodyNode.expression.callee, {
type: 'Identifier',
name: '_defineProperty', // Babel
})
|| j.match(bodyNode.expression.callee, {
type: 'Identifier',
name: '_define_property', // SWC
})
)
) {
const { arguments: args } = bodyNode.expression
if (!args) return

Expand Down Expand Up @@ -277,15 +321,44 @@
bodyList.push(classMethod)
}
}

// extends
/**
* _inherits(SubClass, SuperClass);
*/
if (
j.ExpressionStatement.check(bodyNode)
&& j.CallExpression.check(bodyNode.expression)
&& j.Identifier.check(bodyNode.expression.callee)
&& (inheritsHelpers.includes(bodyNode.expression.callee.name)
|| bodyNode.expression.callee.name === '_inherits' /* Babel/SWC */
|| bodyNode.expression.callee.name === '_inheritsLoose'
|| bodyNode.expression.callee.name === '__extends' /* TypeScript */)
&& bodyNode.expression.arguments.length === 2
&& j.match(bodyNode.expression.arguments[0], {
type: 'Identifier',
name: internalName,
})
&& !j.SpreadElement.check(init.arguments[0])
) {
superClass = init.arguments[0]
}
})

const classBody = j.classBody(bodyList)
const classDeclaration = j.classDeclaration(
j.identifier(className),
classBody,
superClass,
)
p.replace(classDeclaration)
})

inheritsHelpers
.filter(helperLocal => findReferences(j, rootScope, helperLocal).length === 1)
.forEach((helperLocal) => {
removeHelperImport(j, rootScope, helperLocal)
})
}

export default wrap(transformAST)