@grey-ts/transpiler 1.4.3 → 1.4.4

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.
Files changed (2) hide show
  1. package/dist/index.js +87 -74
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -2,16 +2,16 @@
2
2
 
3
3
  // src/index.ts
4
4
  import * as fs3 from "node:fs";
5
- import path5 from "node:path";
5
+ import path6 from "node:path";
6
6
 
7
7
  // src/transpiler.ts
8
8
  import * as fs2 from "node:fs";
9
- import * as path4 from "node:path";
9
+ import * as path5 from "node:path";
10
10
  import ts14 from "typescript";
11
11
 
12
12
  // src/nodeHandler.ts
13
+ import * as path from "node:path";
13
14
  import ts from "typescript";
14
-
15
15
  class NodeHandler {
16
16
  static handlers = new Map;
17
17
  static transpileContext;
@@ -29,13 +29,30 @@ class NodeHandler {
29
29
  }
30
30
  try {
31
31
  const result = handler(node, this.transpileContext);
32
- return result;
32
+ const extra = this.transpileContext.extraOutput.get(node);
33
+ if (!extra)
34
+ return result;
35
+ return [
36
+ ...extra.before ? [extra.before] : [],
37
+ result,
38
+ ...extra.after ? [extra.after] : []
39
+ ].join(`
40
+ `);
33
41
  } catch (error) {
34
42
  console.error(error);
35
43
  this.printLineAndCol(node);
36
44
  return ts.isBlock(node.parent) || ts.isSourceFile(node.parent) ? "" : "null";
37
45
  }
38
46
  }
