Compare commits

..

3 commits

Author SHA1 Message Date
davidodenwald
c4a4618840 add coverage upload to github ci 2023-03-19 18:07:13 +01:00
davidodenwald
74228f1fb3 add locStart and locEnd tests 2023-03-19 18:07:13 +01:00
davidodenwald
f154303f76 add test coverage and check html files on watch mode 2023-03-19 18:02:19 +01:00
21 changed files with 1170 additions and 1402 deletions

View file

@ -17,6 +17,14 @@ jobs:
with: with:
node-version: ${{ matrix.node-version }} node-version: ${{ matrix.node-version }}
cache: "npm" cache: "npm"
- run: npm install -f - name: Install dependencies
- run: npm run build run: npm install -f
- run: npm run test
- name: Build project
run: npm run build
- name: Run tests
run: npm run test
- name: Upload coverage
uses: codecov/codecov-action@v1

3
.gitignore vendored
View file

@ -1,3 +1,4 @@
.vscode .vscode
node_modules node_modules
lib lib
coverage

1
.npmrc
View file

@ -1 +0,0 @@
registry=https://git.fetbuk.ru/api/packages/WerySkok/npm/

View file

@ -1,17 +1,11 @@
# prettier-plugin-askama-template # prettier-plugin-jinja-template
[prettier-plugin-jinja-template](https://github.com/davidodenwald/prettier-plugin-jinja-template) but with patches to work with Askama.
Formatter plugin for jinja2 template files.
## Install ## Install
```bash ```bash
npm install --save-dev prettier https://git.fetbuk.ru/WerySkok/-/packages/npm/prettier-plugin-askama-template/1.0.0/files/2 npm install --save-dev prettier prettier-plugin-jinja-template
```
Add the plugin to your `.prettierrc`:
```json
{
"plugins": ["prettier-plugin-askama-template"]
}
``` ```
## Use ## Use
@ -23,7 +17,7 @@ To format basic .html files, you'll have to override the used parser inside your
{ {
"files": ["*.html"], "files": ["*.html"],
"options": { "options": {
"parser": "askama-template" "parser": "jinja-template"
} }
} }
] ]
@ -33,9 +27,4 @@ To format basic .html files, you'll have to override the used parser inside your
Run it on all html files in your project: Run it on all html files in your project:
```bash ```bash
npx prettier --write **/*.html npx prettier --write **/*.html
```
If you don't have a prettier config you can run the plugin with this command:
```bash
npx prettier --write **/*.html --plugin=prettier-plugin-askama-template
``` ```

View file

@ -1,4 +1,8 @@
const { defaults } = require("jest-config");
/** @type {import('@jest/types').Config.InitialOptions} */ /** @type {import('@jest/types').Config.InitialOptions} */
module.exports = () => ({ module.exports = () => ({
preset: "ts-jest", preset: "ts-jest",
moduleFileExtensions: [...defaults.moduleFileExtensions, "html"],
collectCoverage: true,
}); });

2036
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,41 +1,43 @@
{ {
"name": "prettier-plugin-askama-template", "name": "prettier-plugin-jinja-template",
"version": "1.0.0", "version": "0.2.0",
"description": "prettier-plugin-jinja-template but with patches to work with Askama.", "description": "Prettier plugin for formatting jinja templates.",
"author": "Alexander Minkin", "author": "David Odenwald",
"license": "MIT", "license": "MIT",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://git.fetbuk.ru/WerySkok/prettier-plugin-askama-template.git" "url": "git+https://github.com/davidodenwald/prettier-plugin-jinja-template.git"
}, },
"bugs": { "bugs": {
"url": "https://git.fetbuk.ru/WerySkok/prettier-plugin-askama-template/issues" "url": "https://github.com/davidodenwald/prettier-plugin-jinja-template/issues"
}, },
"homepage": "https://git.fetbuk.ru/WerySkok/prettier-plugin-askama-template#readme", "homepage": "https://github.com/davidodenwald/prettier-plugin-jinja-template#readme",
"keywords": [ "keywords": [
"prettier", "prettier",
"plugin", "plugin",
"template", "template",
"html", "html",
"askama" "jinja",
"jinja2",
"flask"
], ],
"main": "lib/index.js", "main": "lib/index.js",
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"watch": "npm run build -- --watch", "watch": "npm run build -- --watch",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --verbose", "test": "jest --verbose",
"test:watch": "jest --watch --verbose", "test:watch": "jest --watch --verbose",
"publish:release": "npm run test && npm run build && npm publish" "publish:release": "npm run test && npm run build && npm publish"
}, },
"devDependencies": { "devDependencies": {
"@babel/preset-typescript": "^7.22.5", "@babel/preset-typescript": "^7.21.0",
"@types/jest": "^29.5.3", "@types/jest": "^29.5.0",
"jest": "^29.6.2", "jest": "^29.5.0",
"prettier": "^3.0.1", "prettier": "^2.8.4",
"ts-jest": "^29.1.1", "ts-jest": "^29.0.5",
"typescript": "^5.1.6" "typescript": "^5.0.2"
}, },
"peerDependencies": { "peerDependencies": {
"prettier": "^3.0.0" "prettier": "^2.0.0"
} }
} }

View file

@ -1,16 +1,16 @@
import { Node } from "./jinja"; import { Node } from "./jinja";
import { parse } from "./parser"; import { parse } from "./parser";
import { print, embed, getVisitorKeys } from "./printer"; import { print, embed } from "./printer";
import { Parser, Printer, SupportLanguage } from "prettier"; import { Parser, Printer, SupportLanguage } from "prettier";
const PLUGIN_KEY = "askama-template"; const PLUGIN_KEY = "jinja-template";
export const languages: SupportLanguage[] = [ export const languages: SupportLanguage[] = [
{ {
name: "AskamaTemplate", name: "JinjaTemplate",
parsers: [PLUGIN_KEY], parsers: [PLUGIN_KEY],
extensions: [".jinja", ".jinja2", ".j2", ".html", "askama"], extensions: [".jinja", ".jinja2", ".j2", ".html"],
vscodeLanguageIds: ["askama"], vscodeLanguageIds: ["jinja"],
}, },
]; ];
@ -27,6 +27,5 @@ export const printers = {
[PLUGIN_KEY]: <Printer<Node>>{ [PLUGIN_KEY]: <Printer<Node>>{
print, print,
embed, embed,
getVisitorKeys,
}, },
}; };

View file

