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