1 line
50 KiB
Plaintext
1 line
50 KiB
Plaintext
|
{"version":3,"file":"eta.module.mjs","sources":["../src/storage.ts","../src/err.ts","../src/compile.ts","../src/compile-string.ts","../src/utils.ts","../src/config.ts","../src/parse.ts","../src/render.ts","../src/core.ts","../src/file-handling.ts","../src/index.ts"],"sourcesContent":["/**\n * Handles storage and accessing of values\n *\n * In this case, we use it to store compiled template functions\n * Indexed by their `name` or `filename`\n */\n\nexport class Cacher<T> {\n constructor(private cache: Record<string, T>) {}\n define(key: string, val: T): void {\n this.cache[key] = val;\n }\n get(key: string): T {\n return this.cache[key];\n }\n remove(key: string): void {\n delete this.cache[key];\n }\n reset(): void {\n this.cache = {};\n }\n load(cacheObj: Record<string, T>): void {\n this.cache = { ...this.cache, ...cacheObj };\n }\n}\n","export class EtaError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"Eta Error\";\n }\n}\n\n/**\n * Throws an EtaError with a nicely formatted error and message showing where in the template the error occurred.\n */\n\nexport function ParseErr(message: string, str: string, indx: number): void {\n const whitespace = str.slice(0, indx).split(/\\n/);\n\n const lineNo = whitespace.length;\n const colNo = whitespace[lineNo - 1].length + 1;\n message +=\n \" at line \" +\n lineNo +\n \" col \" +\n colNo +\n \":\\n\\n\" +\n \" \" +\n str.split(/\\n/)[lineNo - 1] +\n \"\\n\" +\n \" \" +\n Array(colNo).join(\" \") +\n \"^\";\n throw new EtaError(message);\n}\n\nexport function RuntimeErr(originalError: Error, str: string, lineNo: number, path: string): void {\n // code gratefully taken from https://github.com/mde/ejs and adapted\n\n const lines = str.split(\"\\n\");\n const start = Math.max(lineNo - 3, 0);\n const end = Math.min(lines.length, lineNo + 3);\n const filename = path;\n // Error context\n const context = lines\n .slice(start, end)\n .map(function (line, i) {\n const curr = i + start + 1;\n return (curr == lineNo ? \" >> \" : \" \") + curr + \"| \" + line;\n })\n .join(\"\\n\");\n\n const header = filename ? filename + \":\" + lineNo + \"\\n\" : \"line \" + lineNo + \"\\n\";\n\n const err = new EtaError(header + context + \"\\n\\n\" + originalError.message);\n\n err.name = originalError.name; // the original name (e.g. ReferenceError) may be useful\n\n throw err;\n}\n","import { EtaError } from \"./err.ts\";\n\n/* TYPES */\nimport type { Eta } from \"./core.ts\";\nimport type { EtaConfig, Options } from \"./config.ts\";\n\nexport type TemplateFunction = (this: Eta, data?: object, options?: Partial<Options>) => string;\n/* END TYPES */\n\n/* istanbul ignore next */\nconst AsyncFunction = async function () {}.constructor; // eslint-disable-line @typescript-eslint/no-empty-function\n\n/**\n * Takes a template string and returns a template function that can be called with (data, config)\n *\n * @param str - The template string\n * @param config - A custom configuration object (optional)\n */\n\nexport function compile(this: Eta, str: string, options?: Partial<Options>): TemplateFunction {\n const config: EtaConfig = this.config;\n\n /* ASYNC HANDLING */\n // code gratefully taken from https://github.com/mde/ejs and adapted\n const ctor = options && options.async ? (AsyncFunction as FunctionConstructor) : Function;\n /* END ASYNC HANDLING */\n\n try {\n return new ctor(\n config.varName,\n \"options\",\n this.compileToString.call(this, str, options)\n ) as TemplateFunction; // eslint-disable-line no-new-func\n } catch (e) {\n if (e instanceof SyntaxError) {\n throw new EtaError(\n \"Bad template syntax\\n\\n\" +\n e.message +\n \"\\n\" +\n Array(e.message.length + 1).join(\"=\") +\n \"\\n\" +\n this.compileToString.call(this, str, options) +\n \"\\n\" // This will put an extra newline before the callstack for extra readability\n );\n } else
|