From 1c93d5a6baede945d82d77170177e43d916bfb00 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sun, 13 Nov 2022 21:35:36 +0900 Subject: [PATCH] support for pug (wip) --- package.json | 2 + src/ast/html.ts | 31 +- src/ast/index.ts | 4 +- src/ast/pug.ts | 150 ++ src/context/index.ts | 6 + src/context/script-let.ts | 6 +- src/parser/converts/attr.ts | 2 + src/parser/converts/element.ts | 3 + src/parser/converts/root.ts | 3 + src/parser/index.ts | 13 +- src/parser/pug/context.ts | 162 ++ src/parser/pug/converts/attrs.ts | 188 ++ src/parser/pug/converts/block.ts | 40 + src/parser/pug/converts/common.ts | 31 + src/parser/pug/converts/mixin.ts | 171 ++ .../pug/converts/svelte-fragment-parser.ts | 140 ++ src/parser/pug/converts/tag.ts | 105 + src/parser/pug/converts/text.ts | 51 + src/parser/pug/index.ts | 37 + src/parser/pug/pug-ast.ts | 66 + src/parser/template.ts | 67 +- .../parser/ast/pug/each/each01-output.json | 598 ++++- .../ast/pug/each/each01-scope-output.json | 561 ++++- .../parser/ast/pug/each/each02-input.svelte | 8 + .../parser/ast/pug/each/each02-output.json | 2055 +++++++++++++++++ ...t.json => each02-prefer-const-result.json} | 2 +- .../ast/pug/each/each02-scope-output.json | 1047 +++++++++ tests/src/parser/test-utils.ts | 3 +- 28 files changed, 5468 insertions(+), 84 deletions(-) create mode 100644 src/ast/pug.ts create mode 100644 src/parser/pug/context.ts create mode 100644 src/parser/pug/converts/attrs.ts create mode 100644 src/parser/pug/converts/block.ts create mode 100644 src/parser/pug/converts/common.ts create mode 100644 src/parser/pug/converts/mixin.ts create mode 100644 src/parser/pug/converts/svelte-fragment-parser.ts create mode 100644 src/parser/pug/converts/tag.ts create mode 100644 src/parser/pug/converts/text.ts create mode 100644 src/parser/pug/index.ts create mode 100644 src/parser/pug/pug-ast.ts create mode 100644 tests/fixtures/parser/ast/pug/each/each02-input.svelte create mode 100644 tests/fixtures/parser/ast/pug/each/each02-output.json rename tests/fixtures/parser/ast/pug/each/{each01-no-unused-vars-result.json => each02-prefer-const-result.json} (65%) create mode 100644 tests/fixtures/parser/ast/pug/each/each02-scope-output.json diff --git a/package.json b/package.json index b551f9a0..9a506c86 100644 --- a/package.json +++ b/package.json @@ -91,6 +91,8 @@ "prettier": "^2.0.5", "prettier-plugin-pkg": "^0.17.0", "prettier-plugin-svelte": "^2.5.0", + "pug-lexer": "^5.0.1", + "pug-parser": "^6.0.0", "semver": "^7.3.5", "string-replace-loader": "^3.0.3", "svelte": "^3.46.1", diff --git a/src/ast/html.ts b/src/ast/html.ts index 106a1e83..52518f6d 100644 --- a/src/ast/html.ts +++ b/src/ast/html.ts @@ -1,6 +1,12 @@ import type ESTree from "estree"; import type { BaseNode } from "./base"; import type { Token, Comment } from "./common"; +import type { + SveltePugAttributePlain, + SveltePugElement, + SveltePugTemplateElement, + SveltePugText, +} from "./pug"; export type SvelteHTMLNode = | SvelteProgram @@ -37,7 +43,12 @@ export type SvelteHTMLNode = /** Node of Svelte program root */ export interface SvelteProgram extends BaseNode { type: "Program"; - body: (SvelteScriptElement | SvelteStyleElement | Child)[]; + body: ( + | SvelteScriptElement + | SvelteStyleElement + | SveltePugTemplateElement + | Child + )[]; sourceType: "script" | "module"; comments: Comment[]; tokens: Token[]; @@ -87,7 +98,9 @@ export interface SvelteHTMLElement extends BaseSvelteElement { | SvelteAwaitPendingBlock | SvelteAwaitThenBlock | SvelteAwaitCatchBlock - | SvelteKeyBlock; + | SvelteKeyBlock + // Pug + | SveltePugText; } /** Node of Svelte component element. */ export interface SvelteComponentElement extends BaseSvelteElement { @@ -151,13 +164,17 @@ export interface SvelteEndTag extends BaseNode { export interface SvelteName extends BaseNode { type: "SvelteName"; name: string; + rawName: string; parent: | SvelteElement | SvelteScriptElement | SvelteStyleElement | SvelteAttribute | SvelteMemberExpressionName - | SvelteDirectiveKey; + | SvelteDirectiveKey + // Pug + | SveltePugAttributePlain + | SveltePugElement; } /** Nodes that may be used in component names. The component names separated by dots. */ @@ -194,7 +211,9 @@ export interface SvelteText extends BaseNode { | SvelteAwaitPendingBlock | SvelteAwaitThenBlock | SvelteAwaitCatchBlock - | SvelteKeyBlock; + | SvelteKeyBlock + // Pug + | SveltePugText; } /** Node of literal. */ export interface SvelteLiteral extends BaseNode { @@ -220,7 +239,9 @@ interface BaseSvelteMustacheTag extends BaseNode { | SvelteAwaitCatchBlock | SvelteKeyBlock | SvelteAttribute - | SvelteStyleDirective; + | SvelteStyleDirective + // Pug + | SveltePugText; } /** Node of mustache tag. e.g. `{...}``. Like JSXExpressionContainer */ export interface SvelteMustacheTagText extends BaseSvelteMustacheTag { diff --git a/src/ast/index.ts b/src/ast/index.ts index a271aaa3..e8705058 100644 --- a/src/ast/index.ts +++ b/src/ast/index.ts @@ -1,8 +1,10 @@ import type { SvelteHTMLNode } from "./html"; import type { SvelteScriptNode } from "./script"; +import type { SveltePugNode } from "./pug"; export * from "./common"; export * from "./html"; export * from "./script"; +export * from "./pug"; -export type SvelteNode = SvelteHTMLNode | SvelteScriptNode; +export type SvelteNode = SvelteHTMLNode | SvelteScriptNode | SveltePugNode; diff --git a/src/ast/pug.ts b/src/ast/pug.ts new file mode 100644 index 00000000..fe5015f5 --- /dev/null +++ b/src/ast/pug.ts @@ -0,0 +1,150 @@ +import type ESTree from "estree"; +import type { BaseNode } from "./base"; +import type { + SvelteElement, + SvelteEndTag as HTMLEndTag, + SvelteLiteral, + SvelteMemberExpressionName, + SvelteMustacheTag, + SvelteMustacheTagText, + SvelteName, + SvelteProgram, + SvelteStartTag as HTMLStartTag, + SvelteText, +} from "./html"; + +export type SveltePugNode = + | SveltePugTemplateElement + | SveltePugEachBlock + | SveltePugElement + | SveltePugAttributes + | SveltePugAttribute + | SveltePugAttributeSet + | SveltePugAttributeBlock + | SveltePugSpreadAttribute; + +type Child = SveltePugEachBlock | SveltePugElement | SveltePugText; + +/** Node of `