Skip to content

Commit

Permalink
lint: forbid leading/trailing whitespace within tags
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot committed Aug 18, 2020
1 parent 0bc4039 commit 55e5f70
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/lint/collect-spelling-diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ let matchers = [
pattern: /(?<=\S) +(?! |<\/(td|th|dd|dt)>)/gu,
message: 'multiple consecutive spaces are not allowed',
},
{
pattern: /(?<=<[a-z]+( [a-z]+(="[^"\n]+")?)*>)(?<!(td|th|dd|dt)>) /gu,
message: 'tags should not contain leading whitespace',
},
{
pattern: /(?<=[^ \n]) +<\/(?!td|th|dd|dt)/gu,
message: 'tags should not contain trailing whitespace',
},
];

export function collectSpellingDiagnostics(
Expand Down
29 changes: 29 additions & 0 deletions test/lint-spelling.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,32 @@ windows:${M}\r
);
});

it('leading whitespace within tags', async function () {
await assertLint(
positioned`
<p>${M} This is an example.</p>
`,
{
ruleId: 'spelling',
nodeType: 'html',
message: 'tags should not contain leading whitespace',
}
);
});

it('trailing whitespace within tags', async function () {
await assertLint(
positioned`
<p>This is an example:${M} </p>
`,
{
ruleId: 'spelling',
nodeType: 'html',
message: 'tags should not contain trailing whitespace',
}
);
});

it('negative', async function () {
await assertLintFree(`
<p>
Expand Down Expand Up @@ -241,6 +267,9 @@ windows:${M}\r
<td> Also to pad them a bit, in some cases. </td>
</tr></table>
<p>
Paragraphs with the open/close tags on their own line are OK.
</p>
`);
});
});
4 changes: 2 additions & 2 deletions test/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('linting whole program', function () {
it('error in inline grammar', async function () {
await assertLint(
positioned`
<emu-grammar type="definition"> Statement[${M}a]: \`;\` </emu-grammar>
<emu-grammar type="definition">Statement[${M}a]: \`;\`</emu-grammar>
`,
{
ruleId: 'grammarkdown:2008',
Expand Down Expand Up @@ -91,7 +91,7 @@ describe('linting whole program', function () {
it('error in inline grammar', async function () {
await assertLint(
positioned`
<emu-grammar> ${M}Statement: EmptyStatement </emu-grammar>
<emu-grammar>${M}Statement: EmptyStatement</emu-grammar>
<emu-alg>
1. Return Foo.
</emu-alg>
Expand Down

0 comments on commit 55e5f70

Please sign in to comment.