@grey-ts/transpiler 1.4.0 → 1.4.1
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/dist/index.js +26 -22
- package/package.json +4 -5
package/dist/index.js
CHANGED
|
@@ -746,7 +746,7 @@ NodeHandler.register(ts7.SyntaxKind.BinaryExpression, (node) => {
|
|
|
746
746
|
}
|
|
747
747
|
switch (operatorToken) {
|
|
748
748
|
case "instanceof":
|
|
749
|
-
return
|
|
749
|
+
return `${left} isa ${right}`;
|
|
750
750
|
case "??":
|
|
751
751
|
return callUtilFunction("nullish_coalescing_op", left, right);
|
|
752
752
|
case "??=":
|
|
@@ -1059,6 +1059,29 @@ NodeHandler.register(ts10.SyntaxKind.ImportDeclaration, (node, ctx) => {
|
|
|
1059
1059
|
|
|
1060
1060
|
// src/visitors/objects.ts
|
|
1061
1061
|
import ts11 from "typescript";
|
|
1062
|
+
function shouldGetSafely(node) {
|
|
1063
|
+
if (ts11.isNonNullExpression(node.parent))
|
|
1064
|
+
return false;
|
|
1065
|
+
if (valueIsBeingAssignedToNode(node))
|
|
1066
|
+
return false;
|
|
1067
|
+
const hasQuestionDot = !!node.questionDotToken;
|
|
1068
|
+
if (ts11.isPropertyAccessExpression(node)) {
|
|
1069
|
+
const rightType = checker.getTypeAtLocation(node.name);
|
|
1070
|
+
if (!rightType.isUnion())
|
|
1071
|
+
return hasQuestionDot;
|
|
1072
|
+
const hasUndefined = rightType.types.some((t) => t.flags === ts11.TypeFlags.Undefined);
|
|
1073
|
+
if (!hasUndefined)
|
|
1074
|
+
return false;
|
|
1075
|
+
if (!ts11.isCallExpression(node.parent))
|
|
1076
|
+
return true;
|
|
1077
|
+
if (node.parent.arguments.length)
|
|
1078
|
+
return false;
|
|
1079
|
+
} else {
|
|
1080
|
+
if (ts11.isNumericLiteral(node.argumentExpression))
|
|
1081
|
+
return false;
|
|
1082
|
+
}
|
|
1083
|
+
return hasQuestionDot;
|
|
1084
|
+
}
|
|
1062
1085
|
NodeHandler.register(ts11.SyntaxKind.PropertyAccessExpression, (node, ctx) => {
|
|
1063
1086
|
const left = NodeHandler.handle(node.expression);
|
|
1064
1087
|
let right = NodeHandler.handle(node.name);
|
|
@@ -1066,14 +1089,7 @@ NodeHandler.register(ts11.SyntaxKind.PropertyAccessExpression, (node, ctx) => {
|
|
|
1066
1089
|
const nodeSymbol = checker.getSymbolAtLocation(node);
|
|
1067
1090
|
if (ctx.namespaceImports[ctx.currentFilePath]?.has(left))
|
|
1068
1091
|
return right;
|
|
1069
|
-
|
|
1070
|
-
const rightType = checker.getTypeAtLocation(node.name);
|
|
1071
|
-
if (rightType.isUnion()) {
|
|
1072
|
-
const hasUndefined = rightType.types.some((t) => t.flags === ts11.TypeFlags.Undefined);
|
|
1073
|
-
if (hasUndefined)
|
|
1074
|
-
getSafely = true;
|
|
1075
|
-
}
|
|
1076
|
-
if (!valueIsBeingAssignedToNode(node) && getSafely)
|
|
1092
|
+
if (shouldGetSafely(node))
|
|
1077
1093
|
return callUtilFunction("get_property", left, `"${right}"`);
|
|
1078
1094
|
let output = `${left}.${right}`;
|
|
1079
1095
|
output = replacePropertyAccess(output, nodeSymbol);
|
|
@@ -1090,7 +1106,7 @@ NodeHandler.register(ts11.SyntaxKind.ElementAccessExpression, (node, ctx) => {
|
|
|
1090
1106
|
} else {
|
|
1091
1107
|
right = NodeHandler.handle(node.argumentExpression);
|
|
1092
1108
|
}
|
|
1093
|
-
if (
|
|
1109
|
+
if (shouldGetSafely(node)) {
|
|
1094
1110
|
return callUtilFunction("get_property", left, `${right}`);
|
|
1095
1111
|
}
|
|
1096
1112
|
return `${left}[${right}]`;
|
|
@@ -1724,18 +1740,6 @@ var utilFunctions = {
|
|
|
1724
1740
|
"\treturn target",
|
|
1725
1741
|
"end function"
|
|
1726
1742
|
].join(`
|
|
1727
|
-
`),
|
|
1728
|
-
instance_of: [
|
|
1729
|
-
"instance_of = function(obj, class)",
|
|
1730
|
-
'\tif not obj.hasIndex("__isa") then return 0',
|
|
1731
|
-
"\tisaobj = obj",
|
|
1732
|
-
'\twhile isaobj.hasIndex("__isa")',
|
|
1733
|
-
'\t\tif isaobj["__isa"] == class then return 1',
|
|
1734
|
-
'\t\tisaobj = isaobj["__isa"]',
|
|
1735
|
-
"\tend while",
|
|
1736
|
-
"\treturn 0",
|
|
1737
|
-
"end function"
|
|
1738
|
-
].join(`
|
|
1739
1743
|
`),
|
|
1740
1744
|
nullish_coalescing_op: `nullish_coalescing_op = function(left, right)
|
|
1741
1745
|
if left == null then return @right
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@grey-ts/transpiler",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "Transpiles TypeScript into GreyScript",
|
|
5
5
|
"author": "Okka",
|
|
6
6
|
"module": "src/index.ts",
|
|
@@ -34,12 +34,11 @@
|
|
|
34
34
|
"build": "bun run scripts/build.ts"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@types/bun": "latest"
|
|
37
|
+
"@types/bun": "latest",
|
|
38
|
+
"@grey-ts/types": "latest"
|
|
38
39
|
},
|
|
39
40
|
"peerDependencies": {
|
|
40
|
-
"typescript": "^5"
|
|
41
|
-
},
|
|
42
|
-
"dependencies": {
|
|
41
|
+
"typescript": "^5",
|
|
43
42
|
"@grey-ts/types": "^2.2.0"
|
|
44
43
|
}
|
|
45
44
|
}
|