janja 4.5.0 → 4.6.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.esm.js CHANGED
@@ -5585,84 +5585,6 @@ function countCssBraces(line) {
5585
5585
  return { opens, closes };
5586
5586
  }
5587
5587
 
5588
- /**
5589
- * Brace-depth reindent for `<script>` bodies. Only leading whitespace changes;
5590
- * braces inside strings, comments, template literals, and Janja tags are ignored.
5591
- */
5592
- function indentJs(source, tabWidth) {
5593
- const pad = ' '.repeat(Math.max(0, tabWidth));
5594
- const eol = source.includes('\r\n') ? '\r\n' : '\n';
5595
- const lines = source.split(/\r?\n/);
5596
- let depth = 0;
5597
- const out = [];
5598
- for (const line of lines) {
5599
- const content = line.trim();
5600
- if (content.length === 0) {
5601
- out.push('');
5602
- continue;
5603
- }
5604
- const { opens, closes } = countJsBraces(content);
5605
- const lineDepth = closes > opens ? Math.max(0, depth - (closes - opens)) : depth;
5606
- out.push(pad.repeat(lineDepth) + content);
5607
- depth = Math.max(0, depth + opens - closes);
5608
- }
5609
- return out.join(eol);
5610
- }
5611
- function countJsBraces(line) {
5612
- let opens = 0;
5613
- let closes = 0;
5614
- let quote = null;
5615
- let i = 0;
5616
- while (i < line.length) {
5617
- const c = line[i];
5618
- if (quote) {
5619
- if (c === '\\') {
5620
- i += 2;
5621
- continue;
5622
- }
5623
- if (c === quote) {
5624
- quote = null;
5625
- }
5626
- i += 1;
5627
- continue;
5628
- }
5629
- if (c === '{' && line[i + 1] === '{') {
5630
- i = endOfJanja(line, i);
5631
- continue;
5632
- }
5633
- if (c === '"' || c === "'" || c === '`') {
5634
- quote = c;
5635
- i += 1;
5636
- continue;
5637
- }
5638
- if (c === '/' && line[i + 1] === '*') {
5639
- const end = line.indexOf('*/', i + 2);
5640
- i = end === -1 ? line.length : end + 2;
5641
- continue;
5642
- }
5643
- if (c === '/' && line[i + 1] === '/') {
5644
- break;
5645
- }
5646
- if (c === '{') {
5647
- opens += 1;
5648
- }
5649
- else if (c === '}') {
5650
- closes += 1;
5651
- }
5652
- i += 1;
5653
- }
5654
- return { opens, closes };
5655
- }
5656
- /** Skip a Janja tag starting at `i` (`{{`, `{{=`, `{{#` …). */
5657
- function endOfJanja(line, i) {
5658
- if (line.startsWith('{{#', i)) {
5659
- const end = line.indexOf('#}}', i + 3);
5660
- return end === -1 ? line.length : end + 3;
5661
- }
5662
- const end = line.indexOf('}}', i + 2);
5663
- return end === -1 ? line.length : end + 2;
5664
- }
5665
-
5666
5588
  /** HTML void elements — never take children, so they do not change indent. */
