janja 2.4.0 → 2.4.2

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,7 +885,6 @@ ${output
885
885
  }
886
886
 
887
887
  async function* parseComment({ val, loc, strip }) {
888
- console.log(val);
889
888
  yield 'NEXT';
890
889
  yield new CommentNode(val, loc, strip);
891
890
  }
@@ -1346,17 +1345,22 @@ ${output
1346
1345
  parsers: parsers$6,
1347
1346
  };
1348
1347
 
1349
- async function compileMacro({ val: { left, right }, loc, body }, compiler) {
1348
+ async function compileMacro({ val, loc, body }, compiler) {
1350
1349
  const { context } = compiler;
1351
- const { elements } = right;
1352
1350
  const expCompiler = new ExpCompiler();
1353
- compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${elements
1354
- .map((el) => el.type === 'ASSIGN'
1355
- ? `${el.left.value}=${expCompiler.compile(el.right)}`
1356
- : el.value)
1357
- .join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
1358
- .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1359
- .join(','), '};');
1351
+ if (val.type === 'ID') {
1352
+ compiler.pushRaw(loc, `${expCompiler.compile(val)}=()=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, '};');
1353
+ }
1354
+ else {
1355
+ const { left, right: { elements }, } = val;
1356
+ compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${elements
1357
+ .map((el) => el.type === 'ASSIGN'
1358
+ ? `${el.left.value}=${expCompiler.compile(el.right)}`
1359
+ : el.value)
1360
+ .join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
1361
+ .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1362
+ .join(','), '};');
1363
+ }
1360
1364
  await compiler.compileNodes(body);
1361
1365
  compiler.out();
1362
1366
  compiler.pushRaw(null, '};');
@@ -1450,7 +1454,36 @@ ${output
1450
1454
  else {
1451
1455
  throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
1452
1456
  }
1453
- yield new MacroNode(parser.parseExp(token.expression), body, token.loc, token.strip);
1457
+ const val = parser.parseExp(token.expression);
1458
+ if (val.type === 'ASSIGN') {
1459
+ if (val.left.type !== 'ID') {
1460
+ parser.options.debug?.(new CompileError('Invalid macro name', parser.template, token.loc));
1461
+ return;
1462
+ }
1463
+ if (val.right.type !== 'SEQ') {
1464
+ parser.options.debug?.(new CompileError('Invalid macro parameters', parser.template, token.loc));
1465
+ return;
1466
+ }
1467
+ else {
1468
+ for (const element of val.right.elements) {
1469
+ if (element.type === 'ASSIGN') {
1470
+ if (element.left.type !== 'ID') {
1471
+ parser.options.debug?.(new CompileError('Invalid parameter name', parser.template, token.loc));
1472
+ return;
1473
+ }
1474
+ }
1475
+ else if (element.type !== 'ID') {
1476
+ parser.options.debug?.(new CompileError('Invalid macro parameter', parser.template, token.loc));
1477
+ return;
1478
+ }
1479
+ }
1480
+ }
1481
+ }
1482
+ else if (val.type !== 'ID') {
1483
+ parser.options.debug?.(new CompileError('Invalid macro definition', parser.template, token.loc));
1484
+ return;
1485
+ }
1486
+ yield new MacroNode(val, body, token.loc, token.strip);
1454
1487
  }
1455
1488
  async function* parseCaller(token, parser) {
1456
1489
  if (token.expression) {
@@ -1818,6 +1851,20 @@ ${output
1818
1851
  return mergedOptions;
1819
1852
  };
1820
1853
 
1854
+ class ExpError extends SyntaxError {
1855
+ constructor(message, loc) {
1856
+ super(message);
1857
+ Object.defineProperty(this, "loc", {
1858
+ enumerable: true,
1859
+ configurable: true,
1860
+ writable: true,
1861
+ value: loc
1862
+ });
1863
+ this.name = 'ExpError';
1864
+ ExpError.captureStackTrace?.(this, this.constructor);
1865
+ }
1866
+ }
1867
+
1821
1868
  function updatePosition(str, pos) {
1822
1869
  const newlines = (str.match(/\n/g) || []).length;
1823
1870
  if (newlines > 0) {
@@ -1866,14 +1913,22 @@ ${output
1866
1913
  ',': 'COMMA',
1867
1914
  '.': 'DOT',
1868
1915
  };
1916
+ function normalizeLitValue(value) {
1917
+ switch (value) {
1918
+ case 'true':
1919
+ return true;
1920
+ case 'false':
1921
+ return false;
1922
+ case 'null':
1923
+ return null;
1924
+ case 'undefined':
1925
+ return undefined;
1926
+ default:
1927
+ return value;
1928
+ }
1929
+ }
1869
1930
  class ExpTokenizer {
1870
- constructor(template) {
1871
- Object.defineProperty(this, "template", {
1872
- enumerable: true,
1873
- configurable: true,
1874
- writable: true,
1875
- value: template
1876
- });
1931
+ constructor() {
1877
1932
  Object.defineProperty(this, "val", {
1878
1933
  enumerable: true,
1879
1934
  configurable: true,
@@ -1928,19 +1983,19 @@ ${output
1928
1983
  this.readString(char);
1929
1984
  continue;
1930
1985
  }
1931
- if (isDigit(char)) {
1932
- this.readNumber();
1986
+ if (isDigitStart(char)) {
1987
+ this.readNumber(char);
1933
1988
  continue;
1934
1989
  }
1935
- if (isIdentifierStartChar(char)) {
1936
- this.readIdentifier();
1990
+ if (isIdentifierStart(char)) {
1991
+ this.readIdentifier(char);
1937
1992
  continue;
1938
1993
  }
1939
1994
  if (isSymbol(char)) {
1940
1995
  this.readSymbol(char);
1941
1996
  continue;
1942
1997
  }
1943
- throw new CompileError(`Unexpect "${char}"`, this.template, {
1998
+ throw new ExpError(`Unexpect "${char}"`, {
1944
1999
  start: { line: this.line, column: this.column },
1945
2000
  end: { line: this.line, column: this.column + 1 },
1946
2001
  });
@@ -1983,10 +2038,11 @@ ${output
1983
2038
  raw: `${quoteChar}${value}${quoteChar}`,
1984
2039
  });
1985
2040
  }
1986
- readNumber() {
1987
- let value = '';
2041
+ readNumber(startChar) {
2042
+ let value = startChar;
1988
2043
  let hasDot = false;
1989
2044
  const start = { line: this.line, column: this.column };
2045
+ this.index++;
1990
2046
  while (this.index < this.val.length) {
1991
2047
  const char = this.val[this.index];
1992
2048
  if (isDigit(char)) {
@@ -2011,12 +2067,13 @@ ${output
2011
2067
  raw: value,
2012
2068
  });
2013
2069
  }
2014
- readIdentifier() {
2015
- let value = '';
2070
+ readIdentifier(startChar) {
2071
+ let value = startChar;
2016
2072
  const start = { line: this.line, column: this.column };
2073
+ this.index++;
2017
2074
  while (this.index < this.val.length) {
2018
2075
  const char = this.val[this.index];
2019
- if (isIdentifierChar(char)) {
2076
+ if (isIdentifier(char)) {
2020
2077
  value += char;
2021
2078
  this.index++;
2022
2079
  continue;
@@ -2028,17 +2085,7 @@ ${output
2028
2085
  : 'ID';
2029
2086
  this.tokens.push({
2030
2087
  type,
2031
- value: type === 'LIT'
2032
- ? value === 'true'
2033
- ? true
2034
- : value === 'false'
2035
- ? false
2036
- : value === 'null'
2037
- ? null
2038
- : value === 'undefined'
2039
- ? undefined
2040
- : value
2041
- : value,
2088
+ value: type === 'LIT' ? normalizeLitValue(value) : value,
2042
2089
  loc: {
2043
2090
  start,
2044
2091
  end: updatePosition(value, this),
@@ -2066,13 +2113,16 @@ ${output
2066
2113
  function isSymbol(char) {
2067
2114
  return /[+\-*/%|=(),.]/.test(char);
2068
2115
  }
2069
- function isDigit(char) {
2116
+ function isDigitStart(char) {
2070
2117
  return /[-\d]/.test(char);
2071
2118
  }
2072
- function isIdentifierStartChar(char) {
2119
+ function isDigit(char) {
2120
+ return /\d/.test(char);
2121
+ }
2122
+ function isIdentifierStart(char) {
2073
2123
  return /[a-z_$]/i.test(char);
2074
2124
  }
2075
- function isIdentifierChar(char) {
2125
+ function isIdentifier(char) {
2076
2126
  return /[\w$]/.test(char);
2077
2127
  }
2078
2128
 
@@ -2107,13 +2157,7 @@ ${output
2107
2157
  LIT: 90,
2108
2158
  };
2109
2159
  class ExpParser {
2110
- constructor(template) {
2111
- Object.defineProperty(this, "template", {
2112
- enumerable: true,
2113
- configurable: true,
2114
- writable: true,
2115
- value: template
2116
- });
2160
+ constructor() {
2117
2161
  Object.defineProperty(this, "val", {
2118
2162
  enumerable: true,
2119
2163
  configurable: true,
@@ -2150,7 +2194,7 @@ ${output
2150
2194
  this.index = 0;
2151
2195
  this.line = loc.start.line;
2152
2196
  this.column = loc.start.column;
2153
- this.tokens = new ExpTokenizer(this.template).tokenize(val, loc);
2197
+ this.tokens = new ExpTokenizer().tokenize(val, loc);
2154
2198
  return this.val ? this.parseExp() : null;
2155
2199
  }
2156
2200
  parseExp(checker) {
@@ -2170,12 +2214,12 @@ ${output
2170
2214
  }
2171
2215
  if (token.type === 'LP') {
2172
2216
  if (!this.peek()) {
2173
- throw new CompileError('unexpected end of expression', this.template, token.loc);
2217
+ throw new ExpError('Unexpected end of expression', token.loc);
2174
2218
  }
2175
2219
  const elements = this.parseSequence();
2176
2220
  const rp = this.consume('RP');
2177
2221
  if (!rp) {
2178
- throw new CompileError('expected "RP" after "LP"', this.template, token.loc);
2222
+ throw new ExpError('Expected "RP" after "LP"', token.loc);
2179
2223
  }
2180
2224
  left = {
2181
2225
  ...token,
@@ -2190,7 +2234,7 @@ ${output
2190
2234
  }
2191
2235
  if (token.type === 'NOT') {
2192
2236
  if (!this.peek()) {
2193
- throw new CompileError('unexpected end of expression', this.template, token.loc);
2237
+ throw new ExpError('Unexpected end of expression', token.loc);
2194
2238
  }
2195
2239
  left = {
2196
2240
  ...token,
@@ -2212,7 +2256,7 @@ ${output
2212
2256
  if (lp) {
2213
2257
  left.args = this.parseSequence();
2214
2258
  if (!this.consume('RP')) {
2215
- throw new CompileError('expected "RP" after "LP"', this.template, lp.loc);
2259
+ throw new ExpError('Expected "RP" after "LP"', lp.loc);
2216
2260
  }
2217
2261
  continue;
2218
2262
  }
@@ -2228,7 +2272,7 @@ ${output
2228
2272
  if (token.type === 'IF') {
2229
2273
  const test = this.parseExp((t) => t.type === 'ELSE' ? 'BACK' : isHigher(token, t) ? 'BACK' : undefined);
2230
2274
  if (!test) {
2231
- throw new CompileError('expected test expression', this.template, token.loc);
2275
+ throw new ExpError('Expected test expression', token.loc);
2232
2276
  }
2233
2277
  left = {
2234
2278
  ...token,
@@ -2239,7 +2283,7 @@ ${output
2239
2283
  if (this.consume('ELSE')) {
2240
2284
  const alternative = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
2241
2285
  if (!alternative) {
2242
- throw new CompileError('expected alternative expression', this.template, token.loc);
2286
+ throw new ExpError('Expected alternative expression', token.loc);
2243
2287
  }
2244
2288
  left.alternative = alternative;
2245
2289
  }
@@ -2264,11 +2308,21 @@ ${output
2264
2308
  token.type === 'MOD' ||
2265
2309
  token.type === 'ASSIGN') {
2266
2310
  if (!left) {
2267
- throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
2311
+ throw new ExpError(`No left operand for "${token.type}"`, token.loc);
2312
+ }
2313
+ if (token.type === 'ASSIGN') {
2314
+ if (left.type === 'SEQ') {
2315
+ if (left.elements.some((el) => el.type !== 'ID')) {
2316
+ throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
2317
+ }
2318
+ }
2319
+ else if (left.type !== 'ID') {
2320
+ throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
2321
+ }
2268
2322
  }
2269
2323
  const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
2270
2324
  if (!right) {
2271
- throw new CompileError(`no right operand for "${token.type}"`, this.template, token.loc);
2325
+ throw new ExpError(`No right operand for "${token.type}"`, token.loc);
2272
2326
  }
2273
2327
  left = {
2274
2328
  ...token,
@@ -2280,10 +2334,10 @@ ${output
2280
2334
  }
2281
2335
  if (token.type === 'PIPE') {
2282
2336
  if (!left) {
2283
- throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
2337
+ throw new ExpError(`No left operand for "${token.type}"`, token.loc);
2284
2338
  }
2285
2339
  if (!this.match('ID')) {
2286
- throw new CompileError('expected "ID" after "PIPE"', this.template, token.loc);
2340
+ throw new ExpError('Expected "ID" after "PIPE"', token.loc);
2287
2341
  }
2288
2342
  left = {
2289
2343
  ...token,
@@ -2307,10 +2361,10 @@ ${output
2307
2361
  }
2308
2362
  const token = this.next();
2309
2363
  if (!token) {
2310
- throw new CompileError('expected "ID" after "DOT"', this.template, dot.loc);
2364
+ throw new ExpError('Expected "ID" after "DOT"', dot.loc);
2311
2365
  }
2312
2366
  if (token.type !== 'ID') {
2313
- throw new CompileError('expected "ID" after "DOT"', this.template, dot.loc);
2367
+ throw new ExpError('Expected "ID" after "DOT"', dot.loc);
2314
2368
  }
2315
2369
  ids.push({
2316
2370
  ...token,
@@ -2347,7 +2401,7 @@ ${output
2347
2401
  right.args = args;
2348
2402
  }
2349
2403
  if (!this.consume('RP')) {
2350
- throw new CompileError('expected "RP" after "LP"', this.template, lp.loc);
2404
+ throw new ExpError('Expected "RP" after "LP"', lp.loc);
2351
2405
  }
2352
2406
  }
2353
2407
  return right;
@@ -2579,7 +2633,7 @@ ${output
2579
2633
  this.tokenize(template);
2580
2634
  const start = this.tokens[0]?.loc.start ?? { line: 1, column: 1 };
2581
2635
  const end = this.tokens.at(-1)?.loc.end ?? { line: 1, column: 1 };
2582
- this.expParser = new ExpParser(template);
2636
+ this.expParser = new ExpParser();
2583
2637
  this.cursor = 0;
2584
2638
  return new RootNode(await this.parseUntil(), { start, end });
2585
2639
  }
@@ -2628,7 +2682,14 @@ ${output
2628
2682
  return nodes;
2629
2683
  }
2630
2684
  parseExp({ val, loc }) {
2631
- return this.expParser.parse(val, loc);
2685
+ try {
2686
+ return this.expParser.parse(val, loc);
2687
+ }
2688
+ catch (error) {
2689
+ this.options.debug(error.loc
2690
+ ? new CompileError(error.message, this.template, error.loc)
2691
+ : error);
2692
+ }
2632
2693
  }
2633
2694
  peek() {
2634
2695
  return this.tokens.at(this.cursor) ?? null;
@@ -2640,7 +2701,7 @@ ${output
2640
2701
  return names.includes(token?.name);
2641
2702
  }
2642
2703
  emitExpErr({ name, loc }, required = true) {
2643
- this.options.debug?.(new CompileError(required
2704
+ this.options.debug(new CompileError(required
2644
2705
  ? `"${name}" requires expression`
2645
2706
  : `"${name}" should not have expression`, this.template, loc));
2646
2707
  }
@@ -2739,7 +2800,7 @@ ${output
2739
2800
  template = await this.options.loader(path);
2740
2801
  }
2741
2802
  catch {
2742
- this.options.debug?.(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
2803
+ this.options.debug(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
2743
2804
  return;
2744
2805
  }
2745
2806
  if (template) {
@@ -2800,7 +2861,7 @@ ${output
2800
2861
  this.partials.set(path, code);
2801
2862
  }
2802
2863
  catch {
2803
- this.options.debug?.(new CompileError('Failed to compile template', this.template, node.loc));
2864
+ this.options.debug(new CompileError('Failed to compile template', this.template, node.loc));
2804
2865
  }
2805
2866
  }
2806
2867
  }
@@ -2820,7 +2881,7 @@ ${output
2820
2881
  writable: true,
2821
2882
  value: getLoc
2822
2883
  });
2823
- this.name = 'CompileError';
2884
+ this.name = 'RenderError';
2824
2885
  RenderError.captureStackTrace?.(this, this.constructor);
2825
2886
  }
2826
2887
  get details() {
@@ -2882,7 +2943,20 @@ ${output
2882
2943
  return new Renderer(options).renderFile(filepath, data);
2883
2944
  }
2884
2945
 
2946
+ exports.CompileError = CompileError;
2947
+ exports.Compiler = Compiler;
2948
+ exports.ExpCompiler = ExpCompiler;
2949
+ exports.ExpError = ExpError;
2950
+ exports.ExpParser = ExpParser;
2951
+ exports.ExpTokenizer = ExpTokenizer;
2952
+ exports.Parser = Parser;
2953
+ exports.RenderError = RenderError;
2885
2954
  exports.Renderer = Renderer;
2955
+ exports.RootNode = RootNode;
2956
+ exports.Safe = Safe;
2957
+ exports.Tokenizer = Tokenizer;
2958
+ exports.Traversal = Traversal;
2959
+ exports.expTokenTypes = expTokenTypes;
2886
2960
  exports.render = render;
2887
2961
  exports.renderFile = renderFile;
2888
2962
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "janja",
3
3
  "type": "module",
4
- "version": "2.4.0",
4
+ "version": "2.4.2",
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
  }