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
Show file tree
Hide file tree
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
Expand Up @@ -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
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Up @@ -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"
Expand Down Expand Up @@ -860,6 +861,7 @@ export interface VDocumentFragment
type: "VDocumentFragment"
parent: null
children: (VElement | VText | VExpressionContainer | VStyleElement)[]
pugTokens?: PugToken[]
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/html/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading