typescript-to-lua 0.42.0 → 1.1.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.
Files changed (129) hide show
  1. package/CHANGELOG.md +31 -1
  2. package/dist/CompilerOptions.d.ts +4 -3
  3. package/dist/LuaAST.js +4 -4
  4. package/dist/LuaLib.d.ts +7 -0
  5. package/dist/LuaLib.js +32 -1
  6. package/dist/LuaPrinter.d.ts +6 -2
  7. package/dist/LuaPrinter.js +25 -16
  8. package/dist/cli/diagnostics.js +2 -2
  9. package/dist/cli/parse.d.ts +1 -1
  10. package/dist/cli/parse.js +14 -0
  11. package/dist/cli/report.js +1 -1
  12. package/dist/cli/tsconfig.js +4 -4
  13. package/dist/lualib/Await.lua +59 -0
  14. package/dist/lualib/OptionalMethodCall.lua +3 -1
  15. package/dist/lualib/Promise.lua +172 -0
  16. package/dist/lualib/PromiseAll.lua +44 -0
  17. package/dist/lualib/PromiseAllSettled.lua +48 -0
  18. package/dist/lualib/PromiseAny.lua +41 -0
  19. package/dist/lualib/PromiseRace.lua +28 -0
  20. package/dist/lualib/SourceMapTraceBack.lua +20 -10
  21. package/dist/lualib/StringReplace.lua +8 -13
  22. package/dist/lualib/StringReplaceAll.lua +35 -0
  23. package/dist/lualib/lualib_bundle.lua +503 -62
  24. package/dist/transformation/builtins/array.js +29 -29
  25. package/dist/transformation/builtins/console.js +2 -2
  26. package/dist/transformation/builtins/function.js +9 -9
  27. package/dist/transformation/builtins/global.js +8 -8
  28. package/dist/transformation/builtins/index.js +35 -32
  29. package/dist/transformation/builtins/math.js +4 -4
  30. package/dist/transformation/builtins/number.js +7 -7
  31. package/dist/transformation/builtins/object.js +11 -11
  32. package/dist/transformation/builtins/promise.d.ts +7 -0
  33. package/dist/transformation/builtins/promise.js +46 -0
  34. package/dist/transformation/builtins/string.js +32 -30
  35. package/dist/transformation/builtins/symbol.js +3 -3
  36. package/dist/transformation/context/context.js +5 -5
  37. package/dist/transformation/index.js +1 -1
  38. package/dist/transformation/utils/annotations.d.ts +1 -4
  39. package/dist/transformation/utils/annotations.js +2 -39
  40. package/dist/transformation/utils/assignment-validation.js +8 -7
  41. package/dist/transformation/utils/diagnostics.d.ts +3 -0
  42. package/dist/transformation/utils/diagnostics.js +6 -5
  43. package/dist/transformation/utils/export.d.ts +3 -2
  44. package/dist/transformation/utils/export.js +14 -7
  45. package/dist/transformation/utils/function-context.js +19 -13
  46. package/dist/transformation/utils/lua-ast.js +18 -19
  47. package/dist/transformation/utils/lualib.js +1 -1
  48. package/dist/transformation/utils/safe-names.js +5 -5
  49. package/dist/transformation/utils/scope.d.ts +6 -0
  50. package/dist/transformation/utils/scope.js +70 -40
  51. package/dist/transformation/utils/symbols.js +6 -6
  52. package/dist/transformation/utils/transform.js +3 -3
  53. package/dist/transformation/utils/typescript/nodes.d.ts +2 -0
  54. package/dist/transformation/utils/typescript/nodes.js +21 -1
  55. package/dist/transformation/visitors/access.js +16 -16
  56. package/dist/transformation/visitors/async-await.d.ts +6 -0
  57. package/dist/transformation/visitors/async-await.js +38 -0
  58. package/dist/transformation/visitors/binary-expression/assignments.js +29 -29
  59. package/dist/transformation/visitors/binary-expression/bit.js +4 -4
  60. package/dist/transformation/visitors/binary-expression/compound.js +18 -18
  61. package/dist/transformation/visitors/binary-expression/destructuring-assignments.js +17 -17
  62. package/dist/transformation/visitors/binary-expression/index.js +21 -21
  63. package/dist/transformation/visitors/block.js +6 -6
  64. package/dist/transformation/visitors/break-continue.js +4 -9
  65. package/dist/transformation/visitors/call.js +38 -33
  66. package/dist/transformation/visitors/class/decorators.js +3 -3
  67. package/dist/transformation/visitors/class/index.js +41 -34
  68. package/dist/transformation/visitors/class/members/accessors.js +6 -6
  69. package/dist/transformation/visitors/class/members/constructor.js +7 -7
  70. package/dist/transformation/visitors/class/members/fields.js +6 -6
  71. package/dist/transformation/visitors/class/members/method.js +6 -6
  72. package/dist/transformation/visitors/class/new.js +13 -13
  73. package/dist/transformation/visitors/class/setup.js +11 -11
  74. package/dist/transformation/visitors/class/utils.js +2 -2
  75. package/dist/transformation/visitors/conditional.js +6 -6
  76. package/dist/transformation/visitors/delete.js +4 -4
  77. package/dist/transformation/visitors/enum.js +8 -8
  78. package/dist/transformation/visitors/errors.js +30 -31
  79. package/dist/transformation/visitors/expression-statement.js +10 -6
  80. package/dist/transformation/visitors/function.js +33 -21
  81. package/dist/transformation/visitors/identifier.js +25 -19
  82. package/dist/transformation/visitors/index.js +4 -2
  83. package/dist/transformation/visitors/language-extensions/iterable.js +7 -7
  84. package/dist/transformation/visitors/language-extensions/multi.d.ts +1 -1
  85. package/dist/transformation/visitors/language-extensions/multi.js +5 -7
  86. package/dist/transformation/visitors/language-extensions/operators.js +6 -6
  87. package/dist/transformation/visitors/language-extensions/range.js +6 -6
  88. package/dist/transformation/visitors/language-extensions/table.js +5 -5
  89. package/dist/transformation/visitors/literal.d.ts +1 -2
  90. package/dist/transformation/visitors/literal.js +19 -42
  91. package/dist/transformation/visitors/loops/do-while.js +2 -2
  92. package/dist/transformation/visitors/loops/for-in.js +4 -4
  93. package/dist/transformation/visitors/loops/for-of.js +13 -13
  94. package/dist/transformation/visitors/loops/for.js +3 -3
  95. package/dist/transformation/visitors/loops/utils.js +9 -9
  96. package/dist/transformation/visitors/modules/export.js +13 -14
  97. package/dist/transformation/visitors/modules/import.js +10 -10
  98. package/dist/transformation/visitors/namespace.js +13 -13
  99. package/dist/transformation/visitors/return.d.ts +1 -0
  100. package/dist/transformation/visitors/return.js +41 -49
  101. package/dist/transformation/visitors/sourceFile.js +8 -8
  102. package/dist/transformation/visitors/spread.js +16 -11
  103. package/dist/transformation/visitors/switch.js +132 -30
  104. package/dist/transformation/visitors/template.js +5 -5
  105. package/dist/transformation/visitors/typeof.js +2 -2
  106. package/dist/transformation/visitors/typescript.js +1 -1
  107. package/dist/transformation/visitors/unary-expression.js +9 -9
  108. package/dist/transformation/visitors/variable-declaration.js +28 -28
  109. package/dist/transformation/visitors/void.d.ts +6 -0
  110. package/dist/transformation/visitors/void.js +23 -0
  111. package/dist/transpilation/bundle.d.ts +3 -0
  112. package/dist/transpilation/bundle.js +56 -13
  113. package/dist/transpilation/diagnostics.js +3 -3
  114. package/dist/transpilation/index.js +2 -2
  115. package/dist/transpilation/output-collector.js +2 -2
  116. package/dist/transpilation/plugins.js +1 -1
  117. package/dist/transpilation/resolve.js +28 -15
  118. package/dist/transpilation/transformers.d.ts +1 -0
  119. package/dist/transpilation/transformers.js +61 -25
  120. package/dist/transpilation/transpile.js +7 -7
  121. package/dist/transpilation/transpiler.js +8 -8
  122. package/dist/transpilation/utils.js +3 -2
  123. package/dist/tstl.js +8 -8
  124. package/dist/utils.js +2 -2
  125. package/package.json +15 -12
  126. package/dist/transformation/visitors/jsx/jsx.d.ts +0 -9
  127. package/dist/transformation/visitors/jsx/jsx.js +0 -237
  128. package/dist/transformation/visitors/jsx/xhtml.d.ts +0 -3
  129. package/dist/transformation/visitors/jsx/xhtml.js +0 -260