5667
5589
  const VOID = new Set([
5668
5590
  'area',
@@ -5685,8 +5607,6 @@ const VOID = new Set([
5685
5607
  * whitespace; `script` / `style` are reindented (see body buffer below).
5686
5608
  */
5687
5609
  const SENSITIVE = new Set(['pre', 'textarea', 'script', 'style']);
5688
- /** HTML elements whose bodies are reindented (not left raw). */
5689
- const REINDENT_HTML = new Set(['script', 'style']);
5690
5610
  const DIR_OPEN = new Set([
5691
5611
  'if',
5692
5612
  'for',
@@ -5697,7 +5617,6 @@ const DIR_OPEN = new Set([
5697
5617
  'with',
5698
5618
  'stylesheet',
5699
5619
  ]);
5700
- /** Directives whose bodies keep leading whitespace (like `<style>`). */
5701
5620
  const DIR_SENSITIVE = new Set(['stylesheet']);
5702
5621
  const DIR_MID = new Set(['else', 'elif', 'elseif', 'elsif']);
5703
5622
  const DIR_CLOSE = new Set([
@@ -5710,84 +5629,7 @@ const DIR_CLOSE = new Set([
5710
5629
  'endwith',
5711
5630
  'endstylesheet',
5712
5631
  ]);
5713
- /**
5714
- * Reindent a template that already had its Janja tags normalised.
5715
- * Only leading whitespace on each line changes; line breaks and
5716
- * intra-line spacing stay put. Content inside pre/textarea is left alone;
5717
- * `script` / `style` bodies are brace-reindented; `stylesheet` bodies are
5718
- * formatted as an HTML fragment (so nested `<style>` gets CSS indent).
5719
- */
5720
- function indentHtml(source, options) {
5721
- const tabWidth = options.tabWidth ?? 2;
5722
- const pad = ' '.repeat(Math.max(0, tabWidth));
5723
- const eol = source.includes('\r\n') ? '\r\n' : '\n';
5724
- const lines = source.split(/\r?\n/);
5725
- let depth = 0;
5726
- let sensitive = null;
5727
- /** Raw lines inside a reindented body (`stylesheet` / `script` / `style`). */
5728
- let bodyBuf = null;
5729
- const out = [];
5730
- /** Open `{{#` comment: inner lines are never scanned for pieces. */
5731
- const state = { inComment: false };
5732
- for (const line of lines) {
5733
- const leading = line.match(/^[ \t]*/)?.[0].length ?? 0;
5734
- const content = line.slice(leading);
5735
- const { pieces, prefixClosers } = scanLine(content, options, state);
5736
- const activeSensitive = sensitive;
5737
- const closesSensitive = activeSensitive !== null &&
5738
- pieces.some((piece) => piece.kind === 'close' && piece.sensitive === activeSensitive);
5739
- const reindentBody = activeSensitive === 'stylesheet' ||
5740
- (activeSensitive !== null && REINDENT_HTML.has(activeSensitive));
5741
- if (reindentBody && !closesSensitive) {
5742
- // Do not applyDepth: bodies may contain nested Janja if/for tags.
5743
- (bodyBuf ?? (bodyBuf = [])).push(line);
5744
- continue;
5745
- }
5746
- if (bodyBuf && closesSensitive && reindentBody) {
5747
- // stylesheet: same column as the tags; script/style: one level inside.
5748
- const baseDepth = activeSensitive === 'stylesheet' ? Math.max(0, depth - 1) : depth;
5749
- const raw = bodyBuf.join(eol);
5750
- const formatted = activeSensitive === 'stylesheet'
5751
- ? indentHtml(raw, options)
5752
- : activeSensitive === 'style'
5753
- ? indentCss(raw, tabWidth)
5754
- : indentJs(raw, tabWidth);
5755
- for (const bodyLine of formatted.split(/\r?\n/)) {
5756
- out.push(bodyLine.length === 0 ? '' : pad.repeat(baseDepth) + bodyLine);
5757
- }
5758
- bodyBuf = null;
5759
- }
5760
- else if (sensitive && !closesSensitive) {
5761
- out.push(line);
5762
- applyDepth(pieces);
5763
- continue;
5764
- }
5765
- const lineDepth = Math.max(0, depth - prefixClosers);
5766
- out.push(content.length === 0 ? '' : pad.repeat(lineDepth) + content);
5767
- applyDepth(pieces);
5768
- }
5769
- // split() keeps a final empty string when the source already ends in a
5770
- // newline, so join() alone preserves that trailing newline — do not add
5771
- // another one or each format grows an extra blank line.
5772
- return out.join(eol);
5773
- function applyDepth(pieces) {
5774
- for (const piece of pieces) {
5775
- if (piece.kind === 'open') {
5776
- depth += 1;
5777
- if (piece.sensitive && !sensitive) {
5778
- ({ sensitive } = piece);
5779
- }
5780
- }
5781
- else if (piece.kind === 'close') {
5782
- depth = Math.max(0, depth - 1);
5783
- if (piece.sensitive && sensitive === piece.sensitive) {
5784
- sensitive = null;
5785
- }
5786
- }
5787
- else if (piece.kind === 'mid') ;
5788
- }
5789
- }
5790
- }
5632
+ /** Scan one line for HTML and Janja constructs that affect indentation. */
5791
5633
  function scanLine(line, options, state) {
5792
5634
  const pieces = [];
5793
5635
  let prefixClosers = 0;
@@ -6003,6 +5845,171 @@ function endOfDelimited(line, from, close) {
6003
5845
  return idx === -1 ? line.length : idx + close.length;
6004
5846
  }
6005
5847
 
5848
+ /**
5849
+ * Brace-depth reindent for `<script>` bodies. Only leading whitespace changes;
5850
+ * braces inside strings, comments, template literals, and Janja tags are ignored.
5851
+ */
5852
+ function indentJs(source, tabWidth) {
5853
+ const pad = ' '.repeat(Math.max(0, tabWidth));
5854
+ const eol = source.includes('\r\n') ? '\r\n' : '\n';
5855
+ const lines = source.split(/\r?\n/);
5856
+ let depth = 0;
5857
+ const out = [];
5858
+ for (const line of lines) {
5859
+ const content = line.trim();
5860
+ if (content.length === 0) {
5861
+ out.push('');
5862
+ continue;
5863
+ }
5864
+ const { opens, closes } = countJsBraces(content);
5865
+ const lineDepth = closes > opens ? Math.max(0, depth - (closes - opens)) : depth;
5866
+ out.push(pad.repeat(lineDepth) + content);
5867
+ depth = Math.max(0, depth + opens - closes);
5868
+ }
5869
+ return out.join(eol);
5870
+ }
5871
+ function countJsBraces(line) {
5872
+ let opens = 0;
5873
+ let closes = 0;
5874
+ let quote = null;
5875
+ let i = 0;
5876
+ while (i < line.length) {
5877
+ const c = line[i];
5878
+ if (quote) {
5879
+ if (c === '\\') {
5880
+ i += 2;
5881
+ continue;
5882
+ }
5883
+ if (c === quote) {
5884
+ quote = null;
5885
+ }
5886
+ i += 1;
5887
+ continue;
5888
+ }
5889
+ if (c === '{' && line[i + 1] === '{') {
5890
+ i = endOfJanja(line, i);
5891
+ continue;
5892
+ }
5893
+ if (c === '"' || c === "'" || c === '`') {
5894
+ quote = c;
5895
+ i += 1;
5896
+ continue;
5897
+ }
5898
+ if (c === '/' && line[i + 1] === '*') {
5899
+ const end = line.indexOf('*/', i + 2);
5900
+ i = end === -1 ? line.length : end + 2;
5901
+ continue;
5902
+ }
5903
+ if (c === '/' && line[i + 1] === '/') {
5904
+ break;
5905
+ }
5906
+ if (c === '{') {
5907
+ opens += 1;
5908
+ }
5909
+ else if (c === '}') {
5910
+ closes += 1;
5911
+ }
5912
+ i += 1;
5913
+ }
5914
+ return { opens, closes };
5915
+ }
5916
+ /** Skip a Janja tag starting at `i` (`{{`, `{{=`, `{{#` …). */
5917
+ function endOfJanja(line, i) {
5918
+ if (line.startsWith('{{#', i)) {
5919
+ const end = line.indexOf('#}}', i + 3);
5920
+ return end === -1 ? line.length : end + 3;
5921
+ }
5922
+ const end = line.indexOf('}}', i + 2);
5923
+ return end === -1 ? line.length : end + 2;
5924
+ }
5925
+
5926
+ /** HTML elements whose bodies are reindented (not left raw). */
5927
+ const REINDENT_HTML = new Set(['script', 'style']);
5928
+ /**
5929
+ * Reindent a template that already had its Janja tags normalised.
5930
+ * Only leading whitespace on each line changes; line breaks and
5931
+ * intra-line spacing stay put. Content inside pre/textarea is left alone;
5932
+ * `script` / `style` bodies are brace-reindented; `stylesheet` bodies are
5933
+ * formatted as an HTML fragment (so nested `<style>` gets CSS indent).
5934
+ */
5935
+ function indentHtml(source, options) {
5936
+ const tabWidth = options.tabWidth ?? 2;
5937
+ const pad = ' '.repeat(Math.max(0, tabWidth));
5938
+ const eol = source.includes('\r\n') ? '\r\n' : '\n';
5939
+ const lines = source.split(/\r?\n/);
5940
+ let depth = 0;
5941
+ let sensitive = null;
5942
+ /** Raw lines inside a reindented body (`stylesheet` / `script` / `style`). */
5943
+ let bodyBuf = null;
5944
+ const out = [];
5945
+ /** Open `{{#` comment: inner lines are never scanned for pieces. */
5946
+ const state = { inComment: false };
5947
+ for (const line of lines) {
5948
+ const leading = line.match(/^[ \t]*/)?.[0].length ?? 0;
5949
+ const content = line.slice(leading);
5950
+ const { pieces, prefixClosers } = scanLine(content, options, state);
5951
+ const activeSensitive = sensitive;
5952
+ const closesSensitive = activeSensitive !== null &&
5953
+ pieces.some((piece) => piece.kind === 'close' && piece.sensitive === activeSensitive);
5954
+ const reindentBody = activeSensitive === 'stylesheet' ||
5955
+ (activeSensitive !== null && REINDENT_HTML.has(activeSensitive));
5956
+ if (reindentBody && !closesSensitive) {
5957
+ // Do not applyDepth: bodies may contain nested Janja if/for tags.
5958
+ (bodyBuf ?? (bodyBuf = [])).push(line);
5959
+ continue;
5960
+ }
5961
+ if (bodyBuf && closesSensitive && reindentBody) {
5962
+ // stylesheet: same column as the tags; script/style: one level inside.
5963
+ const baseDepth = activeSensitive === 'stylesheet' ? Math.max(0, depth - 1) : depth;
5964
+ const raw = bodyBuf.join(eol);
5965
+ const formatted = activeSensitive === 'stylesheet'
5966
+ ? indentHtml(raw, options)
5967
+ : activeSensitive === 'style'
5968
+ ? indentCss(raw, tabWidth)
5969
+ : indentJs(raw, tabWidth);
5970
+ for (const bodyLine of formatted.split(/\r?\n/)) {
5971
+ out.push(bodyLine.length === 0 ? '' : pad.repeat(baseDepth) + bodyLine);
5972
+ }
5973
+ bodyBuf = null;
5974
+ }
5975
+ else if (sensitive && !closesSensitive) {
5976
+ out.push(line);
5977
+ applyDepth(pieces);
5978
+ continue;
5979
+ }
5980
+ const lineDepth = Math.max(0, depth - prefixClosers);
5981
+ const continuesSelfClosingTag = /^\/\s*>\s*$/.test(content);
5982
+ out.push(content.length === 0 ? '' : pad.repeat(lineDepth) + content);
5983
+ applyDepth(pieces);
5984
+ // The opening `<tag` was counted on an earlier line. A standalone `/>`
5985
+ // closes that element after this continuation line has been indented.
5986
+ if (continuesSelfClosingTag) {
5987
+ depth = Math.max(0, depth - 1);
5988
+ }
5989
+ }
5990
+ // split() keeps a final empty string when the source already ends in a
5991
+ // newline, so join() alone preserves that trailing newline — do not add
5992
+ // another one or each format grows an extra blank line.
5993
+ return out.join(eol);
5994
+ function applyDepth(pieces) {
5995
+ for (const piece of pieces) {
5996
+ if (piece.kind === 'open') {
5997
+ depth += 1;
5998
+ if (piece.sensitive && !sensitive) {
5999
+ ({ sensitive } = piece);
6000
+ }
6001
+ }
6002
+ else if (piece.kind === 'close') {
6003
+ depth = Math.max(0, depth - 1);
6004
+ if (piece.sensitive && sensitive === piece.sensitive) {
6005
+ sensitive = null;
6006
+ }
6007
+ }
6008
+ else if (piece.kind === 'mid') ;
6009
+ }
6010
+ }
6011
+ }
6012
+
6006
6013
  // An expression without the positions and spellings that formatting changes
6007
6014
  const shape = (exp) => JSON.stringify(exp, (key, value) => key === 'loc' || key === 'raw' ? undefined : (value ?? null));
6008
6015
  /**