crankscript 0.11.11 → 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.
@@ -59,10 +59,8 @@ const getTransformClassDeclaration = (importMap)=>{
59
59
  const staticFieldStatements = staticFields.map((field)=>(0, _fields.transformStaticPropertyDeclaration)(context, field, className)).filter((stmt)=>stmt !== undefined);
60
60
  statements.push(...staticFieldStatements);
61
61
  const constructor = declaration.members.find((n)=>_typescript.isConstructorDeclaration(n) && n.body !== undefined);
62
- if (!constructor) {
63
- return [];
64
- }
65
- const transformedConstructor = (0, _transformConstructor.transformConstructor)(context, className, instanceFields, constructor);
62
+ const constructorToUse = constructor || _typescript.factory.createConstructorDeclaration(undefined, [], _typescript.factory.createBlock([], true));
63
+ const transformedConstructor = (0, _transformConstructor.transformConstructor)(context, className, instanceFields, constructorToUse);
66
64
  if (transformedConstructor) {
67
65
  statements.push(transformedConstructor);
68
66
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../libs/tstl-plugin/src/transformClassDeclaration/getTransformClassDeclaration.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport { FunctionVisitor, TransformationContext } from 'typescript-to-lua';\nimport * as tstl from 'typescript-to-lua';\nimport { transformStaticPropertyDeclaration } from 'typescript-to-lua/dist/transformation/visitors/class/members/fields';\nimport {\n getExtendedNode,\n isStaticNode,\n} from 'typescript-to-lua/dist/transformation/visitors/class/utils';\nimport { getExportedClassDeclarationStatements } from './getExportedClassDeclarationStatements';\nimport { ImportMap } from '../ImportMap';\nimport { ClassSuperInfo } from '../types';\nimport { createClassCall } from './createClasCall';\nimport { transformConstructor } from './transformConstructor';\nimport { transformMethodDeclaration } from './transformMethodDeclaration';\n\nconst hasSuperInfos = (\n context: TransformationContext,\n): context is TransformationContext & {\n classSuperInfos?: [ClassSuperInfo];\n} => {\n return 'classSuperInfos' in context;\n};\n\nexport const getTransformClassDeclaration = (importMap: ImportMap) => {\n return ((declaration, context: TransformationContext) => {\n importMap.add('object');\n\n let className: tstl.Identifier;\n if (declaration.name) {\n className = tstl.createIdentifier(declaration.name.text);\n } else {\n className = tstl.createIdentifier(\n context.createTempName('class'),\n declaration,\n );\n }\n\n const extension = getExtendedNode(declaration);\n\n if (hasSuperInfos(context)) {\n if (context.classSuperInfos) {\n context.classSuperInfos.push({\n className,\n extendedTypeNode: extension,\n });\n } else {\n context.classSuperInfos = [\n { className, extendedTypeNode: extension },\n ];\n }\n }\n\n // Get all properties with value\n const properties = declaration.members\n .filter(ts.isPropertyDeclaration)\n .filter(member => member.initializer);\n\n // Divide properties into static and non-static\n const instanceFields = properties.filter(prop => !isStaticNode(prop));\n const staticFields = properties.filter(prop => isStaticNode(prop));\n\n const statements: tstl.Statement[] = [];\n\n // class('X')\n statements.push(createClassCall(context, className, extension));\n\n // function X:init()\n // X.super.init(self)\n // end\n\n const staticFieldStatements = staticFields\n .map(field =>\n transformStaticPropertyDeclaration(context, field, className),\n )\n .filter(\n (stmt): stmt is tstl.AssignmentStatement => stmt !== undefined,\n );\n statements.push(...staticFieldStatements);\n\n const constructor = declaration.members.find(\n (n): n is ts.ConstructorDeclaration =>\n ts.isConstructorDeclaration(n) && n.body !== undefined,\n );\n\n if (!constructor) {\n return [];\n }\n\n const transformedConstructor = transformConstructor(\n context,\n className,\n instanceFields,\n constructor,\n );\n if (transformedConstructor) {\n statements.push(transformedConstructor);\n }\n\n const methods = declaration.members\n .filter(ts.isMethodDeclaration)\n .map(method =>\n transformMethodDeclaration(context, method, className),\n )\n .filter((method): method is tstl.Statement => method !== undefined);\n statements.push(...methods);\n\n statements.push(\n ...getExportedClassDeclarationStatements(className, declaration),\n );\n\n return statements;\n }) satisfies FunctionVisitor<ts.ClassLikeDeclaration>;\n};\n"],"names":["getTransformClassDeclaration","hasSuperInfos","context","importMap","declaration","add","className","name","tstl","createIdentifier","text","createTempName","extension","getExtendedNode","classSuperInfos","push","extendedTypeNode","properties","members","filter","ts","isPropertyDeclaration","member","initializer","instanceFields","prop","isStaticNode","staticFields","statements","createClassCall","staticFieldStatements","map","field","transformStaticPropertyDeclaration","stmt","undefined","constructor","find","n","isConstructorDeclaration","body","transformedConstructor","transformConstructor","methods","isMethodDeclaration","method","transformMethodDeclaration","getExportedClassDeclarationStatements"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":";;;;+BAuBaA;;;eAAAA;;;;sEAvBO;2EAEE;wBAC6B;uBAI5C;uDAC+C;gCAGtB;sCACK;4CACM;AAE3C,MAAMC,gBAAgB,CAClBC;IAIA,OAAO,qBAAqBA;AAChC;AAEO,MAAMF,+BAA+B,CAACG;IACzC,OAAQ,CAACC,aAAaF;QAClBC,UAAUE,GAAG,CAAC;QAEd,IAAIC;QACJ,IAAIF,YAAYG,IAAI,EAAE;YAClBD,YAAYE,iBAAKC,gBAAgB,CAACL,YAAYG,IAAI,CAACG,IAAI;QAC3D,OAAO;YACHJ,YAAYE,iBAAKC,gBAAgB,CAC7BP,QAAQS,cAAc,CAAC,UACvBP;QAER;QAEA,MAAMQ,YAAYC,IAAAA,sBAAe,EAACT;QAElC,IAAIH,cAAcC,UAAU;YACxB,IAAIA,QAAQY,eAAe,EAAE;gBACzBZ,QAAQY,eAAe,CAACC,IAAI,CAAC;oBACzBT;oBACAU,kBAAkBJ;gBACtB;YACJ,OAAO;gBACHV,QAAQY,eAAe,GAAG;oBACtB;wBAAER;wBAAWU,kBAAkBJ;oBAAU;iBAC5C;YACL;QACJ;QAEA,gCAAgC;QAChC,MAAMK,aAAab,YAAYc,OAAO,CACjCC,MAAM,CAACC,YAAGC,qBAAqB,EAC/BF,MAAM,CAACG,CAAAA,SAAUA,OAAOC,WAAW;QAExC,+CAA+C;QAC/C,MAAMC,iBAAiBP,WAAWE,MAAM,CAACM,CAAAA,OAAQ,CAACC,IAAAA,mBAAY,EAACD;QAC/D,MAAME,eAAeV,WAAWE,MAAM,CAACM,CAAAA,OAAQC,IAAAA,mBAAY,EAACD;QAE5D,MAAMG,aAA+B,EAAE;QAEvC,aAAa;QACbA,WAAWb,IAAI,CAACc,IAAAA,+BAAe,EAAC3B,SAASI,WAAWM;QAEpD,oBAAoB;QACpB,uBAAuB;QACvB,MAAM;QAEN,MAAMkB,wBAAwBH,aACzBI,GAAG,CAACC,CAAAA,QACDC,IAAAA,0CAAkC,EAAC/B,SAAS8B,OAAO1B,YAEtDa,MAAM,CACH,CAACe,OAA2CA,SAASC;QAE7DP,WAAWb,IAAI,IAAIe;QAEnB,MAAMM,cAAchC,YAAYc,OAAO,CAACmB,IAAI,CACxC,CAACC,IACGlB,YAAGmB,wBAAwB,CAACD,MAAMA,EAAEE,IAAI,KAAKL;QAGrD,IAAI,CAACC,aAAa;YACd,OAAO,EAAE;QACb;QAEA,MAAMK,yBAAyBC,IAAAA,0CAAoB,EAC/CxC,SACAI,WACAkB,gBACAY;QAEJ,IAAIK,wBAAwB;YACxBb,WAAWb,IAAI,CAAC0B;QACpB;QAEA,MAAME,UAAUvC,YAAYc,OAAO,CAC9BC,MAAM,CAACC,YAAGwB,mBAAmB,EAC7Bb,GAAG,CAACc,CAAAA,SACDC,IAAAA,sDAA0B,EAAC5C,SAAS2C,QAAQvC,YAE/Ca,MAAM,CAAC,CAAC0B,SAAqCA,WAAWV;QAC7DP,WAAWb,IAAI,IAAI4B;QAEnBf,WAAWb,IAAI,IACRgC,IAAAA,4EAAqC,EAACzC,WAAWF;QAGxD,OAAOwB;IACX;AACJ"}
1
+ {"version":3,"sources":["../../../../../libs/tstl-plugin/src/transformClassDeclaration/getTransformClassDeclaration.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport { FunctionVisitor, TransformationContext } from 'typescript-to-lua';\nimport * as tstl from 'typescript-to-lua';\nimport { transformStaticPropertyDeclaration } from 'typescript-to-lua/dist/transformation/visitors/class/members/fields';\nimport {\n getExtendedNode,\n isStaticNode,\n} from 'typescript-to-lua/dist/transformation/visitors/class/utils';\nimport { getExportedClassDeclarationStatements } from './getExportedClassDeclarationStatements';\nimport { ImportMap } from '../ImportMap';\nimport { ClassSuperInfo } from '../types';\nimport { createClassCall } from './createClasCall';\nimport { transformConstructor } from './transformConstructor';\nimport { transformMethodDeclaration } from './transformMethodDeclaration';\n\nconst hasSuperInfos = (\n context: TransformationContext,\n): context is TransformationContext & {\n classSuperInfos?: [ClassSuperInfo];\n} => {\n return 'classSuperInfos' in context;\n};\n\nexport const getTransformClassDeclaration = (importMap: ImportMap) => {\n return ((declaration, context: TransformationContext) => {\n importMap.add('object');\n\n let className: tstl.Identifier;\n if (declaration.name) {\n className = tstl.createIdentifier(declaration.name.text);\n } else {\n className = tstl.createIdentifier(\n context.createTempName('class'),\n declaration,\n );\n }\n\n const extension = getExtendedNode(declaration);\n\n if (hasSuperInfos(context)) {\n if (context.classSuperInfos) {\n context.classSuperInfos.push({\n className,\n extendedTypeNode: extension,\n });\n } else {\n context.classSuperInfos = [\n { className, extendedTypeNode: extension },\n ];\n }\n }\n\n // Get all properties with value\n const properties = declaration.members\n .filter(ts.isPropertyDeclaration)\n .filter(member => member.initializer);\n\n // Divide properties into static and non-static\n const instanceFields = properties.filter(prop => !isStaticNode(prop));\n const staticFields = properties.filter(prop => isStaticNode(prop));\n\n const statements: tstl.Statement[] = [];\n\n // class('X')\n statements.push(createClassCall(context, className, extension));\n\n // function X:init()\n // X.super.init(self)\n // end\n\n const staticFieldStatements = staticFields\n .map(field =>\n transformStaticPropertyDeclaration(context, field, className),\n )\n .filter(\n (stmt): stmt is tstl.AssignmentStatement => stmt !== undefined,\n );\n statements.push(...staticFieldStatements);\n\n const constructor = declaration.members.find(\n (n): n is ts.ConstructorDeclaration =>\n ts.isConstructorDeclaration(n) && n.body !== undefined,\n );\n\n const constructorToUse =\n constructor ||\n ts.factory.createConstructorDeclaration(\n undefined,\n [],\n ts.factory.createBlock([], true),\n );\n\n const transformedConstructor = transformConstructor(\n context,\n className,\n instanceFields,\n constructorToUse,\n );\n\n if (transformedConstructor) {\n statements.push(transformedConstructor);\n }\n\n const methods = declaration.members\n .filter(ts.isMethodDeclaration)\n .map(method =>\n transformMethodDeclaration(context, method, className),\n )\n .filter((method): method is tstl.Statement => method !== undefined);\n statements.push(...methods);\n\n statements.push(\n ...getExportedClassDeclarationStatements(className, declaration),\n );\n\n return statements;\n }) satisfies FunctionVisitor<ts.ClassLikeDeclaration>;\n};\n"],"names":["getTransformClassDeclaration","hasSuperInfos","context","importMap","declaration","add","className","name","tstl","createIdentifier","text","createTempName","extension","getExtendedNode","classSuperInfos","push","extendedTypeNode","properties","members","filter","ts","isPropertyDeclaration","member","initializer","instanceFields","prop","isStaticNode","staticFields","statements","createClassCall","staticFieldStatements","map","field","transformStaticPropertyDeclaration","stmt","undefined","constructor","find","n","isConstructorDeclaration","body","constructorToUse","factory","createConstructorDeclaration","createBlock","transformedConstructor","transformConstructor","methods","isMethodDeclaration","method","transformMethodDeclaration","getExportedClassDeclarationStatements"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":";;;;+BAuBaA;;;eAAAA;;;;sEAvBO;2EAEE;wBAC6B;uBAI5C;uDAC+C;gCAGtB;sCACK;4CACM;AAE3C,MAAMC,gBAAgB,CAClBC;IAIA,OAAO,qBAAqBA;AAChC;AAEO,MAAMF,+BAA+B,CAACG;IACzC,OAAQ,CAACC,aAAaF;QAClBC,UAAUE,GAAG,CAAC;QAEd,IAAIC;QACJ,IAAIF,YAAYG,IAAI,EAAE;YAClBD,YAAYE,iBAAKC,gBAAgB,CAACL,YAAYG,IAAI,CAACG,IAAI;QAC3D,OAAO;YACHJ,YAAYE,iBAAKC,gBAAgB,CAC7BP,QAAQS,cAAc,CAAC,UACvBP;QAER;QAEA,MAAMQ,YAAYC,IAAAA,sBAAe,EAACT;QAElC,IAAIH,cAAcC,UAAU;YACxB,IAAIA,QAAQY,eAAe,EAAE;g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C7BjB,GAAG,CAACkB,CAAAA,SACDC,IAAAA,sDAA0B,EAAChD,SAAS+C,QAAQ3C,YAE/Ca,MAAM,CAAC,CAAC8B,SAAqCA,WAAWd;QAC7DP,WAAWb,IAAI,IAAIgC;QAEnBnB,WAAWb,IAAI,IACRoC,IAAAA,4EAAqC,EAAC7C,WAAWF;QAGxD,OAAOwB;IACX;AACJ"}
@@ -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","tstl","createIdentifier","classInstanceFields","transformClassInstanceFields","push","parameterAssignments","filter","param","name","modifiers","length","map","ts","isIdentifier","paramName","text","createAssignmentStatement","createTableIndexExpression","createStringLiteral","undefined","stmt","body","transformFunctionBodyContent","reduce","previous","current","isExpressionStatement","isCallExpression","expression","isTableIndexExpression","isStringLiteral","index","value","slice","popScope","createFunctionExpression","createBlock"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":";;;;+BAUaA;;;eAAAA;;;;;sEAVO;2EAEE;uBACI;wBACmB;0BAItC;AAEA,MAAMA,uBAA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,UAAU,CAACG,KAAK,KACxDN,QAAQG,UAAU,CAACA,UAAU,CAACG,KAAK,CAACC,KAAK,KAAK,mBAChD;gBACE,OAAO;oBACH,eACOP;wBACHG,YAAY,eACLH,QAAQG,UAAU;4BACrBA,YAAY,eACLH,QAAQG,UAAU,CAACA,UAAU;gCAChCG,OAAO/B,iBAAKkB,mBAAmB,CAAC;;4BAEpCrB,QAAQ4B,QAAQG,UAAU,CAAC/B,MAAM,CAACoC,KAAK,CAAC;;;uBAG7CT;iBACN;YACL;YAEA,OAAO;mBAAIA;gBAAUC;aAAQ;QACjC,GAAG,EAAE;IACT;IACArC,QAAQ8C,QAAQ;IAChB,OAAOlC,iBAAKgB,yBAAyB,CACjChB,iBAAKiB,0BAA0B,CAC3B5B,WACAW,iBAAKkB,mBAAmB,CAAC1B,cAE7BQ,iBAAKmC,wBAAwB,CAACnC,iBAAKoC,WAAW,CAACxC,iBAAiBC;AAExE"}
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","slice","popScope","createFunctionExpression"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":";;;;+BA4CaA;;;eAAAA;;;;;sEA5CO;2EAEE;qCAC6B;uBACzB;wBACmB;0BAItC;AAEP,MAAMC,iCAAiC,CACnCC,SACAC,WACAC;IAEA,IAAI,CAACA,OAAO,OAAOC;IAEnB,MAAM,EAAEC,mBAAmB,EAAEC,QAAQC,cAAc,EAAE,GACjDC,IAAAA,uDAAkC,EAACP,SAAS,IACxCA,QAAQQ,mBAAmB,CAACN;IAGpC,IAAI,CAACO,iBAAKC,YAAY,CAACJ,iBAAiB;QACpCF,oBAAoBO,IAAI,CACpBF,iBAAKG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B,CAACxB,cAE7Bf,iBAAKqD,wBAAwB,CAACrD,iBAAKW,WAAW,CAACQ,iBAAiBC;AAExE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "crankscript",
3
- "version": "0.11.11",
3
+ "version": "0.11.13",
4
4
  "scripts": {
5
5
  "dev": "tsx src/index.ts",
6
6
  "post-build": "tsc-alias --project tsconfig.json"
@@ -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"}
@@ -0,0 +1,10 @@
1
+ let A = class A {
2
+ method() {
3
+ return 'test';
4
+ }
5
+ };
6
+ playdate.update = ()=>{
7
+ printTable(new A());
8
+ };
9
+
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../../../../../../libs/cli/src/commands/TranspileCommand/fn/_tests/test-implicit-constructor/src/index.ts"],"sourcesContent":["class A {\n method() {\n return 'test';\n }\n}\n\nplaydate.update = () => {\n printTable(new A());\n};\n"],"names":["A","method","playdate","update","printTable"],"rangeMappings":";;;;;;;","mappings":"AAAA,IAAA,AAAMA,IAAN,MAAMA;IACFC,SAAS;QACL,OAAO;IACX;AACJ;AAEAC,SAASC,MAAM,GAAG;IACdC,WAAW,IAAIJ;AACnB"}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../../../../../libs/cli/src/commands/TranspileCommand/fn/_tests/test-static/src/index.ts"],"sourcesContent":["class A {\n static staticField = 'staticField';\n constructor() {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const someVar = 1;\n }\n}\n\nclass B extends A {\n constructor() {\n super();\n }\n}\n\nplaydate.update = () => {\n printTable(new A());\n printTable(new B());\n};\n"],"names":["A","constructor","someVar","staticField","B","playdate","update","printTable"],"rangeMappings":";;;;;;;;;;;;;;;","mappings":"AAAA,IAAA,AAAMA,IAAN,MAAMA;IAEFC,aAAc;QACV,6DAA6D;QAC7D,MAAMC,UAAU;IACpB;AACJ;AANMF,EACKG,cAAc;AAOzB,IAAA,AAAMC,IAAN,MAAMA,UAAUJ;IACZC,aAAc;QACV,KAAK;IACT;AACJ;AAEAI,SAASC,MAAM,GAAG;IACdC,WAAW,IAAIP;IACfO,WAAW,IAAIH;AACnB"}
1
+ {"version":3,"sources":["../../../../../../../../../../libs/cli/src/commands/TranspileCommand/fn/_tests/test-static/src/index.ts"],"sourcesContent":["class A {\n static staticField = 'staticField';\n\n constructor() {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const someVar = 1;\n }\n}\n\nclass B extends A {\n constructor() {\n super();\n }\n}\n\nplaydate.update = () => {\n printTable(new A());\n printTable(new B());\n};\n"],"names":["A","constructor","someVar","staticField","B","playdate","update","printTable"],"rangeMappings":";;;;;;;;;;;;;;;","mappings":"AAAA,IAAA,AAAMA,IAAN,MAAMA;IAGFC,aAAc;QACV,6DAA6D;QAC7D,MAAMC,UAAU;IACpB;AACJ;AAPMF,EACKG,cAAc;AAQzB,IAAA,AAAMC,IAAN,MAAMA,UAAUJ;IACZC,aAAc;QACV,KAAK;IACT;AACJ;AAEAI,SAASC,MAAM,GAAG;IACdC,WAAW,IAAIP;IACfO,WAAW,IAAIH;AACnB"}