@@ -454,6 +454,106 @@ function __TS__ArraySetLength(arr, length)
454
454
  return length
455
455
  end
456
456
 
457
+ ____symbolMetatable = {
458
+ __tostring = function(self)
459
+ return ("Symbol(" .. (self.description or "")) .. ")"
460
+ end
461
+ }
462
+ function __TS__Symbol(description)
463
+ return setmetatable({description = description}, ____symbolMetatable)
464
+ end
465
+ Symbol = {
466
+ iterator = __TS__Symbol("Symbol.iterator"),
467
+ hasInstance = __TS__Symbol("Symbol.hasInstance"),
468
+ species = __TS__Symbol("Symbol.species"),
469
+ toStringTag = __TS__Symbol("Symbol.toStringTag")
470
+ }
471
+
472
+ function __TS__InstanceOf(obj, classTbl)
473
+ if type(classTbl) ~= "table" then
474
+ error("Right-hand side of 'instanceof' is not an object", 0)
475
+ end
476
+ if classTbl[Symbol.hasInstance] ~= nil then
477
+ return not (not classTbl[Symbol.hasInstance](classTbl, obj))
478
+ end
479
+ if type(obj) == "table" then
480
+ local luaClass = obj.constructor
481
+ while luaClass ~= nil do
482
+ if luaClass == classTbl then
483
+ return true
484
+ end
485
+ luaClass = luaClass.____super
486
+ end
487
+ end
488
+ return false
489
+ end
490
+
491
+ function __TS__New(target, ...)
492
+ local instance = setmetatable({}, target.prototype)
493
+ instance:____constructor(...)
494
+ return instance
495
+ end
496
+
497
+ function __TS__AsyncAwaiter(generator)
498
+ return __TS__New(
499
+ __TS__Promise,
500
+ function(____, resolve, reject)
501
+ local adopt, fulfilled, rejected, step, asyncCoroutine
502
+ function adopt(self, value)
503
+ return ((__TS__InstanceOf(value, __TS__Promise) and (function() return value end)) or (function() return __TS__Promise.resolve(value) end))()
504
+ end
505
+ function fulfilled(self, value)
506
+ local success, errorOrErrorHandler, resultOrError = coroutine.resume(asyncCoroutine, value)
507
+ if success then
508
+ step(_G, resultOrError, errorOrErrorHandler)
509
+ else
510
+ reject(_G, resultOrError)
511
+ end
512
+ end
513
+ function rejected(self, handler)
514
+ if handler then
515
+ return function(____, value)
516
+ local success, valueOrError = pcall(handler, value)
517
+ if success then
518
+ step(_G, valueOrError, handler)
519
+ else
520
+ reject(_G, valueOrError)
521
+ end
522
+ end
523
+ else
524
+ return function(____, value)
525
+ reject(_G, value)
526
+ end
527
+ end
528
+ end
529
+ function step(self, result, errorHandler)
530
+ if coroutine.status(asyncCoroutine) == "dead" then
531
+ resolve(_G, result)
532
+ else
533
+ (function()
534
+ local ____self = adopt(_G, result)
535
+ return ____self["then"](
536
+ ____self,
537
+ fulfilled,
538
+ rejected(_G, errorHandler)
539
+ )
540
+ end)()
541
+ end
542
+ end
543
+ asyncCoroutine = coroutine.create(generator)
544
+ local success, errorOrErrorHandler, resultOrError = coroutine.resume(asyncCoroutine)
545
+ if success then
546
+ step(_G, resultOrError, errorOrErrorHandler)
547
+ else
548
+ reject(_G, errorOrErrorHandler)
549
+ end
550
+ end
551
+ )
552
+ end
553
+ function __TS__Await(errorHandler, thing)
554
+ return coroutine.yield(errorHandler, thing)
555
+ end
556
+
457
557
  function __TS__Class(self)
