Compare commits
17 commits
better-tes
...
master
Author | SHA1 | Date | |
---|---|---|---|
6e119d7583 | |||
8409c44d0d | |||
79e54384f3 | |||
559b2b0506 | |||
|
bea4b534bc | ||
|
c0f1ca478d | ||
|
be28ec949f | ||
|
ac512dd0b3 | ||
|
f54a1ff40a | ||
|
fd332a60e1 | ||
|
c854cfd82d | ||
|
1aec2a655e | ||
|
85a635f03b | ||
|
d72173d3fe | ||
|
60a2cc487e | ||
|
65dd4f1aea | ||
|
7620e52d97 |
21 changed files with 1413 additions and 1181 deletions
14
.github/workflows/node.js.yml
vendored
14
.github/workflows/node.js.yml
vendored
|
@ -17,14 +17,6 @@ jobs:
|
|||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
cache: "npm"
|
||||
- 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
|
||||
- run: npm install -f
|
||||
- run: npm run build
|
||||
- run: npm run test
|
||||
|
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,4 +1,3 @@
|
|||
.vscode
|
||||
node_modules
|
||||
lib
|
||||
coverage
|
1
.npmrc
Normal file
1
.npmrc
Normal file
|
@ -0,0 +1 @@
|
|||
registry=https://git.fetbuk.ru/api/packages/WerySkok/npm/
|
21
README.md
21
README.md
|
@ -1,11 +1,17 @@
|
|||
# prettier-plugin-jinja-template
|
||||
|
||||
Formatter plugin for jinja2 template files.
|
||||
# prettier-plugin-askama-template
|
||||
[prettier-plugin-jinja-template](https://github.com/davidodenwald/prettier-plugin-jinja-template) but with patches to work with Askama.
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
npm install --save-dev prettier prettier-plugin-jinja-template
|
||||
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"]
|
||||
}
|
||||
```
|
||||
|
||||
## Use
|
||||
|
@ -17,7 +23,7 @@ To format basic .html files, you'll have to override the used parser inside your
|
|||
{
|
||||
"files": ["*.html"],
|
||||
"options": {
|
||||
"parser": "jinja-template"
|
||||
"parser": "askama-template"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
@ -28,3 +34,8 @@ 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,8 +1,4 @@
|
|||
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,43 +1,41 @@
|
|||
{
|
||||
"name": "prettier-plugin-jinja-template",
|
||||
"version": "0.2.0",
|
||||
"description": "Prettier plugin for formatting jinja templates.",
|
||||
"author": "David Odenwald",
|
||||
"name": "prettier-plugin-askama-template",
|
||||
"version": "1.0.0",
|
||||
"description": "prettier-plugin-jinja-template but with patches to work with Askama.",
|
||||
"author": "Alexander Minkin",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/davidodenwald/prettier-plugin-jinja-template.git"
|
||||
"url": "git+https://git.fetbuk.ru/WerySkok/prettier-plugin-askama-template.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/davidodenwald/prettier-plugin-jinja-template/issues"
|
||||
"url": "https://git.fetbuk.ru/WerySkok/prettier-plugin-askama-template/issues"
|
||||
},
|
||||
"homepage": "https://github.com/davidodenwald/prettier-plugin-jinja-template#readme",
|
||||
"homepage": "https://git.fetbuk.ru/WerySkok/prettier-plugin-askama-template#readme",
|
||||
"keywords": [
|
||||
"prettier",
|
||||
"plugin",
|
||||
"template",
|
||||
"html",
|
||||
"jinja",
|
||||
"jinja2",
|
||||
"flask"
|
||||
"askama"
|
||||
],
|
||||
"main": "lib/index.js",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"watch": "npm run build -- --watch",
|
||||
"test": "jest --verbose",
|
||||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --verbose",
|
||||
"test:watch": "jest --watch --verbose",
|
||||
"publish:release": "npm run test && npm run build && npm publish"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@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"
|
||||
"@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"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"prettier": "^2.0.0"
|
||||
"prettier": "^3.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 } from "./printer";
|
||||
import { print, embed, getVisitorKeys } from "./printer";
|
||||
import { Parser, Printer, SupportLanguage } from "prettier";
|
||||
|
||||
const PLUGIN_KEY = "jinja-template";
|
||||
const PLUGIN_KEY = "askama-template";
|
||||
|
||||
export const languages: SupportLanguage[] = [
|
||||
{
|
||||
name: "JinjaTemplate",
|
||||
name: "AskamaTemplate",
|
||||
parsers: [PLUGIN_KEY],
|
||||
extensions: [".jinja", ".jinja2", ".j2", ".html"],
|
||||
vscodeLanguageIds: ["jinja"],
|
||||
extensions: [".jinja", ".jinja2", ".j2", ".html", "askama"],
|
||||
vscodeLanguageIds: ["askama"],
|
||||
},
|
||||
];
|
||||
|
||||
|
@ -27,5 +27,6 @@ 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" | "ignore";
|
||||
type: "root" | "expression" | "statement" | "block" | "comment" | "ignore";
|
||||
content: string;
|
||||
ownLine: boolean;
|
||||
preNewLines: number;
|
||||
originalText: string;
|
||||
index: number;
|
||||
length: number;
|
||||
|
@ -16,6 +16,7 @@ export interface Node {
|
|||
|
||||
export interface Expression extends Node {
|
||||
type: "expression";
|
||||
delimiter: Delimiter;
|
||||
}
|
||||
|
||||
export type Delimiter = "" | "-" | "+";
|
||||
|
@ -30,10 +31,7 @@ export interface Block extends Node {
|
|||
type: "block";
|
||||
start: Statement;
|
||||
end: Statement;
|
||||
}
|
||||
|
||||
export interface IgnoreBlock extends Node {
|
||||
type: "ignore";
|
||||
containsNewLines: boolean;
|
||||
}
|
||||
|
||||
export const nonClosingStatements = [
|
||||
|
@ -43,4 +41,6 @@ export const nonClosingStatements = [
|
|||
"import",
|
||||
"from",
|
||||
"extends",
|
||||
"pluralize",
|
||||
"when"
|
||||
];
|
||||
|
|
|
@ -3,6 +3,7 @@ import {
|
|||
Delimiter,
|
||||
Node,
|
||||
Placeholder,
|
||||
Expression,
|
||||
Statement,
|
||||
Block,
|
||||
nonClosingStatements,
|
||||
|
@ -11,7 +12,7 @@ import {
|
|||
const NOT_FOUND = -1;
|
||||
|
||||
const regex =
|
||||
/(?<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 -->))/;
|
||||
/(?<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 -->))/;
|
||||
|
||||
export const parse: Parser<Node>["parse"] = (text) => {
|
||||
const statementStack: Statement[] = [];
|
||||
|
@ -20,7 +21,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
|
|||
id: "0",
|
||||
type: "root" as const,
|
||||
content: text,
|
||||
ownLine: false,
|
||||
preNewLines: 0,
|
||||
originalText: text,
|
||||
index: 0,
|
||||
length: 0,
|
||||
|
@ -43,9 +44,6 @@ 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;
|
||||
|
@ -57,31 +55,42 @@ 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,
|
||||
ownLine: newline,
|
||||
preNewLines,
|
||||
originalText: matchText,
|
||||
index: match.index + i + pre.length,
|
||||
index: match.index + i,
|
||||
length: matchText.length,
|
||||
nodes: root.nodes,
|
||||
};
|
||||
|
||||
if (ignoreBlock || comment) {
|
||||
if (comment || ignoreBlock) {
|
||||
root.content = root.content.replace(matchText, placeholder);
|
||||
root.nodes[node.id] = {
|
||||
...node,
|
||||
type: "ignore",
|
||||
content: ignoreBlock || comment,
|
||||
type: comment ? "comment" : "ignore",
|
||||
content: comment || ignoreBlock,
|
||||
};
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -127,7 +136,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
|
|||
}
|
||||
|
||||
throw new Error(
|
||||
`Closung statement "${statement}" doesn't match Opening Statement "${start.content}".`
|
||||
`Closing statement "${statement}" doesn't match Opening Statement "${start.content}".`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -153,7 +162,8 @@ export const parse: Parser<Node>["parse"] = (text) => {
|
|||
start: start,
|
||||
end: end,
|
||||
content: blockText.slice(start.length, blockText.length - end.length),
|
||||
ownLine: originalText.search("\n") !== NOT_FOUND,
|
||||
preNewLines: start.preNewLines,
|
||||
containsNewLines: originalText.search("\n") !== NOT_FOUND,
|
||||
originalText,
|
||||
index: start.index,
|
||||
length: end.index + end.length - start.index,
|
||||
|
|
152
src/printer.ts
152
src/printer.ts
|
@ -1,18 +1,24 @@
|
|||
import { Printer } from "prettier";
|
||||
import { AstPath, Printer, Options, Doc } from "prettier";
|
||||
import { builders, utils } from "prettier/doc";
|
||||
import {
|
||||
Placeholder,
|
||||
Node,
|
||||
Expression,
|
||||
Statement,
|
||||
Block,
|
||||
IgnoreBlock,
|
||||
} from "./jinja";
|
||||
import { Placeholder, Node, Expression, Statement, Block } 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) {
|
||||
|
@ -24,8 +30,10 @@ 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 as IgnoreBlock);
|
||||
return printIgnoreBlock(node);
|
||||
}
|
||||
return [];
|
||||
};
|
||||
|
@ -33,18 +41,24 @@ export const print: Printer<Node>["print"] = (path) => {
|
|||
const printExpression = (node: Expression): builders.Doc => {
|
||||
const multiline = node.content.includes("\n");
|
||||
|
||||
return builders.group(
|
||||
const expression = builders.group(
|
||||
builders.join(" ", [
|
||||
"{{",
|
||||
["{{", node.delimiter],
|
||||
multiline
|
||||
? builders.indent([getMultilineGroup(node.content)])
|
||||
? builders.indent(getMultilineGroup(node.content))
|
||||
: node.content,
|
||||
multiline ? [builders.hardline, "}}"] : "}}",
|
||||
multiline
|
||||
? [builders.hardline, node.delimiter, "}}"]
|
||||
: [node.delimiter, "}}"],
|
||||
]),
|
||||
{
|
||||
shouldBreak: node.ownLine,
|
||||
}
|
||||
shouldBreak: node.preNewLines > 0,
|
||||
},
|
||||
);
|
||||
|
||||
return node.preNewLines > 1
|
||||
? builders.group([builders.trim, builders.hardline, expression])
|
||||
: expression;
|
||||
};
|
||||
|
||||
const printStatement = (node: Statement): builders.Doc => {
|
||||
|
@ -60,44 +74,57 @@ const printStatement = (node: Statement): builders.Doc => {
|
|||
? [builders.hardline, node.delimiter, "%}"]
|
||||
: [node.delimiter, "%}"],
|
||||
]),
|
||||
{ shouldBreak: node.ownLine }
|
||||
{ shouldBreak: node.preNewLines > 0 },
|
||||
);
|
||||
|
||||
if (
|
||||
["else", "elif"].includes(node.keyword) &&
|
||||
surroundingBlock(node)?.ownLine
|
||||
surroundingBlock(node)?.containsNewLines
|
||||
) {
|
||||
return [builders.dedent(builders.hardline), statemnt, builders.hardline];
|
||||
}
|
||||
return statemnt;
|
||||
};
|
||||
|
||||
const printIgnoreBlock = (node: IgnoreBlock): builders.Doc => {
|
||||
return builders.group(node.content, { shouldBreak: node.ownLine });
|
||||
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;
|
||||
};
|
||||
|
||||
export const embed: Printer<Node>["embed"] = (
|
||||
path,
|
||||
print,
|
||||
textToDoc,
|
||||
options
|
||||
) => {
|
||||
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> => {
|
||||
const node = path.getNode();
|
||||
if (!node || !["root", "block"].includes(node.type)) {
|
||||
return null;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const mapped = splitAtElse(node).map((content) => {
|
||||
const mapped = await Promise.all(
|
||||
splitAtElse(node).map(async (content) => {
|
||||
let doc;
|
||||
if (content in node.nodes) {
|
||||
doc = content;
|
||||
} else {
|
||||
doc = utils.stripTrailingHardline(
|
||||
textToDoc(content, {
|
||||
doc = await textToDoc(content, {
|
||||
...options,
|
||||
parser: "html",
|
||||
})
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
let ignoreDoc = false;
|
||||
|
@ -113,7 +140,7 @@ export const embed: Printer<Node>["embed"] = (
|
|||
}
|
||||
|
||||
const idxs = findPlaceholders(currentDoc).filter(
|
||||
([start, end]) => currentDoc.slice(start, end + 1) in node.nodes
|
||||
([start, end]) => currentDoc.slice(start, end + 1) in node.nodes,
|
||||
);
|
||||
if (!idxs.length) {
|
||||
ignoreDoc = false;
|
||||
|
@ -145,25 +172,15 @@ export const embed: Printer<Node>["embed"] = (
|
|||
ignoreDoc = false;
|
||||
return res;
|
||||
});
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
if (node.type === "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),
|
||||
]);
|
||||
const block = buildBlock(path, print, node as Block, mapped);
|
||||
|
||||
return node.preNewLines > 1
|
||||
? builders.group([builders.trim, builders.hardline, block])
|
||||
: block;
|
||||
}
|
||||
return [...mapped, builders.hardline];
|
||||
};
|
||||
|
@ -172,7 +189,7 @@ const getMultilineGroup = (content: String): builders.Group => {
|
|||
return builders.group(
|
||||
content.split("\n").map((line, i) => {
|
||||
return [builders.hardline, line.trim()];
|
||||
})
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
|
@ -181,7 +198,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];
|
||||
|
@ -218,6 +235,35 @@ 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,5 +1,3 @@
|
|||
<div>
|
||||
{{ user.name }}
|
||||
</div>
|
||||
<div>{{ user.name }}</div>
|
||||
|
||||
<div>{{ user.age }}</div>
|
||||
|
|
9
test/cases/expression_whitespace/expected.html
Normal file
9
test/cases/expression_whitespace/expected.html
Normal file
|
@ -0,0 +1,9 @@
|
|||
<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>
|
9
test/cases/expression_whitespace/input.html
Normal file
9
test/cases/expression_whitespace/input.html
Normal file
|
@ -0,0 +1,9 @@
|
|||
<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>
|
46
test/cases/newline_between/expected.html
Normal file
46
test/cases/newline_between/expected.html
Normal file
|
@ -0,0 +1,46 @@
|
|||
{% 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 %}
|
53
test/cases/newline_between/input.html
Normal file
53
test/cases/newline_between/input.html
Normal file
|
@ -0,0 +1,53 @@
|
|||
{% 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('Closung statement "endif" doesn't match Opening Statement "for item in seq".')
|
||||
Error('Closing statement "endif" doesn't match Opening Statement "for item in seq".')
|
||||
|
|
2
test/cases/statement_empty_block/expected.html
Normal file
2
test/cases/statement_empty_block/expected.html
Normal file
|
@ -0,0 +1,2 @@
|
|||
{% block title %}
|
||||
{% endblock %}
|
5
test/cases/statement_empty_block/input.html
Normal file
5
test/cases/statement_empty_block/input.html
Normal file
|
@ -0,0 +1,5 @@
|
|||
{% block title %}
|
||||
|
||||
|
||||
|
||||
{% endblock %}
|
|
@ -1,11 +1,11 @@
|
|||
import { existsSync, readdirSync, readFileSync } from "fs";
|
||||
import { join } from "path";
|
||||
import { format, Options, ParserOptions } from "prettier";
|
||||
import { format, Options } from "prettier";
|
||||
import * as jinjaPlugin from "../src/index";
|
||||
|
||||
const prettify = (code: string, options: Options) =>
|
||||
format(code, {
|
||||
parser: "jinja-template",
|
||||
parser: "askama-template",
|
||||
plugins: [jinjaPlugin],
|
||||
...options,
|
||||
});
|
||||
|
@ -17,7 +17,7 @@ tests.forEach((test) => {
|
|||
if (test.startsWith("_")) {
|
||||
return;
|
||||
}
|
||||
return it(test, () => {
|
||||
return it(test, async () => {
|
||||
const path = join(testFolder, test);
|
||||
const input = readFileSync(join(path, "input.html")).toString();
|
||||
const expected = readFileSync(join(path, "expected.html")).toString();
|
||||
|
@ -34,21 +34,11 @@ tests.forEach((test) => {
|
|||
|
||||
if (expectedError) {
|
||||
jest.spyOn(console, "error").mockImplementation(() => {});
|
||||
expect(format).toThrow(expectedError);
|
||||
await expect(format()).rejects.toThrow(expectedError);
|
||||
} else {
|
||||
const result = format();
|
||||
const result = await format();
|
||||
expect(result).toEqual(expected);
|
||||
expect(prettify(result, configObject)).toEqual(expected);
|
||||
expect(await 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": "ES6",
|
||||
"module": "commonjs",
|
||||
"target": "esnext",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "node",
|
||||
"lib": ["esnext"],
|
||||
"declaration": true,
|
||||
|
|
Loading…
Reference in a new issue