47
+ static addExtraOutput(node, before, after) {
48
+ if (!this.transpileContext.extraOutput.has(node))
49
+ this.transpileContext.extraOutput.set(node, { before: "", after: "" });
50
+ const extra = this.transpileContext.extraOutput.get(node);
51
+ if (before)
52
+ extra.before += before;
53
+ if (after)
54
+ extra.after += after;
55
+ }
39
56
  static printLineAndCol(node) {
40
57
  const source = node.getSourceFile();
41
58
  const lineAndChar = source.getLineAndCharacterOfPosition(node.pos);
@@ -47,6 +64,29 @@ NodeHandler.register(ts.SyntaxKind.InterfaceDeclaration, () => "");
47
64
  NodeHandler.register(ts.SyntaxKind.ModuleDeclaration, () => "");
48
65
  NodeHandler.register(ts.SyntaxKind.EndOfFileToken, () => "");
49
66
  NodeHandler.register(ts.SyntaxKind.EmptyStatement, () => "");
67
+ NodeHandler.register(ts.SyntaxKind.SourceFile, (sourceFile, ctx) => {
68
+ if (ctx.visitedFiles.has(sourceFile.fileName))
69
+ return "";
70
+ ctx.visitedFiles.add(sourceFile.fileName);
71
+ if (sourceFile.isDeclarationFile)
72
+ return "";
73
+ if (program.isSourceFileDefaultLibrary(sourceFile) || program.isSourceFileFromExternalLibrary(sourceFile))
74
+ return "";
75
+ const prevFile = ctx.currentFilePath;
76
+ ctx.currentFilePath = sourceFile.fileName;
77
+ ctx.currentFolder = path.dirname(sourceFile.fileName);
78
+ ctx.namedImports[sourceFile.fileName] = {};
79
+ ctx.namespaceImports[sourceFile.fileName] = new Set;
80
+ sourceFile.forEachChild((node) => {
81
+ const result = NodeHandler.handle(node);
82
+ if (!result)
83
+ return;
84
+ ctx.output.push(result);
85
+ });
86
+ ctx.currentFilePath = prevFile;
87
+ ctx.currentFolder = path.dirname(prevFile);
88
+ return "";
89
+ });
50
90
 
51
91
  // src/parser.ts
52
92
  import ts2 from "typescript";
@@ -57,7 +97,7 @@ var parser_default = parseCode;
57
97
 
58
98
  // src/utils.ts
59
99
  import fs from "node:fs";
60
- import path from "node:path";
100
+ import path2 from "node:path";
61
101
  import ts3 from "typescript";
62
102
 
63
103
  // src/replaceKeywords.ts
@@ -326,7 +366,7 @@ function getSourceFiles(absPath) {
326
366
  const file = filePaths.shift();
327
367
  const stat = fs.statSync(file);
328
368
  if (stat.isDirectory()) {
329
- filePaths.push(...fs.readdirSync(file).map((name) => path.join(file, name)));
369
+ filePaths.push(...fs.readdirSync(file).map((name) => path2.join(file, name)));
330
370
  continue;
331
371
  }
332
372
  const existing = program.getSourceFile(file);
@@ -376,8 +416,8 @@ function replacePropertyAccess(original, symbol) {
376
416
  return replaceValue;
377
417
  }
378
418
  function findProjectRoot(dir, fileToSearch = "package.json") {
379
- while (!fs.existsSync(path.join(dir, fileToSearch))) {
380
- const parent = path.dirname(dir);
419
+ while (!fs.existsSync(path2.join(dir, fileToSearch))) {
420
+ const parent = path2.dirname(dir);
381
421
  if (parent === dir)
382
422
  throw new Error(`No ${fileToSearch} found`);
383
423
  dir = parent;
@@ -520,7 +560,7 @@ end function`;
520
560
  import ts7 from "typescript";
521
561
 
522
562
  // src/call_transformers/callTransformer.ts
523
- import path2 from "node:path";
563
+ import path3 from "node:path";
524
564
  import ts6 from "typescript";
525
565
  class CallTransformer {
526
566
  static handlers = new Map;
@@ -557,10 +597,10 @@ CallTransformer.register("GreyHack.include", (name, args, node, ctx) => {
557
597
  const fileArg = node.arguments[0];
558
598
  if (!ts6.isStringLiteralLike(fileArg))
559
599
  throw "You can't include variables";
560
- const absPath = path2.resolve(ctx.currentFolder, fileArg.text);
600
+ const absPath = path3.resolve(ctx.currentFolder, fileArg.text);
561
601
  const sources = getSourceFiles(absPath);
562
602
  for (const source of sources) {
563
- transpileSourceFile(source, ctx);
603
+ NodeHandler.handle(source);
564
604
  }
565
605
  return "";
566
606
  });
@@ -680,7 +720,7 @@ NodeHandler.register(ts7.SyntaxKind.CallExpression, (node, ctx) => {
680
720
  const transformed = CallTransformer.handle(symbolFullName, name, args, node, ctx);
681
721
  if (transformed !== null)
682
722
  return transformed;
683
- if (!args.length)
723
+ if (!args.length && !ts7.isParenthesizedExpression(node.expression))
684
724
  return name;
685
725
  return `${name}(${args.join(", ")})`;
686
726
  });
@@ -704,7 +744,7 @@ function shouldHaveOuterPrefix(node, operator) {
704
744
  const leftSymbol = checker.getSymbolAtLocation(node.left);
705
745
  if (!leftSymbol?.valueDeclaration)
706
746
  return false;
707
- return leftSymbol.valueDeclaration.pos < functionAncestor.pos || leftSymbol.valueDeclaration.pos > functionAncestor.end;
747
+ return leftSymbol.valueDeclaration.end < functionAncestor.pos || leftSymbol.valueDeclaration.pos > functionAncestor.end;
708
748
  }
709
749
  function isAssignmentChain(node, operator) {
710
750
  if (!assignmentOperators.has(operator))
@@ -928,7 +968,13 @@ NodeHandler.register(ts8.SyntaxKind.ArrowFunction, (node) => {
928
968
  const params = node.parameters.map((param) => NodeHandler.handle(param));
929
969
  const body = ts8.isBlock(node.body) ? NodeHandler.handle(node.body) : ` return ${NodeHandler.handle(node.body)}`;
930
970
  if (ts8.isCallOrNewExpression(node.parent) || ts8.isParenthesizedExpression(node.parent)) {
931
- return "@" + createAnonFunction(body, params).name;
971
+ const mainNode = ts8.findAncestor(node.parent, (n) => n.parent && (ts8.isBlock(n.parent) || ts8.isSourceFile(n.parent)));
972
+ if (!mainNode) {
973
+ return "@" + createAnonFunction(body, params).name;
974
+ }
975
+ const anon = createAnonFunction(body, params, false);
976
+ NodeHandler.addExtraOutput(mainNode, anon.str, null);
977
+ return "@" + anon.name;
932
978
  }
933
979
  if (ts8.hasOnlyExpressionInitializer(node.parent) || ts8.isBinaryExpression(node.parent) || ts8.isReturnStatement(node.parent)) {
934
980
  return `function(${params.join(", ")})
@@ -1009,35 +1055,23 @@ NodeHandler.register(ts9.SyntaxKind.RegularExpressionLiteral, (node) => {
1009
1055
  });
1010
1056
 
1011
1057
  // src/visitors/imports.ts
1012
- import path3 from "node:path";
1058
+ import path4 from "node:path";
1013
1059
  import ts10 from "typescript";
1014
- function importFile(filePath, ctx, returnResult) {
1015
- let srcPath = path3.resolve(ctx.currentFolder, filePath);
1016
- if (!path3.extname(srcPath))
1060
+ function importFile(filePath, ctx) {
1061
+ let srcPath = path4.resolve(ctx.currentFolder, filePath);
1062
+ if (!path4.extname(srcPath))
1017
1063
  srcPath += ".ts";
1018
1064
  const source = program.getSourceFile(srcPath);
1019
1065
  if (!source) {
1020
1066
  console.error(`Failed to find source ${srcPath}`);
1021
1067
  return "";
1022
1068
  }
1023
- return transpileSourceFile(source, ctx, returnResult);
1069
+ return NodeHandler.handle(source);
1024
1070
  }
1025
1071
  NodeHandler.register(ts10.SyntaxKind.ImportDeclaration, (node, ctx) => {
1026
1072
  if (!node.importClause) {
1027
1073
  const moduleName = node.moduleSpecifier.text;
1028
- const transpiledFile = importFile(moduleName, ctx, true);
1029
- if (!transpiledFile)
1030
- return "";
1031
- const rndName = "func_" + (Date.now() * Math.random()).toString().slice(0, 6);
1032
- return [
1033
- `${rndName} = function`,
1034
- transpiledFile.split(`
1035
- `).map((line) => "\t" + line).join(`
1036
- `),
1037
- "end function",
1038
- `${rndName}()`
1039
- ].join(`
1040
- `);
1074
+ return importFile(moduleName, ctx);
1041
1075
  }
1042
1076
  if (node.importClause.phaseModifier)
1043
1077
  return "";
@@ -1758,27 +1792,34 @@ end function`,
1758
1792
  return when_false
1759
1793
  end function`
1760
1794
  };
1761
- function createAnonFunction(body, params) {
1795
+ var anonFunctionsCreated = 0;
1796
+ function createAnonFunction(body, params, insertToUtils = true) {
1762
1797
  const defaultParams = new Array(3).fill(0).map((_, i) => `param${i}`);
1763
- const nextName = `func_${utilitiesToInsert.size}`;
1798
+ const nextName = `func_${anonFunctionsCreated}`;
1764
1799
  const paramString = Object.assign(defaultParams, params).join(",");
1765
1800
  const result = `${nextName} = function(${paramString})
1766
1801
  ${body}
1767
1802
  end function`;
1768
- utilitiesToInsert.set(nextName, result);
1803
+ anonFunctionsCreated++;
1804
+ if (insertToUtils)
1805
+ utilitiesToInsert.set(nextName, result);
1769
1806
  return { name: nextName, str: result };
1770
1807
  }
1771
- function transpileProgram(entryFileRelativePath) {
1772
- const ctx = {
1773
- currentFolder: "",
1774
- currentFilePath: path4.resolve(process.cwd(), entryFileRelativePath),
1808
+ function createContext(currentFileName = "file.ts") {
1809
+ return {
1810
+ currentFolder: process.cwd(),
1811
+ currentFilePath: path5.resolve(process.cwd(), currentFileName),
1775
1812
  namedImports: {},
1776
1813
  namespaceImports: {},
1777
- visitedFiles: {},
1778
- output: []
1814
+ visitedFiles: new Set,
1815
+ output: [],
1816
+ extraOutput: new Map
1779
1817
  };
1818
+ }
1819
+ function transpileProgram(entryFileRelativePath) {
1820
+ const ctx = createContext(entryFileRelativePath);
1780
1821
  NodeHandler.transpileContext = ctx;
1781
- ctx.currentFolder = path4.dirname(ctx.currentFilePath);
1822
+ ctx.currentFolder = path5.dirname(ctx.currentFilePath);
1782
1823
  if (!fs2.existsSync(ctx.currentFilePath)) {
1783
1824
  console.error(`Error: file '${ctx.currentFilePath}' doesn't exist`);
1784
1825
  process.exit(1);
@@ -1786,7 +1827,7 @@ function transpileProgram(entryFileRelativePath) {
1786
1827
  let start = Date.now();
1787
1828
  const tsconfigPath = findProjectRoot(process.cwd(), "tsconfig.json") + "/tsconfig.json";
1788
1829
  const res = ts14.readConfigFile(tsconfigPath, ts14.sys.readFile);
1789
- const parsed = ts14.parseJsonConfigFileContent(res.config, ts14.sys, path4.dirname(tsconfigPath));
1830
+ const parsed = ts14.parseJsonConfigFileContent(res.config, ts14.sys, path5.dirname(tsconfigPath));
1790
1831
  if (!parsed.options.types)
1791
1832
  parsed.options.types = [];
1792
1833
  if (!parsed.options.types.includes("@grey-ts/types")) {
@@ -1805,7 +1846,7 @@ function transpileProgram(entryFileRelativePath) {
1805
1846
  console.error(`Error: failed to find '${ctx.currentFilePath}' from the included sources`);
1806
1847
  process.exit(1);
1807
1848
  }
1808
- transpileSourceFile(entry, ctx);
1849
+ NodeHandler.handle(entry);
1809
1850
  if (utilitiesToInsert.size) {
1810
1851
  ctx.output.unshift(...utilitiesToInsert.values());
1811
1852
  utilitiesToInsert.clear();
@@ -1813,34 +1854,6 @@ function transpileProgram(entryFileRelativePath) {
1813
1854
  console.log(`Transpiling took ${Date.now() - start} ms`);
1814
1855
  return ctx.output;
1815
1856
  }
1816
- function transpileSourceFile(sourceFile, ctx, returnResult) {
1817
- if (ctx.visitedFiles[sourceFile.fileName])
1818
- return "";
1819
- ctx.visitedFiles[sourceFile.fileName] = true;
1820
- if (sourceFile.isDeclarationFile)
1821
- return "";
1822
- if (program.isSourceFileDefaultLibrary(sourceFile) || program.isSourceFileFromExternalLibrary(sourceFile))
1823
- return "";
1824
- const prevFile = ctx.currentFilePath;
1825
- ctx.currentFilePath = sourceFile.fileName;
1826
- ctx.currentFolder = path4.dirname(sourceFile.fileName);
1827
- ctx.namedImports[sourceFile.fileName] = {};
1828
- ctx.namespaceImports[sourceFile.fileName] = new Set;
1829
- const output = [];
1830
- sourceFile.forEachChild((node) => {
1831
- const result = NodeHandler.handle(node);
1832
- if (!result)
1833
- return;
1834
- if (!returnResult)
1835
- ctx.output.push(result);
1836
- else
1837
- output.push(result);
1838
- });
1839
- ctx.currentFilePath = prevFile;
1840
- ctx.currentFolder = path4.dirname(prevFile);
1841
- return output.join(`
1842
- `);
1843
- }
1844
1857
 
1845
1858
  // src/index.ts
1846
1859
  var noMoreFlags = false;
@@ -1872,7 +1885,7 @@ function createOutputFile(fileIndex, basename, content) {
1872
1885
  if (fileIndex > 0)
1873
1886
  basename = `${basename}-${fileIndex}`;
1874
1887
  const outFileName = args.length > 1 ? args[1] : `${basename}.src`;
1875
- const outFilePath = path5.join(outDirPath, outFileName);
1888
+ const outFilePath = path6.join(outDirPath, outFileName);
1876
1889
  fs3.writeFileSync(outFilePath, content);
1877
1890
  }
1878
1891
  if (command === "transpile") {
@@ -1881,7 +1894,7 @@ if (command === "transpile") {
1881
1894
  process.exit(2);
1882
1895
  }
1883
1896
  const entryFile = args[0];
1884
- const basename = path5.basename(entryFile, ".ts");
1897
+ const basename = path6.basename(entryFile, ".ts");
1885
1898
  const transpiledStatements = transpileProgram(entryFile);
1886
1899
  if (flags.includes("--print") || flags.includes("-p")) {
1887
1900
  console.log(transpiledStatements.join(`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grey-ts/transpiler",
3
- "version": "1.4.3",
3
+ "version": "1.4.4",
4
4
  "description": "Transpiles TypeScript into GreyScript",
5
5
  "author": "Okka",
6
6
  "module": "src/index.ts",
@@ -31,7 +31,7 @@
31
31
  ],
32
32
  "scripts": {
33
33
  "prepare": "bun run build",
34
- "build": "bun run scripts/build.ts"
34
+ "build": "bun test --only-failures && bun run scripts/build.ts"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/bun": "latest",