@typespec/prettier-plugin-typespec 0.66.0-dev.0 → 0.67.0-dev.0

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
@@ -446,7 +446,9 @@ const diagnostics = {
446
446
  severity: "error",
447
447
  messages: {
448
448
  default: `Augment decorator first argument must be a type reference.`,
449
- noInstance: `Cannot reference template instances`,
449
+ noInstance: `Cannot reference template instances.`,
450
+ noModelExpression: `Cannot augment model expressions.`,
451
+ noUnionExpression: `Cannot augment union expressions.`,
450
452
  },
451
453
  },
452
454
  "duplicate-decorator": {
@@ -1110,6 +1112,9 @@ const diagnostics = {
1110
1112
  severity: "error",
1111
1113
  messages: {
1112
1114
  default: paramMessage `Union variant "${"name"}" must be a model type.`,
1115
+ noEnvelopeModel: paramMessage `Union variant "${"name"}" must be a model type when the union has envelope: none.`,
1116
+ discriminantMismatch: paramMessage `Variant "${"name"}" explicitly defines the discriminator property "${"discriminant"}" but the value "${"propertyValue"}" do not match the variant name "${"variantName"}".`,
1117
+ duplicateDefaultVariant: `Discriminated union only allow a single default variant(Without a variant name).`,
1113
1118
  noDiscriminant: paramMessage `Variant "${"name"}" type is missing the discriminant property "${"discriminant"}".`,
1114
1119
  wrongDiscriminantType: paramMessage `Variant "${"name"}" type's discriminant property "${"discriminant"}" must be a string literal or string enum member.`,
1115
1120
  },
@@ -1292,12 +1297,28 @@ const diagnostics = {
1292
1297
  default: "Cannot apply both string (legacy) visibility modifiers and enum-based visibility modifiers to a property.",
1293
1298
  },
1294
1299
  },
1300
+ "visibility-legacy": {
1301
+ severity: "warning",
1302
+ messages: {
1303
+ default: paramMessage `String (legacy) visibility modifier '${"modifier"}' is deprecated and will be removed in TypeSpec version 1.0-rc. Use '${"newModifier"}' instead.`,
1304
+ unrecognized: paramMessage `String (legacy) visibility modifier '${"modifier"}' is not recognized. This warning will become an error in TypeSpec 1.0-rc.`,
1305
+ none: "String (legacy) visibility modifier 'none' should be replaced with '@invisible(Lifecycle)'. This warning will become an error in TypeSpec 1.0-rc.",
1306
+ },
1307
+ },
1295
1308
  "default-visibility-not-member": {
1296
1309
  severity: "error",
1297
1310
  messages: {
1298
1311
  default: "The default visibility modifiers of a class must be members of the class enum.",
1299
1312
  },
1300
1313
  },
1314
+ "operation-visibility-constraint-empty": {
1315
+ severity: "warning",
1316
+ messages: {
1317
+ default: "Operation visibility constraints with no arguments are deprecated and will become an error in TypeSpec version 1.0-rc.",
1318
+ returnType: "Return type visibility constraints with no arguments are deprecated and will become an error in TypeSpec version 1.0-rc.",
1319
+ parameter: "Parameter visibility constraints with no arguments are deprecated and will become an error in TypeSpec version 1.0-rc. To disable effective PATCH optionality, use @patch(#{ implicitOptionality: false }) instead.",
1320
+ },
1321
+ },
1301
1322
  // #endregion
1302
1323
  // #region CLI
1303
1324
  "no-compatible-vs-installed": {
@@ -1415,6 +1436,9 @@ function compilerAssert(condition, message, target) {
1415
1436
  }
1416
1437
  throw new Error(message);
1417
1438
  }
1439
+ function defineCodeFix(fix) {
1440
+ return fix;
1441
+ }
1418
1442
 
1419
1443
  //
1420
1444
  // Generated by scripts/regen-nonascii-map.js
@@ -2235,6 +2259,52 @@ function lookupInNonAsciiMap(codePoint, map) {
2235
2259
  return false;
2236
2260
  }
2237
2261
 
2262
+ function createTripleQuoteIndentCodeFix(location) {
2263
+ return defineCodeFix({
2264
+ id: "triple-quote-indent",
2265
+ label: "Format triple-quote-indent",
2266
+ fix: (context) => {
2267
+ const splitStr = "\n";
2268
+ const tripleQuote = '"""';
2269
+ const tripleQuoteLen = tripleQuote.length;
2270
+ const text = location.file.text.slice(location.pos + tripleQuoteLen, location.end - tripleQuoteLen);
2271
+ const lines = splitLines(text);
2272
+ if (lines.length === 0) {
2273
+ return;
2274
+ }
2275
+ if (lines.length === 1) {
2276
+ const indentNumb = getIndentNumbInLine(lines[0]);
2277
+ const prefix = " ".repeat(indentNumb);
2278
+ return context.replaceText(location, [tripleQuote, lines[0], `${prefix}${tripleQuote}`].join(splitStr));
2279
+ }
2280
+ if (lines[0].trim() === "") {
2281
+ lines.shift();
2282
+ }
2283
+ const lastLine = lines[lines.length - 1];
2284
+ if (lastLine.trim() === "") {
2285
+ lines.pop();
2286
+ }
2287
+ let prefix = "";
2288
+ const minIndentNumb = Math.min(...lines.map((line) => getIndentNumbInLine(line)));
2289
+ const lastLineIndentNumb = getIndentNumbInLine(lastLine);
2290
+ if (minIndentNumb < lastLineIndentNumb) {
2291
+ const indentDiff = lastLineIndentNumb - minIndentNumb;
2292
+ prefix = " ".repeat(indentDiff);
2293
+ }
2294
+ const middle = lines.map((line) => `${prefix}${line}`).join(splitStr);
2295
+ return context.replaceText(location, `${tripleQuote}${splitStr}${middle}${splitStr}${" ".repeat(lastLineIndentNumb)}${tripleQuote}`);
2296
+ function getIndentNumbInLine(lineText) {
2297
+ let curStart = 0;
2298
+ while (curStart < lineText.length &&
2299
+ isWhiteSpaceSingleLine(lineText.charCodeAt(curStart))) {
2300
+ curStart++;
2301
+ }
2302
+ return curStart;
2303
+ }
2304
+ },
2305
+ });
2306
+ }
2307
+
2238
2308
  // All conflict markers consist of the same character repeated seven times. If it is
2239
2309
  // a <<<<<<< or >>>>>>> marker then it is also followed by a space.
2240
2310
  const mergeConflictMarkerLength = 7;
@@ -3062,9 +3132,6 @@ function createScanner(source, diagnosticHandler) {
3062
3132
  }
3063
3133
  end--;
3064
3134
  }
