@typespec/prettier-plugin-typespec 1.9.0 → 1.10.0-dev.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 CHANGED
@@ -316,6 +316,13 @@ var diagnostics = {
316
316
  /**
317
317
  * Checker
318
318
  */
319
+ "experimental-feature": {
320
+ severity: "warning",
321
+ messages: {
322
+ default: paramMessage`${"feature"} is an experimental feature. It may change in the future or be removed. Use with caution and consider providing feedback on this feature.`,
323
+ functionDeclarations: "Function declarations are an experimental feature that may change in the future. Use with caution and consider providing feedback to the TypeSpec team."
324
+ }
325
+ },
319
326
  "using-invalid-ref": {
320
327
  severity: "error",
321
328
  messages: {
@@ -459,13 +466,15 @@ var diagnostics = {
459
466
  model: paramMessage`${"name"} refers to a model type, but is being used as a value here. Use #{} to create an object value.`,
460
467
  modelExpression: `Is a model expression type, but is being used as a value here. Use #{} to create an object value.`,
461
468
  tuple: `Is a tuple type, but is being used as a value here. Use #[] to create an array value.`,
462
- templateConstraint: paramMessage`${"name"} template parameter can be a type but is being used as a value here.`
469
+ templateConstraint: paramMessage`${"name"} template parameter can be a type but is being used as a value here.`,
470
+ functionReturn: paramMessage`Function returned a type, but a value was expected.`
463
471
  }
464
472
  },
465
473
  "non-callable": {
466
474
  severity: "error",
467
475
  messages: {
468
- default: paramMessage`Type ${"type"} is not is not callable.`
476
+ default: paramMessage`Type ${"type"} is not is not callable.`,
477
+ templateParameter: paramMessage`Template parameter '${"name"} extends ${"constraint"}' is not callable. Ensure it is constrained to a function value or callable type (scalar or scalar constructor).`
469
478
  }
470
479
  },
471
480
  "named-init-required": {
@@ -505,6 +514,14 @@ var diagnostics = {
505
514
  default: paramMessage`Property '${"propName"}' is required in type '${"targetType"}' but here is optional.`
506
515
  }
507
516
  },
517
+ "parameter-required": {
518
+ severity: "error",
519
+ messages: {
520
+ default: paramMessage`Parameter '${"paramName"}' is required, but optional in the target function.`,
521
+ missing: paramMessage`Parameter '${"paramName"}' is required, but missing in the target function.`,
522
+ "rest-to-required": paramMessage`Parameter '${"paramName"}' is required and not satisfied by a rest parameter, as rest parameters are effectively optional.`
523
+ }
524
+ },
508
525
  "value-in-type": {
509
526
  severity: "error",
510
527
  messages: {
@@ -603,10 +620,18 @@ var diagnostics = {
603
620
  default: "A function declaration must be prefixed with the 'extern' modifier."
604
621
  }
605
622
  },
606
- "function-unsupported": {
623
+ "function-return": {
607
624
  severity: "error",
608
625
  messages: {
609
- default: "Function are currently not supported."
626
+ default: "Function implementation returned an invalid result.",
627
+ "invalid-value": paramMessage`Function implementation returned invalid JS value '${"value"}'.`,
628
+ unassignable: paramMessage`Implementation of '${"name"}' returned ${"entityKind"} '${"return"}', which is not assignable to the declared return type '${"type"}'.`
629
+ }
630
+ },
631
+ "fn-in-union-expression": {
632
+ severity: "error",
633
+ messages: {
634
+ default: "Function types in anonymous union expressions must be parenthesized."
610
635
  }
611
636
  },
612
637
  "missing-implementation": {
@@ -1261,6 +1286,7 @@ var SyntaxKind;
1261
1286
  SyntaxKind2[SyntaxKind2["ConstStatement"] = 67] = "ConstStatement";
1262
1287
  SyntaxKind2[SyntaxKind2["CallExpression"] = 68] = "CallExpression";
1263
1288
  SyntaxKind2[SyntaxKind2["ScalarConstructor"] = 69] = "ScalarConstructor";
1289
+ SyntaxKind2[SyntaxKind2["FunctionTypeExpression"] = 70] = "FunctionTypeExpression";
1264
1290
  })(SyntaxKind || (SyntaxKind = {}));
1265
1291
  var IdentifierKind;
1266
1292
  (function(IdentifierKind2) {
@@ -3037,14 +3063,14 @@ var Token;
3037
3063
  Token2[Token2["ElseKeyword"] = 64] = "ElseKeyword";
3038
3064
  Token2[Token2["IfKeyword"] = 65] = "IfKeyword";
3039
3065
  Token2[Token2["DecKeyword"] = 66] = "DecKeyword";
3040
- Token2[Token2["FnKeyword"] = 67] = "FnKeyword";
3041
- Token2[Token2["ConstKeyword"] = 68] = "ConstKeyword";
3042
- Token2[Token2["InitKeyword"] = 69] = "InitKeyword";
3043
- Token2[Token2["__EndStatementKeyword"] = 70] = "__EndStatementKeyword";
3044
- Token2[Token2["__StartModifierKeyword"] = 70] = "__StartModifierKeyword";
3045
- Token2[Token2["ExternKeyword"] = 70] = "ExternKeyword";
3046
- Token2[Token2["__EndModifierKeyword"] = 71] = "__EndModifierKeyword";
3047
- Token2[Token2["ExtendsKeyword"] = 71] = "ExtendsKeyword";
3066
+ Token2[Token2["ConstKeyword"] = 67] = "ConstKeyword";
3067
+ Token2[Token2["InitKeyword"] = 68] = "InitKeyword";
3068
+ Token2[Token2["__EndStatementKeyword"] = 69] = "__EndStatementKeyword";
3069
+ Token2[Token2["__StartModifierKeyword"] = 69] = "__StartModifierKeyword";
3070
+ Token2[Token2["ExternKeyword"] = 69] = "ExternKeyword";
3071
+ Token2[Token2["__EndModifierKeyword"] = 70] = "__EndModifierKeyword";
3072
+ Token2[Token2["ExtendsKeyword"] = 70] = "ExtendsKeyword";
3073
+ Token2[Token2["FnKeyword"] = 71] = "FnKeyword";
3048
3074
  Token2[Token2["TrueKeyword"] = 72] = "TrueKeyword";
3049
3075
  Token2[Token2["FalseKeyword"] = 73] = "FalseKeyword";
3050
3076
  Token2[Token2["ReturnKeyword"] = 74] = "ReturnKeyword";
@@ -4827,7 +4853,7 @@ function createParser(code, options = {}) {
4827
4853
  const id = parseIdentifier();
4828
4854
  let constraint;
4829
4855
  if (parseOptional(Token.ExtendsKeyword)) {
4830
- constraint = parseMixedParameterConstraint();
4856
+ constraint = parseMixedConstraint();
4831
4857
  }
4832
4858
  let def;
4833
4859
  if (parseOptional(Token.Equals)) {
@@ -4845,13 +4871,13 @@ function createParser(code, options = {}) {
4845
4871
  if (token() === Token.ValueOfKeyword) {
4846
4872
  return parseValueOfExpression();
4847
4873
  } else if (parseOptional(Token.OpenParen)) {
4848
- const expr = parseMixedParameterConstraint();
4874
+ const expr = parseMixedConstraint();
4849
4875
  parseExpected(Token.CloseParen);
4850
4876
  return expr;
4851
4877
  }
4852
4878
  return parseIntersectionExpressionOrHigher();
4853
4879
  }
4854
- function parseMixedParameterConstraint() {
4880
+ function parseMixedConstraint() {
4855
4881
  const pos = tokenPos();
4856
4882
  parseOptional(Token.Bar);
4857
4883
  const node = parseValueOfExpressionOrIntersectionOrHigher();
@@ -5076,6 +5102,14 @@ function createParser(code, options = {}) {
5076
5102
  const expr = parseIntersectionExpressionOrHigher();
5077
5103
  options2.push(expr);
5078
5104
  }
5105
+ for (const fnVariant of options2.filter((n) => n.kind === SyntaxKind.FunctionTypeExpression)) {
5106
+ if (!fnVariant.parenthesized) {
5107
+ error({
5108
+ code: "fn-in-union-expression",
5109
+ target: fnVariant
5110
+ });
5111
+ }
5112
+ }
5079
5113
  return {
5080
5114
  kind: SyntaxKind.UnionExpression,
5081
5115
  options: options2,
@@ -5166,6 +5200,19 @@ function createParser(code, options = {}) {
5166
5200
  }
5167
5201
  }
5168
5202
  }
5203
+ function parseFunctionTypeExpression() {
5204
+ const pos = tokenPos();
5205
+ parseExpected(Token.FnKeyword);
5206
+ const { items: parameters } = parseFunctionParameters();
5207
+ const optionalReturnType = parseOptional(Token.EqualsGreaterThan);
5208
+ const returnType = optionalReturnType ? parseMixedConstraint() : void 0;
5209
+ return {
5210
+ kind: SyntaxKind.FunctionTypeExpression,
5211
+ parameters,
5212
+ returnType,
5213
+ ...finishNode(pos)
5214
+ };
5215
+ }
5169
5216
  function parseReferenceExpression(message) {
5170
5217
  const pos = tokenPos();
5171
5218
  const target = parseIdentifierOrMemberExpression({
@@ -5391,6 +5438,8 @@ function createParser(code, options = {}) {
5391
5438
  switch (token()) {
5392
5439
  case Token.TypeOfKeyword:
5393
5440
  return parseTypeOfExpression();
5441
+ case Token.FnKeyword:
5442
+ return parseFunctionTypeExpression();
5394
5443
  case Token.Identifier:
5395
5444
  return parseCallOrReferenceExpression();
5396
5445
  case Token.StringLiteral:
@@ -5468,7 +5517,7 @@ function createParser(code, options = {}) {
5468
5517
  parseExpected(Token.OpenParen);
5469
5518
  const expr = parseExpression();
5470
5519
  parseExpected(Token.CloseParen);
5471
- return { ...expr, ...finishNode(pos) };
5520
+ return { parenthesized: true, ...expr, ...finishNode(pos) };
5472
5521
  }
5473
5522
  function parseTupleExpression() {
5474
5523
  const pos = tokenPos();
@@ -5706,7 +5755,7 @@ function createParser(code, options = {}) {
5706
5755
  const { items: parameters } = parseFunctionParameters();
5707
5756
  let returnType;
5708
5757
  if (parseOptional(Token.Colon)) {
5709
- returnType = parseExpression();
5758
+ returnType = parseMixedConstraint();
5710
5759
  }
5711
5760
  parseExpected(Token.Semicolon);
5712
5761
  return {
@@ -5746,7 +5795,7 @@ function createParser(code, options = {}) {
5746
5795
  const optional = parseOptional(Token.Question);
5747
5796
  let type;
5748
5797
  if (parseOptional(Token.Colon)) {
5749
- type = parseMixedParameterConstraint();
5798
+ type = parseMixedConstraint();
5750
5799
  }
5751
5800
  return {
5752
5801
  kind: SyntaxKind.FunctionParameter,
@@ -6346,6 +6395,8 @@ function visitChildren(node, cb) {
6346
6395
  return visitEach(cb, node.modifiers) || visitNode(cb, node.id) || visitNode(cb, node.target) || visitEach(cb, node.parameters);
6347
6396
  case SyntaxKind.FunctionDeclarationStatement:
6348
6397
  return visitEach(cb, node.modifiers) || visitNode(cb, node.id) || visitEach(cb, node.parameters) || visitNode(cb, node.returnType);
6398
+ case SyntaxKind.FunctionTypeExpression:
6399
+ return visitEach(cb, node.parameters) || visitNode(cb, node.returnType);
6349
6400
  case SyntaxKind.FunctionParameter:
6350
6401
  return visitNode(cb, node.id) || visitNode(cb, node.type);
6351
6402
  case SyntaxKind.TypeReference:
@@ -6680,6 +6731,8 @@ function printNode(path, options, print) {
6680
6731
  return printDecoratorDeclarationStatement(path, options, print);
6681
6732
  case SyntaxKind.FunctionDeclarationStatement:
6682
6733
  return printFunctionDeclarationStatement(path, options, print);
6734
+ case SyntaxKind.FunctionTypeExpression:
6735
+ return printFunctionTypeExpression(path, options, print);
6683
6736
  case SyntaxKind.FunctionParameter:
6684
6737
  return printFunctionParameterDeclaration(path, options, print);
6685
6738
  case SyntaxKind.ExternKeyword:
@@ -7391,6 +7444,17 @@ function printFunctionDeclarationStatement(path, options, print) {
7391
7444
  const returnType = node.returnType ? [": ", path.call(print, "returnType")] : "";
7392
7445
  return [printModifiers(path, options, print), "fn ", id, "(", parameters, ")", returnType, ";"];
7393
7446
  }
7447
+ function printFunctionTypeExpression(path, options, print) {
7448
+ const node = path.node;
7449
+ const parameters = [
7450
+ group([
7451
+ indent(join(", ", path.map((arg) => [softline, print(arg)], "parameters"))),
7452
+ softline
7453
+ ])
7454
+ ];
7455
+ const returnType = node.returnType ? [" => ", path.call(print, "returnType")] : "";
7456
+ return ["fn", "(", parameters, ")", returnType];
7457
+ }
7394
7458
  function printFunctionParameterDeclaration(path, options, print) {
7395
7459
  const node = path.node;
7396
7460
  const id = printIdentifier2(node.id, "allow-reserved");