Skip to content

Commit a44eddd

Browse files
committed
Closes #1317: Add filepath to the start event
1 parent 01a3194 commit a44eddd

7 files changed

Lines changed: 46 additions & 11 deletions

File tree

dist/cli/htmlhint.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core/core.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core/htmlparser.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli/htmlhint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ function hintFile(filepath: string, ruleset?: Ruleset) {
505505
// ignore
506506
}
507507

508-
return HTMLHint.verify(content, ruleset)
508+
return HTMLHint.verify(content, ruleset, filepath)
509509
}
510510

511511
// hint stdin

src/core/core.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ class HTMLHintCore {
2727
this.rules[rule.id] = rule
2828
}
2929

30-
public verify(html: string, ruleset: Ruleset = this.defaultRuleset) {
30+
public verify(
31+
html: string,
32+
ruleset: Ruleset = this.defaultRuleset,
33+
filepath: string = ''
34+
) {
3135
if (Object.keys(ruleset).length === 0) {
3236
ruleset = this.defaultRuleset
3337
}
@@ -74,7 +78,7 @@ class HTMLHintCore {
7478
}
7579
}
7680

77-
parser.parse(html)
81+
parser.parse(html, filepath)
7882

7983
return reporter.messages
8084
}

src/core/htmlparser.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface Block {
1818
long: boolean
1919
close: string
2020
lastEvent?: Partial<Block>
21+
filepath?: string
2122
}
2223

2324
export type Listener = (event: Block) => void
@@ -49,7 +50,7 @@ export default class HTMLParser {
4950
return obj
5051
}
5152

52-
public parse(html: string): void {
53+
public parse(html: string, filepath: string = ''): void {
5354
const mapCdataTags = this._mapCdataTags
5455

5556
const regTag =
@@ -78,6 +79,7 @@ export default class HTMLParser {
7879
pos: 0,
7980
line: 1,
8081
col: 1,
82+
filepath: filepath,
8183
})
8284

8385
// Do not ignore validation inside <script type="ng/template"> template

test/htmlparser.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,3 +655,31 @@ describe('HTMLParser: Case parse', () => {
655655
parser.parse('<img src="aaa" alt= />')
656656
})
657657
})
658+
659+
describe('HTMLParser: Filepath', () => {
660+
it('should include filepath in start event when provided to parse', (done) => {
661+
const parser = new HTMLParser()
662+
const arrEvents = []
663+
getAllEvents(parser, arrEvents, () => {
664+
// The start event must be the first event (index 0)
665+
expect(arrEvents[0]).toEvent('start', {
666+
filepath: '/some/example.txt',
667+
})
668+
done()
669+
})
670+
parser.parse('', '/some/example.txt')
671+
})
672+
673+
it('should include empty string filepath in start event when not provided', (done) => {
674+
const parser = new HTMLParser()
675+
const arrEvents = []
676+
getAllEvents(parser, arrEvents, () => {
677+
// The start event must be the first event (index 0)
678+
expect(arrEvents[0]).toEvent('start', {
679+
filepath: '',
680+
})
681+
done()
682+
})
683+
parser.parse('')
684+
})
685+
})

0 commit comments

Comments
 (0)