crankscript 0.11.12 → 0.11.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/transformClassDeclaration/transformConstructor.js +19 -0
- package/assets/transformClassDeclaration/transformConstructor.js.map +1 -1
- package/package.json +1 -1
- package/src/commands/TranspileCommand/fn/_tests/test-default-parameters/src/index.js +17 -0
- package/src/commands/TranspileCommand/fn/_tests/test-default-parameters/src/index.js.map +1 -0
@@ -12,9 +12,20 @@ const _extends = require("@swc/helpers/_/_extends");
|
|
12
12
|
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
|
13
13
|
const _typescript = /*#__PURE__*/ _interop_require_wildcard._(require("typescript"));
|
14
14
|
const _typescripttolua = /*#__PURE__*/ _interop_require_wildcard._(require("typescript-to-lua"));
|
15
|
+
const _precedingstatements = require("typescript-to-lua/dist/transformation/utils/preceding-statements");
|
15
16
|
const _scope = require("typescript-to-lua/dist/transformation/utils/scope");
|
16
17
|
const _fields = require("typescript-to-lua/dist/transformation/visitors/class/members/fields");
|
17
18
|
const _function = require("typescript-to-lua/dist/transformation/visitors/function");
|
19
|
+
const transformParameterDefaultValue = (context, paramName, value)=>{
|
20
|
+
if (!value) return undefined;
|
21
|
+
const { precedingStatements, result: parameterValue } = (0, _precedingstatements.transformInPrecedingStatementScope)(context, ()=>context.transformExpression(value));
|
22
|
+
if (!_typescripttolua.isNilLiteral(parameterValue)) {
|
23
|
+
precedingStatements.push(_typescripttolua.createAssignmentStatement(paramName, parameterValue));
|
24
|
+
}
|
25
|
+
if (precedingStatements.length === 0) return undefined;
|
26
|
+
const nilCondition = _typescripttolua.createBinaryExpression(paramName, _typescripttolua.createNilLiteral(), _typescripttolua.SyntaxKind.EqualityOperator);
|
27
|
+
return _typescripttolua.createIfStatement(nilCondition, _typescripttolua.createBlock(precedingStatements), undefined);
|
28
|
+
};
|
18
29
|
const transformConstructor = (context, className, instanceFields, constructor)=>{
|
19
30
|
const methodName = 'init';
|
20
31
|
context.pushScope(_scope.ScopeType.Function);
|
@@ -22,6 +33,14 @@ const transformConstructor = (context, className, instanceFields, constructor)=>
|
|
22
33
|
const params = (0, _function.transformParameters)(context, constructor == null ? void 0 : constructor.parameters, _typescripttolua.createIdentifier('self'))[0];
|
23
34
|
const classInstanceFields = (0, _fields.transformClassInstanceFields)(context, instanceFields);
|
24
35
|
bodyStatements.push(...classInstanceFields);
|
36
|
+
const defaultParamStatements = constructor == null ? void 0 : constructor.parameters.filter((param)=>param.initializer !== undefined).map((param)=>{
|
37
|
+
if (_typescript.isIdentifier(param.name) && param.initializer) {
|
38
|
+
const paramName = _typescripttolua.createIdentifier(param.name.text);
|
39
|
+
return transformParameterDefaultValue(context, paramName, param.initializer);
|
40
|
+
}
|
41
|
+
return undefined;
|
42
|
+
}).filter((stmt)=>stmt !== undefined);
|
43
|
+
bodyStatements.push(...defaultParamStatements);
|
25
44
|
const parameterAssignments = constructor == null ? void 0 : constructor.parameters.filter((param)=>param.name && param.modifiers && param.modifiers.length > 0).map((param)=>{
|
26
45
|
if (_typescript.isIdentifier(param.name)) {
|
27
46
|
const paramName = param.name.text;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../../../libs/tstl-plugin/src/transformClassDeclaration/transformConstructor.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport { TransformationContext } from 'typescript-to-lua';\nimport * as tstl from 'typescript-to-lua';\nimport { ScopeType } from 'typescript-to-lua/dist/transformation/utils/scope';\nimport { transformClassInstanceFields } from 'typescript-to-lua/dist/transformation/visitors/class/members/fields';\nimport {\n transformFunctionBodyContent,\n transformParameters,\n} from 'typescript-to-lua/dist/transformation/visitors/function';\n\nexport const transformConstructor = (\n context: TransformationContext,\n className: tstl.Identifier,\n instanceFields: ts.PropertyDeclaration[],\n constructor: ts.ConstructorDeclaration,\n): tstl.Statement | undefined => {\n const methodName = 'init';\n context.pushScope(ScopeType.Function);\n let bodyStatements: tstl.Statement[] = [];\n const params = transformParameters(\n context,\n constructor?.parameters,\n tstl.createIdentifier('self'),\n )[0];\n const classInstanceFields = transformClassInstanceFields(\n context,\n instanceFields,\n );\n\n bodyStatements.push(...classInstanceFields);\n\n const parameterAssignments = constructor?.parameters\n .filter(\n param =>\n param.name && param.modifiers && param.modifiers.length > 0,\n )\n .map(param => {\n if (ts.isIdentifier(param.name)) {\n const paramName = param.name.text;\n return tstl.createAssignmentStatement(\n tstl.createTableIndexExpression(\n tstl.createIdentifier('self'),\n tstl.createStringLiteral(paramName),\n ),\n tstl.createIdentifier(paramName),\n );\n }\n return undefined;\n })\n .filter((stmt): stmt is tstl.AssignmentStatement => stmt !== undefined);\n\n bodyStatements.push(...parameterAssignments);\n\n if (constructor?.body) {\n const body = transformFunctionBodyContent(context, constructor.body);\n\n bodyStatements.push(...body);\n bodyStatements = bodyStatements.reduce((previous, current) => {\n if (\n tstl.isExpressionStatement(current) &&\n tstl.isCallExpression(current.expression) &&\n tstl.isTableIndexExpression(current.expression.expression) &&\n tstl.isStringLiteral(current.expression.expression.index) &&\n current.expression.expression.index.value === '____constructor'\n ) {\n return [\n {\n ...current,\n expression: {\n ...current.expression,\n expression: {\n ...current.expression.expression,\n index: tstl.createStringLiteral('init'),\n },\n params: current.expression.params.slice(1),\n },\n },\n ...previous,\n ];\n }\n\n return [...previous, current];\n }, [] as tstl.Statement[]);\n }\n context.popScope();\n return tstl.createAssignmentStatement(\n tstl.createTableIndexExpression(\n className,\n tstl.createStringLiteral(methodName),\n ),\n tstl.createFunctionExpression(tstl.createBlock(bodyStatements), params),\n );\n};\n"],"names":["transformConstructor","context","className","instanceFields","constructor","methodName","pushScope","ScopeType","Function","bodyStatements","params","transformParameters","parameters","
|
1
|
+
{"version":3,"sources":["../../../../../libs/tstl-plugin/src/transformClassDeclaration/transformConstructor.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport { TransformationContext } from 'typescript-to-lua';\nimport * as tstl from 'typescript-to-lua';\nimport { transformInPrecedingStatementScope } from 'typescript-to-lua/dist/transformation/utils/preceding-statements';\nimport { ScopeType } from 'typescript-to-lua/dist/transformation/utils/scope';\nimport { transformClassInstanceFields } from 'typescript-to-lua/dist/transformation/visitors/class/members/fields';\nimport {\n transformFunctionBodyContent,\n transformParameters,\n} from 'typescript-to-lua/dist/transformation/visitors/function';\n\nconst transformParameterDefaultValue = (\n context: TransformationContext,\n paramName: tstl.Identifier,\n value: ts.Expression,\n): tstl.Statement | undefined => {\n if (!value) return undefined;\n\n const { precedingStatements, result: parameterValue } =\n transformInPrecedingStatementScope(context, () =>\n context.transformExpression(value),\n );\n\n if (!tstl.isNilLiteral(parameterValue)) {\n precedingStatements.push(\n tstl.createAssignmentStatement(paramName, parameterValue),\n );\n }\n\n if (precedingStatements.length === 0) return undefined;\n\n const nilCondition = tstl.createBinaryExpression(\n paramName,\n tstl.createNilLiteral(),\n tstl.SyntaxKind.EqualityOperator,\n );\n\n return tstl.createIfStatement(\n nilCondition,\n tstl.createBlock(precedingStatements),\n undefined,\n );\n};\n\nexport const transformConstructor = (\n context: TransformationContext,\n className: tstl.Identifier,\n instanceFields: ts.PropertyDeclaration[],\n constructor: ts.ConstructorDeclaration,\n): tstl.Statement | undefined => {\n const methodName = 'init';\n context.pushScope(ScopeType.Function);\n let bodyStatements: tstl.Statement[] = [];\n const params = transformParameters(\n context,\n constructor?.parameters,\n tstl.createIdentifier('self'),\n )[0];\n const classInstanceFields = transformClassInstanceFields(\n context,\n instanceFields,\n );\n\n bodyStatements.push(...classInstanceFields);\n\n const defaultParamStatements = constructor?.parameters\n .filter(param => param.initializer !== undefined)\n .map(param => {\n if (ts.isIdentifier(param.name) && param.initializer) {\n const paramName = tstl.createIdentifier(param.name.text);\n\n return transformParameterDefaultValue(\n context,\n paramName,\n param.initializer,\n );\n }\n\n return undefined;\n })\n .filter((stmt): stmt is tstl.Statement => stmt !== undefined);\n\n bodyStatements.push(...defaultParamStatements);\n\n const parameterAssignments = constructor?.parameters\n .filter(\n param =>\n param.name && param.modifiers && param.modifiers.length > 0,\n )\n .map(param => {\n if (ts.isIdentifier(param.name)) {\n const paramName = param.name.text;\n return tstl.createAssignmentStatement(\n tstl.createTableIndexExpression(\n tstl.createIdentifier('self'),\n tstl.createStringLiteral(paramName),\n ),\n tstl.createIdentifier(paramName),\n );\n }\n return undefined;\n })\n .filter((stmt): stmt is tstl.AssignmentStatement => stmt !== undefined);\n\n bodyStatements.push(...parameterAssignments);\n\n if (constructor?.body) {\n const body = transformFunctionBodyContent(context, constructor.body);\n\n bodyStatements.push(...body);\n bodyStatements = bodyStatements.reduce((previous, current) => {\n if (\n tstl.isExpressionStatement(current) &&\n tstl.isCallExpression(current.expression) &&\n tstl.isTableIndexExpression(current.expression.expression) &&\n tstl.isStringLiteral(current.expression.expression.index) &&\n current.expression.expression.index.value === '____constructor'\n ) {\n return [\n {\n ...current,\n expression: {\n ...current.expression,\n expression: {\n ...current.expression.expression,\n index: tstl.createStringLiteral('init'),\n },\n params: current.expression.params.slice(1),\n },\n },\n ...previous,\n ];\n }\n\n return [...previous, current];\n }, [] as tstl.Statement[]);\n }\n context.popScope();\n return tstl.createAssignmentStatement(\n tstl.createTableIndexExpression(\n className,\n tstl.createStringLiteral(methodName),\n ),\n tstl.createFunctionExpression(tstl.createBlock(bodyStatements), params),\n );\n};\n"],"names":["transformConstructor","transformParameterDefaultValue","context","paramName","value","undefined","precedingStatements","result","parameterValue","transformInPrecedingStatementScope","transformExpression","tstl","isNilLiteral","push","createAssignmentStatement","length","nilCondition","createBinaryExpression","createNilLiteral","SyntaxKind","EqualityOperator","createIfStatement","createBlock","className","instanceFields","constructor","methodName","pushScope","ScopeType","Function","bodyStatements","params","transformParameters","parameters","createIdentifier","classInstanceFields","transformClassInstanceFields","defaultParamStatements","filter","param","initializer","map","ts","isIdentifier","name","text","stmt","parameterAssignments","modifiers","createTableIndexExpression","createStringLiteral","body","transformFunctionBodyContent","reduce","previous","current","isExpressionStatement","isCallExpression","expression","isTableIndexExpression","isStringLiteral","index","sl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K,KACxDN,QAAQG,UAAU,CAACA,UAAU,CAACG,KAAK,CAACzD,KAAK,KAAK,mBAChD;gBACE,OAAO;oBACH,eACOmD;wBACHG,YAAY,eACLH,QAAQG,UAAU;4BACrBA,YAAY,eACLH,QAAQG,UAAU,CAACA,UAAU;gCAChCG,OAAOlD,iBAAKuC,mBAAmB,CAAC;;4BAEpCnB,QAAQwB,QAAQG,UAAU,CAAC3B,MAAM,CAAC+B,KAAK,CAAC;;;uBAG7CR;iBACN;YACL;YAEA,OAAO;mBAAIA;gBAAUC;aAAQ;QACjC,GAAG,EAAE;IACT;IACArD,QAAQ6D,QAAQ;IAChB,OAAOpD,iBAAKG,yBAAyB,CACjCH,iBAAKsC,0BAA0B,CAC3B1B,WACAZ,iBAAKuC,mBAAmB,CAACxB,cAE7Bf,iBAAKqD,wBAAwB,CAACrD,iBAAKW,WAAW,CAACQ,iBAAiBC;AAExE"}
|
package/package.json
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
let A = class A {
|
2
|
+
method(c, d = 5) {
|
3
|
+
console.log(c, d);
|
4
|
+
}
|
5
|
+
constructor(a, b = 5){
|
6
|
+
console.log(a, b);
|
7
|
+
}
|
8
|
+
};
|
9
|
+
function test(x, y = 5) {
|
10
|
+
console.log(x, y);
|
11
|
+
}
|
12
|
+
playdate.update = ()=>{
|
13
|
+
printTable(new A(1));
|
14
|
+
test(1);
|
15
|
+
};
|
16
|
+
|
17
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../../../libs/cli/src/commands/TranspileCommand/fn/_tests/test-default-parameters/src/index.ts"],"sourcesContent":["class A {\n constructor(a: number, b = 5) {\n console.log(a, b);\n }\n\n method(c: number, d = 5) {\n console.log(c, d);\n }\n}\n\nfunction test(x: number, y = 5) {\n console.log(x, y);\n}\n\nplaydate.update = () => {\n printTable(new A(1));\n test(1);\n};\n"],"names":["A","method","c","d","console","log","constructor","a","b","test","x","y","playdate","update","printTable"],"rangeMappings":";;;;;;;;;;;;;;","mappings":"AAAA,IAAA,AAAMA,IAAN,MAAMA;IAKFC,OAAOC,CAAS,EAAEC,IAAI,CAAC,EAAE;QACrBC,QAAQC,GAAG,CAACH,GAAGC;IACnB;IANAG,YAAYC,CAAS,EAAEC,IAAI,CAAC,CAAE;QAC1BJ,QAAQC,GAAG,CAACE,GAAGC;IACnB;AAKJ;AAEA,SAASC,KAAKC,CAAS,EAAEC,IAAI,CAAC;IAC1BP,QAAQC,GAAG,CAACK,GAAGC;AACnB;AAEAC,SAASC,MAAM,GAAG;IACdC,WAAW,IAAId,EAAE;IACjBS,KAAK;AACT"}
|