typedraft
Version:
TypeDraft is a superset of typescript with built-in support for DSL extension and literate programming.
70 lines (68 loc) • 1.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("@babel/types");
/**
* # Class Method
* As we want to rearrange code in literate programming, we will seperate class method from class body.
*/
class MethodCode {
constructor(path) {
this.m_Path = path;
}
get m_Code() {
return this.m_Path.node;
}
get m_Left() {
return this.m_Expression.left;
}
get m_Right() {
return this.m_Expression.right;
}
get m_Expression() {
return this.m_Code.expression;
}
get m_ClassName() {
return this.m_Left.openingElement.name.name;
}
/**
* The syntax will be:
*
```ts
export class Foo {
}
<Foo/> + function Test(this: Foo, a: number, b: string) {
return a.toString()+b;
}
```
*
* After transcription, we will restore `Foo` class to:
*
```ts
export class Foo {
static foo : number;
Test(a: number, b: string) {
return a.toString()+b;
}
}
```
*
* ## Restore: To Class Method
*/
ToClassMethod() {
const { id, params: raw_params, body, typeParameters } = this.m_Right;
/**
* remove param "this":
*/
const params = raw_params.filter(param => {
if (types_1.isIdentifier(param) && param.name === "this") {
return false;
}
return true;
});
const kind = id.name === "constructor" ? id.name : "method";
const class_method = types_1.classMethod(kind, id, params, body);
class_method.typeParameters = typeParameters;
return class_method;
}
}
exports.MethodCode = MethodCode;