3065
- else {
3066
- error({ code: "no-new-line-end-triple-quote" });
3067
- }
3068
3135
  return [indentationStart, indentationEnd];
3069
3136
  }
3070
3137
  function unindentAndUnescapeTripleQuotedString(start, end, indentationStart, indentationEnd, token, tokenFlags) {
@@ -3085,7 +3152,10 @@ function createScanner(source, diagnosticHandler) {
3085
3152
  start++;
3086
3153
  }
3087
3154
  else {
3088
- error({ code: "no-new-line-start-triple-quote" });
3155
+ error({
3156
+ code: "no-new-line-start-triple-quote",
3157
+ codefixes: [createTripleQuoteIndentCodeFix({ file, pos: tokenPosition, end: position })],
3158
+ });
3089
3159
  }
3090
3160
  }
3091
3161
  if (token === Token.StringLiteral || token === Token.StringTemplateTail) {
@@ -3100,7 +3170,10 @@ function createScanner(source, diagnosticHandler) {
3100
3170
  end--;
3101
3171
  }
3102
3172
  else {
3103
- error({ code: "no-new-line-end-triple-quote" });
3173
+ error({
3174
+ code: "no-new-line-end-triple-quote",
3175
+ codefixes: [createTripleQuoteIndentCodeFix({ file, pos: tokenPosition, end: position })],
3176
+ });
3104
3177
  }
3105
3178
  }
3106
3179
  let skipUnindentOnce = false;
@@ -3171,7 +3244,10 @@ function createScanner(source, diagnosticHandler) {
3171
3244
  break;
3172
3245
  }
3173
3246
  if (ch !== input.charCodeAt(indentationPos)) {
3174
- error({ code: "triple-quote-indent" });
3247
+ error({
3248
+ code: "triple-quote-indent",
3249
+ codefixes: [createTripleQuoteIndentCodeFix({ file, pos: tokenPosition, end: position })],
3250
+ });
3175
3251
  break;
3176
3252
  }
3177
3253
  indentationPos++;
@@ -3381,6 +3457,77 @@ function getTokenDisplayTable(entries) {
3381
3457
  return table;
3382
3458
  }
3383
3459
 
