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