janja 2.3.0 → 2.4.1

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
@@ -885,6 +885,7 @@ ${output
885
885
  }
886
886
 
887
887
  async function* parseComment({ val, loc, strip }) {
888
+ console.log(val);
888
889
  yield 'NEXT';
889
890
  yield new CommentNode(val, loc, strip);
890
891
  }
@@ -897,20 +898,20 @@ ${output
897
898
  parsers: parsers$9,
898
899
  };
899
900
 
900
- async function compileFor({ loop, body }, compiler) {
901
+ async function compileFor({ loop: { left, right, loc }, body }, compiler) {
901
902
  const { context } = compiler;
902
903
  const expCompiler = new ExpCompiler();
903
- if (loop.left.type === 'SEQ') {
904
- compiler.pushRaw(loop.loc, '{', `const a_=${expCompiler.compile(loop.right, context)},n_=a_.length;`, 'let i_=0;', `for(const {${loop.left.elements
904
+ if (left.type === 'SEQ') {
905
+ compiler.pushRaw(loc, '{', `const a_=${expCompiler.compile(right, context)},n_=a_.length;`, 'let i_=0;', `for(const {${left.elements
905
906
  .map((el) => el.type === 'ASSIGN'
906
907
  ? `${el.left.value}=${expCompiler.compile(el.right, context)}`
907
908
  : el.value)
908
- .join(',')}} of a_){`, `const ${compiler.in()}={`, `...${context},`, 'loop:{', 'first:i_===0,', 'index:i_++,', 'last:i_===n_,', '},', `${loop.left.elements
909
+ .join(',')}} of a_){`, `const ${compiler.in()}={`, `...${context},`, 'loop:{', 'first:i_===0,', 'index:i_++,', 'last:i_===n_,', '},', `${left.elements
909
910
  .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
910
911
  .join(',')},`, '};');
911
912
  }
912
913
  else {
913
- compiler.pushRaw(loop.loc, '{', `const a_=${expCompiler.compile(loop.right, context)},n_=a_.length;`, 'let i_=0;', `for(const ${loop.left.value} of a_){`, `const ${compiler.in()}={`, `...${context},`, 'loop:{', 'first:i_===0,', 'index:i_++,', 'last:i_===n_,', '},', `${loop.left.value},`, '};');
914
+ compiler.pushRaw(loc, '{', `const a_=${expCompiler.compile(right, context)},n_=a_.length;`, 'let i_=0;', `for(const ${left.value} of a_){`, `const ${compiler.in()}={`, `...${context},`, 'loop:{', 'first:i_===0,', 'index:i_++,', 'last:i_===n_,', '},', `${left.value},`, '};');
914
915
  }
915
916
  await compiler.compileNodes(body);
916
917
  compiler.out();
@@ -1640,11 +1641,11 @@ ${output
1640
1641
  parsers: parsers$2,
1641
1642
  };
1642
1643
 
1643
- async function compileUnexpected$1({ name, val, loc }, compiler) {
1644
- compiler.pushRaw(loc, `<!-- unexpected "${name}" with value "${val}" -->`);
1644
+ async function compileUnexpected({ name, loc }, compiler) {
1645
+ compiler.pushRaw(loc, `<!--Unexpected "${name}"-->`);
1645
1646
  }
1646
1647
  const compilers$1 = {
1647
- UNEXPECTED: compileUnexpected$1,
1648
+ UNEXPECTED: compileUnexpected,
1648
1649
  };
1649
1650
 
1650
1651
  class UnexpectedNode extends Traversal {
@@ -1685,7 +1686,7 @@ ${output
1685
1686
 
1686
1687
  async function* parseUnexpected(token, parser) {
1687
1688
  const name = token.name ?? token.type;
1688
- parser.options.debug?.(new CompileError(`Unexpected "${name}" node`, parser.template, token.loc));
1689
+ parser.options.debug?.(new CompileError(`Unexpected "${name}"`, parser.template, token.loc));
1689
1690
  yield 'NEXT';
1690
1691
  yield new UnexpectedNode(name, token.val, token.loc, token.strip);
1691
1692
  }
@@ -1698,11 +1699,11 @@ ${output
1698
1699
  parsers: parsers$1,
1699
1700
  };
1700
1701
 
1701
- async function compileUnexpected({ name, val, loc }, compiler) {
1702
- compiler.pushRaw(loc, `<!-- unexpected "${name}" with value "${val}" -->`);
1702
+ async function compileUnknown({ name, loc }, compiler) {
1703
+ compiler.pushRaw(loc, `<!--Unknown "${name}"-->`);
1703
1704
  }
1704
1705
  const compilers = {
1705
- UNEXPECTED: compileUnexpected,
1706
+ UNKNOWN: compileUnknown,
1706
1707
  };
1707
1708
 
1708
1709
  class UnknownNode extends Traversal {
@@ -1743,7 +1744,7 @@ ${output
1743
1744
 
1744
1745
  async function* parseUnknown(token, parser) {
1745
1746
  const name = token.name ?? token.type;
1746
- parser.options.debug?.(new CompileError(`Unknown "${name}" node`, parser.template, token.loc));
1747
+ parser.options.debug?.(new CompileError(`Unknown "${name}"`, parser.template, token.loc));
1747
1748
  yield 'NEXT';
1748
1749
  yield new UnknownNode(name, token.val, token.loc, token.strip);
1749
1750
  }
@@ -1782,7 +1783,7 @@ ${output
1782
1783
  const parserOptions = {
1783
1784
  ...baseOptions,
1784
1785
  commentOpen: '{{#',
1785
- commentClose: '}}',
1786
+ commentClose: '#}}',
1786
1787
  directiveOpen: '{{',
1787
1788
  directiveClose: '}}',
1788
1789
  outputOpen: '{{=',
@@ -1817,6 +1818,20 @@ ${output
1817
1818
  return mergedOptions;
1818
1819
  };
1819
1820
 
1821
+ class ExpError extends SyntaxError {
1822
+ constructor(message, loc) {
1823
+ super(message);
1824
+ Object.defineProperty(this, "loc", {
1825
+ enumerable: true,
1826
+ configurable: true,
1827
+ writable: true,
1828
+ value: loc
1829
+ });
1830
+ this.name = 'ExpError';
1831
+ ExpError.captureStackTrace?.(this, this.constructor);
1832
+ }
1833
+ }
1834
+
1820
1835
  function updatePosition(str, pos) {
1821
1836
  const newlines = (str.match(/\n/g) || []).length;
1822
1837
  if (newlines > 0) {
@@ -1865,14 +1880,22 @@ ${output
1865
1880
  ',': 'COMMA',
1866
1881
  '.': 'DOT',
1867
1882
  };
1883
+ function normalizeLitValue(value) {
1884
+ switch (value) {
1885
+ case 'true':
1886
+ return true;
1887
+ case 'false':
1888
+ return false;
1889
+ case 'null':
1890
+ return null;
1891
+ case 'undefined':
1892
+ return undefined;
1893
+ default:
1894
+ return value;
1895
+ }
1896
+ }
1868
1897
  class ExpTokenizer {
1869
- constructor(template) {
1870
- Object.defineProperty(this, "template", {
1871
- enumerable: true,
1872
- configurable: true,
1873
- writable: true,
1874
- value: template
1875
- });
1898
+ constructor() {
1876
1899
  Object.defineProperty(this, "val", {
1877
1900
  enumerable: true,
1878
1901
  configurable: true,
@@ -1927,19 +1950,19 @@ ${output
1927
1950
  this.readString(char);
1928
1951
  continue;
1929
1952
  }
1930
- if (isDigit(char)) {
1931
- this.readNumber();
1953
+ if (isDigitStart(char)) {
1954
+ this.readNumber(char);
1932
1955
  continue;
1933
1956
  }
1934
- if (isIdentifierStartChar(char)) {
1935
- this.readIdentifier();
1957
+ if (isIdentifierStart(char)) {
1958
+ this.readIdentifier(char);
1936
1959
  continue;
1937
1960
  }
1938
1961
  if (isSymbol(char)) {
1939
1962
  this.readSymbol(char);
1940
1963
  continue;
1941
1964
  }
1942
- throw new CompileError(`Unexpect "${char}"`, this.template, {
1965
+ throw new ExpError(`Unexpect "${char}"`, {
1943
1966
  start: { line: this.line, column: this.column },
1944
1967
  end: { line: this.line, column: this.column + 1 },
1945
1968
  });
@@ -1982,10 +2005,11 @@ ${output
1982
2005
  raw: `${quoteChar}${value}${quoteChar}`,
1983
2006
  });
1984
2007
  }
1985
- readNumber() {
1986
- let value = '';
2008
+ readNumber(startChar) {
2009
+ let value = startChar;
1987
2010
  let hasDot = false;
1988
2011
  const start = { line: this.line, column: this.column };
2012
+ this.index++;
1989
2013
  while (this.index < this.val.length) {
1990
2014
  const char = this.val[this.index];
1991
2015
  if (isDigit(char)) {
@@ -2010,12 +2034,13 @@ ${output
2010
2034
  raw: value,
2011
2035
  });
2012
2036
  }
2013
- readIdentifier() {
2014
- let value = '';
2037
+ readIdentifier(startChar) {
2038
+ let value = startChar;
2015
2039
  const start = { line: this.line, column: this.column };
2040
+ this.index++;
2016
2041
  while (this.index < this.val.length) {
2017
2042
  const char = this.val[this.index];
2018
- if (isIdentifierChar(char)) {
2043
+ if (isIdentifier(char)) {
2019
2044
  value += char;
2020
2045
  this.index++;
2021
2046
  continue;
@@ -2027,17 +2052,7 @@ ${output
2027
2052
  : 'ID';
2028
2053
  this.tokens.push({
2029
2054
  type,
2030
- value: type === 'LIT'
2031
- ? value === 'true'
2032
- ? true
2033
- : value === 'false'
2034
- ? false
2035
- : value === 'null'
2036
- ? null
2037
- : value === 'undefined'
2038
- ? undefined
2039
- : value
2040
- : value,
2055
+ value: type === 'LIT' ? normalizeLitValue(value) : value,
2041
2056
  loc: {
2042
2057
  start,
2043
2058
  end: updatePosition(value, this),
@@ -2065,13 +2080,16 @@ ${output
2065
2080
  function isSymbol(char) {
2066
2081
  return /[+\-*/%|=(),.]/.test(char);
2067
2082
  }
2068
- function isDigit(char) {
2083
+ function isDigitStart(char) {
2069
2084
  return /[-\d]/.test(char);
2070
2085
  }
2071
- function isIdentifierStartChar(char) {
2086
+ function isDigit(char) {
2087
+ return /\d/.test(char);
2088
+ }
2089
+ function isIdentifierStart(char) {
2072
2090
  return /[a-z_$]/i.test(char);
2073
2091
  }
2074
- function isIdentifierChar(char) {
2092
+ function isIdentifier(char) {
2075
2093
  return /[\w$]/.test(char);
2076
2094
  }
2077
2095
 
@@ -2106,13 +2124,7 @@ ${output
2106
2124
  LIT: 90,
2107
2125
  };
2108
2126
  class ExpParser {
2109
- constructor(template) {
2110
- Object.defineProperty(this, "template", {
2111
- enumerable: true,
2112
- configurable: true,
2113
- writable: true,
2114
- value: template
2115
- });
2127
+ constructor() {
2116
2128
  Object.defineProperty(this, "val", {
2117
2129
  enumerable: true,
2118
2130
  configurable: true,
@@ -2149,7 +2161,7 @@ ${output
2149
2161
  this.index = 0;
2150
2162
  this.line = loc.start.line;
2151
2163
  this.column = loc.start.column;
2152
- this.tokens = new ExpTokenizer(this.template).tokenize(val, loc);
2164
+ this.tokens = new ExpTokenizer().tokenize(val, loc);
2153
2165
  return this.val ? this.parseExp() : null;
2154
2166
  }
2155
2167
  parseExp(checker) {
@@ -2169,12 +2181,12 @@ ${output
2169
2181
  }
2170
2182
  if (token.type === 'LP') {
2171
2183
  if (!this.peek()) {
2172
- throw new CompileError('unexpected end of expression', this.template, token.loc);
2184
+ throw new ExpError('Unexpected end of expression', token.loc);
2173
2185
  }
2174
2186
  const elements = this.parseSequence();
2175
2187
  const rp = this.consume('RP');
2176
2188
  if (!rp) {
2177
- throw new CompileError('expected "RP" after "LP"', this.template, token.loc);
2189
+ throw new ExpError('Expected "RP" after "LP"', token.loc);
2178
2190
  }
2179
2191
  left = {
2180
2192
  ...token,
@@ -2189,7 +2201,7 @@ ${output
2189
2201
  }
2190
2202
  if (token.type === 'NOT') {
2191
2203
  if (!this.peek()) {
2192
- throw new CompileError('unexpected end of expression', this.template, token.loc);
2204
+ throw new ExpError('Unexpected end of expression', token.loc);
2193
2205
  }
2194
2206
  left = {
2195
2207
  ...token,
@@ -2211,7 +2223,7 @@ ${output
2211
2223
  if (lp) {
2212
2224
  left.args = this.parseSequence();
2213
2225
  if (!this.consume('RP')) {
2214
- throw new CompileError('expected "RP" after "LP"', this.template, lp.loc);
2226
+ throw new ExpError('Expected "RP" after "LP"', lp.loc);
2215
2227
  }
2216
2228
  continue;
2217
2229
  }
@@ -2227,7 +2239,7 @@ ${output
2227
2239
  if (token.type === 'IF') {
2228
2240
  const test = this.parseExp((t) => t.type === 'ELSE' ? 'BACK' : isHigher(token, t) ? 'BACK' : undefined);
2229
2241
  if (!test) {
2230
- throw new CompileError('expected test expression', this.template, token.loc);
2242
+ throw new ExpError('Expected test expression', token.loc);
2231
2243
  }
2232
2244
  left = {
2233
2245
  ...token,
@@ -2238,7 +2250,7 @@ ${output
2238
2250
  if (this.consume('ELSE')) {
2239
2251
  const alternative = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
2240
2252
  if (!alternative) {
2241
- throw new CompileError('expected alternative expression', this.template, token.loc);
2253
+ throw new ExpError('Expected alternative expression', token.loc);
2242
2254
  }
2243
2255
  left.alternative = alternative;
2244
2256
  }
@@ -2263,11 +2275,11 @@ ${output
2263
2275
  token.type === 'MOD' ||
2264
2276
  token.type === 'ASSIGN') {
2265
2277
  if (!left) {
2266
- throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
2278
+ throw new ExpError(`No left operand for "${token.type}"`, token.loc);
2267
2279
  }
2268
2280
  const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
2269
2281
  if (!right) {
2270
- throw new CompileError(`no right operand for "${token.type}"`, this.template, token.loc);
2282
+ throw new ExpError(`No right operand for "${token.type}"`, token.loc);
2271
2283
  }
2272
2284
  left = {
2273
2285
  ...token,
@@ -2279,10 +2291,10 @@ ${output
2279
2291
  }
2280
2292
  if (token.type === 'PIPE') {
2281
2293
  if (!left) {
2282
- throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
2294
+ throw new ExpError(`No left operand for "${token.type}"`, token.loc);
2283
2295
  }
2284
2296
  if (!this.match('ID')) {
2285
- throw new CompileError('expected "ID" after "PIPE"', this.template, token.loc);
2297
+ throw new ExpError('Expected "ID" after "PIPE"', token.loc);
2286
2298
  }
2287
2299
  left = {
2288
2300
  ...token,
@@ -2306,10 +2318,10 @@ ${output
2306
2318
  }
2307
2319
  const token = this.next();
2308
2320
  if (!token) {
2309
- throw new CompileError('expected "ID" after "DOT"', this.template, dot.loc);
2321
+ throw new ExpError('Expected "ID" after "DOT"', dot.loc);
2310
2322
  }
2311
2323
  if (token.type !== 'ID') {
2312
- throw new CompileError('expected "ID" after "DOT"', this.template, dot.loc);
2324
+ throw new ExpError('Expected "ID" after "DOT"', dot.loc);
2313
2325
  }
2314
2326
  ids.push({
2315
2327
  ...token,
@@ -2346,7 +2358,7 @@ ${output
2346
2358
  right.args = args;
2347
2359
  }
2348
2360
  if (!this.consume('RP')) {
2349
- throw new CompileError('expected "RP" after "LP"', this.template, lp.loc);
2361
+ throw new ExpError('Expected "RP" after "LP"', lp.loc);
2350
2362
  }
2351
2363
  }
2352
2364
  return right;
@@ -2578,7 +2590,7 @@ ${output
2578
2590
  this.tokenize(template);
2579
2591
  const start = this.tokens[0]?.loc.start ?? { line: 1, column: 1 };
2580
2592
  const end = this.tokens.at(-1)?.loc.end ?? { line: 1, column: 1 };
2581
- this.expParser = new ExpParser(template);
2593
+ this.expParser = new ExpParser();
2582
2594
  this.cursor = 0;
2583
2595
  return new RootNode(await this.parseUntil(), { start, end });
2584
2596
  }
@@ -2627,7 +2639,14 @@ ${output
2627
2639
  return nodes;
2628
2640
  }
2629
2641
  parseExp({ val, loc }) {
2630
- return this.expParser.parse(val, loc);
2642
+ try {
2643
+ return this.expParser.parse(val, loc);
2644
+ }
2645
+ catch (error) {
2646
+ this.options.debug(error.loc
2647
+ ? new CompileError(error.message, this.template, error.loc)
2648
+ : error);
2649
+ }
2631
2650
  }
2632
2651
  peek() {
2633
2652
  return this.tokens.at(this.cursor) ?? null;
@@ -2639,7 +2658,7 @@ ${output
2639
2658
  return names.includes(token?.name);
2640
2659
  }
2641
2660
  emitExpErr({ name, loc }, required = true) {
2642
- this.options.debug?.(new CompileError(required
2661
+ this.options.debug(new CompileError(required
2643
2662
  ? `"${name}" requires expression`
2644
2663
  : `"${name}" should not have expression`, this.template, loc));
2645
2664
  }
@@ -2738,7 +2757,7 @@ ${output
2738
2757
  template = await this.options.loader(path);
2739
2758
  }
2740
2759
  catch {
2741
- this.options.debug?.(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
2760
+ this.options.debug(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
2742
2761
  return;
2743
2762
  }
2744
2763
  if (template) {
@@ -2799,7 +2818,7 @@ ${output
2799
2818
  this.partials.set(path, code);
2800
2819
  }
2801
2820
  catch {
2802
- this.options.debug?.(new CompileError('Failed to compile template', this.template, node.loc));
2821
+ this.options.debug(new CompileError('Failed to compile template', this.template, node.loc));
2803
2822
  }
2804
2823
  }
2805
2824
  }
@@ -2881,7 +2900,20 @@ ${output
2881
2900
  return new Renderer(options).renderFile(filepath, data);
2882
2901
  }
2883
2902
 
2903
+ exports.CompileError = CompileError;
2904
+ exports.Compiler = Compiler;
2905
+ exports.ExpCompiler = ExpCompiler;
2906
+ exports.ExpError = ExpError;
2907
+ exports.ExpParser = ExpParser;
2908
+ exports.ExpTokenizer = ExpTokenizer;
2909
+ exports.Parser = Parser;
2910
+ exports.RenderError = RenderError;
2884
2911
  exports.Renderer = Renderer;
2912
+ exports.RootNode = RootNode;
2913
+ exports.Safe = Safe;
2914
+ exports.Tokenizer = Tokenizer;
2915
+ exports.Traversal = Traversal;
2916
+ exports.expTokenTypes = expTokenTypes;
2885
2917
  exports.render = render;
2886
2918
  exports.renderFile = renderFile;
2887
2919
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "janja",
3
3
  "type": "module",
4
- "version": "2.3.0",
4
+ "version": "2.4.1",
5
5
  "packageManager": "yarn@4.9.2",
6
6
  "description": "A simple, fast, lightweight, and extensible template engine for Node.js and browsers.",
7
7
  "license": "MIT",
@@ -30,8 +30,8 @@
30
30
  "@rollup/plugin-node-resolve": "^16.0.3",
31
31
  "@rollup/plugin-typescript": "^12.3.0",
32
32
  "@types/node": "^24.10.1",
33
- "@vitest/coverage-v8": "4.0.14",
34
- "@vitest/ui": "4.0.14",
33
+ "@vitest/coverage-v8": "4.0.15",
34
+ "@vitest/ui": "4.0.15",
35
35
  "eslint": "^9.39.1",
36
36
  "eslint-import-resolver-typescript": "^4.4.4",
37
37
  "eslint-plugin-import": "^2.32.0",
@@ -40,13 +40,13 @@
40
40
  "globals": "^16.5.0",
41
41
  "husky": "^9.1.7",
42
42
  "jiti": "^2.6.1",
43
- "prettier": "^3.7.3",
43
+ "prettier": "^3.7.4",
44
44
  "rollup": "^4.53.3",
45
45
  "rollup-plugin-dts": "^6.3.0",
46
46
  "tslib": "^2.8.1",
47
47
  "typescript": "^5.9.3",
48
- "typescript-eslint": "^8.48.0",
49
- "vite": "^7.2.4",
50
- "vitest": "^4.0.14"
48
+ "typescript-eslint": "^8.48.1",
49
+ "vite": "^7.2.6",
50
+ "vitest": "^4.0.15"
51
51
  }
52
52
  }