@ -5,9 +5,9 @@ export const Placeholder = {
export interface Node { export interface Node {
id: string; id: string;
type: "root" | "expression" | "statement" | "block" | "comment" | "ignore"; type: "root" | "expression" | "statement" | "block" | "ignore";
content: string; content: string;
preNewLines: number; ownLine: boolean;
originalText: string; originalText: string;
index: number; index: number;
length: number; length: number;
@ -16,7 +16,6 @@ export interface Node {
export interface Expression extends Node { export interface Expression extends Node {
type: "expression"; type: "expression";
delimiter: Delimiter;
} }
export type Delimiter = "" | "-" | "+"; export type Delimiter = "" | "-" | "+";
@ -31,7 +30,10 @@ export interface Block extends Node {
type: "block"; type: "block";
start: Statement; start: Statement;
end: Statement; end: Statement;
containsNewLines: boolean; }
export interface IgnoreBlock extends Node {
type: "ignore";
} }
export const nonClosingStatements = [ export const nonClosingStatements = [
@ -41,6 +43,4 @@ export const nonClosingStatements = [
"import", "import",
"from", "from",
"extends", "extends",
"pluralize",
"when"
]; ];

View file

@ -3,7 +3,6 @@ import {
Delimiter, Delimiter,
Node, Node,
Placeholder, Placeholder,
Expression,
Statement, Statement,
Block, Block,
nonClosingStatements, nonClosingStatements,
@ -12,7 +11,7 @@ import {
const NOT_FOUND = -1; const NOT_FOUND = -1;
const regex = const regex =
/(?<node>{{(?<startDelimiterEx>[-+]?)\s*(?<expression>'([^']|\\')*'|"([^"]|\\")*"|[\S\s]*?)\s*(?<endDelimiterEx>[-+]?)}}|{%(?<startDelimiter>[-+]?)\s*(?<statement>(?<keyword>\w+)('([^']|\\')*'|"([^"]|\\")*"|[\S\s])*?)\s*(?<endDelimiter>[-+]?)%}|(?<comment>{#[\S\s]*?#})|(?<scriptBlock><(script)((?!<)[\s\S])*>((?!<\/script)[\s\S])*?{{[\s\S]*?<\/(script)>)|(?<styleBlock><(style)((?!<)[\s\S])*>((?!<\/style)[\s\S])*?{{[\s\S]*?<\/(style)>)|(?<ignoreBlock><!-- prettier-ignore-start -->[\s\S]*<!-- prettier-ignore-end -->))/; /(?<pre>(?<newline>\n)?(\s*?))(?<node>{{\s*(?<expression>'([^']|\\')*'|"([^"]|\\")*"|[\S\s]*?)\s*}}|{%(?<startDelimiter>[-+]?)\s*(?<statement>(?<keyword>\w+)('([^']|\\')*'|"([^"]|\\")*"|[\S\s])*?)\s*(?<endDelimiter>[-+]?)%}|(?<comment>{#[\S\s]*?#})|(?<scriptBlock><(script)((?!<)[\s\S])*>((?!<\/script)[\s\S])*?{{[\s\S]*?<\/(script)>)|(?<styleBlock><(style)((?!<)[\s\S])*>((?!<\/style)[\s\S])*?{{[\s\S]*?<\/(style)>)|(?<ignoreBlock><!-- prettier-ignore-start -->[\s\S]*<!-- prettier-ignore-end -->))/;
export const parse: Parser<Node>["parse"] = (text) => { export const parse: Parser<Node>["parse"] = (text) => {
const statementStack: Statement[] = []; const statementStack: Statement[] = [];
@ -21,7 +20,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
id: "0", id: "0",
type: "root" as const, type: "root" as const,
content: text, content: text,
preNewLines: 0, ownLine: false,
originalText: text, originalText: text,
index: 0, index: 0,
length: 0, length: 0,
@ -44,6 +43,9 @@ export const parse: Parser<Node>["parse"] = (text) => {
continue; continue;
} }
const pre = match.groups.pre || "";
const newline = !!match.groups.newline;
const matchText = match.groups.node; const matchText = match.groups.node;
const expression = match.groups.expression; const expression = match.groups.expression;
const statement = match.groups.statement; const statement = match.groups.statement;
@ -55,42 +57,31 @@ export const parse: Parser<Node>["parse"] = (text) => {
} }
const placeholder = generatePlaceholder(); const placeholder = generatePlaceholder();
const emptyLinesBetween = text.slice(i, i + match.index).match(/^\s+$/) || [
"",
];
const preNewLines = emptyLinesBetween.length
? emptyLinesBetween[0].split("\n").length - 1
: 0;
const node = { const node = {
id: placeholder, id: placeholder,
preNewLines, ownLine: newline,
originalText: matchText, originalText: matchText,
index: match.index + i, index: match.index + i + pre.length,
length: matchText.length, length: matchText.length,
nodes: root.nodes, nodes: root.nodes,
}; };
if (comment || ignoreBlock) { if (ignoreBlock || comment) {
root.content = root.content.replace(matchText, placeholder); root.content = root.content.replace(matchText, placeholder);
root.nodes[node.id] = { root.nodes[node.id] = {
...node, ...node,
type: comment ? "comment" : "ignore", type: "ignore",
content: comment || ignoreBlock, content: ignoreBlock || comment,
}; };
} }
if (expression) { if (expression) {
const delimiter = (match.groups.startDelimiterEx ||
match.groups.endDelimiterEx) as Delimiter;
root.content = root.content.replace(matchText, placeholder); root.content = root.content.replace(matchText, placeholder);
root.nodes[node.id] = { root.nodes[node.id] = {
...node, ...node,
type: "expression", type: "expression",
content: expression, content: expression,
delimiter, };
} as Expression;
} }
if (statement) { if (statement) {
@ -136,7 +127,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
} }
throw new Error( throw new Error(
`Closing statement "${statement}" doesn't match Opening Statement "${start.content}".` `Closung statement "${statement}" doesn't match Opening Statement "${start.content}".`
); );
} }
} }
@ -162,8 +153,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
start: start, start: start,
end: end, end: end,
content: blockText.slice(start.length, blockText.length - end.length), content: blockText.slice(start.length, blockText.length - end.length),
preNewLines: start.preNewLines, ownLine: originalText.search("\n") !== NOT_FOUND,
containsNewLines: originalText.search("\n") !== NOT_FOUND,
originalText, originalText,
index: start.index, index: start.index,
length: end.index + end.length - start.index, length: end.index + end.length - start.index,

View file

@ -1,24 +1,18 @@
import { AstPath, Printer, Options, Doc } from "prettier"; import { Printer } from "prettier";
import { builders, utils } from "prettier/doc"; import { builders, utils } from "prettier/doc";
import { Placeholder, Node, Expression, Statement, Block } from "./jinja"; import {
Placeholder,
Node,
Expression,
Statement,
Block,
IgnoreBlock,
} from "./jinja";
const NOT_FOUND = -1; const NOT_FOUND = -1;
process.env.PRETTIER_DEBUG = "true"; process.env.PRETTIER_DEBUG = "true";
export const getVisitorKeys = (
ast: Node | { [id: string]: Node },
): string[] => {
if ("type" in ast) {
return ast.type === "root" ? ["nodes"] : [];
}
return Object.values(ast)
.filter((node) => {
return node.type === "block";
})
.map((e) => e.id);
};
export const print: Printer<Node>["print"] = (path) => { export const print: Printer<Node>["print"] = (path) => {
const node = path.getNode(); const node = path.getNode();
if (!node) { if (!node) {
@ -30,10 +24,8 @@ export const print: Printer<Node>["print"] = (path) => {
return printExpression(node as Expression); return printExpression(node as Expression);
case "statement": case "statement":
return printStatement(node as Statement); return printStatement(node as Statement);
case "comment":
return printCommentBlock(node);
case "ignore": case "ignore":
return printIgnoreBlock(node); return printIgnoreBlock(node as IgnoreBlock);
} }
return []; return [];
}; };
@ -41,24 +33,18 @@ export const print: Printer<Node>["print"] = (path) => {
const printExpression = (node: Expression): builders.Doc => { const printExpression = (node: Expression): builders.Doc => {
const multiline = node.content.includes("\n"); const multiline = node.content.includes("\n");
const expression = builders.group( return builders.group(
builders.join(" ", [ builders.join(" ", [
["{{", node.delimiter], "{{",
multiline multiline
? builders.indent(getMultilineGroup(node.content)) ? builders.indent([getMultilineGroup(node.content)])
: node.content, : node.content,
multiline multiline ? [builders.hardline, "}}"] : "}}",
? [builders.hardline, node.delimiter, "}}"]
: [node.delimiter, "}}"],
]), ]),
{ {
shouldBreak: node.preNewLines > 0, shouldBreak: node.ownLine,
}, }
); );
return node.preNewLines > 1
? builders.group([builders.trim, builders.hardline, expression])
: expression;
}; };
const printStatement = (node: Statement): builders.Doc => { const printStatement = (node: Statement): builders.Doc => {
@ -74,113 +60,110 @@ const printStatement = (node: Statement): builders.Doc => {
? [builders.hardline, node.delimiter, "%}"] ? [builders.hardline, node.delimiter, "%}"]
: [node.delimiter, "%}"], : [node.delimiter, "%}"],
]), ]),
{ shouldBreak: node.preNewLines > 0 }, { shouldBreak: node.ownLine }
); );
if ( if (
["else", "elif"].includes(node.keyword) && ["else", "elif"].includes(node.keyword) &&
surroundingBlock(node)?.containsNewLines surroundingBlock(node)?.ownLine
) { ) {
return [builders.dedent(builders.hardline), statemnt, builders.hardline]; return [builders.dedent(builders.hardline), statemnt, builders.hardline];
} }
return statemnt; return statemnt;
}; };
const printCommentBlock = (node: Node): builders.Doc => { const printIgnoreBlock = (node: IgnoreBlock): builders.Doc => {
const comment = builders.group(node.content, { return builders.group(node.content, { shouldBreak: node.ownLine });
shouldBreak: node.preNewLines > 0,
});
return node.preNewLines > 1
? builders.group([builders.trim, builders.hardline, comment])
: comment;
}; };
const printIgnoreBlock = (node: Node): builders.Doc => { export const embed: Printer<Node>["embed"] = (
return node.content; path,
}; print,
textToDoc,
export const embed: Printer<Node>["embed"] = () => { options
return _embed; ) => {
};
const _embed = async (
textToDoc: (text: string, options: Options) => Promise<Doc>,
print: (selector?: string | number | Array<string | number> | AstPath) => Doc,
path: AstPath,
options: Options,
): Promise<Doc | undefined> => {
const node = path.getNode(); const node = path.getNode();
if (!node || !["root", "block"].includes(node.type)) { if (!node || !["root", "block"].includes(node.type)) {
return undefined; return null;
} }
const mapped = await Promise.all( const mapped = splitAtElse(node).map((content) => {
splitAtElse(node).map(async (content) => { let doc;
let doc; if (content in node.nodes) {
if (content in node.nodes) { doc = content;
doc = content; } else {
} else { doc = utils.stripTrailingHardline(
doc = await textToDoc(content, { textToDoc(content, {
...options, ...options,
parser: "html", parser: "html",
}); })
);
}
let ignoreDoc = false;
return utils.mapDoc(doc, (currentDoc) => {
if (typeof currentDoc !== "string") {
return currentDoc;
} }
let ignoreDoc = false; if (currentDoc === "<!-- prettier-ignore -->") {
ignoreDoc = true;
return utils.mapDoc(doc, (currentDoc) => { return currentDoc;
if (typeof currentDoc !== "string") { }
return currentDoc;
}
if (currentDoc === "<!-- prettier-ignore -->") {
ignoreDoc = true;
return currentDoc;
}
const idxs = findPlaceholders(currentDoc).filter(
([start, end]) => currentDoc.slice(start, end + 1) in node.nodes,
);
if (!idxs.length) {
ignoreDoc = false;
return currentDoc;
}
const res: builders.Doc = [];
let lastEnd = 0;
for (const [start, end] of idxs) {
if (lastEnd < start) {
res.push(currentDoc.slice(lastEnd, start));
}
const p = currentDoc.slice(start, end + 1) as string;
if (ignoreDoc) {
res.push(node.nodes[p].originalText);
} else {
res.push(path.call(print, "nodes", p));
}
lastEnd = end + 1;
}
if (lastEnd > 0 && currentDoc.length > lastEnd) {
res.push(currentDoc.slice(lastEnd));
}
const idxs = findPlaceholders(currentDoc).filter(
([start, end]) => currentDoc.slice(start, end + 1) in node.nodes
);
if (!idxs.length) {
ignoreDoc = false; ignoreDoc = false;
return res; return currentDoc;
}); }
}),
); const res: builders.Doc = [];
let lastEnd = 0;
for (const [start, end] of idxs) {
if (lastEnd < start) {
res.push(currentDoc.slice(lastEnd, start));
}
const p = currentDoc.slice(start, end + 1) as string;
if (ignoreDoc) {
res.push(node.nodes[p].originalText);
} else {
res.push(path.call(print, "nodes", p));
}
lastEnd = end + 1;
}
if (lastEnd > 0 && currentDoc.length > lastEnd) {
res.push(currentDoc.slice(lastEnd));
}
ignoreDoc = false;
return res;
});
});
if (node.type === "block") { if (node.type === "block") {
const block = buildBlock(path, print, node as Block, mapped); if (node.content.includes("\n")) {
return builders.group([
return node.preNewLines > 1 path.call(print, "nodes", (node as Block).start.id),
? builders.group([builders.trim, builders.hardline, block]) builders.indent([
: block; builders.softline,
utils.stripTrailingHardline(mapped),
]),
builders.hardline,
path.call(print, "nodes", (node as Block).end.id),
]);
}
return builders.group([
path.call(print, "nodes", (node as Block).start.id),
utils.stripTrailingHardline(mapped),
path.call(print, "nodes", (node as Block).end.id),
]);
} }
return [...mapped, builders.hardline]; return [...mapped, builders.hardline];
}; };
@ -189,7 +172,7 @@ const getMultilineGroup = (content: String): builders.Group => {
return builders.group( return builders.group(
content.split("\n").map((line, i) => { content.split("\n").map((line, i) => {
return [builders.hardline, line.trim()]; return [builders.hardline, line.trim()];
}), })
); );
}; };
@ -198,7 +181,7 @@ const splitAtElse = (node: Node): string[] => {
(n) => (n) =>
n.type === "statement" && n.type === "statement" &&
["else", "elif"].includes((n as Statement).keyword) && ["else", "elif"].includes((n as Statement).keyword) &&
node.content.search(n.id) !== NOT_FOUND, node.content.search(n.id) !== NOT_FOUND
); );
if (!elseNodes.length) { if (!elseNodes.length) {
return [node.content]; return [node.content];
@ -235,35 +218,6 @@ export const findPlaceholders = (text: string): [number, number][] => {
export const surroundingBlock = (node: Node): Block | undefined => { export const surroundingBlock = (node: Node): Block | undefined => {
return Object.values(node.nodes).find( return Object.values(node.nodes).find(
(n) => n.type === "block" && n.content.search(node.id) !== NOT_FOUND, (n) => n.type === "block" && n.content.search(node.id) !== NOT_FOUND
) as Block; ) as Block;
}; };
const buildBlock = (
path: AstPath<Node>,
print: (path: AstPath<Node>) => builders.Doc,
block: Block,
mapped: (string | builders.Doc[] | builders.DocCommand)[],
): builders.Doc => {
// if the content is empty or whitespace only.
if (block.content.match(/^\s*$/)) {
return builders.fill([
path.call(print, "nodes", block.start.id),
builders.softline,
path.call(print, "nodes", block.end.id),
]);
}
if (block.containsNewLines) {
return builders.group([
path.call(print, "nodes", block.start.id),
builders.indent([builders.softline, mapped]),
builders.hardline,
path.call(print, "nodes", block.end.id),
]);
}
return builders.group([
path.call(print, "nodes", block.start.id),
mapped,
path.call(print, "nodes", block.end.id),
]);
};

View file

@ -1,3 +1,5 @@
<div>{{ user.name }}</div> <div>
{{ user.name }}
</div>
<div>{{ user.age }}</div> <div>{{ user.age }}</div>

View file

@ -1,9 +0,0 @@
<div class="extemely_long__class_name extemely_long__class_name--modifier">
{{- some_variable -}}
</div>
<div class="extemely_long__class_name extemely_long__class_name--modifier">
{{- some_variable -}}
</div>
<div class="extemely_long__class_name extemely_long__class_name--modifier">
{{- some_variable -}}
</div>

View file

@ -1,9 +0,0 @@
<div class="extemely_long__class_name extemely_long__class_name--modifier">
{{- some_variable -}}
</div>
<div class="extemely_long__class_name extemely_long__class_name--modifier">
{{ some_variable -}}
</div>
<div class="extemely_long__class_name extemely_long__class_name--modifier">
{{- some_variable }}
</div>

View file

@ -1,46 +0,0 @@
{% extends "base.html" %}
{% block title %}Index{% endblock %}
{% block head %}
{{ super() }}
<style type="text/css">
.important {
color: #336699;
}
</style>
{% endblock %}
{{ title }}
{# content #}
{% block content %}
<h1>Index</h1>
<p class="important">Welcome to my awesome homepage.</p>
{% endblock %}
<div></div>
{{ name }}
{% for user in users %}
<li>{{ user.username|e }}</li>
{% else %}
<li><em>no users found</em></li>
{% endfor %}
{% macro input(name, value='', type='text', size=20) %}
{{ my_variable|default('my_variable is not defined') }}
{% for city, items in users|groupby("city") %}
<li>
{{ city }}
<ul>
{% for user in items %}
<li>{{ user.name }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
{% endmacro %}

View file

@ -1,53 +0,0 @@
{% extends "base.html" %}
{% block title %}Index{% endblock %}
{% block head %}
{{ super() }}
<style type="text/css">
.important { color: #336699; }
</style>
{% endblock %}
{{title}}
{# content #}
{% block content %}
<h1>Index</h1>
<p class="important">
Welcome to my awesome homepage.
</p>
{% endblock %}
<div></div>
{{name}}
{% for user in users %}
<li>{{ user.username|e }}</li>
{% else %}
<li><em>no users found</em></li>
{% endfor %}
{% macro input(name, value='', type='text', size=20) %}
{{ my_variable|default('my_variable is not defined') }}
{% for city, items in users|groupby("city") %}
<li>{{ city }}
<ul>{% for user in items %}
<li>{{ user.name }}
{% endfor %}</ul>
</li>
{% endfor %}
{% endmacro %}

View file

@ -1 +1 @@
Error('Closing statement "endif" doesn't match Opening Statement "for item in seq".') Error('Closung statement "endif" doesn't match Opening Statement "for item in seq".')

View file

@ -1,2 +0,0 @@
{% block title %}
{% endblock %}

View file

@ -1,5 +0,0 @@
{% block title %}
{% endblock %}

View file

@ -1,11 +1,11 @@
import { existsSync, readdirSync, readFileSync } from "fs"; import { existsSync, readdirSync, readFileSync } from "fs";
import { join } from "path"; import { join } from "path";
import { format, Options } from "prettier"; import { format, Options, ParserOptions } from "prettier";
import * as jinjaPlugin from "../src/index"; import * as jinjaPlugin from "../src/index";
const prettify = (code: string, options: Options) => const prettify = (code: string, options: Options) =>
format(code, { format(code, {
parser: "askama-template", parser: "jinja-template",
plugins: [jinjaPlugin], plugins: [jinjaPlugin],
...options, ...options,
}); });
@ -17,7 +17,7 @@ tests.forEach((test) => {
if (test.startsWith("_")) { if (test.startsWith("_")) {
return; return;
} }
return it(test, async () => { return it(test, () => {
const path = join(testFolder, test); const path = join(testFolder, test);
const input = readFileSync(join(path, "input.html")).toString(); const input = readFileSync(join(path, "input.html")).toString();
const expected = readFileSync(join(path, "expected.html")).toString(); const expected = readFileSync(join(path, "expected.html")).toString();
@ -34,11 +34,21 @@ tests.forEach((test) => {
if (expectedError) { if (expectedError) {
jest.spyOn(console, "error").mockImplementation(() => {}); jest.spyOn(console, "error").mockImplementation(() => {});
await expect(format()).rejects.toThrow(expectedError); expect(format).toThrow(expectedError);
} else { } else {
const result = await format(); const result = format();
expect(result).toEqual(expected); expect(result).toEqual(expected);
expect(await prettify(result, configObject)).toEqual(expected); expect(prettify(result, configObject)).toEqual(expected);
} }
}); });
}); });
test("node has correct locStart and -End", () => {
const plugin = jinjaPlugin.parsers["jinja-template"];
const ast = plugin.parse(`<p>{{ test }}</p>`, {}, {} as ParserOptions);
const node = Object.values(ast.nodes)[0]!;
expect(plugin.locStart(node)).toEqual(3);
expect(plugin.locEnd(node)).toEqual(13);
});

View file

@ -1,8 +1,8 @@
{ {
"compilerOptions": { "compilerOptions": {
"outDir": "./lib", "outDir": "./lib",
"target": "esnext", "target": "ES6",
"module": "NodeNext", "module": "commonjs",
"moduleResolution": "node", "moduleResolution": "node",
"lib": ["esnext"], "lib": ["esnext"],
"declaration": true, "declaration": true,