Compare commits
3 commits
master
...
better-tes
Author | SHA1 | Date | |
---|---|---|---|
|
c4a4618840 | ||
|
74228f1fb3 | ||
|
f154303f76 |
21 changed files with 1170 additions and 1402 deletions
14
.github/workflows/node.js.yml
vendored
14
.github/workflows/node.js.yml
vendored
|
@ -17,6 +17,14 @@ jobs:
|
|||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
cache: "npm"
|
||||
- run: npm install -f
|
||||
- run: npm run build
|
||||
- run: npm run test
|
||||
- name: Install dependencies
|
||||
run: npm install -f
|
||||
|
||||
- name: Build project
|
||||
run: npm run build
|
||||
|
||||
- name: Run tests
|
||||
run: npm run test
|
||||
|
||||
- name: Upload coverage
|
||||
uses: codecov/codecov-action@v1
|
||||
|
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
|||
.vscode
|
||||
node_modules
|
||||
lib
|
||||
coverage
|
1
.npmrc
1
.npmrc
|
@ -1 +0,0 @@
|
|||
registry=https://git.fetbuk.ru/api/packages/WerySkok/npm/
|
21
README.md
21
README.md
|
@ -1,17 +1,11 @@
|
|||
# prettier-plugin-askama-template
|
||||
[prettier-plugin-jinja-template](https://github.com/davidodenwald/prettier-plugin-jinja-template) but with patches to work with Askama.
|
||||
# prettier-plugin-jinja-template
|
||||
|
||||
Formatter plugin for jinja2 template files.
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
npm install --save-dev prettier https://git.fetbuk.ru/WerySkok/-/packages/npm/prettier-plugin-askama-template/1.0.0/files/2
|
||||
```
|
||||
|
||||
Add the plugin to your `.prettierrc`:
|
||||
```json
|
||||
{
|
||||
"plugins": ["prettier-plugin-askama-template"]
|
||||
}
|
||||
npm install --save-dev prettier prettier-plugin-jinja-template
|
||||
```
|
||||
|
||||
## Use
|
||||
|
@ -23,7 +17,7 @@ To format basic .html files, you'll have to override the used parser inside your
|
|||
{
|
||||
"files": ["*.html"],
|
||||
"options": {
|
||||
"parser": "askama-template"
|
||||
"parser": "jinja-template"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
@ -34,8 +28,3 @@ Run it on all html files in your project:
|
|||
```bash
|
||||
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
|
||||
```
|
|
@ -1,4 +1,8 @@
|
|||
const { defaults } = require("jest-config");
|
||||
|
||||
/** @type {import('@jest/types').Config.InitialOptions} */
|
||||
module.exports = () => ({
|
||||
preset: "ts-jest",
|
||||
moduleFileExtensions: [...defaults.moduleFileExtensions, "html"],
|
||||
collectCoverage: true,
|
||||
});
|
||||
|
|
2036
package-lock.json
generated
2036
package-lock.json
generated
File diff suppressed because it is too large
Load diff
34
package.json
34
package.json
|
@ -1,41 +1,43 @@
|
|||
{
|
||||
"name": "prettier-plugin-askama-template",
|
||||
"version": "1.0.0",
|
||||
"description": "prettier-plugin-jinja-template but with patches to work with Askama.",
|
||||
"author": "Alexander Minkin",
|
||||
"name": "prettier-plugin-jinja-template",
|
||||
"version": "0.2.0",
|
||||
"description": "Prettier plugin for formatting jinja templates.",
|
||||
"author": "David Odenwald",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"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": {
|
||||
"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": [
|
||||
"prettier",
|
||||
"plugin",
|
||||
"template",
|
||||
"html",
|
||||
"askama"
|
||||
"jinja",
|
||||
"jinja2",
|
||||
"flask"
|
||||
],
|
||||
"main": "lib/index.js",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"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",
|
||||
"publish:release": "npm run test && npm run build && npm publish"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/preset-typescript": "^7.22.5",
|
||||
"@types/jest": "^29.5.3",
|
||||
"jest": "^29.6.2",
|
||||
"prettier": "^3.0.1",
|
||||
"ts-jest": "^29.1.1",
|
||||
"typescript": "^5.1.6"
|
||||
"@babel/preset-typescript": "^7.21.0",
|
||||
"@types/jest": "^29.5.0",
|
||||
"jest": "^29.5.0",
|
||||
"prettier": "^2.8.4",
|
||||
"ts-jest": "^29.0.5",
|
||||
"typescript": "^5.0.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"prettier": "^3.0.0"
|
||||
"prettier": "^2.0.0"
|
||||
}
|
||||
}
|
||||
|
|
11
src/index.ts
11
src/index.ts
|
@ -1,16 +1,16 @@
|
|||
import { Node } from "./jinja";
|
||||
import { parse } from "./parser";
|
||||
import { print, embed, getVisitorKeys } from "./printer";
|
||||
import { print, embed } from "./printer";
|
||||
import { Parser, Printer, SupportLanguage } from "prettier";
|
||||
|
||||
const PLUGIN_KEY = "askama-template";
|
||||
const PLUGIN_KEY = "jinja-template";
|
||||
|
||||
export const languages: SupportLanguage[] = [
|
||||
{
|
||||
name: "AskamaTemplate",
|
||||
name: "JinjaTemplate",
|
||||
parsers: [PLUGIN_KEY],
|
||||
extensions: [".jinja", ".jinja2", ".j2", ".html", "askama"],
|
||||
vscodeLanguageIds: ["askama"],
|
||||
extensions: [".jinja", ".jinja2", ".j2", ".html"],
|
||||
vscodeLanguageIds: ["jinja"],
|
||||
},
|
||||
];
|
||||
|
||||
|
@ -27,6 +27,5 @@ export const printers = {
|
|||
[PLUGIN_KEY]: <Printer<Node>>{
|
||||
print,
|
||||
embed,
|
||||
getVisitorKeys,
|
||||
},
|
||||
};
|
||||
|
|
12
src/jinja.ts
12
src/jinja.ts
|
@ -5,9 +5,9 @@ export const Placeholder = {
|
|||
|
||||
export interface Node {
|
||||
id: string;
|
||||
type: "root" | "expression" | "statement" | "block" | "comment" | "ignore";
|
||||
type: "root" | "expression" | "statement" | "block" | "ignore";
|
||||
content: string;
|
||||
preNewLines: number;
|
||||
ownLine: boolean;
|
||||
originalText: string;
|
||||
index: number;
|
||||
length: number;
|
||||
|
@ -16,7 +16,6 @@ export interface Node {
|
|||
|
||||
export interface Expression extends Node {
|
||||
type: "expression";
|
||||
delimiter: Delimiter;
|
||||
}
|
||||
|
||||
export type Delimiter = "" | "-" | "+";
|
||||
|
@ -31,7 +30,10 @@ export interface Block extends Node {
|
|||
type: "block";
|
||||
start: Statement;
|
||||
end: Statement;
|
||||
containsNewLines: boolean;
|
||||
}
|
||||
|
||||
export interface IgnoreBlock extends Node {
|
||||
type: "ignore";
|
||||
}
|
||||
|
||||
export const nonClosingStatements = [
|
||||
|
@ -41,6 +43,4 @@ export const nonClosingStatements = [
|
|||
"import",
|
||||
"from",
|
||||
"extends",
|
||||
"pluralize",
|
||||
"when"
|
||||
];
|
||||
|
|
|
@ -3,7 +3,6 @@ import {
|
|||
Delimiter,
|
||||
Node,
|
||||
Placeholder,
|
||||
Expression,
|
||||
Statement,
|
||||
Block,
|
||||
nonClosingStatements,
|
||||
|
@ -12,7 +11,7 @@ import {
|
|||
const NOT_FOUND = -1;
|
||||
|
||||
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) => {
|
||||
const statementStack: Statement[] = [];
|
||||
|
@ -21,7 +20,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
|
|||
id: "0",
|
||||
type: "root" as const,
|
||||
content: text,
|
||||
preNewLines: 0,
|
||||
ownLine: false,
|
||||
originalText: text,
|
||||
index: 0,
|
||||
length: 0,
|
||||
|
@ -44,6 +43,9 @@ export const parse: Parser<Node>["parse"] = (text) => {
|
|||
continue;
|
||||
}
|
||||
|
||||
const pre = match.groups.pre || "";
|
||||
const newline = !!match.groups.newline;
|
||||
|
||||
const matchText = match.groups.node;
|
||||
const expression = match.groups.expression;
|
||||
const statement = match.groups.statement;
|
||||
|
@ -55,42 +57,31 @@ export const parse: Parser<Node>["parse"] = (text) => {
|
|||
}
|
||||
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 = {
|
||||
id: placeholder,
|
||||
preNewLines,
|
||||
ownLine: newline,
|
||||
originalText: matchText,
|
||||
index: match.index + i,
|
||||
index: match.index + i + pre.length,
|
||||
length: matchText.length,
|
||||
nodes: root.nodes,
|
||||
};
|
||||
|
||||
if (comment || ignoreBlock) {
|
||||
if (ignoreBlock || comment) {
|
||||
root.content = root.content.replace(matchText, placeholder);
|
||||
root.nodes[node.id] = {
|
||||
...node,
|
||||
type: comment ? "comment" : "ignore",
|
||||
content: comment || ignoreBlock,
|
||||
type: "ignore",
|
||||
content: ignoreBlock || comment,
|
||||
};
|
||||
}
|
||||
|
||||
if (expression) {
|
||||
const delimiter = (match.groups.startDelimiterEx ||
|
||||
match.groups.endDelimiterEx) as Delimiter;
|
||||
|
||||
root.content = root.content.replace(matchText, placeholder);
|
||||
root.nodes[node.id] = {
|
||||
...node,
|
||||
type: "expression",
|
||||
content: expression,
|
||||
delimiter,
|
||||
} as Expression;
|
||||
};
|
||||
}
|
||||
|
||||
if (statement) {
|
||||
|
@ -136,7 +127,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
|
|||
}
|
||||
|
||||
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,
|
||||
end: end,
|
||||
content: blockText.slice(start.length, blockText.length - end.length),
|
||||
preNewLines: start.preNewLines,
|
||||
containsNewLines: originalText.search("\n") !== NOT_FOUND,
|
||||
ownLine: originalText.search("\n") !== NOT_FOUND,
|
||||
originalText,
|
||||
index: start.index,
|
||||
length: end.index + end.length - start.index,
|
||||
|
|
244
src/printer.ts
244
src/printer.ts
|
@ -1,24 +1,18 @@
|
|||
import { AstPath, Printer, Options, Doc } from "prettier";
|
||||
import { Printer } from "prettier";
|
||||
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;
|
||||
|
||||
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) => {
|
||||
const node = path.getNode();
|
||||
if (!node) {
|
||||
|
@ -30,10 +24,8 @@ export const print: Printer<Node>["print"] = (path) => {
|
|||
return printExpression(node as Expression);
|
||||
case "statement":
|
||||
return printStatement(node as Statement);
|
||||
case "comment":
|
||||
return printCommentBlock(node);
|
||||
case "ignore":
|
||||
return printIgnoreBlock(node);
|
||||
return printIgnoreBlock(node as IgnoreBlock);
|
||||
}
|
||||
return [];
|
||||
};
|
||||
|
@ -41,24 +33,18 @@ export const print: Printer<Node>["print"] = (path) => {
|
|||
const printExpression = (node: Expression): builders.Doc => {
|
||||
const multiline = node.content.includes("\n");
|
||||
|
||||
const expression = builders.group(
|
||||
return builders.group(
|
||||
builders.join(" ", [
|
||||
["{{", node.delimiter],
|
||||
"{{",
|
||||
multiline
|
||||
? builders.indent(getMultilineGroup(node.content))
|
||||
? builders.indent([getMultilineGroup(node.content)])
|
||||
: node.content,
|
||||
multiline
|
||||
? [builders.hardline, node.delimiter, "}}"]
|
||||
: [node.delimiter, "}}"],
|
||||
multiline ? [builders.hardline, "}}"] : "}}",
|
||||
]),
|
||||
{
|
||||
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 => {
|
||||
|
@ -74,113 +60,110 @@ const printStatement = (node: Statement): builders.Doc => {
|
|||
? [builders.hardline, node.delimiter, "%}"]
|
||||
: [node.delimiter, "%}"],
|
||||
]),
|
||||
{ shouldBreak: node.preNewLines > 0 },
|
||||
{ shouldBreak: node.ownLine }
|
||||
);
|
||||
|
||||
if (
|
||||
["else", "elif"].includes(node.keyword) &&
|
||||
surroundingBlock(node)?.containsNewLines
|
||||
surroundingBlock(node)?.ownLine
|
||||
) {
|
||||
return [builders.dedent(builders.hardline), statemnt, builders.hardline];
|
||||
}
|
||||
return statemnt;
|
||||
};
|
||||
|
||||
const printCommentBlock = (node: Node): builders.Doc => {
|
||||
const comment = builders.group(node.content, {
|
||||
shouldBreak: node.preNewLines > 0,
|
||||
});
|
||||
|
||||
return node.preNewLines > 1
|
||||
? builders.group([builders.trim, builders.hardline, comment])
|
||||
: comment;
|
||||
const printIgnoreBlock = (node: IgnoreBlock): builders.Doc => {
|
||||
return builders.group(node.content, { shouldBreak: node.ownLine });
|
||||
};
|
||||
|
||||
const printIgnoreBlock = (node: Node): builders.Doc => {
|
||||
return node.content;
|
||||
};
|
||||
|
||||
export const embed: Printer<Node>["embed"] = () => {
|
||||
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> => {
|
||||
export const embed: Printer<Node>["embed"] = (
|
||||
path,
|
||||
print,
|
||||
textToDoc,
|
||||
options
|
||||
) => {
|
||||
const node = path.getNode();
|
||||
if (!node || !["root", "block"].includes(node.type)) {
|
||||
return undefined;
|
||||
return null;
|
||||
}
|
||||
|
||||
const mapped = await Promise.all(
|
||||
splitAtElse(node).map(async (content) => {
|
||||
let doc;
|
||||
if (content in node.nodes) {
|
||||
doc = content;
|
||||
} else {
|
||||
doc = await textToDoc(content, {
|
||||
const mapped = splitAtElse(node).map((content) => {
|
||||
let doc;
|
||||
if (content in node.nodes) {
|
||||
doc = content;
|
||||
} else {
|
||||
doc = utils.stripTrailingHardline(
|
||||
textToDoc(content, {
|
||||
...options,
|
||||
parser: "html",
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
let ignoreDoc = false;
|
||||
|
||||
return utils.mapDoc(doc, (currentDoc) => {
|
||||
if (typeof currentDoc !== "string") {
|
||||
return currentDoc;
|
||||
}
|
||||
|
||||
let ignoreDoc = false;
|
||||
|
||||
return utils.mapDoc(doc, (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));
|
||||
}
|
||||
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 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") {
|
||||
const block = buildBlock(path, print, node as Block, mapped);
|
||||
|
||||
return node.preNewLines > 1
|
||||
? builders.group([builders.trim, builders.hardline, block])
|
||||
: block;
|
||||
if (node.content.includes("\n")) {
|
||||
return builders.group([
|
||||
path.call(print, "nodes", (node as Block).start.id),
|
||||
builders.indent([
|
||||
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];
|
||||
};
|
||||
|
@ -189,7 +172,7 @@ const getMultilineGroup = (content: String): builders.Group => {
|
|||
return builders.group(
|
||||
content.split("\n").map((line, i) => {
|
||||
return [builders.hardline, line.trim()];
|
||||
}),
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
|
@ -198,7 +181,7 @@ const splitAtElse = (node: Node): string[] => {
|
|||
(n) =>
|
||||
n.type === "statement" &&
|
||||
["else", "elif"].includes((n as Statement).keyword) &&
|
||||
node.content.search(n.id) !== NOT_FOUND,
|
||||
node.content.search(n.id) !== NOT_FOUND
|
||||
);
|
||||
if (!elseNodes.length) {
|
||||
return [node.content];
|
||||
|
@ -235,35 +218,6 @@ export const findPlaceholders = (text: string): [number, number][] => {
|
|||
|
||||
export const surroundingBlock = (node: Node): Block | undefined => {
|
||||
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;
|
||||
};
|
||||
|
||||
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),
|
||||
]);
|
||||
};
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
<div>{{ user.name }}</div>
|
||||
<div>
|
||||
{{ user.name }}
|
||||
</div>
|
||||
|
||||
<div>{{ user.age }}</div>
|
||||
|
|
|
@ -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>
|
|
@ -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>
|
|
@ -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 %}
|
|
@ -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 %}
|
|
@ -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".')
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
{% block title %}
|
||||
{% endblock %}
|
|
@ -1,5 +0,0 @@
|
|||
{% block title %}
|
||||
|
||||
|
||||
|
||||
{% endblock %}
|
|
@ -1,11 +1,11 @@
|
|||
import { existsSync, readdirSync, readFileSync } from "fs";
|
||||
import { join } from "path";
|
||||
import { format, Options } from "prettier";
|
||||
import { format, Options, ParserOptions } from "prettier";
|
||||
import * as jinjaPlugin from "../src/index";
|
||||
|
||||
const prettify = (code: string, options: Options) =>
|
||||
format(code, {
|
||||
parser: "askama-template",
|
||||
parser: "jinja-template",
|
||||
plugins: [jinjaPlugin],
|
||||
...options,
|
||||
});
|
||||
|
@ -17,7 +17,7 @@ tests.forEach((test) => {
|
|||
if (test.startsWith("_")) {
|
||||
return;
|
||||
}
|
||||
return it(test, async () => {
|
||||
return it(test, () => {
|
||||
const path = join(testFolder, test);
|
||||
const input = readFileSync(join(path, "input.html")).toString();
|
||||
const expected = readFileSync(join(path, "expected.html")).toString();
|
||||
|
@ -34,11 +34,21 @@ tests.forEach((test) => {
|
|||
|
||||
if (expectedError) {
|
||||
jest.spyOn(console, "error").mockImplementation(() => {});
|
||||
await expect(format()).rejects.toThrow(expectedError);
|
||||
expect(format).toThrow(expectedError);
|
||||
} else {
|
||||
const result = await format();
|
||||
const result = format();
|
||||
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);
|
||||
});
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"outDir": "./lib",
|
||||
"target": "esnext",
|
||||
"module": "NodeNext",
|
||||
"target": "ES6",
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"lib": ["esnext"],
|
||||
"declaration": true,
|
||||
|
|
Loading…
Reference in a new issue