Skip to content
Snippets Groups Projects
Commit e8747848 authored by David Sveningsson's avatar David Sveningsson
Browse files

feat(rules): use more precise location from `DOMTokenList`

parent 4f4dfe05
No related branches found
No related tags found
1 merge request!406DOMTokenList location
Pipeline #139984594 passed
......@@ -9,15 +9,15 @@ Array [
"filePath": "inline",
"messages": Array [
Object {
"column": 13,
"column": 21,
"context": undefined,
"line": 1,
"message": "Class \\"foo\\" duplicated",
"offset": 12,
"offset": 20,
"ruleId": "no-dup-class",
"selector": "div",
"severity": 2,
"size": 11,
"size": 3,
},
],
"source": "<div class=\\"foo bar foo\\"></div>",
......
......@@ -5,7 +5,7 @@ category: style
summary: Require classes to match a specific pattern
---
# require a specific class format (`class-pattern`)
# Require a specific class format (`class-pattern`)
Requires all classes to match a given pattern.
......@@ -14,13 +14,13 @@ Requires all classes to match a given pattern.
Examples of **incorrect** code for this rule:
<validate name="incorrect" rules="class-pattern">
<div class="fooBar"></foobar>
<div class="fooBar"></foobar>
</validate>
Examples of **correct** code for this rule:
<validate name="correct" rules="class-pattern">
<div class="foo-bar"></div>
<div class="foo-bar"></div>
</validate>
## Options
......
......@@ -4,7 +4,7 @@ name: no-dup-class
summary: Disallow duplicated classes
---
# disallows duplicated classes on same element (`no-dup-class`)
# Disallows duplicated classes on same element (`no-dup-class`)
Prevents unnecessary duplication of class names.
......
......@@ -14,26 +14,26 @@ Array [
"filePath": "test-files/rules/class-pattern.html",
"messages": Array [
Object {
"column": 13,
"column": 17,
"context": undefined,
"line": 3,
"message": "Class \\"foo_bar\\" does not match required pattern \\"/^[a-z0-9-]+$/\\"",
"offset": 49,
"offset": 53,
"ruleId": "class-pattern",
"selector": "div:nth-child(2)",
"severity": 2,
"size": 15,
"size": 7,
},
Object {
"column": 13,
"column": 17,
"context": undefined,
"line": 5,
"message": "Class \\"fooBar\\" does not match required pattern \\"/^[a-z0-9-]+$/\\"",
"offset": 86,
"offset": 90,
"ruleId": "class-pattern",
"selector": "div:nth-child(3)",
"severity": 2,
"size": 14,
"size": 6,
},
],
"source": "<div class=\\"foo foo-bar bar\\"></div>
......
......@@ -14,15 +14,15 @@ Array [
"filePath": "test-files/rules/no-dup-class.html",
"messages": Array [
Object {
"column": 13,
"column": 21,
"context": undefined,
"line": 5,
"message": "Class \\"foo\\" duplicated",
"offset": 66,
"offset": 74,
"ruleId": "no-dup-class",
"selector": "div:nth-child(3)",
"severity": 2,
"size": 11,
"size": 3,
},
],
"source": "<div class=\\"foo\\"></div>
......
......@@ -33,13 +33,14 @@ export default class ClassPattern extends Rule<void, RuleOptions> {
return;
}
const classes = new DOMTokenList(event.value);
classes.forEach((cur) => {
const classes = new DOMTokenList(event.value, event.valueLocation);
classes.forEach((cur: string, index: number) => {
if (!cur.match(this.pattern)) {
const location = classes.location(index);
this.report(
event.target,
`Class "${cur}" does not match required pattern "${this.pattern}"`,
event.valueLocation
location
);
}
});
......
......@@ -16,15 +16,12 @@ export default class NoDupClass extends Rule {
return;
}
const classes = new DOMTokenList(event.value);
const classes = new DOMTokenList(event.value, event.valueLocation);
const unique: Set<string> = new Set();
classes.forEach((cur) => {
classes.forEach((cur: string, index: number) => {
if (unique.has(cur)) {
this.report(
event.target,
`Class "${cur}" duplicated`,
event.valueLocation
);
const location = classes.location(index);
this.report(event.target, `Class "${cur}" duplicated`, location);
}
unique.add(cur);
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment