brighterscript 1.0.0-alpha.16 → 1.0.0-alpha.17

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 (88) hide show
  1. package/CHANGELOG.md +42 -1
  2. package/README.md +28 -9
  3. package/dist/Logger.js +5 -5
  4. package/dist/Logger.js.map +1 -1
  5. package/dist/Program.d.ts +2 -2
  6. package/dist/Program.js +3 -3
  7. package/dist/Program.js.map +1 -1
  8. package/dist/ProgramBuilder.js +1 -1
  9. package/dist/ProgramBuilder.js.map +1 -1
  10. package/dist/Scope.js +13 -9
  11. package/dist/Scope.js.map +1 -1
  12. package/dist/XmlScope.js +1 -1
  13. package/dist/XmlScope.js.map +1 -1
  14. package/dist/astUtils/creators.d.ts +3 -3
  15. package/dist/astUtils/creators.js +8 -8
  16. package/dist/astUtils/creators.js.map +1 -1
  17. package/dist/astUtils/creators.spec.js +10 -0
  18. package/dist/astUtils/creators.spec.js.map +1 -1
  19. package/dist/astUtils/reflection.d.ts +2 -1
  20. package/dist/astUtils/reflection.js +5 -1
  21. package/dist/astUtils/reflection.js.map +1 -1
  22. package/dist/astUtils/visitors.spec.js +2 -0
  23. package/dist/astUtils/visitors.spec.js.map +1 -1
  24. package/dist/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.js +2 -2
  25. package/dist/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.js.map +1 -1
  26. package/dist/bscPlugin/transpile/BrsFilePreTranspileProcessor.js +7 -3
  27. package/dist/bscPlugin/transpile/BrsFilePreTranspileProcessor.js.map +1 -1
  28. package/dist/bscPlugin/transpile/BrsFilePreTranspileProcessor.spec.d.ts +1 -0
  29. package/dist/bscPlugin/transpile/BrsFilePreTranspileProcessor.spec.js +32 -0
  30. package/dist/bscPlugin/transpile/BrsFilePreTranspileProcessor.spec.js.map +1 -0
  31. package/dist/examples/plugins/removePrint.js +12 -14
  32. package/dist/examples/plugins/removePrint.js.map +1 -1
  33. package/dist/files/BrsFile.Class.spec.js +1 -1
  34. package/dist/files/BrsFile.d.ts +1 -2
  35. package/dist/files/BrsFile.js +24 -20
  36. package/dist/files/BrsFile.js.map +1 -1
  37. package/dist/files/BrsFile.spec.js +171 -64
  38. package/dist/files/BrsFile.spec.js.map +1 -1
  39. package/dist/parser/Expression.d.ts +68 -6
  40. package/dist/parser/Expression.js +133 -18
  41. package/dist/parser/Expression.js.map +1 -1
  42. package/dist/parser/Parser.Class.spec.js +1 -1
  43. package/dist/parser/Parser.Class.spec.js.map +1 -1
  44. package/dist/parser/Parser.d.ts +1 -1
  45. package/dist/parser/Parser.js +49 -46
  46. package/dist/parser/Parser.js.map +1 -1
  47. package/dist/parser/Parser.spec.js +57 -2
  48. package/dist/parser/Parser.spec.js.map +1 -1
  49. package/dist/parser/Statement.d.ts +17 -13
  50. package/dist/parser/Statement.js +36 -26
  51. package/dist/parser/Statement.js.map +1 -1
  52. package/dist/types/BooleanType.d.ts +4 -2
  53. package/dist/types/BooleanType.js +5 -1
  54. package/dist/types/BooleanType.js.map +1 -1
  55. package/dist/types/DoubleType.d.ts +2 -0
  56. package/dist/types/DoubleType.js +5 -1
  57. package/dist/types/DoubleType.js.map +1 -1
  58. package/dist/types/DynamicType.d.ts +2 -0
  59. package/dist/types/DynamicType.js +5 -1
  60. package/dist/types/DynamicType.js.map +1 -1
  61. package/dist/types/FloatType.d.ts +3 -1
  62. package/dist/types/FloatType.js +5 -1
  63. package/dist/types/FloatType.js.map +1 -1
  64. package/dist/types/IntegerType.d.ts +3 -1
  65. package/dist/types/IntegerType.js +5 -1
  66. package/dist/types/IntegerType.js.map +1 -1
  67. package/dist/types/InvalidType.d.ts +4 -2
  68. package/dist/types/InvalidType.js +5 -1
  69. package/dist/types/InvalidType.js.map +1 -1
  70. package/dist/types/LongIntegerType.d.ts +3 -1
  71. package/dist/types/LongIntegerType.js +5 -1
  72. package/dist/types/LongIntegerType.js.map +1 -1
  73. package/dist/types/ObjectType.d.ts +2 -1
  74. package/dist/types/ObjectType.js +4 -2
  75. package/dist/types/ObjectType.js.map +1 -1
  76. package/dist/types/StringType.d.ts +4 -2
  77. package/dist/types/StringType.js +5 -1
  78. package/dist/types/StringType.js.map +1 -1
  79. package/dist/types/UninitializedType.js.map +1 -1
  80. package/dist/types/VoidType.d.ts +4 -2
  81. package/dist/types/VoidType.js +5 -1
  82. package/dist/types/VoidType.js.map +1 -1
  83. package/dist/util.d.ts +1 -1
  84. package/dist/util.js +22 -33
  85. package/dist/util.js.map +1 -1
  86. package/dist/validators/ClassValidator.js +3 -3
  87. package/dist/validators/ClassValidator.js.map +1 -1
  88. package/package.json +2 -2