458
558
  local c = {prototype = {}}
459
559
  c.prototype.__index = c.prototype
@@ -722,12 +822,6 @@ function __TS__DelegatedYield(iterable)
722
822
  end
723
823
  end
724
824
 
725
- function __TS__New(target, ...)
726
- local instance = setmetatable({}, target.prototype)
727
- instance:____constructor(...)
728
- return instance
729
- end
730
-
731
825
  function __TS__GetErrorStack(self, constructor)
732
826
  local level = 1
733
827
  while true do
@@ -824,21 +918,6 @@ function __TS__FunctionBind(fn, thisArg, ...)
824
918
  end
825
919
  end
826
920
 
827
- ____symbolMetatable = {
828
- __tostring = function(self)
829
- return ("Symbol(" .. (self.description or "")) .. ")"
830
- end
831
- }
832
- function __TS__Symbol(description)
833
- return setmetatable({description = description}, ____symbolMetatable)
834
- end
835
- Symbol = {
836
- iterator = __TS__Symbol("Symbol.iterator"),
837
- hasInstance = __TS__Symbol("Symbol.hasInstance"),
838
- species = __TS__Symbol("Symbol.species"),
839
- toStringTag = __TS__Symbol("Symbol.toStringTag")
840
- }
841
-
842
921
  function __TS__GeneratorIterator(self)
