Writing custom parsers
If you want to use your own parser and provide additional capabilities for your rules, you can specify your own custom parser. The parser should expose, as default, a single function that take the source code as the first argument. The function should simply return an AST.
⚠️The first node of the tree should have the property type
with the value root
.
You can find an LintHTML parser project here.
{
"parser": "./path/to/my-parser.js"
}
- Parser CJS format
- Parser CJS format
// my-parser.js
const myParser = require("customParser");
module.exports = function(code) {
return myParser(code)
};
// my-parser.js
import myParser from "customParser";
export default function(code) {
return myParser(code)
};
The AST specification
Please refer to the API documentation to know how to construct nodes matching the AST specification.
interface Node {
type: 'tag' | 'text' | 'directive' | 'comment' | 'root' | 'script' | 'style' | 'cdata' | 'doctype';
name: string;
data?: string // only 'text', 'directive' and 'comment' nodes
attributes?: NodeAttribute[]; // only 'tag' nodes
parent?: Node;
previousSibling: Node;
nextSibling?: Node;
startIndex: number;
endIndex: number;
openIndex: number;
closeIndex: number;
children: Node[];
loc: Position;
open: CharValue;
close: CharValue;
}
interface CharValue {
chars: string; // tag name, attribute name or attribute value without `<, >, ', ", \s, \t` chars
raw?: string; // Same as `chars` but with `<, >, ', ",` chars
loc: Position;
}
interface NodeAttribute {
type: string;
name: CharValue;
value: CharValue;
equal: CharValue;
loc: Position;
}
interface Position {
start: Range;
end: Range;
}
interface Range {
line: number;
column: number;
}