@markuplint/parser-utils 4.8.9 → 4.8.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.ja.md +208 -0
- package/ARCHITECTURE.md +251 -0
- package/CHANGELOG.md +5 -1
- package/README.md +6 -0
- package/SKILL.md +126 -0
- package/docs/maintenance.ja.md +176 -0
- package/docs/maintenance.md +176 -0
- package/docs/parser-class.ja.md +655 -0
- package/docs/parser-class.md +655 -0
- package/lib/debugger.d.ts +25 -0
- package/lib/debugger.js +25 -0
- package/lib/enums.d.ts +10 -0
- package/lib/enums.js +10 -0
- package/lib/get-namespace.d.ts +2 -0
- package/lib/get-namespace.js +29 -0
- package/lib/idl-attributes.d.ts +9 -0
- package/lib/idl-attributes.js +9 -0
- package/lib/parser-error.d.ts +16 -0
- package/lib/parser-error.js +12 -0
- package/lib/parser.d.ts +282 -0
- package/lib/parser.js +265 -3
- package/lib/script-parser.d.ts +21 -0
- package/lib/script-parser.js +17 -0
- package/lib/types.d.ts +57 -0
- package/package.json +10 -10
package/lib/debugger.js
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a list of AST nodes into human-readable debug strings showing
|
|
3
|
+
* each node's position, type, and raw content. Useful for snapshot testing.
|
|
4
|
+
*
|
|
5
|
+
* @param nodeList - The flat list of AST nodes to convert
|
|
6
|
+
* @param withAttr - Whether to include detailed attribute debug info for start tags
|
|
7
|
+
* @returns An array of formatted debug strings, one per node (plus attribute lines when enabled)
|
|
8
|
+
*/
|
|
1
9
|
export function nodeListToDebugMaps(nodeList, withAttr = false) {
|
|
2
10
|
return nodeList.flatMap(n => {
|
|
3
11
|
const r = [];
|
|
@@ -8,6 +16,14 @@ export function nodeListToDebugMaps(nodeList, withAttr = false) {
|
|
|
8
16
|
return r;
|
|
9
17
|
});
|
|
10
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Converts a list of AST attributes into detailed debug strings showing
|
|
21
|
+
* each attribute's components (name, equal sign, value, quotes) with
|
|
22
|
+
* their positions and additional metadata like directives and dynamic values.
|
|
23
|
+
*
|
|
24
|
+
* @param attributes - The list of attributes to convert into debug representations
|
|
25
|
+
* @returns An array of string arrays, one inner array per attribute containing its debug lines
|
|
26
|
+
*/
|
|
11
27
|
export function attributesToDebugMaps(attributes) {
|
|
12
28
|
return attributes.map(n => {
|
|
13
29
|
const r = [
|
|
@@ -36,6 +52,15 @@ export function attributesToDebugMaps(attributes) {
|
|
|
36
52
|
return r;
|
|
37
53
|
});
|
|
38
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Produces a tree-style debug view of AST nodes, showing indentation
|
|
57
|
+
* based on depth, parent-child relationships, pair node links,
|
|
58
|
+
* and ghost/bogus markers. Useful for visualizing the parsed DOM structure.
|
|
59
|
+
*
|
|
60
|
+
* @param nodeTree - The flat list of AST nodes to visualize as a tree
|
|
61
|
+
* @param idFilter - Whether to replace UUIDs with short sequential hex IDs for readability
|
|
62
|
+
* @returns An array of formatted strings representing the tree view
|
|
63
|
+
*/
|
|
39
64
|
export function nodeTreeDebugView(nodeTree, idFilter = false) {
|
|
40
65
|
const filter = idFilter ? uuidFilter : (id) => id;
|
|
41
66
|
return nodeTree
|
package/lib/enums.d.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* States of the tag-level state machine used during tokenization.
|
|
3
|
+
* Transitions drive the parser through detecting the opening bracket,
|
|
4
|
+
* tag name, attributes, and closing bracket of an HTML/XML tag.
|
|
5
|
+
*/
|
|
1
6
|
export declare enum TagState {
|
|
2
7
|
BeforeOpenTag = 0,
|
|
3
8
|
FirstCharOfTagName = 1,
|
|
@@ -6,6 +11,11 @@ export declare enum TagState {
|
|
|
6
11
|
AfterAttrs = 4,
|
|
7
12
|
AfterOpenTag = 5
|
|
8
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* States of the attribute-level state machine used during attribute tokenization.
|
|
16
|
+
* Transitions drive the parser through the attribute name, equals sign,
|
|
17
|
+
* and value portions of an HTML/XML attribute.
|
|
18
|
+
*/
|
|
9
19
|
export declare enum AttrState {
|
|
10
20
|
BeforeName = 0,
|
|
11
21
|
Name = 1,
|
package/lib/enums.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* States of the tag-level state machine used during tokenization.
|
|
3
|
+
* Transitions drive the parser through detecting the opening bracket,
|
|
4
|
+
* tag name, attributes, and closing bracket of an HTML/XML tag.
|
|
5
|
+
*/
|
|
1
6
|
export var TagState;
|
|
2
7
|
(function (TagState) {
|
|
3
8
|
TagState[TagState["BeforeOpenTag"] = 0] = "BeforeOpenTag";
|
|
@@ -7,6 +12,11 @@ export var TagState;
|
|
|
7
12
|
TagState[TagState["AfterAttrs"] = 4] = "AfterAttrs";
|
|
8
13
|
TagState[TagState["AfterOpenTag"] = 5] = "AfterOpenTag";
|
|
9
14
|
})(TagState || (TagState = {}));
|
|
15
|
+
/**
|
|
16
|
+
* States of the attribute-level state machine used during attribute tokenization.
|
|
17
|
+
* Transitions drive the parser through the attribute name, equals sign,
|
|
18
|
+
* and value portions of an HTML/XML attribute.
|
|
19
|
+
*/
|
|
10
20
|
export var AttrState;
|
|
11
21
|
(function (AttrState) {
|
|
12
22
|
AttrState[AttrState["BeforeName"] = 0] = "BeforeName";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export function getNamespace(currentNodeName, parentNode) {
|
|
2
|
+
const parentNS = getParentNamespace(parentNode);
|
|
3
|
+
if (parentNS === 'http://www.w3.org/1999/xhtml' && currentNodeName?.toLowerCase() === 'svg') {
|
|
4
|
+
return 'http://www.w3.org/2000/svg';
|
|
5
|
+
}
|
|
6
|
+
else if (parentNS === 'http://www.w3.org/2000/svg' && parentNode?.nodeName === 'foreignObject') {
|
|
7
|
+
return 'http://www.w3.org/1999/xhtml';
|
|
8
|
+
}
|
|
9
|
+
else if (parentNS === 'http://www.w3.org/1999/xhtml' && currentNodeName?.toLowerCase() === 'math') {
|
|
10
|
+
return 'http://www.w3.org/1998/Math/MathML';
|
|
11
|
+
}
|
|
12
|
+
return parentNS;
|
|
13
|
+
}
|
|
14
|
+
function getParentNamespace(parentNode) {
|
|
15
|
+
if (!parentNode) {
|
|
16
|
+
return 'http://www.w3.org/1999/xhtml';
|
|
17
|
+
}
|
|
18
|
+
if ('namespace' in parentNode && parentNode.namespace) {
|
|
19
|
+
const ns = parentNode.namespace.toLowerCase().trim();
|
|
20
|
+
return ns === 'http://www.w3.org/1999/xhtml'
|
|
21
|
+
? 'http://www.w3.org/1999/xhtml'
|
|
22
|
+
: ns === 'http://www.w3.org/2000/svg'
|
|
23
|
+
? 'http://www.w3.org/2000/svg'
|
|
24
|
+
: ns === 'http://www.w3.org/1998/Math/MathML'
|
|
25
|
+
? 'http://www.w3.org/1998/Math/MathML'
|
|
26
|
+
: 'http://www.w3.org/1999/xhtml';
|
|
27
|
+
}
|
|
28
|
+
return getParentNamespace(parentNode.parentNode);
|
|
29
|
+
}
|
package/lib/idl-attributes.d.ts
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Searches the IDL-to-content-attribute mapping to find the corresponding
|
|
3
|
+
* IDL property name and content attribute name for a given attribute name.
|
|
4
|
+
* Handles camelCase IDL names, lowercase content attribute names, hyphenated names,
|
|
5
|
+
* and event handler attributes (e.g., `onclick`).
|
|
6
|
+
*
|
|
7
|
+
* @param name - The attribute name to look up (can be IDL, content, or hyphenated form)
|
|
8
|
+
* @returns An object with the matching `idlPropName` and `contentAttrName`, both undefined if no match is found
|
|
9
|
+
*/
|
|
1
10
|
export declare function searchIDLAttribute(name: string): {
|
|
2
11
|
idlPropName: string | undefined;
|
|
3
12
|
contentAttrName: string | undefined;
|
package/lib/idl-attributes.js
CHANGED
|
@@ -416,6 +416,15 @@ const idlContentMap = {
|
|
|
416
416
|
credentialless: 'credentialless',
|
|
417
417
|
};
|
|
418
418
|
const list = Object.entries(idlContentMap);
|
|
419
|
+
/**
|
|
420
|
+
* Searches the IDL-to-content-attribute mapping to find the corresponding
|
|
421
|
+
* IDL property name and content attribute name for a given attribute name.
|
|
422
|
+
* Handles camelCase IDL names, lowercase content attribute names, hyphenated names,
|
|
423
|
+
* and event handler attributes (e.g., `onclick`).
|
|
424
|
+
*
|
|
425
|
+
* @param name - The attribute name to look up (can be IDL, content, or hyphenated form)
|
|
426
|
+
* @returns An object with the matching `idlPropName` and `contentAttrName`, both undefined if no match is found
|
|
427
|
+
*/
|
|
419
428
|
export function searchIDLAttribute(name) {
|
|
420
429
|
const camelizedName = camelize(name);
|
|
421
430
|
const [idlPropName, contentAttrName] = /^on[a-z]/.test(name)
|
package/lib/parser-error.d.ts
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Positional and contextual information associated with a parser error,
|
|
3
|
+
* used to construct meaningful error messages with source locations.
|
|
4
|
+
*/
|
|
1
5
|
export type ParserErrorInfo = {
|
|
2
6
|
readonly line?: number;
|
|
3
7
|
readonly col?: number;
|
|
4
8
|
readonly raw?: string;
|
|
5
9
|
readonly stack?: string;
|
|
6
10
|
};
|
|
11
|
+
/**
|
|
12
|
+
* An error that occurs during parsing, carrying the source line, column,
|
|
13
|
+
* and raw text where the error was encountered.
|
|
14
|
+
*/
|
|
7
15
|
export declare class ParserError extends Error {
|
|
8
16
|
readonly col: number;
|
|
9
17
|
readonly line: number;
|
|
@@ -11,6 +19,10 @@ export declare class ParserError extends Error {
|
|
|
11
19
|
readonly raw: string;
|
|
12
20
|
constructor(message: string, info: ParserErrorInfo);
|
|
13
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* A parser error specific to a particular HTML element, including
|
|
24
|
+
* the node name of the element that caused the error in the message.
|
|
25
|
+
*/
|
|
14
26
|
export declare class TargetParserError extends ParserError {
|
|
15
27
|
name: string;
|
|
16
28
|
readonly nodeName: string | null;
|
|
@@ -18,6 +30,10 @@ export declare class TargetParserError extends ParserError {
|
|
|
18
30
|
readonly nodeName?: string | null;
|
|
19
31
|
});
|
|
20
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* A parser error that occurs while reading a configuration file,
|
|
35
|
+
* including the file path in the error message for easier debugging.
|
|
36
|
+
*/
|
|
21
37
|
export declare class ConfigParserError extends ParserError {
|
|
22
38
|
readonly filePath: string;
|
|
23
39
|
name: string;
|
package/lib/parser-error.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An error that occurs during parsing, carrying the source line, column,
|
|
3
|
+
* and raw text where the error was encountered.
|
|
4
|
+
*/
|
|
1
5
|
export class ParserError extends Error {
|
|
2
6
|
constructor(message, info) {
|
|
3
7
|
super(message);
|
|
@@ -8,6 +12,10 @@ export class ParserError extends Error {
|
|
|
8
12
|
this.stack = info.stack ?? this.stack;
|
|
9
13
|
}
|
|
10
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* A parser error specific to a particular HTML element, including
|
|
17
|
+
* the node name of the element that caused the error in the message.
|
|
18
|
+
*/
|
|
11
19
|
export class TargetParserError extends ParserError {
|
|
12
20
|
constructor(message, info) {
|
|
13
21
|
const errMsg = info.nodeName
|
|
@@ -18,6 +26,10 @@ export class TargetParserError extends ParserError {
|
|
|
18
26
|
this.nodeName = info.nodeName ?? null;
|
|
19
27
|
}
|
|
20
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* A parser error that occurs while reading a configuration file,
|
|
31
|
+
* including the file path in the error message for easier debugging.
|
|
32
|
+
*/
|
|
21
33
|
export class ConfigParserError extends ParserError {
|
|
22
34
|
constructor(message, info) {
|
|
23
35
|
const pos = info.line != null && info.line != null ? `(${info.line}:${info.col})` : '';
|
package/lib/parser.d.ts
CHANGED
|
@@ -2,10 +2,29 @@ import type { Token, ChildToken, QuoteSet, ParseOptions, ParserOptions, Tokenize
|
|
|
2
2
|
import type { EndTagType, MLASTDocument, MLASTParentNode, MLParser, ParserAuthoredElementNameDistinguishing, MLASTElement, MLASTElementCloseTag, MLASTToken, MLASTNodeTreeItem, MLASTTag, MLASTText, MLASTAttr, MLASTChildNode, MLASTSpreadAttr, ElementType, Walker, MLASTHTMLAttr, MLASTPreprocessorSpecificBlockConditionalType } from '@markuplint/ml-ast';
|
|
3
3
|
import { AttrState } from './enums.js';
|
|
4
4
|
import { ParserError } from './parser-error.js';
|
|
5
|
+
/**
|
|
6
|
+
* Abstract base class for all markuplint parsers. Provides the core parsing pipeline
|
|
7
|
+
* including tokenization, tree traversal, node flattening, and error handling.
|
|
8
|
+
* Subclasses must implement `nodeize` to convert language-specific AST nodes
|
|
9
|
+
* into the markuplint AST format.
|
|
10
|
+
*
|
|
11
|
+
* @template Node - The language-specific AST node type produced by the tokenizer
|
|
12
|
+
* @template State - An optional parser state type that persists across tokenization
|
|
13
|
+
*/
|
|
5
14
|
export declare abstract class Parser<Node extends {} = {}, State extends unknown = null> implements MLParser {
|
|
6
15
|
#private;
|
|
7
16
|
state: State;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new Parser instance with the given options and initial state.
|
|
19
|
+
*
|
|
20
|
+
* @param options - Configuration options controlling tag handling, whitespace, and quoting behavior
|
|
21
|
+
* @param defaultState - The initial parser state, cloned and restored after each parse call
|
|
22
|
+
*/
|
|
8
23
|
constructor(options?: ParserOptions, defaultState?: State);
|
|
24
|
+
/**
|
|
25
|
+
* The pattern used to distinguish authored (component) element names
|
|
26
|
+
* from native HTML elements, as specified by the parse options.
|
|
27
|
+
*/
|
|
9
28
|
get authoredElementName(): ParserAuthoredElementNameDistinguishing | undefined;
|
|
10
29
|
/**
|
|
11
30
|
* Detect value as a true if its attribute is booleanish value and omitted.
|
|
@@ -26,41 +45,175 @@ export declare abstract class Parser<Node extends {} = {}, State extends unknown
|
|
|
26
45
|
* - `"never"`: Never need
|
|
27
46
|
*/
|
|
28
47
|
get endTag(): EndTagType;
|
|
48
|
+
/**
|
|
49
|
+
* The current raw source code being parsed, which may have been
|
|
50
|
+
* preprocessed (e.g., ignore blocks masked, front matter removed).
|
|
51
|
+
*/
|
|
29
52
|
get rawCode(): string;
|
|
53
|
+
/**
|
|
54
|
+
* Whether tag names should be compared in a case-sensitive manner.
|
|
55
|
+
* When false (the default), tag name comparisons are case-insensitive (HTML behavior).
|
|
56
|
+
*/
|
|
30
57
|
get tagNameCaseSensitive(): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Tokenizes the raw source code into language-specific AST nodes.
|
|
60
|
+
* Subclasses should override this method to provide actual tokenization logic.
|
|
61
|
+
*
|
|
62
|
+
* @param options - Parse options controlling offset, depth, and other parse-time settings
|
|
63
|
+
* @returns The tokenized result containing the AST node array and fragment flag
|
|
64
|
+
*/
|
|
31
65
|
tokenize(options?: ParseOptions): Tokenized<Node, State>;
|
|
66
|
+
/**
|
|
67
|
+
* Hook called before parsing begins, allowing subclasses to preprocess
|
|
68
|
+
* the raw source code. The default implementation prepends offset spaces
|
|
69
|
+
* based on the parse options.
|
|
70
|
+
*
|
|
71
|
+
* @param rawCode - The raw source code about to be parsed
|
|
72
|
+
* @param options - Parse options that may specify offset positioning
|
|
73
|
+
* @returns The preprocessed source code to be used for tokenization
|
|
74
|
+
*/
|
|
32
75
|
beforeParse(rawCode: string, options?: ParseOptions): string;
|
|
76
|
+
/**
|
|
77
|
+
* Parses raw source code through the full pipeline: preprocessing, tokenization,
|
|
78
|
+
* traversal, flattening, ignore-block restoration, and post-processing.
|
|
79
|
+
* Returns the complete markuplint AST document.
|
|
80
|
+
*
|
|
81
|
+
* @param rawCode - The raw source code to parse
|
|
82
|
+
* @param options - Parse options controlling offsets, depth, front matter, and authored element names
|
|
83
|
+
* @returns The parsed AST document containing the node list and fragment flag
|
|
84
|
+
*/
|
|
33
85
|
parse(rawCode: string, options?: ParseOptions): MLASTDocument;
|
|
86
|
+
/**
|
|
87
|
+
* Hook called after the main parse pipeline completes, allowing subclasses
|
|
88
|
+
* to perform final transformations on the node list. The default implementation
|
|
89
|
+
* removes any offset spaces that were prepended during preprocessing.
|
|
90
|
+
*
|
|
91
|
+
* @param nodeList - The fully parsed and flattened node list
|
|
92
|
+
* @param options - The parse options used for this parse invocation
|
|
93
|
+
* @returns The post-processed node list
|
|
94
|
+
*/
|
|
34
95
|
afterParse(nodeList: readonly MLASTNodeTreeItem[], options?: ParseOptions): readonly MLASTNodeTreeItem[];
|
|
96
|
+
/**
|
|
97
|
+
* Wraps an arbitrary error into a ParserError with source location information.
|
|
98
|
+
* Extracts line and column numbers from common error formats.
|
|
99
|
+
*
|
|
100
|
+
* @param error - The original error to wrap
|
|
101
|
+
* @returns A ParserError containing the original error's message and location data
|
|
102
|
+
*/
|
|
35
103
|
parseError(error: any): ParserError;
|
|
104
|
+
/**
|
|
105
|
+
* Recursively traverses language-specific AST nodes by calling `nodeize` on each,
|
|
106
|
+
* filtering duplicates, and separating child nodes from ancestor-level siblings.
|
|
107
|
+
*
|
|
108
|
+
* @param originNodes - The language-specific AST nodes to traverse
|
|
109
|
+
* @param parentNode - The parent markuplint AST node, or null for top-level nodes
|
|
110
|
+
* @param depth - The current nesting depth in the tree
|
|
111
|
+
* @returns An object containing `childNodes` at the current depth and `siblings` that belong to ancestor levels
|
|
112
|
+
*/
|
|
36
113
|
traverse(originNodes: readonly Node[], parentNode: (MLASTParentNode | null) | undefined, depth: number): {
|
|
37
114
|
childNodes: readonly MLASTChildNode[];
|
|
38
115
|
siblings: readonly MLASTNodeTreeItem[];
|
|
39
116
|
};
|
|
117
|
+
/**
|
|
118
|
+
* Hook called after traversal completes, used to sort the resulting node tree
|
|
119
|
+
* by source position. Subclasses may override for custom post-traversal logic.
|
|
120
|
+
*
|
|
121
|
+
* @param nodeTree - The unsorted node tree produced by traversal
|
|
122
|
+
* @returns The node tree sorted by source position
|
|
123
|
+
*/
|
|
40
124
|
afterTraverse(nodeTree: readonly MLASTNodeTreeItem[]): readonly MLASTNodeTreeItem[];
|
|
125
|
+
/**
|
|
126
|
+
* Converts a single language-specific AST node into one or more markuplint AST nodes.
|
|
127
|
+
* Subclasses must override this method to provide actual node conversion logic
|
|
128
|
+
* using visitor methods like `visitElement`, `visitText`, `visitComment`, etc.
|
|
129
|
+
*
|
|
130
|
+
* @param originNode - The language-specific AST node to convert
|
|
131
|
+
* @param parentNode - The parent markuplint AST node, or null for top-level nodes
|
|
132
|
+
* @param depth - The current nesting depth in the tree
|
|
133
|
+
* @returns An array of markuplint AST nodes produced from the origin node
|
|
134
|
+
*/
|
|
41
135
|
nodeize(originNode: Node, parentNode: MLASTParentNode | null, depth: number): readonly MLASTNodeTreeItem[];
|
|
136
|
+
/**
|
|
137
|
+
* Post-processes the nodes produced by `nodeize`, separating them into siblings
|
|
138
|
+
* at the current depth and ancestors that belong to a shallower depth level.
|
|
139
|
+
* Doctype nodes at depth 0 are promoted to ancestors.
|
|
140
|
+
*
|
|
141
|
+
* @param siblings - The nodes produced by `nodeize` for a single origin node
|
|
142
|
+
* @param parentNode - The parent markuplint AST node, or null for top-level nodes
|
|
143
|
+
* @param depth - The current nesting depth
|
|
144
|
+
* @returns An object with `siblings` at the current depth and `ancestors` at shallower depths
|
|
145
|
+
*/
|
|
42
146
|
afterNodeize(siblings: readonly MLASTNodeTreeItem[], parentNode: MLASTParentNode | null, depth: number): {
|
|
43
147
|
siblings: MLASTChildNode[];
|
|
44
148
|
ancestors: MLASTNodeTreeItem[];
|
|
45
149
|
};
|
|
150
|
+
/**
|
|
151
|
+
* Flattens a hierarchical node tree into a flat, sorted list by walking
|
|
152
|
+
* the tree depth-first and removing duplicated nodes.
|
|
153
|
+
*
|
|
154
|
+
* @param nodeTree - The hierarchical node tree to flatten
|
|
155
|
+
* @returns A flat array of all nodes in source order
|
|
156
|
+
*/
|
|
46
157
|
flattenNodes(nodeTree: readonly MLASTNodeTreeItem[]): readonly MLASTNodeTreeItem[];
|
|
158
|
+
/**
|
|
159
|
+
* Post-processes the flattened node list by exposing remnant whitespace and
|
|
160
|
+
* invalid nodes between known nodes, converting orphan end tags to bogus markers,
|
|
161
|
+
* concatenating adjacent text nodes, and trimming overlapping text.
|
|
162
|
+
*
|
|
163
|
+
* @param nodeList - The flat node list to post-process
|
|
164
|
+
* @param options - Controls which post-processing steps are applied
|
|
165
|
+
* @returns The cleaned-up flat node list
|
|
166
|
+
*/
|
|
47
167
|
afterFlattenNodes(nodeList: readonly MLASTNodeTreeItem[], options?: {
|
|
48
168
|
readonly exposeInvalidNode?: boolean;
|
|
49
169
|
readonly exposeWhiteSpace?: boolean;
|
|
50
170
|
readonly concatText?: boolean;
|
|
51
171
|
}): readonly MLASTNodeTreeItem[];
|
|
172
|
+
/**
|
|
173
|
+
* Creates an AST doctype node from a token containing the doctype
|
|
174
|
+
* name, public ID, and system ID.
|
|
175
|
+
*
|
|
176
|
+
* @param token - The child token with doctype-specific properties
|
|
177
|
+
* @returns An array containing the single doctype AST node
|
|
178
|
+
*/
|
|
52
179
|
visitDoctype(token: ChildToken & {
|
|
53
180
|
readonly name: string;
|
|
54
181
|
readonly publicId: string;
|
|
55
182
|
readonly systemId: string;
|
|
56
183
|
}): readonly MLASTNodeTreeItem[];
|
|
184
|
+
/**
|
|
185
|
+
* Creates an AST comment node from a token. Automatically detects whether
|
|
186
|
+
* the comment is a bogus comment (not starting with `<!--`).
|
|
187
|
+
*
|
|
188
|
+
* @param token - The child token containing the comment's raw text and position
|
|
189
|
+
* @param options - Optional settings to override the bogus detection
|
|
190
|
+
* @returns An array containing the single comment AST node
|
|
191
|
+
*/
|
|
57
192
|
visitComment(token: ChildToken, options?: {
|
|
58
193
|
readonly isBogus?: boolean;
|
|
59
194
|
}): readonly MLASTNodeTreeItem[];
|
|
195
|
+
/**
|
|
196
|
+
* Creates AST text node(s) from a token. Optionally re-parses the text content
|
|
197
|
+
* to discover embedded HTML tags within it.
|
|
198
|
+
*
|
|
199
|
+
* @param token - The child token containing the text content and position
|
|
200
|
+
* @param options - Controls whether to search for embedded tags and how to handle invalid ones
|
|
201
|
+
* @returns An array of AST nodes; a single text node or multiple tag/text nodes if tags were found
|
|
202
|
+
*/
|
|
60
203
|
visitText(token: ChildToken, options?: {
|
|
61
204
|
readonly researchTags?: boolean;
|
|
62
205
|
readonly invalidTagAsText?: boolean;
|
|
63
206
|
}): readonly MLASTNodeTreeItem[];
|
|
207
|
+
/**
|
|
208
|
+
* Creates AST element node(s) from a token, including the start tag, optional end tag,
|
|
209
|
+
* and recursively traversed child nodes. Handles ghost elements (empty raw),
|
|
210
|
+
* self-closing tags, and nameless fragments (e.g., JSX `<>`).
|
|
211
|
+
*
|
|
212
|
+
* @param token - The child token with the element's node name and namespace
|
|
213
|
+
* @param childNodes - The language-specific child AST nodes to traverse
|
|
214
|
+
* @param options - Controls end tag creation, fragment handling, and property overrides
|
|
215
|
+
* @returns An array of AST nodes including the start tag, optional end tag, and any sibling nodes
|
|
216
|
+
*/
|
|
64
217
|
visitElement(token: ChildToken & {
|
|
65
218
|
readonly nodeName: string;
|
|
66
219
|
readonly namespace: string;
|
|
@@ -69,12 +222,48 @@ export declare abstract class Parser<Node extends {} = {}, State extends unknown
|
|
|
69
222
|
readonly namelessFragment?: boolean;
|
|
70
223
|
readonly overwriteProps?: Partial<MLASTElement>;
|
|
71
224
|
}): readonly MLASTNodeTreeItem[];
|
|
225
|
+
/**
|
|
226
|
+
* Creates an AST preprocessor-specific block node (e.g., for template directives
|
|
227
|
+
* like `{#if}`, `{#each}`, or front matter). Recursively traverses child nodes.
|
|
228
|
+
*
|
|
229
|
+
* @param token - The child token with the block's node name and fragment flag
|
|
230
|
+
* @param childNodes - The language-specific child AST nodes to traverse
|
|
231
|
+
* @param conditionalType - The conditional type if this is a conditional block (e.g., "if", "else")
|
|
232
|
+
* @param originBlockNode - The original language-specific block node for reference
|
|
233
|
+
* @returns An array of AST nodes including the block node and any sibling nodes
|
|
234
|
+
*/
|
|
72
235
|
visitPsBlock(token: ChildToken & {
|
|
73
236
|
readonly nodeName: string;
|
|
74
237
|
readonly isFragment: boolean;
|
|
75
238
|
}, childNodes?: readonly Node[], conditionalType?: MLASTPreprocessorSpecificBlockConditionalType, originBlockNode?: Node): readonly MLASTNodeTreeItem[];
|
|
239
|
+
/**
|
|
240
|
+
* Traverses a list of child nodes under the given parent, appending the resulting
|
|
241
|
+
* child AST nodes to the parent and returning any sibling nodes that belong
|
|
242
|
+
* to ancestor levels. Skips traversal for raw text elements (e.g., `<script>`, `<style>`).
|
|
243
|
+
*
|
|
244
|
+
* @param children - The language-specific child AST nodes to traverse
|
|
245
|
+
* @param parentNode - The parent markuplint AST node to which children will be appended
|
|
246
|
+
* @returns An array of sibling nodes that belong to ancestor depth levels
|
|
247
|
+
*/
|
|
76
248
|
visitChildren(children: readonly Node[], parentNode: MLASTParentNode | null): readonly MLASTNodeTreeItem[];
|
|
249
|
+
/**
|
|
250
|
+
* Attempts to parse a token as a JSX spread attribute (e.g., `{...props}`).
|
|
251
|
+
* Returns null if the token does not match the spread attribute pattern.
|
|
252
|
+
*
|
|
253
|
+
* @param token - The token to inspect for spread attribute syntax
|
|
254
|
+
* @returns A spread attribute AST node, or null if the token is not a spread attribute
|
|
255
|
+
*/
|
|
77
256
|
visitSpreadAttr(token: Token): MLASTSpreadAttr | null;
|
|
257
|
+
/**
|
|
258
|
+
* Parses a token into a fully structured attribute AST node, breaking it down
|
|
259
|
+
* into its constituent parts: spaces, name, equal sign, quotes, and value.
|
|
260
|
+
* Also detects spread attributes. If there is leftover text after the attribute,
|
|
261
|
+
* it is returned in the `__rightText` property for further processing.
|
|
262
|
+
*
|
|
263
|
+
* @param token - The token containing the raw attribute text and position
|
|
264
|
+
* @param options - Controls quoting behavior, value types, and the initial parser state
|
|
265
|
+
* @returns The parsed attribute AST node with an optional `__rightText` for remaining unparsed content
|
|
266
|
+
*/
|
|
78
267
|
visitAttr(token: Token, options?: {
|
|
79
268
|
readonly quoteSet?: readonly QuoteSet[];
|
|
80
269
|
readonly noQuoteValueType?: ValueType;
|
|
@@ -83,9 +272,25 @@ export declare abstract class Parser<Node extends {} = {}, State extends unknown
|
|
|
83
272
|
}): MLASTAttr & {
|
|
84
273
|
__rightText?: string;
|
|
85
274
|
};
|
|
275
|
+
/**
|
|
276
|
+
* Re-parses a text token to discover embedded HTML/XML tags within it,
|
|
277
|
+
* splitting the content into a sequence of tag and text AST nodes.
|
|
278
|
+
* Handles self-closing detection, depth tracking, and void element recognition.
|
|
279
|
+
*
|
|
280
|
+
* @param token - The child token containing the code fragment to re-parse
|
|
281
|
+
* @param options - Controls whether nameless fragments (JSX `<>`) are recognized
|
|
282
|
+
* @returns An array of tag and text AST nodes discovered in the code fragment
|
|
283
|
+
*/
|
|
86
284
|
parseCodeFragment(token: ChildToken, options?: {
|
|
87
285
|
readonly namelessFragment?: boolean;
|
|
88
286
|
}): (MLASTTag | MLASTText)[];
|
|
287
|
+
/**
|
|
288
|
+
* Updates the position and depth properties of an AST node, recalculating
|
|
289
|
+
* end offsets, lines, and columns based on the new start values.
|
|
290
|
+
*
|
|
291
|
+
* @param node - The AST node whose location should be updated
|
|
292
|
+
* @param props - The new position and depth values to apply (only provided values are changed)
|
|
293
|
+
*/
|
|
89
294
|
updateLocation(node: MLASTNodeTreeItem, props: Partial<Pick<MLASTNodeTreeItem, 'startOffset' | 'startLine' | 'startCol' | 'depth'>>): void;
|
|
90
295
|
/**
|
|
91
296
|
* Set new raw code to target node.
|
|
@@ -96,18 +301,95 @@ export declare abstract class Parser<Node extends {} = {}, State extends unknown
|
|
|
96
301
|
* @param raw new raw code
|
|
97
302
|
*/
|
|
98
303
|
updateRaw(node: MLASTToken, raw: string): void;
|
|
304
|
+
/**
|
|
305
|
+
* Updates the node name and/or element type of an element or close tag AST node.
|
|
306
|
+
* Useful for renaming elements or changing their classification after initial parsing.
|
|
307
|
+
*
|
|
308
|
+
* @param el - The element or close tag AST node to update
|
|
309
|
+
* @param props - The properties to overwrite on the element
|
|
310
|
+
*/
|
|
99
311
|
updateElement(el: MLASTElement, props: Partial<Pick<MLASTElement, 'nodeName' | 'elementType'>>): void;
|
|
100
312
|
updateElement(el: MLASTElementCloseTag, props: Partial<Pick<MLASTElementCloseTag, 'nodeName'>>): void;
|
|
313
|
+
/**
|
|
314
|
+
* Updates metadata properties on an HTML attribute AST node, such as marking
|
|
315
|
+
* it as a directive, dynamic value, or setting its potential name/value
|
|
316
|
+
* for preprocessor-specific attribute transformations.
|
|
317
|
+
*
|
|
318
|
+
* @param attr - The HTML attribute AST node to update
|
|
319
|
+
* @param props - The metadata properties to overwrite on the attribute
|
|
320
|
+
*/
|
|
101
321
|
updateAttr(attr: MLASTHTMLAttr, props: Partial<Pick<MLASTHTMLAttr, 'isDynamicValue' | 'isDirective' | 'potentialName' | 'potentialValue' | 'valueType' | 'candidate' | 'isDuplicatable'>>): void;
|
|
322
|
+
/**
|
|
323
|
+
* Determines the element type (e.g., "html", "web-component", "authored") for a
|
|
324
|
+
* given tag name, using the parser's authored element name distinguishing pattern.
|
|
325
|
+
*
|
|
326
|
+
* @param nodeName - The tag name to classify
|
|
327
|
+
* @param defaultPattern - A fallback pattern if no authored element name pattern is set
|
|
328
|
+
* @returns The element type classification
|
|
329
|
+
*/
|
|
102
330
|
detectElementType(nodeName: string, defaultPattern?: ParserAuthoredElementNameDistinguishing): ElementType;
|
|
331
|
+
/**
|
|
332
|
+
* Creates a new MLASTToken with a generated UUID and computed end position.
|
|
333
|
+
* Accepts either a Token object or a raw string with explicit start coordinates.
|
|
334
|
+
*
|
|
335
|
+
* @param token - A Token object or raw string to create the AST token from
|
|
336
|
+
* @param startOffset - The byte offset where the token starts (required when token is a string)
|
|
337
|
+
* @param startLine - The line number where the token starts (required when token is a string)
|
|
338
|
+
* @param startCol - The column number where the token starts (required when token is a string)
|
|
339
|
+
* @returns A fully populated AST token with UUID, start/end positions, and raw content
|
|
340
|
+
*/
|
|
103
341
|
createToken(token: Token): MLASTToken;
|
|
104
342
|
createToken(token: string, startOffset: number, startLine: number, startCol: number): MLASTToken;
|
|
343
|
+
/**
|
|
344
|
+
* Extracts a Token from the current raw code at the given byte offset range,
|
|
345
|
+
* computing the line and column from the source position.
|
|
346
|
+
*
|
|
347
|
+
* @param start - The starting byte offset (inclusive) in the raw code
|
|
348
|
+
* @param end - The ending byte offset (exclusive) in the raw code; if omitted, slices to the end
|
|
349
|
+
* @returns A Token containing the sliced raw content and its start position
|
|
350
|
+
*/
|
|
105
351
|
sliceFragment(start: number, end?: number): Token;
|
|
352
|
+
/**
|
|
353
|
+
* Calculates start and end byte offsets from line/column positions
|
|
354
|
+
* within the current raw source code.
|
|
355
|
+
*
|
|
356
|
+
* @param startLine - The starting line number (1-based)
|
|
357
|
+
* @param startCol - The starting column number (1-based)
|
|
358
|
+
* @param endLine - The ending line number (1-based)
|
|
359
|
+
* @param endCol - The ending column number (1-based)
|
|
360
|
+
* @returns The computed start and end byte offsets
|
|
361
|
+
*/
|
|
106
362
|
getOffsetsFromCode(startLine: number, startCol: number, endLine: number, endCol: number): {
|
|
107
363
|
offset: number;
|
|
108
364
|
endOffset: number;
|
|
109
365
|
};
|
|
366
|
+
/**
|
|
367
|
+
* Walks through a node list depth-first, invoking the walker callback for each node.
|
|
368
|
+
* The walker receives the current node, the sequentially previous node, and the depth.
|
|
369
|
+
* Automatically recurses into child nodes of parent elements and preprocessor blocks.
|
|
370
|
+
*
|
|
371
|
+
* @template Node - The specific AST node type being walked
|
|
372
|
+
* @param nodeList - The list of nodes to walk
|
|
373
|
+
* @param walker - The callback invoked for each node during the walk
|
|
374
|
+
* @param depth - The current depth (starts at 0 for top-level calls)
|
|
375
|
+
*/
|
|
110
376
|
walk<Node extends MLASTNodeTreeItem>(nodeList: readonly Node[], walker: Walker<Node>, depth?: number): void;
|
|
377
|
+
/**
|
|
378
|
+
* Appends child nodes to a parent node, updating parent references and
|
|
379
|
+
* maintaining sorted order by source position. If a child already exists
|
|
380
|
+
* in the parent (by UUID), it is replaced in place rather than duplicated.
|
|
381
|
+
*
|
|
382
|
+
* @param parentNode - The parent node to append children to, or null (no-op)
|
|
383
|
+
* @param childNodes - The child nodes to append
|
|
384
|
+
*/
|
|
111
385
|
appendChild(parentNode: MLASTParentNode | null, ...childNodes: readonly MLASTChildNode[]): void;
|
|
386
|
+
/**
|
|
387
|
+
* Replaces a child node within a parent's child list with one or more replacement nodes.
|
|
388
|
+
* If the old child is not found in the parent, the operation is a no-op.
|
|
389
|
+
*
|
|
390
|
+
* @param parentNode - The parent node containing the child to replace
|
|
391
|
+
* @param oldChildNode - The existing child node to be replaced
|
|
392
|
+
* @param replacementChildNodes - The replacement nodes to insert at the old child's position
|
|
393
|
+
*/
|
|
112
394
|
replaceChild(parentNode: MLASTParentNode, oldChildNode: MLASTChildNode, ...replacementChildNodes: readonly MLASTChildNode[]): void;
|
|
113
395
|
}
|