843
922
  return self
844
923
  end
@@ -872,25 +951,6 @@ function __TS__Generator(fn)
872
951
  end
873
952
  end
874
953
 
875
- function __TS__InstanceOf(obj, classTbl)
876
- if type(classTbl) ~= "table" then
877
- error("Right-hand side of 'instanceof' is not an object", 0)
878
- end
879
- if classTbl[Symbol.hasInstance] ~= nil then
880
- return not (not classTbl[Symbol.hasInstance](classTbl, obj))
881
- end
882
- if type(obj) == "table" then
883
- local luaClass = obj.constructor
884
- while luaClass ~= nil do
885
- if luaClass == classTbl then
886
- return true
887
- end
888
- luaClass = luaClass.____super
889
- end
890
- end
891
- return false
892
- end
893
-
894
954
  function __TS__InstanceOfObject(value)
895
955
  local valueType = type(value)
896
956
  return (valueType == "table") or (valueType == "function")
@@ -1257,11 +1317,13 @@ function __TS__OptionalFunctionCall(f, ...)
1257
1317
  return nil
1258
1318
  end
1259
1319
 
1260
- function __TS__OptionalMethodCall(____table, methodName, ...)
1320
+ function __TS__OptionalMethodCall(____table, methodName, isMethodOptional, ...)
1261
1321
  if ____table then
1262
1322
  local method = ____table[methodName]
1263
1323
  if method then
1264
1324
  return method(____table, ...)
1325
+ elseif not isMethodOptional then
1326
+ error(methodName .. " is not a function", 0)
1265
1327
  end
1266
1328
  end
1267
1329
  return nil
@@ -1341,6 +1403,344 @@ function __TS__ParseInt(numberString, base)
1341
1403
  end
1342
1404
  end
1343
1405
 
