janja 2.2.0 → 2.3.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.js CHANGED
@@ -191,7 +191,7 @@ class Context extends OutScript {
191
191
  writable: true,
192
192
  value: CONTEXT
193
193
  });
194
- Object.defineProperty(this, "contexts", {
194
+ Object.defineProperty(this, "stack", {
195
195
  enumerable: true,
196
196
  configurable: true,
197
197
  writable: true,
@@ -206,15 +206,15 @@ class Context extends OutScript {
206
206
  }
207
207
  in() {
208
208
  this.context = `${this.context}_${this.index++}`;
209
- this.contexts.push(this.context);
209
+ this.stack.push(this.context);
210
210
  return this.context;
211
211
  }
212
212
  out() {
213
213
  if (this.context === CONTEXT) {
214
214
  return;
215
215
  }
216
- this.contexts.pop();
217
- this.context = this.contexts.at(-1);
216
+ this.stack.pop();
217
+ this.context = this.stack.at(-1);
218
218
  }
219
219
  }
220
220
 
@@ -634,8 +634,6 @@ class ExpCompiler {
634
634
  return this.compileLit(expression);
635
635
  case 'PIPE':
636
636
  return this.compilePipe(expression);
637
- case 'OF':
638
- return this.compileOf(expression);
639
637
  case 'ASSIGN':
640
638
  return this.compileAssign(expression);
641
639
  case 'IS':
@@ -687,13 +685,6 @@ class ExpCompiler {
687
685
  const { value: name, args = [] } = right;
688
686
  return `(await ${this.filters}.${name}.call(${[this.context, this.compileExp(left), ...args.map((arg) => (arg.type === 'ASSIGN' ? `${this.context}.${arg.left.value}=${this.compileExp(arg.right)}` : this.compileExp(arg)))].join(',')}))`;
689
687
  }
690
- compileOf({ left, right }) {
691
- // destructuring
692
- if (left.type === 'SEQ') {
693
- return `const {${left.elements.map(({ value }) => value).join(',')}} of ${this.compileExp(right)}`;
694
- }
695
- return `const ${left.value} of ${this.compileExp(right)}`;
696
- }
697
688
  compileAssign({ left, right }) {
698
689
  if (left.type === 'ID') {
699
690
  return `Object.assign(${this.context},{${left.value}:${this.compileExp(right)}});`;
@@ -907,15 +898,22 @@ const plugin$9 = {
907
898
 
908
899
  async function compileFor({ loop, body }, compiler) {
909
900
  const { context } = compiler;
901
+ const expCompiler = new ExpCompiler();
910
902
  if (loop.left.type === 'SEQ') {
911
- compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.elements.map(({ value }) => value).join(',')},`, '};');
903
+ compiler.pushRaw(loop.loc, '{', `const a_=${expCompiler.compile(loop.right, context)},n_=a_.length;`, 'let i_=0;', `for(const {${loop.left.elements
904
+ .map((el) => el.type === 'ASSIGN'
905
+ ? `${el.left.value}=${expCompiler.compile(el.right, context)}`
906
+ : el.value)
907
+ .join(',')}} of a_){`, `const ${compiler.in()}={`, `...${context},`, 'loop:{', 'first:i_===0,', 'index:i_++,', 'last:i_===n_,', '},', `${loop.left.elements
908
+ .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
909
+ .join(',')},`, '};');
912
910
  }
913
911
  else {
914
- compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.value},`, '};');
912
+ 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},`, '};');
915
913
  }
916
914
  await compiler.compileNodes(body);
917
915
  compiler.out();
918
- compiler.pushRaw(null, '}');
916
+ compiler.pushRaw(null, '}', '};');
919
917
  }
920
918
  function compileBreak({ loc }, compiler) {
921
919
  compiler.pushRaw(loc, 'break;');
package/dist/index.d.ts CHANGED
@@ -53,7 +53,7 @@ declare class OutScript extends SourceMap {
53
53
 
54
54
  declare class Context extends OutScript {
55
55
  context: string;
56
- private contexts;
56
+ private stack;
57
57
  private index;
58
58
  in(): string;
59
59
  out(): void;
package/dist/index.esm.js CHANGED
@@ -189,7 +189,7 @@ class Context extends OutScript {
189
189
  writable: true,
190
190
  value: CONTEXT
191
191
  });
192
- Object.defineProperty(this, "contexts", {
192
+ Object.defineProperty(this, "stack", {
193
193
  enumerable: true,
194
194
  configurable: true,
195
195
  writable: true,
@@ -204,15 +204,15 @@ class Context extends OutScript {
204
204
  }
205
205
  in() {
206
206
  this.context = `${this.context}_${this.index++}`;
207
- this.contexts.push(this.context);
207
+ this.stack.push(this.context);
208
208
  return this.context;
209
209
  }
210
210
  out() {
211
211
  if (this.context === CONTEXT) {
212
212
  return;
213
213
  }
214
- this.contexts.pop();
215
- this.context = this.contexts.at(-1);
214
+ this.stack.pop();
215
+ this.context = this.stack.at(-1);
216
216
  }
217
217
  }
218
218
 
@@ -632,8 +632,6 @@ class ExpCompiler {
632
632
  return this.compileLit(expression);
633
633
  case 'PIPE':
634
634
  return this.compilePipe(expression);
635
- case 'OF':
636
- return this.compileOf(expression);
637
635
  case 'ASSIGN':
638
636
  return this.compileAssign(expression);
639
637
  case 'IS':
@@ -685,13 +683,6 @@ class ExpCompiler {
685
683
  const { value: name, args = [] } = right;
686
684
  return `(await ${this.filters}.${name}.call(${[this.context, this.compileExp(left), ...args.map((arg) => (arg.type === 'ASSIGN' ? `${this.context}.${arg.left.value}=${this.compileExp(arg.right)}` : this.compileExp(arg)))].join(',')}))`;
687
685
  }
688
- compileOf({ left, right }) {
689
- // destructuring
690
- if (left.type === 'SEQ') {
691
- return `const {${left.elements.map(({ value }) => value).join(',')}} of ${this.compileExp(right)}`;
692
- }
693
- return `const ${left.value} of ${this.compileExp(right)}`;
694
- }
695
686
  compileAssign({ left, right }) {
696
687
  if (left.type === 'ID') {
697
688
  return `Object.assign(${this.context},{${left.value}:${this.compileExp(right)}});`;
@@ -905,15 +896,22 @@ const plugin$9 = {
905
896
 
906
897
  async function compileFor({ loop, body }, compiler) {
907
898
  const { context } = compiler;
899
+ const expCompiler = new ExpCompiler();
908
900
  if (loop.left.type === 'SEQ') {
909
- compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.elements.map(({ value }) => value).join(',')},`, '};');
901
+ compiler.pushRaw(loop.loc, '{', `const a_=${expCompiler.compile(loop.right, context)},n_=a_.length;`, 'let i_=0;', `for(const {${loop.left.elements
902
+ .map((el) => el.type === 'ASSIGN'
903
+ ? `${el.left.value}=${expCompiler.compile(el.right, context)}`
904
+ : el.value)
905
+ .join(',')}} of a_){`, `const ${compiler.in()}={`, `...${context},`, 'loop:{', 'first:i_===0,', 'index:i_++,', 'last:i_===n_,', '},', `${loop.left.elements
906
+ .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
907
+ .join(',')},`, '};');
910
908
  }
911
909
  else {
912
- compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.value},`, '};');
910
+ 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},`, '};');
913
911
  }
914
912
  await compiler.compileNodes(body);
915
913
  compiler.out();
916
- compiler.pushRaw(null, '}');
914
+ compiler.pushRaw(null, '}', '};');
917
915
  }
918
916
  function compileBreak({ loc }, compiler) {
919
917
  compiler.pushRaw(loc, 'break;');
package/dist/index.js CHANGED
@@ -192,7 +192,7 @@ ${output
192
192
  writable: true,
193
193
  value: CONTEXT
194
194
  });
195
- Object.defineProperty(this, "contexts", {
195
+ Object.defineProperty(this, "stack", {
196
196
  enumerable: true,
197
197
  configurable: true,
198
198
  writable: true,
@@ -207,15 +207,15 @@ ${output
207
207
  }
208
208
  in() {
209
209
  this.context = `${this.context}_${this.index++}`;
210
- this.contexts.push(this.context);
210
+ this.stack.push(this.context);
211
211
  return this.context;
212
212
  }
213
213
  out() {
214
214
  if (this.context === CONTEXT) {
215
215
  return;
216
216
  }
217
- this.contexts.pop();
218
- this.context = this.contexts.at(-1);
217
+ this.stack.pop();
218
+ this.context = this.stack.at(-1);
219
219
  }
220
220
  }
221
221
 
@@ -635,8 +635,6 @@ ${output
635
635
  return this.compileLit(expression);
636
636
  case 'PIPE':
637
637
  return this.compilePipe(expression);
638
- case 'OF':
639
- return this.compileOf(expression);
640
638
  case 'ASSIGN':
641
639
  return this.compileAssign(expression);
642
640
  case 'IS':
@@ -688,13 +686,6 @@ ${output
688
686
  const { value: name, args = [] } = right;
689
687
  return `(await ${this.filters}.${name}.call(${[this.context, this.compileExp(left), ...args.map((arg) => (arg.type === 'ASSIGN' ? `${this.context}.${arg.left.value}=${this.compileExp(arg.right)}` : this.compileExp(arg)))].join(',')}))`;
690
688
  }
691
- compileOf({ left, right }) {
692
- // destructuring
693
- if (left.type === 'SEQ') {
694
- return `const {${left.elements.map(({ value }) => value).join(',')}} of ${this.compileExp(right)}`;
695
- }
696
- return `const ${left.value} of ${this.compileExp(right)}`;
697
- }
698
689
  compileAssign({ left, right }) {
699
690
  if (left.type === 'ID') {
700
691
  return `Object.assign(${this.context},{${left.value}:${this.compileExp(right)}});`;
@@ -908,15 +899,22 @@ ${output
908
899
 
909
900
  async function compileFor({ loop, body }, compiler) {
910
901
  const { context } = compiler;
902
+ const expCompiler = new ExpCompiler();
911
903
  if (loop.left.type === 'SEQ') {
912
- compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.elements.map(({ value }) => value).join(',')},`, '};');
904
+ compiler.pushRaw(loop.loc, '{', `const a_=${expCompiler.compile(loop.right, context)},n_=a_.length;`, 'let i_=0;', `for(const {${loop.left.elements
905
+ .map((el) => el.type === 'ASSIGN'
906
+ ? `${el.left.value}=${expCompiler.compile(el.right, context)}`
907
+ : el.value)
908
+ .join(',')}} of a_){`, `const ${compiler.in()}={`, `...${context},`, 'loop:{', 'first:i_===0,', 'index:i_++,', 'last:i_===n_,', '},', `${loop.left.elements
909
+ .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
910
+ .join(',')},`, '};');
913
911
  }
914
912
  else {
915
- compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.value},`, '};');
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},`, '};');
916
914
  }
917
915
  await compiler.compileNodes(body);
918
916
  compiler.out();
919
- compiler.pushRaw(null, '}');
917
+ compiler.pushRaw(null, '}', '};');
920
918
  }
921
919
  function compileBreak({ loc }, compiler) {
922
920
  compiler.pushRaw(loc, 'break;');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "janja",
3
3
  "type": "module",
4
- "version": "2.2.0",
4
+ "version": "2.3.0",
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",
@@ -40,7 +40,7 @@
40
40
  "globals": "^16.5.0",
41
41
  "husky": "^9.1.7",
42
42
  "jiti": "^2.6.1",
43
- "prettier": "^3.7.2",
43
+ "prettier": "^3.7.3",
44
44
  "rollup": "^4.53.3",
45
45
  "rollup-plugin-dts": "^6.3.0",
46
46
  "tslib": "^2.8.1",