Skip to content

Add pug support for vue templates #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -7,3 +7,6 @@
/npm-debug.log
/test.js
/test/fixtures/espree-v8/node_modules
.yarn
.yarnrc.yml
yarn.lock
Comment on lines +10 to +12
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use yarn locally.
To prevent accidental commit of 80+ files, I ignored these for now.
Can revert it when out of draft

10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -10,7 +10,13 @@
"index.*"
],
"peerDependencies": {
"eslint": ">=5.0.0"
"eslint": ">=5.0.0",
"pug-lexer": ">=5.0.0"
},
"peerDependenciesMeta": {
"pug-lexer": {
"optional": true
}
},
"dependencies": {
"debug": "^4.1.1",
@@ -52,8 +58,10 @@
"nyc": "^14.0.0",
"opener": "^1.5.1",
"prettier": "^2.3.1",
"pug-lexer": "^5.0.1",
"rimraf": "^2.6.3",
"rollup": "^1.1.2",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^4.0.0",
"rollup-plugin-sourcemaps": "^0.4.2",
"ts-node": "^8.1.0",
3 changes: 2 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
*/
import resolve from "rollup-plugin-node-resolve"
import sourcemaps from "rollup-plugin-sourcemaps"
import commonjs from "rollup-plugin-commonjs"

const pkg = require("./package.json")
const deps = new Set(
@@ -23,6 +24,6 @@ export default {
* See LICENSE file in root directory for full license.
*/`,
},
plugins: [sourcemaps(), resolve()],
plugins: [sourcemaps(), resolve(), commonjs()],
external: id => deps.has(id) || id.startsWith("lodash"),
}
2 changes: 2 additions & 0 deletions src/ast/nodes.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
* @copyright 2017 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
import type { Token as PugToken } from "pug-lexer"
import type { ScopeManager } from "eslint-scope"
import type { ParseError } from "./errors"
import type { HasLocation } from "./locations"
@@ -860,6 +861,7 @@ export interface VDocumentFragment
type: "VDocumentFragment"
parent: null
children: (VElement | VText | VExpressionContainer | VStyleElement)[]
pugTokens?: PugToken[]
}

/**
14 changes: 14 additions & 0 deletions src/html/parser.ts
Original file line number Diff line number Diff line change
@@ -51,6 +51,7 @@ import {
getScriptParser,
getParserLangFromSFC,
} from "../common/parser-options"
import lex from "pug-lexer"

const DIRECTIVE_NAME = /^(?:v-|[.:@#]).*[^.:@#]$/u
const DT_DD = /^d[dt]$/u
@@ -300,6 +301,19 @@ export class Parser {
}
this.postProcessesForScript = []

try {
// Process pug
const match =
/<template\s+lang="pug">(?<content>.*)<\/template>/isu.exec(
this.text,
)
if (match && match.groups && match.groups.content) {
doc.pugTokens = lex(match.groups.content)
}
} catch {
/* ignore */
}

return doc
}

3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -103,7 +103,8 @@ export function parseForESLint(
errors: rootAST.errors,
}
const templateBody =
template != null && templateLang === "html"
template != null &&
(templateLang === "html" || templateLang === "pug")
? Object.assign(template, concreteInfo)
: undefined

527 changes: 526 additions & 1 deletion test/fixtures/ast/pug/ast.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/fixtures/ast/pug/source.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<template lang="pug">
Hello!
p {{ greeting }} World!
</template>
20 changes: 19 additions & 1 deletion test/fixtures/ast/pug/token-ranges.json
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
[]
[
"<template",
"lang",
"=",
"\"pug\"",
">",
"\n",
"p",
" ",
"{{",
"greeting",
"}}",
" ",
"World!",
"\n",
"</template",
">",
"\n"
]
57 changes: 56 additions & 1 deletion test/fixtures/ast/pug/tree.json
Original file line number Diff line number Diff line change
@@ -1 +1,56 @@
[]
[
{
"type": "VElement",
"text": "<template lang=\"pug\">\np {{ greeting }} World!\n</template>",
"children": [
{
"type": "VStartTag",
"text": "<template lang=\"pug\">",
"children": [
{
"type": "VAttribute",
"text": "lang=\"pug\"",
"children": [
{
"type": "VIdentifier",
"text": "lang",
"children": []
},
{
"type": "VLiteral",
"text": "\"pug\"",
"children": []
}
]
}
]
},
{
"type": "VText",
"text": "\np ",
"children": []
},
{
"type": "VExpressionContainer",
"text": "{{ greeting }}",
"children": [
{
"type": "Identifier",
"text": "greeting",
"children": []
}
]
},
{
"type": "VText",
"text": " World!\n",
"children": []
},
{
"type": "VEndTag",
"text": "</template>",
"children": []
}
]
}
]