ventojs 1.12.11 → 1.12.13
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/esm/mod.d.ts +2 -1
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +1 -1
- package/esm/plugins/auto_trim.d.ts.map +1 -1
- package/esm/plugins/auto_trim.js +3 -0
- package/esm/plugins/for.js +1 -1
- package/esm/plugins/set.js +1 -1
- package/esm/src/environment.d.ts +1 -4
- package/esm/src/environment.d.ts.map +1 -1
- package/esm/src/environment.js +16 -33
- package/esm/src/errors.d.ts +21 -0
- package/esm/src/errors.d.ts.map +1 -0
- package/esm/src/errors.js +44 -0
- package/esm/src/transformer.d.ts.map +1 -1
- package/esm/src/transformer.js +21 -3
- package/package.json +1 -1
package/esm/mod.d.ts
CHANGED
|
@@ -2,8 +2,9 @@ import { Environment } from "./src/environment.js";
|
|
|
2
2
|
import { Loader } from "./src/loader.js";
|
|
3
3
|
export interface Options {
|
|
4
4
|
includes?: string | Loader;
|
|
5
|
-
/** @deprecated */
|
|
5
|
+
/** @deprecated Use autoDataVarname */
|
|
6
6
|
useWith?: boolean;
|
|
7
|
+
autoDataVarname?: boolean;
|
|
7
8
|
dataVarname?: string;
|
|
8
9
|
autoescape?: boolean;
|
|
9
10
|
}
|
package/esm/mod.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAc,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAerD,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC3B,
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAc,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAerD,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC3B,sCAAsC;IACtC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,CAAC,OAAO,WAAW,OAAO,GAAE,OAAY,GAAG,WAAW,CA4B3D"}
|
package/esm/mod.js
CHANGED
|
@@ -22,7 +22,7 @@ export default function (options = {}) {
|
|
|
22
22
|
loader,
|
|
23
23
|
dataVarname: options.dataVarname || "it",
|
|
24
24
|
autoescape: options.autoescape ?? false,
|
|
25
|
-
|
|
25
|
+
autoDataVarname: options.autoDataVarname ?? options.useWith ?? true,
|
|
26
26
|
});
|
|
27
27
|
// Register basic plugins
|
|
28
28
|
env.use(ifTag());
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auto_trim.d.ts","sourceRoot":"","sources":["../../src/plugins/auto_trim.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,KAAK,EAAe,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAEjE,eAAO,MAAM,WAAW,UAgBvB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAAE,IAAI,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AAEjD,MAAM,CAAC,OAAO,WACZ,OAAO,GAAE,eAAuC,GAC/C,MAAM,CAIR;AAED,wBAAgB,QAAQ,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,eAAe,
|
|
1
|
+
{"version":3,"file":"auto_trim.d.ts","sourceRoot":"","sources":["../../src/plugins/auto_trim.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,KAAK,EAAe,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAEjE,eAAO,MAAM,WAAW,UAgBvB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAAE,IAAI,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AAEjD,MAAM,CAAC,OAAO,WACZ,OAAO,GAAE,eAAuC,GAC/C,MAAM,CAIR;AAED,wBAAgB,QAAQ,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,eAAe,QAoBjE"}
|
package/esm/plugins/auto_trim.js
CHANGED
package/esm/plugins/for.js
CHANGED
|
@@ -32,7 +32,7 @@ item, withKeys = false) {
|
|
|
32
32
|
return [];
|
|
33
33
|
}
|
|
34
34
|
if (Array.isArray(item)) {
|
|
35
|
-
return withKeys ?
|
|
35
|
+
return withKeys ? item.map((value, i) => [i, value]) : item;
|
|
36
36
|
}
|
|
37
37
|
if (typeof item === "function") {
|
|
38
38
|
return toIterator(item(), withKeys);
|
package/esm/plugins/set.js
CHANGED
|
@@ -18,8 +18,8 @@ function setTag(env, code, _output, tokens) {
|
|
|
18
18
|
const [, variable, value] = match;
|
|
19
19
|
const val = env.compileFilters(tokens, value);
|
|
20
20
|
return `
|
|
21
|
-
${dataVarname}["${variable}"] = ${val};
|
|
22
21
|
var ${variable} = ${val};
|
|
22
|
+
${dataVarname}["${variable}"] = ${variable};
|
|
23
23
|
`;
|
|
24
24
|
}
|
|
25
25
|
// Value is captured (eg: {{ set foo }}bar{{ /set }})
|
package/esm/src/environment.d.ts
CHANGED
|
@@ -28,7 +28,7 @@ export interface Options {
|
|
|
28
28
|
loader: Loader;
|
|
29
29
|
dataVarname: string;
|
|
30
30
|
autoescape: boolean;
|
|
31
|
-
|
|
31
|
+
autoDataVarname: boolean;
|
|
32
32
|
}
|
|
33
33
|
export declare class Environment {
|
|
34
34
|
cache: Map<string, Template>;
|
|
@@ -48,8 +48,5 @@ export declare class Environment {
|
|
|
48
48
|
load(file: string, from?: string): Promise<Template>;
|
|
49
49
|
compileTokens(tokens: Token[], outputVar?: string, stopAt?: string[]): string[];
|
|
50
50
|
compileFilters(tokens: Token[], output: string, autoescape?: boolean): string;
|
|
51
|
-
createError(path: string, source: string, position: number, cause: Error): Error;
|
|
52
51
|
}
|
|
53
|
-
/** Returns the number and code of the errored line */
|
|
54
|
-
export declare function errorLine(source: string, pos: number): [number, number, string];
|
|
55
52
|
//# sourceMappingURL=environment.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"environment.d.ts","sourceRoot":"","sources":["../../src/src/environment.ts"],"names":[],"mappings":"AACA,OAAiB,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAEjD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"environment.d.ts","sourceRoot":"","sources":["../../src/src/environment.ts"],"names":[],"mappings":"AACA,OAAiB,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAEjD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAI1C,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,QAAQ;IACvB,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;IAC1D,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,YAAY;IAC3B,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,cAAc,CAAC;IACjD,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,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import { transformTemplateCode } from "./transformer.js";
|
|
4
|
+
import { TemplateError, TransformError } from "./errors.js";
|
|
4
5
|
export class Environment {
|
|
5
6
|
cache = new Map();
|
|
6
7
|
options;
|
|
@@ -41,11 +42,19 @@ export class Environment {
|
|
|
41
42
|
}
|
|
42
43
|
const tokens = this.tokenize(source, path);
|
|
43
44
|
let code = this.compileTokens(tokens).join("\n");
|
|
44
|
-
const { dataVarname,
|
|
45
|
-
if (
|
|
46
|
-
|
|
45
|
+
const { dataVarname, autoDataVarname } = this.options;
|
|
46
|
+
if (autoDataVarname) {
|
|
47
|
+
try {
|
|
48
|
+
code = transformTemplateCode(code, dataVarname);
|
|
49
|
+
}
|
|
50
|
+
catch (cause) {
|
|
51
|
+
if (cause instanceof TransformError) {
|
|
52
|
+
throw new TemplateError(path, source, cause.position, cause);
|
|
53
|
+
}
|
|
54
|
+
throw new Error(`Unknown error while transforming ${path}`, { cause });
|
|
55
|
+
}
|
|
47
56
|
}
|
|
48
|
-
const constructor = new Function("__file", "__env", "__defaults", `return${sync ? "" : " async"} function (${dataVarname}) {
|
|
57
|
+
const constructor = new Function("__file", "__env", "__defaults", "__err", `return${sync ? "" : " async"} function (${dataVarname}) {
|
|
49
58
|
let __pos = 0;
|
|
50
59
|
try {
|
|
51
60
|
${dataVarname} = Object.assign({}, __defaults, ${dataVarname});
|
|
@@ -54,11 +63,11 @@ export class Environment {
|
|
|
54
63
|
return __exports;
|
|
55
64
|
} catch (cause) {
|
|
56
65
|
const template = __env.cache.get(__file);
|
|
57
|
-
throw
|
|
66
|
+
throw new __err(__file, template?.source, __pos, cause);
|
|
58
67
|
}
|
|
59
68
|
}
|
|
60
69
|
`);
|
|
61
|
-
const template = constructor(path, this, defaults);
|
|
70
|
+
const template = constructor(path, this, defaults, TemplateError);
|
|
62
71
|
template.file = path;
|
|
63
72
|
template.code = code;
|
|
64
73
|
template.source = source;
|
|
@@ -69,7 +78,7 @@ export class Environment {
|
|
|
69
78
|
let { tokens } = result;
|
|
70
79
|
const { position, error } = result;
|
|
71
80
|
if (error) {
|
|
72
|
-
throw
|
|
81
|
+
throw new TemplateError(path, source, position, error);
|
|
73
82
|
}
|
|
74
83
|
for (const tokenPreprocessor of this.tokenPreprocessors) {
|
|
75
84
|
const result = tokenPreprocessor(this, tokens, path);
|
|
@@ -160,13 +169,6 @@ export class Environment {
|
|
|
160
169
|
}
|
|
161
170
|
return output;
|
|
162
171
|
}
|
|
163
|
-
createError(path, source, position, cause) {
|
|
164
|
-
if (!source) {
|
|
165
|
-
return cause;
|
|
166
|
-
}
|
|
167
|
-
const [line, column, code] = errorLine(source, position);
|
|
168
|
-
return new Error(`Error in the template ${path}:${line}:${column}\n\n${code.trim()}\n\n> ${cause.message}\n`, { cause });
|
|
169
|
-
}
|
|
170
172
|
}
|
|
171
173
|
function isGlobal(name) {
|
|
172
174
|
// @ts-ignore TS doesn't know about globalThis
|
|
@@ -179,25 +181,6 @@ function isGlobal(name) {
|
|
|
179
181
|
return typeof dntShim.dntGlobalThis[obj]?.[prop] === "function";
|
|
180
182
|
}
|
|
181
183
|
}
|
|
182
|
-
/** Returns the number and code of the errored line */
|
|
183
|
-
export function errorLine(source, pos) {
|
|
184
|
-
let line = 1;
|
|
185
|
-
let column = 1;
|
|
186
|
-
for (let index = 0; index < pos; index++) {
|
|
187
|
-
if (source[index] === "\n" ||
|
|
188
|
-
(source[index] === "\r" && source[index + 1] === "\n")) {
|
|
189
|
-
line++;
|
|
190
|
-
column = 1;
|
|
191
|
-
if (source[index] === "\r") {
|
|
192
|
-
index++;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
else {
|
|
196
|
-
column++;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
return [line, column, source.split("\n")[line - 1]];
|
|
200
|
-
}
|
|
201
184
|
function checkAsync(fn) {
|
|
202
185
|
return fn.constructor?.name === "AsyncFunction";
|
|
203
186
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
declare class VentoBaseError extends Error {
|
|
2
|
+
name: string;
|
|
3
|
+
}
|
|
4
|
+
export declare class TemplateError extends VentoBaseError {
|
|
5
|
+
path: string;
|
|
6
|
+
source: string;
|
|
7
|
+
position: number;
|
|
8
|
+
constructor(path?: string, source?: string, position?: number, cause?: Error);
|
|
9
|
+
}
|
|
10
|
+
export declare class TransformError extends VentoBaseError {
|
|
11
|
+
position: number;
|
|
12
|
+
constructor(message: string, position?: number, cause?: Error);
|
|
13
|
+
}
|
|
14
|
+
/** Returns the number and code of the errored line */
|
|
15
|
+
export declare function errorLine(source: string, position: number): {
|
|
16
|
+
line: number;
|
|
17
|
+
column: number;
|
|
18
|
+
code: string;
|
|
19
|
+
};
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/src/errors.ts"],"names":[],"mappings":"AAAA,cAAM,cAAe,SAAQ,KAAK;IACvB,IAAI,SAAyB;CACvC;AAED,qBAAa,aAAc,SAAQ,cAAc;IAEtC,IAAI,EAAE,MAAM;IACZ,MAAM,EAAE,MAAM;IACd,QAAQ,EAAE,MAAM;gBAFhB,IAAI,GAAE,MAAoB,EAC1B,MAAM,GAAE,MAAuB,EAC/B,QAAQ,GAAE,MAAU,EAC3B,KAAK,CAAC,EAAE,KAAK;CAYhB;AAED,qBAAa,cAAe,SAAQ,cAAc;IAGvC,QAAQ,EAAE,MAAM;gBADvB,OAAO,EAAE,MAAM,EACR,QAAQ,GAAE,MAAU,EAC3B,KAAK,CAAC,EAAE,KAAK;CAIhB;AAED,sDAAsD;AACtD,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,GACf;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAqBhD"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
class VentoBaseError extends Error {
|
|
2
|
+
name = this.constructor.name;
|
|
3
|
+
}
|
|
4
|
+
export class TemplateError extends VentoBaseError {
|
|
5
|
+
path;
|
|
6
|
+
source;
|
|
7
|
+
position;
|
|
8
|
+
constructor(path = "<unknown>", source = "<empty file>", position = 0, cause) {
|
|
9
|
+
const { line, column, code } = errorLine(source, position);
|
|
10
|
+
super(`Error in template ${path}:${line}:${column}\n\n${code.trim()}\n\n`, { cause });
|
|
11
|
+
this.path = path;
|
|
12
|
+
this.source = source;
|
|
13
|
+
this.position = position;
|
|
14
|
+
if (cause) {
|
|
15
|
+
this.message += `(via ${cause.name})\n`;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export class TransformError extends VentoBaseError {
|
|
20
|
+
position;
|
|
21
|
+
constructor(message, position = 0, cause) {
|
|
22
|
+
super(message, { cause });
|
|
23
|
+
this.position = position;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** Returns the number and code of the errored line */
|
|
27
|
+
export function errorLine(source, position) {
|
|
28
|
+
let line = 1;
|
|
29
|
+
let column = 1;
|
|
30
|
+
for (let index = 0; index < position; index++) {
|
|
31
|
+
if (source[index] === "\n" ||
|
|
32
|
+
(source[index] === "\r" && source[index + 1] === "\n")) {
|
|
33
|
+
line++;
|
|
34
|
+
column = 1;
|
|
35
|
+
if (source[index] === "\r") {
|
|
36
|
+
index++;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
column++;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return { line, column, code: source.split("\n")[line - 1] };
|
|
44
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transformer.d.ts","sourceRoot":"","sources":["../../src/src/transformer.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"transformer.d.ts","sourceRoot":"","sources":["../../src/src/transformer.ts"],"names":[],"mappings":"AA4HA,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,MAAM,EACZ,aAAa,EAAE,MAAM,GACpB,MAAM,CAwJR"}
|
package/esm/src/transformer.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as dntShim from "../_dnt.shims.js";
|
|
2
2
|
import { astring, meriyah, walker } from "../deps.js";
|
|
3
|
+
import { TransformError } from "./errors.js";
|
|
3
4
|
// List of identifiers that are in globalThis
|
|
4
5
|
// but should be accessed as templateState.identifier
|
|
5
6
|
const INCLUDE_GLOBAL = [
|
|
@@ -94,17 +95,34 @@ export function transformTemplateCode(code, templateState) {
|
|
|
94
95
|
if (!code.trim()) {
|
|
95
96
|
return code;
|
|
96
97
|
}
|
|
97
|
-
|
|
98
|
+
let parsed;
|
|
99
|
+
try {
|
|
100
|
+
parsed = meriyah.parseScript(code, { module: true });
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
const { message, start, loc } = error;
|
|
104
|
+
// Use information from `meriyah` to annotate the part of
|
|
105
|
+
// the compiled template function that triggered the ParseError
|
|
106
|
+
const annotation = `\u001B[2m${loc.start.line}\u001B[0m ` +
|
|
107
|
+
code.split("\n")[loc.start.line - 1] +
|
|
108
|
+
`\n${" ".repeat(loc.start.column)}\u001B[31m^\u001B[39m`;
|
|
109
|
+
// Grab the last instance of Vento's `__pos` variable before the
|
|
110
|
+
// error was thrown. Pass this back to Vento to
|
|
111
|
+
// tie this error with problmatic template code
|
|
112
|
+
const matches = [...code.slice(0, start).matchAll(/__pos = (\d+);/g)];
|
|
113
|
+
const position = Number(matches.at(-1)?.[1]);
|
|
114
|
+
throw new TransformError(`[meriyah] ${message} while parsing compiled template function:\n\n${annotation}`, position);
|
|
115
|
+
}
|
|
98
116
|
const tracker = new ScopeTracker();
|
|
99
117
|
const exclude = [
|
|
100
118
|
templateState,
|
|
101
119
|
...DEFAULT_EXCLUDES,
|
|
102
120
|
];
|
|
103
121
|
if (parsed.type !== "Program") {
|
|
104
|
-
throw new
|
|
122
|
+
throw new TransformError("[meriyah] Expected a program");
|
|
105
123
|
}
|
|
106
124
|
if (parsed.body.length === 0) {
|
|
107
|
-
throw new
|
|
125
|
+
throw new TransformError("[meriyah] Empty program");
|
|
108
126
|
}
|
|
109
127
|
// Transforms an identifier to a MemberExpression
|
|
110
128
|
// if it's not in the exclude list
|