@markuplint/ml-core 4.13.1 → 4.13.3
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 +467 -0
- package/ARCHITECTURE.md +467 -0
- package/CHANGELOG.md +5 -1
- package/README.md +5 -0
- package/SKILL.md +61 -0
- package/docs/linting-pipeline.ja.md +303 -0
- package/docs/linting-pipeline.md +303 -0
- package/docs/maintenance.ja.md +210 -0
- package/docs/maintenance.md +210 -0
- package/docs/ml-dom/attr.ja.md +95 -0
- package/docs/ml-dom/attr.md +95 -0
- package/docs/ml-dom/block.ja.md +272 -0
- package/docs/ml-dom/block.md +272 -0
- package/docs/ml-dom/document.ja.md +141 -0
- package/docs/ml-dom/document.md +141 -0
- package/docs/ml-dom/element.ja.md +176 -0
- package/docs/ml-dom/element.md +176 -0
- package/docs/ml-dom/helpers.ja.md +203 -0
- package/docs/ml-dom/helpers.md +203 -0
- package/docs/ml-dom/node.ja.md +200 -0
- package/docs/ml-dom/node.md +200 -0
- package/docs/ml-dom/others.ja.md +119 -0
- package/docs/ml-dom/others.md +119 -0
- package/docs/ml-dom/overview.ja.md +102 -0
- package/docs/ml-dom/overview.md +102 -0
- package/docs/ml-dom/pretender.ja.md +269 -0
- package/docs/ml-dom/pretender.md +269 -0
- package/docs/ml-dom/rule-mapping.ja.md +371 -0
- package/docs/ml-dom/rule-mapping.md +371 -0
- package/docs/ml-dom.ja.md +18 -0
- package/docs/ml-dom.md +18 -0
- package/docs/rule-system.ja.md +270 -0
- package/docs/rule-system.md +270 -0
- package/lib/convert-ruleset.d.ts +7 -0
- package/lib/convert-ruleset.js +7 -0
- package/lib/debug.d.ts +4 -0
- package/lib/debug.js +4 -0
- package/lib/ml-core.d.ts +36 -0
- package/lib/ml-core.js +29 -0
- package/lib/ml-dom/helper/get-indent.d.ts +4 -1
- package/lib/ml-dom/helper/get-indent.js +4 -1
- package/lib/ml-dom/node/attr.d.ts +65 -4
- package/lib/ml-dom/node/attr.js +53 -4
- package/lib/ml-dom/node/block.d.ts +21 -0
- package/lib/ml-dom/node/block.js +14 -0
- package/lib/ml-dom/node/child-node.d.ts +9 -0
- package/lib/ml-dom/node/child-node.js +9 -0
- package/lib/ml-dom/node/comment.d.ts +7 -0
- package/lib/ml-dom/node/comment.js +7 -0
- package/lib/ml-dom/node/document-fragment.d.ts +8 -0
- package/lib/ml-dom/node/document-fragment.js +8 -0
- package/lib/ml-dom/node/document-type.d.ts +22 -0
- package/lib/ml-dom/node/document-type.js +13 -0
- package/lib/ml-dom/node/document.d.ts +74 -4
- package/lib/ml-dom/node/document.js +57 -2
- package/lib/ml-dom/node/element.d.ts +136 -2
- package/lib/ml-dom/node/element.js +115 -2
- package/lib/ml-dom/node/node.d.ts +16 -0
- package/lib/ml-dom/node/node.js +16 -0
- package/lib/ml-dom/node/text.d.ts +12 -0
- package/lib/ml-dom/node/text.js +12 -0
- package/lib/ml-dom/node/types.d.ts +68 -0
- package/lib/ml-dom/token/token.d.ts +42 -0
- package/lib/ml-dom/token/token.js +36 -0
- package/lib/ml-rule/create-rule.d.ts +9 -0
- package/lib/ml-rule/create-rule.js +9 -0
- package/lib/ml-rule/ml-rule.d.ts +33 -0
- package/lib/ml-rule/ml-rule.js +30 -0
- package/lib/ml-rule/types.d.ts +41 -0
- package/lib/plugin/plugin.d.ts +8 -0
- package/lib/plugin/plugin.js +8 -0
- package/lib/plugin/types.d.ts +21 -0
- package/lib/ruleset/index.d.ts +10 -0
- package/lib/ruleset/index.js +7 -0
- package/lib/test/index.d.ts +42 -1
- package/lib/test/index.js +35 -1
- package/lib/types.d.ts +8 -0
- package/lib/violation-collector.d.ts +33 -0
- package/lib/violation-collector.js +33 -0
- package/package.json +13 -13
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
# Rule System
|
|
2
|
+
|
|
3
|
+
Detailed reference for the rule framework in `@markuplint/ml-core`.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The rule framework handles the full lifecycle of lint rules: definition, configuration, mapping to nodes, execution, and violation collection. The key components are:
|
|
8
|
+
|
|
9
|
+
- **RuleSeed** -- Rule definition type (verify/fix functions + defaults)
|
|
10
|
+
- **MLRule** -- Rule execution class (wraps a seed with name and config resolution)
|
|
11
|
+
- **MLRuleContext** -- Execution context for rules (document access, translation, violation reporting)
|
|
12
|
+
- **RuleMapper** -- Maps rule configurations to specific DOM nodes based on selector specificity
|
|
13
|
+
- **Ruleset** -- Extracts rules, nodeRules, and childNodeRules from Config
|
|
14
|
+
|
|
15
|
+
## RuleSeed
|
|
16
|
+
|
|
17
|
+
Source: `src/ml-rule/types.ts`
|
|
18
|
+
|
|
19
|
+
The `RuleSeed<T, O>` type defines a rule's implementation.
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
type RuleSeed<T extends RuleConfigValue = boolean, O extends PlainData = undefined> = {
|
|
23
|
+
readonly meta?: {
|
|
24
|
+
readonly category?: 'validation' | 'style' | 'naming-convention' | 'a11y' | 'maintainability';
|
|
25
|
+
};
|
|
26
|
+
readonly defaultSeverity?: Severity;
|
|
27
|
+
readonly defaultValue?: T;
|
|
28
|
+
readonly defaultOptions?: O;
|
|
29
|
+
verify(context: ProvidedContext<T, O>): void | Promise<void>;
|
|
30
|
+
fix?(context: ProvidedContext<T, O>): void | Promise<void>;
|
|
31
|
+
};
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Category Values
|
|
35
|
+
|
|
36
|
+
| Category | Description |
|
|
37
|
+
| --------------------- | ------------------------------- |
|
|
38
|
+
| `'validation'` | HTML standard compliance checks |
|
|
39
|
+
| `'style'` | Code style and formatting rules |
|
|
40
|
+
| `'naming-convention'` | Naming convention enforcement |
|
|
41
|
+
| `'a11y'` | Accessibility checks |
|
|
42
|
+
| `'maintainability'` | Code maintainability rules |
|
|
43
|
+
|
|
44
|
+
### Default Values
|
|
45
|
+
|
|
46
|
+
- `defaultSeverity` defaults to `'error'` if not specified
|
|
47
|
+
- `defaultValue` defaults to `true` if not specified
|
|
48
|
+
- `defaultOptions` defaults to `undefined`
|
|
49
|
+
|
|
50
|
+
## createRule
|
|
51
|
+
|
|
52
|
+
Source: `src/ml-rule/create-rule.ts`
|
|
53
|
+
|
|
54
|
+
Factory function for type-safe rule seed creation:
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
function createRule<T extends RuleConfigValue, O extends PlainData = undefined>(
|
|
58
|
+
seed: Readonly<RuleSeed<T, O>>,
|
|
59
|
+
): RuleSeed<T, O>;
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Returns the seed as-is. Serves primarily as a type helper for TypeScript inference.
|
|
63
|
+
|
|
64
|
+
### Usage
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { createRule } from '@markuplint/ml-core';
|
|
68
|
+
|
|
69
|
+
export default createRule({
|
|
70
|
+
defaultSeverity: 'error',
|
|
71
|
+
defaultValue: true,
|
|
72
|
+
async verify({ document, report, t }) {
|
|
73
|
+
await document.walkOn('Element', el => {
|
|
74
|
+
if (/* violation condition */) {
|
|
75
|
+
report({ scope: el, message: t('Error message') });
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## MLRule
|
|
83
|
+
|
|
84
|
+
Source: `src/ml-rule/ml-rule.ts`
|
|
85
|
+
|
|
86
|
+
`MLRule<T, O>` wraps a `RuleSeed` with a name and provides configuration resolution and verification execution.
|
|
87
|
+
|
|
88
|
+
### Constructor
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
constructor(o: Readonly<RuleSeed<T, O>> & { readonly name: string })
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Properties
|
|
95
|
+
|
|
96
|
+
| Property | Type | Description |
|
|
97
|
+
| ----------------- | ---------- | ----------------------------------------------- |
|
|
98
|
+
| `name` | `string` | Rule identifier (e.g., `"attr-duplication"`) |
|
|
99
|
+
| `defaultSeverity` | `Severity` | Default severity level (from seed or `'error'`) |
|
|
100
|
+
| `defaultValue` | `T` | Default config value (from seed or `true`) |
|
|
101
|
+
| `defaultOptions` | `O` | Default options (from seed) |
|
|
102
|
+
|
|
103
|
+
### Methods
|
|
104
|
+
|
|
105
|
+
#### `verify(document, locale, fix): Promise<Violation[]>`
|
|
106
|
+
|
|
107
|
+
Executes the rule against a document.
|
|
108
|
+
|
|
109
|
+
**Flow:**
|
|
110
|
+
|
|
111
|
+
1. `document.setRule(this)` -- sets current rule context on document
|
|
112
|
+
2. `new MLRuleContext(document, locale)` -- creates execution context
|
|
113
|
+
3. `context.provide()` -- generates providable context object
|
|
114
|
+
4. `await seed.verify(context)` -- runs verification
|
|
115
|
+
5. `await seed.fix(context)` -- runs fix (if `fix=true` and fix function exists)
|
|
116
|
+
6. `context.reports` -> `Violation[]` -- maps reports to violations
|
|
117
|
+
7. `document.setRule(null)` -- clears rule context
|
|
118
|
+
|
|
119
|
+
**Report -> Violation mapping:**
|
|
120
|
+
|
|
121
|
+
- Scope-based reports: extracts `line`, `col`, `raw` from `report.scope` (the node), severity from `report.scope.rule.severity`
|
|
122
|
+
- Direct reports: uses `report.line`, `report.col`, `report.raw` directly, severity from `document.rule.severity`
|
|
123
|
+
|
|
124
|
+
#### `getRuleInfo(ruleSet, ruleName): GlobalRuleInfo<T, O>`
|
|
125
|
+
|
|
126
|
+
Resolves the full rule information from a ruleset.
|
|
127
|
+
|
|
128
|
+
Returns:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
{
|
|
132
|
+
...RuleInfo<T, O>, // Global rule config
|
|
133
|
+
nodeRules: RuleInfo<T, O>[], // Non-disabled node-level overrides
|
|
134
|
+
childNodeRules: RuleInfo<T, O>[], // Non-disabled child-node-level overrides
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
#### `optimizeOption(configSettings): RuleInfo<T, O>`
|
|
139
|
+
|
|
140
|
+
Normalizes raw rule settings into resolved `RuleInfo`.
|
|
141
|
+
|
|
142
|
+
| Input | Result |
|
|
143
|
+
| ---------------------- | --------------------------------------------------------------------------------------- |
|
|
144
|
+
| `undefined` or `false` | `{ disabled: true, severity: default, value: default, options: default }` |
|
|
145
|
+
| `true` | `{ disabled: false, severity: default, value: default, options: default }` |
|
|
146
|
+
| `RuleConfig` object | `{ disabled: false, severity: config/default, value: config/default, options: merged }` |
|
|
147
|
+
| Primitive value | `{ disabled: false, severity: default, value: input, options: default }` |
|
|
148
|
+
|
|
149
|
+
Options merging: arrays are spread (`[...a, ...b]`), objects are spread (`{...a, ...b}`), otherwise fallback to `b ?? a`.
|
|
150
|
+
|
|
151
|
+
## MLRuleContext
|
|
152
|
+
|
|
153
|
+
Source: `src/ml-rule/ml-rule-context.ts`
|
|
154
|
+
|
|
155
|
+
`MLRuleContext<T, O>` provides the execution context for rules.
|
|
156
|
+
|
|
157
|
+
### Constructor
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
constructor(document: MLDocument<T, O>, locale: LocaleSet)
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Creates translator from locale, stores document reference.
|
|
164
|
+
|
|
165
|
+
### Properties
|
|
166
|
+
|
|
167
|
+
| Property | Type | Description |
|
|
168
|
+
| ----------- | ------------------ | --------------------------- |
|
|
169
|
+
| `document` | `MLDocument<T, O>` | The document being verified |
|
|
170
|
+
| `locale` | `string` | Locale string |
|
|
171
|
+
| `translate` | `Translator` | i18n message translator |
|
|
172
|
+
|
|
173
|
+
### `provide(): ProvidedContext`
|
|
174
|
+
|
|
175
|
+
Returns the context object passed to `RuleSeed.verify()` and `RuleSeed.fix()`:
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
{
|
|
179
|
+
document: MLDocument<T, O>,
|
|
180
|
+
translate: Translator,
|
|
181
|
+
t: Translator, // alias for translate
|
|
182
|
+
reports: Report<T, O>[],
|
|
183
|
+
report: (report) => void | boolean,
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### `report(report)`
|
|
188
|
+
|
|
189
|
+
Two overloads:
|
|
190
|
+
|
|
191
|
+
1. **Direct report** (`Report<T, O>`): Pushes the report directly. Returns `undefined`.
|
|
192
|
+
2. **Checker report** (`CheckerReport<T, O>`): Calls the function with translator. If it returns a report, pushes it and returns `true`. If `null`/`undefined`, returns `false`.
|
|
193
|
+
|
|
194
|
+
### Deduplication
|
|
195
|
+
|
|
196
|
+
Reports are deduplicated in `_push()` using:
|
|
197
|
+
|
|
198
|
+
- **Scope-based**: same `scope` object + same `message`
|
|
199
|
+
- **Position-based**: same `col` + `line` + `message` + `raw`
|
|
200
|
+
|
|
201
|
+
### Message Finalization
|
|
202
|
+
|
|
203
|
+
For English locale (`'en'`), the first lowercase letter is capitalized. Other locales pass through unchanged.
|
|
204
|
+
|
|
205
|
+
## Checker Types
|
|
206
|
+
|
|
207
|
+
Source: `src/ml-rule/types.ts`
|
|
208
|
+
|
|
209
|
+
Utility types for building checker functions:
|
|
210
|
+
|
|
211
|
+
| Type | Signature | Description |
|
|
212
|
+
| ------------------------- | ------------------------------------------------------------ | -------------------------- |
|
|
213
|
+
| `Checker<T, O, P>` | `(params: P) => CheckerReport<T, O>` | Generic checker |
|
|
214
|
+
| `ElementChecker<T, O, P>` | `(params: P & { el: Element<T, O> }) => CheckerReport<T, O>` | Element-specific checker |
|
|
215
|
+
| `AttrChecker<T, O, P>` | `(params: P & { attr: Attr<T, O> }) => CheckerReport<T, O>` | Attribute-specific checker |
|
|
216
|
+
| `CheckerReport<T, O>` | `(t: Translator) => Report<T, O> \| undefined \| null` | Deferred report function |
|
|
217
|
+
|
|
218
|
+
## Rule Mapping
|
|
219
|
+
|
|
220
|
+
For detailed documentation on `RuleMapper`, rule configuration resolution (three-layer processing with `rules`, `nodeRules`, `childNodeRules`), specificity-based conflict resolution, merging behavior, and regex selector templates, see the dedicated [Rule Mapping](./ml-dom/rule-mapping.md) reference.
|
|
221
|
+
|
|
222
|
+
## Ruleset
|
|
223
|
+
|
|
224
|
+
Source: `src/ruleset/index.ts`
|
|
225
|
+
|
|
226
|
+
Extracts rule configuration from a `Config` object.
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
class Ruleset {
|
|
230
|
+
readonly rules: Rules;
|
|
231
|
+
readonly nodeRules: readonly NodeRule[];
|
|
232
|
+
readonly childNodeRules: readonly ChildNodeRule[];
|
|
233
|
+
|
|
234
|
+
constructor(config: Config);
|
|
235
|
+
}
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
- `rules` -- Global rule definitions (from `config.rules`, defaults to `{}`)
|
|
239
|
+
- `nodeRules` -- Node-specific overrides (from `config.nodeRules`, defaults to `[]`)
|
|
240
|
+
- `childNodeRules` -- Child-node-specific overrides (from `config.childNodeRules`, defaults to `[]`)
|
|
241
|
+
|
|
242
|
+
## Test Utilities
|
|
243
|
+
|
|
244
|
+
Source: `src/ml-rule/create-test-rule.ts`
|
|
245
|
+
|
|
246
|
+
### createTestRule
|
|
247
|
+
|
|
248
|
+
```typescript
|
|
249
|
+
function createRule<T, O>(seed: Readonly<RuleSeed<T, O>> & { readonly name: string }): MLRule<T, O>;
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
Creates an `MLRule` instance for testing. Unlike `createRule()` in `create-rule.ts`, this requires a `name` property and returns an actual `MLRule` instance.
|
|
253
|
+
|
|
254
|
+
### Test Pattern
|
|
255
|
+
|
|
256
|
+
```typescript
|
|
257
|
+
import { createRule } from '@markuplint/ml-core/test';
|
|
258
|
+
import { createTestDocument } from '@markuplint/ml-core/test';
|
|
259
|
+
|
|
260
|
+
const rule = createRule({
|
|
261
|
+
name: 'my-rule',
|
|
262
|
+
defaultSeverity: 'error',
|
|
263
|
+
async verify({ document, report, t }) {
|
|
264
|
+
// verification logic
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
const doc = createTestDocument('<div></div>');
|
|
269
|
+
const violations = await rule.verify(doc, { locale: 'en' }, false);
|
|
270
|
+
```
|
package/lib/convert-ruleset.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
1
|
import type { Config } from '@markuplint/ml-config';
|
|
2
2
|
import { Ruleset } from './ruleset/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Converts a markuplint {@link Config} object into a {@link Ruleset} instance
|
|
5
|
+
* that can be used by the linting engine.
|
|
6
|
+
*
|
|
7
|
+
* @param config - The configuration to convert (defaults to an empty config)
|
|
8
|
+
* @returns A new Ruleset instance
|
|
9
|
+
*/
|
|
3
10
|
export declare function convertRuleset(config?: Config): Ruleset;
|
package/lib/convert-ruleset.js
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import { Ruleset } from './ruleset/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Converts a markuplint {@link Config} object into a {@link Ruleset} instance
|
|
4
|
+
* that can be used by the linting engine.
|
|
5
|
+
*
|
|
6
|
+
* @param config - The configuration to convert (defaults to an empty config)
|
|
7
|
+
* @returns A new Ruleset instance
|
|
8
|
+
*/
|
|
2
9
|
export function convertRuleset(config = {}) {
|
|
3
10
|
return new Ruleset(config);
|
|
4
11
|
}
|
package/lib/debug.d.ts
CHANGED
package/lib/debug.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import debug from 'debug';
|
|
2
2
|
const CLI_NS = 'markuplint-cli';
|
|
3
3
|
export const log = debug('ml-core');
|
|
4
|
+
/**
|
|
5
|
+
* Enables debug logging for the `ml-core` namespace and the CLI namespace.
|
|
6
|
+
* No-op if debug logging is already enabled.
|
|
7
|
+
*/
|
|
4
8
|
export function enableDebug() {
|
|
5
9
|
if (!log.enabled) {
|
|
6
10
|
debug.enable(`${log.namespace}*`);
|
package/lib/ml-core.d.ts
CHANGED
|
@@ -2,17 +2,53 @@ import type { MLFabric } from './types.js';
|
|
|
2
2
|
import type { PlainData, RuleConfigValue, Violation } from '@markuplint/ml-config';
|
|
3
3
|
import { ParserError } from '@markuplint/parser-utils';
|
|
4
4
|
import { Document } from './ml-dom/index.js';
|
|
5
|
+
/**
|
|
6
|
+
* Parameters for constructing an {@link MLCore} instance.
|
|
7
|
+
* Extends {@link MLFabric} with the source code, filename, and debug flag.
|
|
8
|
+
*/
|
|
5
9
|
export type MLCoreParams = {
|
|
10
|
+
/** The markup source code to lint */
|
|
6
11
|
readonly sourceCode: string;
|
|
12
|
+
/** The filename associated with the source code */
|
|
7
13
|
readonly filename: string;
|
|
14
|
+
/** Whether to enable debug logging */
|
|
8
15
|
readonly debug?: boolean;
|
|
9
16
|
} & MLFabric;
|
|
17
|
+
/**
|
|
18
|
+
* The core linting engine for markuplint.
|
|
19
|
+
*
|
|
20
|
+
* Parses markup source code into an AST, constructs a DOM document,
|
|
21
|
+
* and verifies it against configured rules to produce violations.
|
|
22
|
+
*/
|
|
10
23
|
export declare class MLCore {
|
|
11
24
|
#private;
|
|
12
25
|
constructor({ parser, sourceCode, ruleset, rules, locale, schemas, parserOptions, severity, pretenders, filename, debug, configErrors, }: MLCoreParams);
|
|
26
|
+
/**
|
|
27
|
+
* The parsed document, or a {@link ParserError} if parsing failed.
|
|
28
|
+
*/
|
|
13
29
|
get document(): ParserError | Document<RuleConfigValue, PlainData>;
|
|
30
|
+
/**
|
|
31
|
+
* Replaces the source code and re-parses the document.
|
|
32
|
+
*
|
|
33
|
+
* @param sourceCode - The new markup source code
|
|
34
|
+
*/
|
|
14
35
|
setCode(sourceCode: string): void;
|
|
36
|
+
/**
|
|
37
|
+
* Updates the linting configuration and re-creates the document.
|
|
38
|
+
* Only re-parses if parser options have changed.
|
|
39
|
+
*
|
|
40
|
+
* @param fabric - Partial fabric with the properties to update
|
|
41
|
+
*/
|
|
15
42
|
update({ parser, ruleset, rules, locale, schemas, parserOptions, configErrors }: Partial<MLFabric>): void;
|
|
43
|
+
/**
|
|
44
|
+
* Runs all configured rules against the parsed document and returns violations.
|
|
45
|
+
*
|
|
46
|
+
* If the document failed to parse, a single parse-error violation is returned
|
|
47
|
+
* (unless parse errors are suppressed via severity options).
|
|
48
|
+
*
|
|
49
|
+
* @param fix - Whether to attempt auto-fixing violations
|
|
50
|
+
* @returns An array of violations found during verification
|
|
51
|
+
*/
|
|
16
52
|
verify(fix?: boolean): Promise<Violation[]>;
|
|
17
53
|
private _createDocument;
|
|
18
54
|
private _createParseError;
|
package/lib/ml-core.js
CHANGED
|
@@ -14,6 +14,12 @@ import { ParserError } from '@markuplint/parser-utils';
|
|
|
14
14
|
import { log, enableDebug } from './debug.js';
|
|
15
15
|
import { Document } from './ml-dom/index.js';
|
|
16
16
|
const resultLog = log.extend('result');
|
|
17
|
+
/**
|
|
18
|
+
* The core linting engine for markuplint.
|
|
19
|
+
*
|
|
20
|
+
* Parses markup source code into an AST, constructs a DOM document,
|
|
21
|
+
* and verifies it against configured rules to produce violations.
|
|
22
|
+
*/
|
|
17
23
|
export class MLCore {
|
|
18
24
|
constructor({ parser, sourceCode, ruleset, rules, locale, schemas, parserOptions, severity, pretenders, filename, debug, configErrors, }) {
|
|
19
25
|
_MLCore_ast.set(this, null);
|
|
@@ -50,14 +56,28 @@ export class MLCore {
|
|
|
50
56
|
this._parse();
|
|
51
57
|
this._createDocument();
|
|
52
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* The parsed document, or a {@link ParserError} if parsing failed.
|
|
61
|
+
*/
|
|
53
62
|
get document() {
|
|
54
63
|
return __classPrivateFieldGet(this, _MLCore_document, "f");
|
|
55
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Replaces the source code and re-parses the document.
|
|
67
|
+
*
|
|
68
|
+
* @param sourceCode - The new markup source code
|
|
69
|
+
*/
|
|
56
70
|
setCode(sourceCode) {
|
|
57
71
|
__classPrivateFieldSet(this, _MLCore_sourceCode, sourceCode, "f");
|
|
58
72
|
this._parse();
|
|
59
73
|
this._createDocument();
|
|
60
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Updates the linting configuration and re-creates the document.
|
|
77
|
+
* Only re-parses if parser options have changed.
|
|
78
|
+
*
|
|
79
|
+
* @param fabric - Partial fabric with the properties to update
|
|
80
|
+
*/
|
|
61
81
|
update({ parser, ruleset, rules, locale, schemas, parserOptions, configErrors }) {
|
|
62
82
|
__classPrivateFieldSet(this, _MLCore_parser, parser ?? __classPrivateFieldGet(this, _MLCore_parser, "f"), "f");
|
|
63
83
|
__classPrivateFieldSet(this, _MLCore_ruleset, {
|
|
@@ -76,6 +96,15 @@ export class MLCore {
|
|
|
76
96
|
}
|
|
77
97
|
this._createDocument();
|
|
78
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* Runs all configured rules against the parsed document and returns violations.
|
|
101
|
+
*
|
|
102
|
+
* If the document failed to parse, a single parse-error violation is returned
|
|
103
|
+
* (unless parse errors are suppressed via severity options).
|
|
104
|
+
*
|
|
105
|
+
* @param fix - Whether to attempt auto-fixing violations
|
|
106
|
+
* @returns An array of violations found during verification
|
|
107
|
+
*/
|
|
79
108
|
async verify(fix = false) {
|
|
80
109
|
log('verify: start');
|
|
81
110
|
const violations = [];
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import type { MLNode } from '../node/node.js';
|
|
2
2
|
import type { MLText } from '../node/text.js';
|
|
3
3
|
/**
|
|
4
|
+
* Computes the indentation preceding the given node by analyzing
|
|
5
|
+
* the whitespace in adjacent text nodes.
|
|
4
6
|
*
|
|
5
7
|
* @deprecated
|
|
6
|
-
* @param node
|
|
8
|
+
* @param node - The node whose indentation to determine
|
|
9
|
+
* @returns An indentation object describing the whitespace, or null if no indentation is found
|
|
7
10
|
*/
|
|
8
11
|
export declare function getIndent(node: MLNode<any, any>): MLDOMIndentation | null;
|
|
9
12
|
declare class MLDOMIndentation {
|
|
@@ -11,9 +11,12 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
11
11
|
};
|
|
12
12
|
var _MLDOMIndentation_fixed, _MLDOMIndentation_node, _MLDOMIndentation_parent;
|
|
13
13
|
/**
|
|
14
|
+
* Computes the indentation preceding the given node by analyzing
|
|
15
|
+
* the whitespace in adjacent text nodes.
|
|
14
16
|
*
|
|
15
17
|
* @deprecated
|
|
16
|
-
* @param node
|
|
18
|
+
* @param node - The node whose indentation to determine
|
|
19
|
+
* @returns An indentation object describing the whitespace, or null if no indentation is found
|
|
17
20
|
*/
|
|
18
21
|
export function getIndent(
|
|
19
22
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
@@ -5,24 +5,69 @@ import type { PlainData, RuleConfigValue } from '@markuplint/ml-config';
|
|
|
5
5
|
import { MLToken } from '../token/token.js';
|
|
6
6
|
import { MLDomTokenList } from './dom-token-list.js';
|
|
7
7
|
import { MLNode } from './node.js';
|
|
8
|
+
/**
|
|
9
|
+
* Represents a DOM Attr (attribute) node wrapper in the markuplint DOM tree.
|
|
10
|
+
* Wraps an AST attribute token and provides access to the attribute's name, value,
|
|
11
|
+
* tokens (name, equal sign, quotes, value), and metadata such as whether
|
|
12
|
+
* the attribute is a directive or has a dynamic value.
|
|
13
|
+
*
|
|
14
|
+
* @template T - The rule configuration value type
|
|
15
|
+
* @template O - The rule options type
|
|
16
|
+
*/
|
|
8
17
|
export declare class MLAttr<T extends RuleConfigValue, O extends PlainData = undefined> extends MLNode<T, O, MLASTAttr> implements Attr {
|
|
9
18
|
#private;
|
|
19
|
+
/**
|
|
20
|
+
* A candidate attribute name suggested by the parser, if available.
|
|
21
|
+
*/
|
|
10
22
|
readonly candidate?: string;
|
|
23
|
+
/**
|
|
24
|
+
* The end quote token of the attribute value, or null if the attribute has no value or quotes.
|
|
25
|
+
*/
|
|
11
26
|
readonly endQuote: MLToken | null;
|
|
27
|
+
/**
|
|
28
|
+
* The equal sign token between the attribute name and value, or null if absent.
|
|
29
|
+
*/
|
|
12
30
|
readonly equal: MLToken | null;
|
|
31
|
+
/**
|
|
32
|
+
* Whether this attribute is a directive (e.g., framework-specific attributes like `v-if` or `@click`).
|
|
33
|
+
*/
|
|
13
34
|
readonly isDirective?: true;
|
|
35
|
+
/**
|
|
36
|
+
* Whether this attribute can be duplicated on the same element.
|
|
37
|
+
*/
|
|
14
38
|
readonly isDuplicatable: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Whether this attribute has a dynamic value (e.g., a template expression rather than a static string).
|
|
41
|
+
*/
|
|
15
42
|
readonly isDynamicValue?: true;
|
|
43
|
+
/**
|
|
44
|
+
* The token representing the attribute name, or null for spread attributes.
|
|
45
|
+
*/
|
|
16
46
|
readonly nameNode: MLToken | null;
|
|
17
47
|
/**
|
|
18
48
|
* @implements DOM API: `Attr`
|
|
19
49
|
* @see https://dom.spec.whatwg.org/#ref-for-dom-node-previoussibling%E2%91%A0
|
|
20
50
|
*/
|
|
21
51
|
readonly ownerElement: MLElement<T, O>;
|
|
52
|
+
/**
|
|
53
|
+
* The whitespace token after the equal sign, or null if absent.
|
|
54
|
+
*/
|
|
22
55
|
readonly spacesAfterEqual: MLToken | null;
|
|
56
|
+
/**
|
|
57
|
+
* The whitespace token before the equal sign, or null if absent.
|
|
58
|
+
*/
|
|
23
59
|
readonly spacesBeforeEqual: MLToken | null;
|
|
60
|
+
/**
|
|
61
|
+
* The whitespace token before the attribute name, or null if absent.
|
|
62
|
+
*/
|
|
24
63
|
readonly spacesBeforeName: MLToken | null;
|
|
64
|
+
/**
|
|
65
|
+
* The start quote token of the attribute value, or null if the attribute has no value or quotes.
|
|
66
|
+
*/
|
|
25
67
|
readonly startQuote: MLToken | null;
|
|
68
|
+
/**
|
|
69
|
+
* The token representing the attribute value, or null if the attribute has no value.
|
|
70
|
+
*/
|
|
26
71
|
readonly valueNode: MLToken | null;
|
|
27
72
|
/**
|
|
28
73
|
* Returns the "string" if HTML syntax. Otherwise, returns a type in its syntax.
|
|
@@ -31,25 +76,30 @@ export declare class MLAttr<T extends RuleConfigValue, O extends PlainData = und
|
|
|
31
76
|
* @implements `@markuplint/ml-core` API: `MLAttr`
|
|
32
77
|
*/
|
|
33
78
|
readonly valueType: 'string' | 'number' | 'boolean' | 'code';
|
|
79
|
+
/**
|
|
80
|
+
* Creates a new MLAttr instance from an AST attribute token.
|
|
81
|
+
*
|
|
82
|
+
* @param astToken - The AST attribute token to wrap
|
|
83
|
+
* @param ownElement - The element that owns this attribute
|
|
84
|
+
*/
|
|
34
85
|
constructor(astToken: MLASTAttr, ownElement: MLElement<T, O>);
|
|
35
86
|
/**
|
|
36
|
-
*
|
|
87
|
+
* Returns the local name portion of the attribute (without namespace prefix).
|
|
37
88
|
*
|
|
38
|
-
* @unsupported
|
|
39
89
|
* @implements DOM API: `Attr`
|
|
40
90
|
* @see https://dom.spec.whatwg.org/#ref-for-dom-attr-localname
|
|
41
91
|
*/
|
|
42
92
|
get localName(): string;
|
|
43
93
|
/**
|
|
94
|
+
* Returns the qualified attribute name (the potential name resolved by the parser).
|
|
44
95
|
*
|
|
45
96
|
* @implements DOM API: `Attr`
|
|
46
97
|
* @see https://dom.spec.whatwg.org/#dom-attr-name
|
|
47
98
|
*/
|
|
48
99
|
get name(): string;
|
|
49
100
|
/**
|
|
50
|
-
*
|
|
101
|
+
* Returns the namespace URI of this attribute, resolved from the attribute name.
|
|
51
102
|
*
|
|
52
|
-
* @unsupported
|
|
53
103
|
* @implements DOM API: `Attr`
|
|
54
104
|
* @see https://dom.spec.whatwg.org/#ref-for-dom-attr-namespaceuri
|
|
55
105
|
*/
|
|
@@ -64,6 +114,12 @@ export declare class MLAttr<T extends RuleConfigValue, O extends PlainData = und
|
|
|
64
114
|
* Returns a number appropriate for the type of `Attr`
|
|
65
115
|
*/
|
|
66
116
|
get nodeType(): AttributeNodeType;
|
|
117
|
+
/**
|
|
118
|
+
* Returns the attribute value, equivalent to the `value` property.
|
|
119
|
+
*
|
|
120
|
+
* @implements DOM API: `Attr`
|
|
121
|
+
* @see https://dom.spec.whatwg.org/#dom-node-nodevalue
|
|
122
|
+
*/
|
|
67
123
|
get nodeValue(): string;
|
|
68
124
|
/**
|
|
69
125
|
* **IT THROWS AN ERROR WHEN CALLING THIS.**
|
|
@@ -106,7 +162,12 @@ export declare class MLAttr<T extends RuleConfigValue, O extends PlainData = und
|
|
|
106
162
|
*/
|
|
107
163
|
fix(raw: string): void;
|
|
108
164
|
/**
|
|
165
|
+
* Returns a normalized string representation of the attribute,
|
|
166
|
+
* stripping extraneous whitespace around the name, equal sign, and value tokens.
|
|
167
|
+
* Falls back to the raw string if any token is missing.
|
|
168
|
+
*
|
|
109
169
|
* @implements `@markuplint/ml-core` API: `MLAttr`
|
|
170
|
+
* @returns The normalized attribute string
|
|
110
171
|
*/
|
|
111
172
|
toNormalizeString(): string;
|
|
112
173
|
/**
|