@@ -246,13 +246,12 @@ class Parser {
246
246
  interfaceFieldStatement() {
247
247
  const name = this.identifier(...TokenKind_1.AllowedProperties);
248
248
  let asToken = this.consumeToken(TokenKind_1.TokenKind.As);
249
- let typeToken = this.typeToken();
250
- const type = util_1.util.tokenToBscType(typeToken);
251
- if (!type) {
252
- this.diagnostics.push(Object.assign(Object.assign({}, DiagnosticMessages_1.DiagnosticMessages.functionParameterTypeIsInvalid(name.text, typeToken.text)), { range: typeToken.range }));
249
+ let typeExpr = this.typeExpression();
250
+ if (!typeExpr.isValidType()) {
251
+ this.diagnostics.push(Object.assign(Object.assign({}, DiagnosticMessages_1.DiagnosticMessages.functionParameterTypeIsInvalid(name.text, typeExpr.getText())), { range: typeExpr.range }));
253
252
  throw this.lastDiagnosticAsError();
254
253
  }
255
- return new Statement_1.InterfaceFieldStatement(name, asToken, typeToken, type);
254
+ return new Statement_1.InterfaceFieldStatement(name, asToken, typeExpr);
256
255
  }
257
256
  /**
258
257
  * Create a new InterfaceMethodStatement. This should only be called from within `interfaceDeclaration()`
@@ -264,17 +263,16 @@ class Parser {
264
263
  const params = [];
265
264
  const rightParen = this.consumeToken(TokenKind_1.TokenKind.RightParen);
266
265
  let asToken = null;
267
- let returnTypeToken = null;
266
+ let returnTypeExpr;
268
267
  if (this.check(TokenKind_1.TokenKind.As)) {
269
268
  asToken = this.advance();
270
- returnTypeToken = this.typeToken();
271
- const returnType = util_1.util.tokenToBscType(returnTypeToken);
272
- if (!returnType) {
273
- this.diagnostics.push(Object.assign(Object.assign({}, DiagnosticMessages_1.DiagnosticMessages.functionParameterTypeIsInvalid(name.text, returnTypeToken.text)), { range: returnTypeToken.range }));
269
+ returnTypeExpr = this.typeExpression();
270
+ if (!returnTypeExpr.isValidType(this.options.mode)) {
271
+ this.diagnostics.push(Object.assign(Object.assign({}, DiagnosticMessages_1.DiagnosticMessages.functionParameterTypeIsInvalid(name.text, returnTypeExpr.getText())), { range: returnTypeExpr.range }));
274
272
  throw this.lastDiagnosticAsError();
275
273
  }
276
274
  }
277
- return new Statement_1.InterfaceMethodStatement(functionType, name, leftParen, params, rightParen, asToken, returnTypeToken, util_1.util.tokenToBscType(returnTypeToken));
275
+ return new Statement_1.InterfaceMethodStatement(functionType, name, leftParen, params, rightParen, asToken, returnTypeExpr);
278
276
  }
279
277
  interfaceDeclaration() {
280
278
  this.warnIfNotBrighterScriptMode('interface declarations');
@@ -476,13 +474,13 @@ class Parser {
476
474
  classFieldDeclaration(accessModifier) {
477
475
  let name = this.consume(DiagnosticMessages_1.DiagnosticMessages.expectedClassFieldIdentifier(), TokenKind_1.TokenKind.Identifier, ...TokenKind_1.AllowedProperties);
478
476
  let asToken;
479
- let fieldType;
477
+ let fieldTypeExpr;
480
478
  //look for `as SOME_TYPE`
481
479
  if (this.check(TokenKind_1.TokenKind.As)) {
482
480
  asToken = this.advance();
483
- fieldType = this.typeToken();
481
+ fieldTypeExpr = this.typeExpression();
484
482
  //no field type specified
485
- if (!util_1.util.tokenToBscType(fieldType, true, this.currentNamespaceName)) {
483
+ if (!fieldTypeExpr.isValidType(this.options.mode)) {
486
484
  this.diagnostics.push(Object.assign(Object.assign({}, DiagnosticMessages_1.DiagnosticMessages.expectedValidTypeToFollowAsKeyword()), { range: this.peek().range }));
487
485
  }
488
486
  }
@@ -493,7 +491,7 @@ class Parser {
493
491
  equal = this.advance();
494
492
  initialValue = this.expression();
495
493
  }
496
- return new Statement_1.ClassFieldStatement(accessModifier, name, asToken, fieldType, equal, initialValue, this.currentNamespaceName);
494
+ return new Statement_1.ClassFieldStatement(accessModifier, name, asToken, fieldTypeExpr, equal, initialValue, this.currentNamespaceName);
497
495
  }
498
496
  functionDeclaration(isAnonymous, checkIdentifier = true, forClassMethod = false) {
499
497
  var _a, _b, _c, _d;
@@ -543,7 +541,7 @@ class Parser {
543
541
  }
544
542
  let params = [];
545
543
  let asToken;
546
- let typeToken;
544
+ let typeExpr;
547
545
  if (!this.check(TokenKind_1.TokenKind.RightParen)) {
548
546
  do {
549
547
  if (params.length >= Expression_1.CallExpression.MaximumArguments) {
@@ -555,9 +553,9 @@ class Parser {
555
553
  let rightParen = this.advance();
556
554
  if (this.check(TokenKind_1.TokenKind.As)) {
557
555
  asToken = this.advance();
558
- typeToken = this.typeToken();
559
- if (!util_1.util.tokenToBscType(typeToken, this.options.mode === ParseMode.BrighterScript, this.currentNamespaceName)) {
560
- this.diagnostics.push(Object.assign(Object.assign({}, DiagnosticMessages_1.DiagnosticMessages.invalidFunctionReturnType((_a = typeToken.text) !== null && _a !== void 0 ? _a : '')), { range: typeToken.range }));
556
+ typeExpr = this.typeExpression();
557
+ if (!typeExpr.isValidType(this.options.mode)) {
558
+ this.diagnostics.push(Object.assign(Object.assign({}, DiagnosticMessages_1.DiagnosticMessages.invalidFunctionReturnType((_a = typeExpr.getText()) !== null && _a !== void 0 ? _a : '')), { range: typeExpr.range }));
561
559
  }
562
560
  }
563
561
  params.reduce((haveFoundOptional, param) => {
@@ -569,7 +567,7 @@ class Parser {
569
567
  this.consumeStatementSeparators(true);
570
568
  let func = new Expression_1.FunctionExpression(params, undefined, //body
571
569
  functionType, undefined, //ending keyword
572
- leftParen, rightParen, asToken, typeToken, //return type
570
+ leftParen, rightParen, asToken, typeExpr, //return type
573
571
  this.currentFunctionExpression, this.currentNamespaceName, (_c = (_b = this.currentNamespace) === null || _b === void 0 ? void 0 : _b.symbolTable) !== null && _c !== void 0 ? _c : this.symbolTable);
574
572
  //if there is a parent function, register this function with the parent
575
573
  if (this.currentFunctionExpression) {
@@ -638,7 +636,7 @@ class Parser {
638
636
  throw this.lastDiagnosticAsError();
639
637
  }
640
638
  const name = this.identifier(...TokenKind_1.AllowedLocalIdentifiers);
641
- let typeToken;
639
+ let typeExpr;
642
640
  let defaultValue;
643
641
  let equalsToken;
644
642
  // parse argument default value
@@ -650,26 +648,26 @@ class Parser {
650
648
  let asToken = null;
651
649
  if (this.check(TokenKind_1.TokenKind.As)) {
652
650
  asToken = this.advance();
653
- typeToken = this.typeToken();
654
- if (!util_1.util.tokenToBscType(typeToken, this.options.mode === ParseMode.BrighterScript, this.currentNamespaceName)) {
655
- this.diagnostics.push(Object.assign(Object.assign({}, DiagnosticMessages_1.DiagnosticMessages.functionParameterTypeIsInvalid(name.text, typeToken.text)), { range: typeToken.range }));
651
+ typeExpr = this.typeExpression();
652
+ if (!typeExpr.isValidType(this.options.mode)) {
653
+ this.diagnostics.push(Object.assign(Object.assign({}, DiagnosticMessages_1.DiagnosticMessages.functionParameterTypeIsInvalid(name.text, typeExpr.getText())), { range: typeExpr.range }));
656
654
  throw this.lastDiagnosticAsError();
657
655
  }
658
656
  }
659
- let type;
660
- if (typeToken) {
661
- type = util_1.util.tokenToBscType(typeToken, true, this.currentNamespaceName);
657
+ let typeInContext;
658
+ if (typeExpr) {
659
+ typeInContext = typeExpr.type;
662
660
  }
663
661
  else if (defaultValue) {
664
- type = getBscTypeFromExpression(defaultValue, this.currentFunctionExpression);
665
- if ((0, reflection_1.isInvalidType)(type)) {
666
- type = new DynamicType_1.DynamicType();
662
+ typeInContext = getBscTypeFromExpression(defaultValue, this.currentFunctionExpression);
663
+ if ((0, reflection_1.isInvalidType)(typeInContext)) {
664
+ typeInContext = new DynamicType_1.DynamicType();
667
665
  }
668
666
  }
669
667
  else {
670
- type = new DynamicType_1.DynamicType();
668
+ typeInContext = new DynamicType_1.DynamicType();
671
669
  }
672
- return new Expression_1.FunctionParameterExpression(name, type, equalsToken, defaultValue, asToken, typeToken, this.currentNamespaceName);
670
+ return new Expression_1.FunctionParameterExpression(name, typeInContext, equalsToken, defaultValue, asToken, typeExpr, this.currentNamespaceName);
673
671
  }
674
672
  assignment() {
675
673
  let name = this.identifier(...this.allowedLocalIdentifiers);
@@ -863,6 +861,12 @@ class Parser {
863
861
  this.diagnostics.push(Object.assign(Object.assign({}, DiagnosticMessages_1.DiagnosticMessages.expectedExpressionAfterForEachIn()), { range: this.peek().range }));
864
862
  throw this.lastDiagnosticAsError();
865
863
  }
864
+ let itemType = new DynamicType_1.DynamicType();
865
+ const targetType = getBscTypeFromExpression(target, this.currentFunctionExpression);
866
+ if ((0, reflection_1.isArrayType)(targetType)) {
867
+ itemType = targetType.getDefaultType();
868
+ }
869
+ this.currentSymbolTable.addSymbol(name.text, name.range, itemType);
866
870
  this.consumeStatementSeparators();
867
871
  let body = this.block(TokenKind_1.TokenKind.EndFor, TokenKind_1.TokenKind.Next);
868
872
  if (!body) {
@@ -870,12 +874,6 @@ class Parser {
870
874
  throw this.lastDiagnosticAsError();
871
875
  }
872
876
  let endFor = this.advance();
873
- let itemType = new DynamicType_1.DynamicType();
874
- const targetType = getBscTypeFromExpression(target, this.currentFunctionExpression);
875
- if ((0, reflection_1.isArrayType)(targetType)) {
876
- itemType = targetType.getDefaultType();
877
- }
878
- this.currentSymbolTable.addSymbol(name.text, name.range, itemType);
879
877
  return new Statement_1.ForEachStatement(forEach, name, maybeIn, target, body, endFor);
880
878
  }
881
879
  exitFor() {
@@ -1784,7 +1782,7 @@ class Parser {
1784
1782
  * Allows for built-in types (double, string, etc.) or namespaced custom types in Brighterscript mode
1785
1783
  * Will return a token of whatever is next to be parsed (unless `advanceIfUnknown` is false, in which case undefined will be returned instead
1786
1784
  */
1787
- typeToken() {
1785
+ typeExpression() {
1788
1786
  let typeToken;
1789
1787
  if (this.checkAny(...TokenKind_1.DeclarableTypes)) {
1790
1788
  // Token is a built in type
@@ -1805,18 +1803,23 @@ class Parser {
1805
1803
  // just get whatever's next
1806
1804
  typeToken = this.advance();
1807
1805
  }
1808
- if (typeToken && this.options.mode === ParseMode.BrighterScript) {
1806
+ //TODO: to support InterfaceTypeLiterals - (eg. `{name as string; age as integer}`), check if "typeToken" is a curly bracket, and do something else
1807
+ let typeExpr = new Expression_1.TypeExpression({ type: typeToken }, this.currentNamespaceName);
1808
+ if (this.options.mode === ParseMode.BrighterScript) {
1809
1809
  // Check if it is an array - that is, if it has `[]` after the type
1810
- // eg. `string[]` or `SomeKlass[]`
1811
- if (this.check(TokenKind_1.TokenKind.LeftSquareBracket)) {
1812
- this.advance();
1810
+ // eg. `string[]` or `SomeKlass[]` or `float[][][]`
1811
+ while (this.check(TokenKind_1.TokenKind.LeftSquareBracket)) {
1812
+ const leftBracket = this.advance();
1813
1813
  if (this.check(TokenKind_1.TokenKind.RightSquareBracket)) {
1814
1814
  const rightBracket = this.advance();
1815
- typeToken = (0, creators_1.createToken)(TokenKind_1.TokenKind.Identifier, typeToken.text + '[]', util_1.util.getRange(typeToken, rightBracket));
1815
+ typeExpr = new Expression_1.ArrayTypeExpression([typeExpr], { leftBracket: leftBracket, rightBracket: rightBracket }, this.currentNamespaceName);
1816
+ }
1817
+ else {
1818
+ break;
1816
1819
  }
1817
1820
  }
1818
1821
  }
1819
- return typeToken;
1822
+ return typeExpr;
1820
1823
  }
1821
1824
  primary() {
1822
1825
  switch (true) {
@@ -2587,7 +2590,7 @@ function getBscTypeFromExpression(expression, functionExpression) {
2587
2590
  //Associative array literal
2588
2591
  }
2589
2592
  else if ((0, reflection_1.isAALiteralExpression)(expression)) {
2590
- return new ObjectType_1.ObjectType(expression.memberTable);
2593
+ return new ObjectType_1.ObjectType('object', expression.memberTable);
2591
2594
  //Array literal
2592
2595
  }
2593
2596
  else if ((0, reflection_1.isArrayLiteralExpression)(expression)) {