prettier-plugin-razor2 0.1.2 → 0.1.3

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 +45 -1
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -59,6 +59,24 @@ function readTagName(src, i) {
59
59
  while (j < src.length && /[A-Za-z0-9]/.test(src[j])) j++;
60
60
  return src.slice(i, j);
61
61
  }
62
+ function readCSharpIdent(src, i) {
63
+ if (!/[A-Za-z_]/.test(src[i] ?? "")) return "";
64
+ let j = i + 1;
65
+ while (j < src.length && /[A-Za-z0-9_]/.test(src[j])) j++;
66
+ return src.slice(i, j);
67
+ }
68
+ function readImplicitExpression(src, at) {
69
+ let i = at + 1 + readCSharpIdent(src, at + 1).length;
70
+ for (;;) {
71
+ const c = src[i];
72
+ if (c === "(") i = matchParen(src, i);
73
+ else if (c === "[") i = matchDelimited(src, i, "[", "]");
74
+ else if (c === "." && readCSharpIdent(src, i + 1) !== "") i += 1 + readCSharpIdent(src, i + 1).length;
75
+ else if (c === "?" && src[i + 1] === "." && readCSharpIdent(src, i + 2) !== "") i += 2 + readCSharpIdent(src, i + 2).length;
76
+ else break;
77
+ }
78
+ return i;
79
+ }
62
80
  function atLineStart(src, i) {
63
81
  let j = i - 1;
64
82
  while (j >= 0 && (src[j] === " " || src[j] === " ")) j--;
@@ -412,6 +430,17 @@ function mask(src) {
412
430
  i = end;
413
431
  continue;
414
432
  }
433
+ if (/[A-Za-z_]/.test(src[i + 1] ?? "")) {
434
+ const end = readImplicitExpression(src, i);
435
+ const text = src.slice(i, end);
436
+ if (text.includes("\"") || text.includes("'")) out += inlinePlaceholder({
437
+ kind: "inline",
438
+ text
439
+ });
440
+ else out += text;
441
+ i = end;
442
+ continue;
443
+ }
415
444
  out += c;
416
445
  i++;
417
446
  }
@@ -701,13 +730,28 @@ const options = {
701
730
  description: "Command used to invoke the CSharpier CLI."
702
731
  }
703
732
  };
733
+ function warnUnformatted(filepath, error) {
734
+ const where = filepath ? ` (${filepath})` : "";
735
+ const why = error instanceof Error ? `: ${error.message.split("\n")[0]}` : "";
736
+ console.warn(`[prettier-plugin-razor2] Could not format this file${where}; leaving it unchanged${why}`);
737
+ }
704
738
  const printers = { "razor-ast": {
705
739
  print: () => "",
706
740
  embed(path) {
707
741
  const node = path.node;
708
742
  if (node.type !== "razor-root") return null;
709
743
  return async (textToDoc, _print, _embedPath, options) => {
710
- return (await formatDocument(node.source, textToDoc, options)).replace(/\s+$/, "") + "\n";
744
+ try {
745
+ const formatted = await formatDocument(node.source, textToDoc, options);
746
+ if (formatted.trim() === "" && node.source.trim() !== "") {
747
+ warnUnformatted(options.filepath);
748
+ return node.source;
749
+ }
750
+ return formatted.replace(/\s+$/, "") + "\n";
751
+ } catch (error) {
752
+ warnUnformatted(options.filepath, error);
753
+ return node.source;
754
+ }
711
755
  };
712
756
  }
713
757
  } };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-razor2",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Prettier plugin for Razor (Blazor) files — a fork of prettier-plugin-razor",
5
5
  "keywords": [
6
6
  "prettier",