3460
+ /**
3461
+ * Print a string as a TypeSpec identifier. If the string is a valid identifier, return it as is otherwise wrap it into backticks.
3462
+ * @param sv Identifier string value.
3463
+ * @returns Identifier string as it would be represented in a TypeSpec file.
3464
+ *
3465
+ * @example
3466
+ * ```ts
3467
+ * printIdentifier("foo") // foo
3468
+ * printIdentifier("foo bar") // `foo bar`
3469
+ * ```
3470
+ */
3471
+ function printIdentifier$1(sv) {
3472
+ if (needBacktick(sv)) {
3473
+ const escapedString = sv
3474
+ .replace(/\\/g, "\\\\")
3475
+ .replace(/\n/g, "\\n")
3476
+ .replace(/\r/g, "\\r")
3477
+ .replace(/\t/g, "\\t")
3478
+ .replace(/`/g, "\\`");
3479
+ return `\`${escapedString}\``;
3480
+ }
3481
+ else {
3482
+ return sv;
3483
+ }
3484
+ }
3485
+ function needBacktick(sv) {
3486
+ if (sv.length === 0) {
3487
+ return false;
3488
+ }
3489
+ if (Keywords.has(sv)) {
3490
+ return true;
3491
+ }
3492
+ let cp = sv.codePointAt(0);
3493
+ if (!isIdentifierStart(cp)) {
3494
+ return true;
3495
+ }
3496
+ let pos = 0;
3497
+ do {
3498
+ pos += utf16CodeUnits(cp);
3499
+ } while (pos < sv.length && isIdentifierContinue((cp = sv.codePointAt(pos))));
3500
+ return pos < sv.length;
3501
+ }
3502
+ function splitLines(text) {
3503
+ const lines = [];
3504
+ let start = 0;
3505
+ let pos = 0;
3506
+ while (pos < text.length) {
3507
+ const ch = text.charCodeAt(pos);
3508
+ switch (ch) {
3509
+ case 13 /* CharCode.CarriageReturn */:
3510
+ if (text.charCodeAt(pos + 1) === 10 /* CharCode.LineFeed */) {
3511
+ lines.push(text.slice(start, pos));
3512
+ start = pos + 2;
3513
+ pos++;
3514
+ }
3515
+ else {
3516
+ lines.push(text.slice(start, pos));
3517
+ start = pos + 1;
3518
+ }
3519
+ break;
3520
+ case 10 /* CharCode.LineFeed */:
3521
+ lines.push(text.slice(start, pos));
3522
+ start = pos + 1;
3523
+ break;
3524
+ }
3525
+ pos++;
3526
+ }
3527
+ lines.push(text.slice(start));
3528
+ return lines;
3529
+ }
3530
+
3384
3531
  /**
3385
3532
  * The fixed set of options for each of the kinds of delimited lists in TypeSpec.
3386
3533
  */
@@ -6427,49 +6574,6 @@ function isBlocklessNamespace(node) {
6427
6574
  return node.statements === undefined;
6428
6575
  }
6429
6576
 
6430
- /**
6431
- * Print a string as a TypeSpec identifier. If the string is a valid identifier, return it as is otherwise wrap it into backticks.
6432
- * @param sv Identifier string value.
6433
- * @returns Identifier string as it would be represented in a TypeSpec file.
6434
- *
6435
- * @example
6436
- * ```ts
6437
- * printIdentifier("foo") // foo
6438
- * printIdentifier("foo bar") // `foo bar`
6439
- * ```
6440
- */
6441
- function printIdentifier$1(sv) {
6442
- if (needBacktick(sv)) {
6443
- const escapedString = sv
6444
- .replace(/\\/g, "\\\\")
6445
- .replace(/\n/g, "\\n")
6446
- .replace(/\r/g, "\\r")
6447
- .replace(/\t/g, "\\t")
6448
- .replace(/`/g, "\\`");
6449
- return `\`${escapedString}\``;
6450
- }
6451
- else {
6452
- return sv;
6453
- }
6454
- }
6455
- function needBacktick(sv) {
6456
- if (sv.length === 0) {
6457
- return false;
6458
- }
6459
- if (Keywords.has(sv)) {
6460
- return true;
6461
- }
6462
- let cp = sv.codePointAt(0);
6463
- if (!isIdentifierStart(cp)) {
6464
- return true;
6465
- }
6466
- let pos = 0;
6467
- do {
6468
- pos += utf16CodeUnits(cp);
6469
- } while (pos < sv.length && isIdentifierContinue((cp = sv.codePointAt(pos))));
6470
- return pos < sv.length;
6471
- }
6472
-
6473
6577
  var yu=Object.create;var vt=Object.defineProperty;var Au=Object.getOwnPropertyDescriptor;var vu=Object.getOwnPropertyNames;var Bu=Object.getPrototypeOf,wu=Object.prototype.hasOwnProperty;var fr=e=>{throw TypeError(e)};var dr=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),Bt=(e,t)=>{for(var r in t)vt(e,r,{get:t[r],enumerable:true});},_u=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let u of vu(t))!wu.call(e,u)&&u!==r&&vt(e,u,{get:()=>t[u],enumerable:!(n=Au(t,u))||n.enumerable});return e};var Me=(e,t,r)=>(r=e!=null?yu(Bu(e)):{},_u(vt(r,"default",{value:e,enumerable:true}),e));var xu=(e,t,r)=>t.has(e)||fr("Cannot "+r);var pr=(e,t,r)=>t.has(e)?fr("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(e):t.set(e,r);var pe=(e,t,r)=>(xu(e,t,"access private method"),r);var st=dr((na,Fn)=>{var pn=new Proxy(String,{get:()=>pn});Fn.exports=pn;});var Wn=dr(nr=>{Object.defineProperty(nr,"__esModule",{value:true});function Bi(){return new Proxy({},{get:()=>e=>e})}var Hn=/\r\n|[\n\r\u2028\u2029]/;function wi(e,t,r){let n=Object.assign({column:0,line:-1},e.start),u=Object.assign({},n,e.end),{linesAbove:i=2,linesBelow:o=3}=r||{},s=n.line,a=n.column,D=u.line,l=u.column,p=Math.max(s-(i+1),0),f=Math.min(t.length,D+o);s===-1&&(p=0),D===-1&&(f=t.length);let d=D-s,c={};if(d)for(let F=0;F<=d;F++){let m=F+s;if(!a)c[m]=true;else if(F===0){let h=t[m-1].length;c[m]=[a,h-a+1];}else if(F===d)c[m]=[0,l];else {let h=t[m-F].length;c[m]=[0,h];}}else a===l?a?c[s]=[a,0]:c[s]=true:c[s]=[a,l-a];return {start:p,end:f,markerLines:c}}function _i(e,t,r={}){let u=Bi(),i=e.split(Hn),{start:o,end:s,markerLines:a}=wi(t,i,r),D=t.start&&typeof t.start.column=="number",l=String(s).length,f=e.split(Hn,s).slice(o,s).map((d,c)=>{let F=o+1+c,h=` ${` ${F}`.slice(-l)} |`,C=a[F],v=!a[F+1];if(C){let E="";if(Array.isArray(C)){let g=d.slice(0,Math.max(C[0]-1,0)).replace(/[^\t]/g," "),j=C[1]||1;E=[`
6474
6578
  `,u.gutter(h.replace(/\d/g," "))," ",g,u.marker("^").repeat(j)].join(""),v&&r.message&&(E+=" "+u.message(r.message));}return [u.marker(">"),u.gutter(h),d.length>0?` ${d}`:"",E].join("")}else return ` ${u.gutter(h)}${d.length>0?` ${d}`:""}`}).join(`
6475
6579
  `);return r.message&&!D&&(f=`${" ".repeat(l+1)}${r.message}
@@ -9067,34 +9171,6 @@ function printStringTemplateExpression(path, options, print) {
9067
9171
  return content;
9068
9172
  }
9069
9173
  }
9070
- function splitLines(text) {
9071
- const lines = [];
9072
- let start = 0;
9073
- let pos = 0;
9074
- while (pos < text.length) {
9075
- const ch = text.charCodeAt(pos);
9076
- switch (ch) {
9077
- case 13 /* CharCode.CarriageReturn */:
9078
- if (text.charCodeAt(pos + 1) === 10 /* CharCode.LineFeed */) {
9079
- lines.push(text.slice(start, pos));
9080
- start = pos;
9081
- pos++;
9082
- }
9083
- else {
9084
- lines.push(text.slice(start, pos));
9085
- start = pos;
9086
- }
9087
- break;
9088
- case 10 /* CharCode.LineFeed */:
9089
- lines.push(text.slice(start, pos));
9090
- start = pos;
9091
- break;
9092
- }
9093
- pos++;
9094
- }
9095
- lines.push(text.slice(start));
9096
- return lines;
9097
- }
9098
9174
  function trimMultilineString(lines, whitespaceIndent) {
9099
9175
  const newLines = [];
9100
9176
  for (let i = 0; i < lines.length; i++) {