1406
+ __TS__PromiseState = __TS__PromiseState or ({})
1407
+ __TS__PromiseState.Pending = 0
1408
+ __TS__PromiseState[__TS__PromiseState.Pending] = "Pending"
1409
+ __TS__PromiseState.Fulfilled = 1
1410
+ __TS__PromiseState[__TS__PromiseState.Fulfilled] = "Fulfilled"
1411
+ __TS__PromiseState.Rejected = 2
1412
+ __TS__PromiseState[__TS__PromiseState.Rejected] = "Rejected"
1413
+ function __TS__PromiseDeferred(self)
1414
+ local resolve
1415
+ local reject
1416
+ local promise = __TS__New(
1417
+ __TS__Promise,
1418
+ function(____, res, rej)
1419
+ resolve = res
1420
+ reject = rej
1421
+ end
1422
+ )
1423
+ return {promise = promise, resolve = resolve, reject = reject}
1424
+ end
1425
+ function __TS__IsPromiseLike(self, thing)
1426
+ return __TS__InstanceOf(thing, __TS__Promise)
1427
+ end
1428
+ __TS__Promise = __TS__Class()
1429
+ __TS__Promise.name = "__TS__Promise"
1430
+ function __TS__Promise.prototype.____constructor(self, executor)
1431
+ self.state = __TS__PromiseState.Pending
1432
+ self.fulfilledCallbacks = {}
1433
+ self.rejectedCallbacks = {}
1434
+ self.finallyCallbacks = {}
1435
+ do
1436
+ local function ____catch(e)
1437
+ self:reject(e)
1438
+ end
1439
+ local ____try, ____hasReturned = pcall(
1440
+ function()
1441
+ executor(
1442
+ _G,
1443
+ __TS__FunctionBind(self.resolve, self),
1444
+ __TS__FunctionBind(self.reject, self)
1445
+ )
1446
+ end
1447
+ )
1448
+ if not ____try then
1449
+ ____hasReturned, ____returnValue = ____catch(____hasReturned)
1450
+ end
1451
+ end
1452
+ end
1453
+ function __TS__Promise.resolve(data)
1454
+ local promise = __TS__New(
1455
+ __TS__Promise,
1456
+ function()
1457
+ end
1458
+ )
1459
+ promise.state = __TS__PromiseState.Fulfilled
1460
+ promise.value = data
1461
+ return promise
1462
+ end
1463
+ function __TS__Promise.reject(reason)
1464
+ local promise = __TS__New(
1465
+ __TS__Promise,
1466
+ function()
1467
+ end
1468
+ )
1469
+ promise.state = __TS__PromiseState.Rejected
1470
+ promise.rejectionReason = reason
1471
+ return promise
1472
+ end
1473
+ __TS__Promise.prototype["then"] = function(self, onFulfilled, onRejected)
1474
+ local ____ = __TS__PromiseDeferred(_G)
1475
+ local promise = ____.promise
1476
+ local resolve = ____.resolve
1477
+ local reject = ____.reject
1478
+ local isFulfilled = self.state == __TS__PromiseState.Fulfilled
1479
+ local isRejected = self.state == __TS__PromiseState.Rejected
1480
+ if onFulfilled then
1481
+ local internalCallback = self:createPromiseResolvingCallback(onFulfilled, resolve, reject)
1482
+ __TS__ArrayPush(self.fulfilledCallbacks, internalCallback)
1483
+ if isFulfilled then
1484
+ internalCallback(_G, self.value)
1485
+ end
1486
+ else
1487
+ __TS__ArrayPush(
1488
+ self.fulfilledCallbacks,
1489
+ function() return resolve(_G, nil) end
1490
+ )
1491
+ end
1492
+ if onRejected then
1493
+ local internalCallback = self:createPromiseResolvingCallback(onRejected, resolve, reject)
1494
+ __TS__ArrayPush(self.rejectedCallbacks, internalCallback)
1495
+ if isRejected then
1496
+ internalCallback(_G, self.rejectionReason)
1497
+ end
1498
+ end
1499
+ if isFulfilled then
1500
+ resolve(_G, self.value)
1501
+ end
1502
+ if isRejected then
1503
+ reject(_G, self.rejectionReason)
1504
+ end
1505
+ return promise
1506
+ end
1507
+ function __TS__Promise.prototype.catch(self, onRejected)
1508
+ return self["then"](self, nil, onRejected)
1509
+ end
1510
+ function __TS__Promise.prototype.finally(self, onFinally)
1511
+ if onFinally then
1512
+ __TS__ArrayPush(self.finallyCallbacks, onFinally)
1513
+ if self.state ~= __TS__PromiseState.Pending then
1514
+ onFinally(_G)
1515
+ end
1516
+ end
1517
+ return self
1518
+ end
1519
+ function __TS__Promise.prototype.resolve(self, data)
1520
+ if self.state == __TS__PromiseState.Pending then
1521
+ self.state = __TS__PromiseState.Fulfilled
1522
+ self.value = data
1523
+ for ____, callback in ipairs(self.fulfilledCallbacks) do
1524
+ callback(_G, data)
1525
+ end
1526
+ for ____, callback in ipairs(self.finallyCallbacks) do
1527
+ callback(_G)
1528
+ end
1529
+ end
1530
+ end
1531
+ function __TS__Promise.prototype.reject(self, reason)
1532
+ if self.state == __TS__PromiseState.Pending then
1533
+ self.state = __TS__PromiseState.Rejected
1534
+ self.rejectionReason = reason
1535
+ for ____, callback in ipairs(self.rejectedCallbacks) do
1536
+ callback(_G, reason)
1537
+ end
1538
+ for ____, callback in ipairs(self.finallyCallbacks) do
1539
+ callback(_G)
1540
+ end
1541
+ end
1542
+ end
1543
+ function __TS__Promise.prototype.createPromiseResolvingCallback(self, f, resolve, reject)
1544
+ return function(____, value)
1545
+ do
1546
+ local function ____catch(e)
1547
+ reject(_G, e)
1548
+ end
1549
+ local ____try, ____hasReturned = pcall(
1550
+ function()
1551
+ self:handleCallbackData(
1552
+ f(_G, value),
1553
+ resolve,
1554
+ reject
1555
+ )
1556
+ end
1557
+ )
1558
+ if not ____try then
1559
+ ____hasReturned, ____returnValue = ____catch(____hasReturned)
1560
+ end
1561
+ end
1562
+ end
1563
+ end
1564
+ function __TS__Promise.prototype.handleCallbackData(self, data, resolve, reject)
1565
+ if __TS__IsPromiseLike(_G, data) then
1566
+ local nextpromise = data
1567
+ if nextpromise.state == __TS__PromiseState.Fulfilled then
1568
+ resolve(_G, nextpromise.value)
1569
+ elseif nextpromise.state == __TS__PromiseState.Rejected then
1570
+ reject(_G, nextpromise.rejectionReason)
1571
+ else
1572
+ data["then"](data, resolve, reject)
1573
+ end
1574
+ else
1575
+ resolve(_G, data)
1576
+ end
1577
+ end
1578
+
1579
+ function __TS__PromiseAll(iterable)
1580
+ local results = {}
1581
+ local toResolve = {}
1582
+ local numToResolve = 0
1583
+ local i = 0
1584
+ for ____, item in __TS__Iterator(iterable) do
1585
+ if __TS__InstanceOf(item, __TS__Promise) then
1586
+ if item.state == __TS__PromiseState.Fulfilled then
1587
+ results[i + 1] = item.value
1588
+ elseif item.state == __TS__PromiseState.Rejected then
1589
+ return __TS__Promise.reject(item.rejectionReason)
1590
+ else
1591
+ numToResolve = numToResolve + 1
1592
+ toResolve[i] = item
1593
+ end
1594
+ else
1595
+ results[i + 1] = item
1596
+ end
1597
+ i = i + 1
1598
+ end
1599
+ if numToResolve == 0 then
1600
+ return __TS__Promise.resolve(results)
1601
+ end
1602
+ return __TS__New(
1603
+ __TS__Promise,
1604
+ function(____, resolve, reject)
1605
+ for index, promise in pairs(toResolve) do
1606
+ promise["then"](
1607
+ promise,
1608
+ function(____, data)
1609
+ results[index + 1] = data
1610
+ numToResolve = numToResolve - 1
1611
+ if numToResolve == 0 then
1612
+ resolve(_G, results)
1613
+ end
1614
+ end,
1615
+ function(____, reason)
1616
+ reject(_G, reason)
1617
+ end
1618
+ )
1619
+ end
1620
+ end
1621
+ )
1622
+ end
1623
+
1624
+ function __TS__PromiseAllSettled(iterable)
1625
+ local results = {}
1626
+ local toResolve = {}
1627
+ local numToResolve = 0
1628
+ local i = 0
1629
+ for ____, item in __TS__Iterator(iterable) do
1630
+ if __TS__InstanceOf(item, __TS__Promise) then
1631
+ if item.state == __TS__PromiseState.Fulfilled then
1632
+ results[i + 1] = {status = "fulfilled", value = item.value}
1633
+ elseif item.state == __TS__PromiseState.Rejected then
1634
+ results[i + 1] = {status = "rejected", reason = item.rejectionReason}
1635
+ else
1636
+ numToResolve = numToResolve + 1
1637
+ toResolve[i] = item
1638
+ end
1639
+ else
1640
+ results[i + 1] = {status = "fulfilled", value = item}
1641
+ end
1642
+ i = i + 1
1643
+ end
1644
+ if numToResolve == 0 then
1645
+ return __TS__Promise.resolve(results)
1646
+ end
1647
+ return __TS__New(
1648
+ __TS__Promise,
1649
+ function(____, resolve)
1650
+ for index, promise in pairs(toResolve) do
1651
+ promise["then"](
1652
+ promise,
1653
+ function(____, data)
1654
+ results[index + 1] = {status = "fulfilled", value = data}
1655
+ numToResolve = numToResolve - 1
1656
+ if numToResolve == 0 then
1657
+ resolve(_G, results)
1658
+ end
1659
+ end,
1660
+ function(____, reason)
1661
+ results[index + 1] = {status = "rejected", reason = reason}
1662
+ numToResolve = numToResolve - 1
1663
+ if numToResolve == 0 then
1664
+ resolve(_G, results)
1665
+ end
1666
+ end
1667
+ )
1668
+ end
1669
+ end
1670
+ )
1671
+ end
1672
+
1673
+ function __TS__PromiseAny(iterable)
1674
+ local rejections = {}
1675
+ local pending = {}
1676
+ for ____, item in __TS__Iterator(iterable) do
1677
+ if __TS__InstanceOf(item, __TS__Promise) then
1678
+ if item.state == __TS__PromiseState.Fulfilled then
1679
+ return __TS__Promise.resolve(item.value)
1680
+ elseif item.state == __TS__PromiseState.Rejected then
1681
+ __TS__ArrayPush(rejections, item.rejectionReason)
1682
+ else
1683
+ __TS__ArrayPush(pending, item)
1684
+ end
1685
+ else
1686
+ return __TS__Promise.resolve(item)
1687
+ end
1688
+ end
1689
+ if #pending == 0 then
1690
+ return __TS__Promise.reject("No promises to resolve with .any()")
1691
+ end
1692
+ local numResolved = 0
1693
+ return __TS__New(
1694
+ __TS__Promise,
1695
+ function(____, resolve, reject)
1696
+ for ____, promise in ipairs(pending) do
1697
+ promise["then"](
1698
+ promise,
1699
+ function(____, data)
1700
+ resolve(_G, data)
1701
+ end,
1702
+ function(____, reason)
1703
+ __TS__ArrayPush(rejections, reason)
1704
+ numResolved = numResolved + 1
1705
+ if numResolved == #pending then
1706
+ reject(_G, {name = "AggregateError", message = "All Promises rejected", errors = rejections})
1707
+ end
1708
+ end
1709
+ )
1710
+ end
1711
+ end
1712
+ )
1713
+ end
1714
+
1715
+ function __TS__PromiseRace(iterable)
1716
+ local pending = {}
1717
+ for ____, item in __TS__Iterator(iterable) do
1718
+ if __TS__InstanceOf(item, __TS__Promise) then
1719
+ if item.state == __TS__PromiseState.Fulfilled then
1720
+ return __TS__Promise.resolve(item.value)
1721
+ elseif item.state == __TS__PromiseState.Rejected then
1722
+ return __TS__Promise.reject(item.rejectionReason)
1723
+ else
1724
+ __TS__ArrayPush(pending, item)
1725
+ end
1726
+ else
1727
+ return __TS__Promise.resolve(item)
1728
+ end
1729
+ end
1730
+ return __TS__New(
1731
+ __TS__Promise,
1732
+ function(____, resolve, reject)
1733
+ for ____, promise in ipairs(pending) do
1734
+ promise["then"](
1735
+ promise,
1736
+ function(____, value) return resolve(_G, value) end,
1737
+ function(____, reason) return reject(_G, reason) end
1738
+ )
1739
+ end
1740
+ end
1741
+ )
1742
+ end
1743
+
1344
1744
  Set = (function()
1345
1745
  local Set = __TS__Class()
1346
1746
  Set.name = "Set"
@@ -1570,23 +1970,33 @@ function __TS__SourceMapTraceBack(fileName, sourceMap)
1570
1970
  debug.traceback = function(thread, message, level)
1571
1971
  local trace
1572
1972
  if ((thread == nil) and (message == nil)) and (level == nil) then
1573
- trace = _G:__TS__originalTraceback()
1973
+ trace = _G.__TS__originalTraceback()
1574
1974
  else
1575
- trace = _G:__TS__originalTraceback(thread, message, level)
1975
+ trace = _G.__TS__originalTraceback(thread, message, level)
1576
1976
  end
1577
1977
  if type(trace) ~= "string" then
1578
1978
  return trace
1579
1979
  end
1580
- local result = string.gsub(
1581
- trace,
1582
- "(%S+).lua:(%d+)",
1583
- function(file, line)
1584
- local fileSourceMap = _G.__TS__sourcemap[file .. ".lua"]
1585
- if fileSourceMap and fileSourceMap[line] then
1586
- return (file .. ".ts:") .. tostring(fileSourceMap[line])
1980
+ local function replacer(____, file, srcFile, line)
1981
+ local fileSourceMap = _G.__TS__sourcemap[file]
1982
+ if fileSourceMap and fileSourceMap[line] then
1983
+ local data = fileSourceMap[line]
1984
+ if type(data) == "number" then
1985
+ return (srcFile .. ":") .. tostring(data)
1587
1986
  end
1588
- return (file .. ".lua:") .. line
1987
+ return (tostring(data.file) .. ":") .. tostring(data.line)
1589
1988
  end
1989
+ return (file .. ":") .. line
1990
+ end
1991
+ local result = string.gsub(
1992
+ trace,
1993
+ "(%S+)%.lua:(%d+)",
1994
+ function(file, line) return replacer(_G, file .. ".lua", file .. ".ts", line) end
1995
+ )
1996
+ result = string.gsub(
1997
+ result,
1998
+ "(%[string \"[^\"]+\"%]):(%d+)",
1999
+ function(file, line) return replacer(_G, file, "unknown", line) end
1590
2000
  )
1591
2001
  return result
1592
2002
  end
@@ -1712,20 +2122,51 @@ function __TS__StringPadStart(self, maxLength, fillString)
1712
2122
  end
1713
2123
 
1714
2124
  function __TS__StringReplace(source, searchValue, replaceValue)
1715
- searchValue = string.gsub(searchValue, "[%%%(%)%.%+%-%*%?%[%^%$]", "%%%1")
2125
+ local startPos, endPos = string.find(source, searchValue, nil, true)
2126
+ if not startPos then
2127
+ return source
2128
+ end
2129
+ local sub = string.sub
2130
+ local before = sub(source, 1, startPos - 1)
2131
+ local replacement = (((type(replaceValue) == "string") and (function() return replaceValue end)) or (function() return replaceValue(_G, searchValue, startPos - 1, source) end))()
2132
+ local after = sub(source, endPos + 1)
2133
+ return (before .. replacement) .. after
2134
+ end
2135
+
2136
+ function __TS__StringReplaceAll(source, searchValue, replaceValue)
2137
+ local replacer
1716
2138
  if type(replaceValue) == "string" then
1717
- replaceValue = string.gsub(replaceValue, "%%", "%%%%")
1718
- local result = string.gsub(source, searchValue, replaceValue, 1)
1719
- return result
2139
+ replacer = function() return replaceValue end
1720
2140
  else
1721
- local result = string.gsub(
1722
- source,
1723
- searchValue,
1724
- function(match) return replaceValue(_G, match) end,
1725
- 1
1726
- )
1727
- return result
2141
+ replacer = replaceValue
2142
+ end
2143
+ local parts = {}
2144
+ local partsIndex = 1
2145
+ local sub = string.sub
2146
+ if #searchValue == 0 then
2147
+ parts[1] = replacer(_G, "", 0, source)
2148
+ partsIndex = 2
2149
+ for i = 1, #source do
2150
+ parts[partsIndex] = sub(source, i, i)
2151
+ parts[partsIndex + 1] = replacer(_G, "", i, source)
2152
+ partsIndex = partsIndex + 2
2153
+ end
2154
+ else
2155
+ local find = string.find
2156
+ local currentPos = 1
2157
+ while true do
2158
+ local startPos, endPos = find(source, searchValue, currentPos, true)
2159
+ if not startPos then
2160
+ break
2161
+ end
2162
+ parts[partsIndex] = sub(source, currentPos, startPos - 1)
2163
+ parts[partsIndex + 1] = replacer(_G, searchValue, startPos - 1, source)
2164
+ partsIndex = partsIndex + 2
2165
+ currentPos = endPos + 1
2166
+ end
2167
+ parts[partsIndex] = sub(source, currentPos)
1728
2168
  end
2169
+ return table.concat(parts)
1729
2170
  end
1730
2171
 
1731
2172
  function __TS__StringSlice(self, start, ____end)