janja 2.1.3 → 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 +23 -19
- package/dist/index.d.ts +3 -3
- package/dist/index.esm.js +23 -19
- package/dist/index.js +23 -19
- package/package.json +2 -2
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, "
|
|
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.
|
|
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.
|
|
217
|
-
this.context = this.
|
|
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, `
|
|
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, `
|
|
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;');
|
|
@@ -2602,8 +2600,8 @@ class Parser extends Tokenizer {
|
|
|
2602
2600
|
}
|
|
2603
2601
|
let parser = this.options.parsers[token.name?.toLowerCase() ??
|
|
2604
2602
|
token.type?.toLowerCase()];
|
|
2605
|
-
if (parser === '
|
|
2606
|
-
parser = this.options.parsers[
|
|
2603
|
+
if (typeof parser === 'string') {
|
|
2604
|
+
parser = this.options.parsers[parser];
|
|
2607
2605
|
}
|
|
2608
2606
|
if (!parser) {
|
|
2609
2607
|
parser = this.options.parsers['unknown'];
|
|
@@ -2780,7 +2778,13 @@ class Compiler extends Context {
|
|
|
2780
2778
|
await this.compileNodes(node.body);
|
|
2781
2779
|
return;
|
|
2782
2780
|
}
|
|
2783
|
-
|
|
2781
|
+
let compiler = this.options.compilers[node.type];
|
|
2782
|
+
if (typeof compiler === 'string') {
|
|
2783
|
+
compiler = this.options.compilers[compiler];
|
|
2784
|
+
}
|
|
2785
|
+
if (!compiler) {
|
|
2786
|
+
compiler = this.options.compilers['UNKNOWN'];
|
|
2787
|
+
}
|
|
2784
2788
|
await compiler?.(node, this);
|
|
2785
2789
|
}
|
|
2786
2790
|
async compileNodes(nodes) {
|
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
|
|
56
|
+
private stack;
|
|
57
57
|
private index;
|
|
58
58
|
in(): string;
|
|
59
59
|
out(): void;
|
|
@@ -226,9 +226,9 @@ interface Plugin {
|
|
|
226
226
|
compilers?: CompilerMap;
|
|
227
227
|
}
|
|
228
228
|
type ParserFn<T extends Token = any, U extends SyntaxNode = any> = (token: T, parser: Parser) => AsyncGenerator<U | 'NEXT' | void>;
|
|
229
|
-
type ParserMap = Record<string,
|
|
229
|
+
type ParserMap = Record<string, string | ParserFn>;
|
|
230
230
|
type CompilerFn<T extends SyntaxNode = any> = (node: T, compiler: Compiler) => MaybePromise<void>;
|
|
231
|
-
type CompilerMap = Record<string, CompilerFn>;
|
|
231
|
+
type CompilerMap = Record<string, string | CompilerFn>;
|
|
232
232
|
|
|
233
233
|
interface BaseOptions {
|
|
234
234
|
debug?: (error: Error) => any;
|
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, "
|
|
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.
|
|
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.
|
|
215
|
-
this.context = this.
|
|
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, `
|
|
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, `
|
|
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;');
|
|
@@ -2600,8 +2598,8 @@ class Parser extends Tokenizer {
|
|
|
2600
2598
|
}
|
|
2601
2599
|
let parser = this.options.parsers[token.name?.toLowerCase() ??
|
|
2602
2600
|
token.type?.toLowerCase()];
|
|
2603
|
-
if (parser === '
|
|
2604
|
-
parser = this.options.parsers[
|
|
2601
|
+
if (typeof parser === 'string') {
|
|
2602
|
+
parser = this.options.parsers[parser];
|
|
2605
2603
|
}
|
|
2606
2604
|
if (!parser) {
|
|
2607
2605
|
parser = this.options.parsers['unknown'];
|
|
@@ -2778,7 +2776,13 @@ class Compiler extends Context {
|
|
|
2778
2776
|
await this.compileNodes(node.body);
|
|
2779
2777
|
return;
|
|
2780
2778
|
}
|
|
2781
|
-
|
|
2779
|
+
let compiler = this.options.compilers[node.type];
|
|
2780
|
+
if (typeof compiler === 'string') {
|
|
2781
|
+
compiler = this.options.compilers[compiler];
|
|
2782
|
+
}
|
|
2783
|
+
if (!compiler) {
|
|
2784
|
+
compiler = this.options.compilers['UNKNOWN'];
|
|
2785
|
+
}
|
|
2782
2786
|
await compiler?.(node, this);
|
|
2783
2787
|
}
|
|
2784
2788
|
async compileNodes(nodes) {
|
package/dist/index.js
CHANGED
|
@@ -192,7 +192,7 @@ ${output
|
|
|
192
192
|
writable: true,
|
|
193
193
|
value: CONTEXT
|
|
194
194
|
});
|
|
195
|
-
Object.defineProperty(this, "
|
|
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.
|
|
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.
|
|
218
|
-
this.context = this.
|
|
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, `
|
|
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, `
|
|
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;');
|
|
@@ -2603,8 +2601,8 @@ ${output
|
|
|
2603
2601
|
}
|
|
2604
2602
|
let parser = this.options.parsers[token.name?.toLowerCase() ??
|
|
2605
2603
|
token.type?.toLowerCase()];
|
|
2606
|
-
if (parser === '
|
|
2607
|
-
parser = this.options.parsers[
|
|
2604
|
+
if (typeof parser === 'string') {
|
|
2605
|
+
parser = this.options.parsers[parser];
|
|
2608
2606
|
}
|
|
2609
2607
|
if (!parser) {
|
|
2610
2608
|
parser = this.options.parsers['unknown'];
|
|
@@ -2781,7 +2779,13 @@ ${output
|
|
|
2781
2779
|
await this.compileNodes(node.body);
|
|
2782
2780
|
return;
|
|
2783
2781
|
}
|
|
2784
|
-
|
|
2782
|
+
let compiler = this.options.compilers[node.type];
|
|
2783
|
+
if (typeof compiler === 'string') {
|
|
2784
|
+
compiler = this.options.compilers[compiler];
|
|
2785
|
+
}
|
|
2786
|
+
if (!compiler) {
|
|
2787
|
+
compiler = this.options.compilers['UNKNOWN'];
|
|
2788
|
+
}
|
|
2785
2789
|
await compiler?.(node, this);
|
|
2786
2790
|
}
|
|
2787
2791
|
async compileNodes(nodes) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "janja",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.
|
|
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.
|
|
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",
|