Skip to content

Commit 03b283b

Browse files
committed
Sync with template-lint
1 parent 90b7967 commit 03b283b

3 files changed

Lines changed: 69 additions & 135 deletions

File tree

docs/rules/template-no-whitespace-for-layout.md

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,34 @@
22

33
<!-- end auto-generated rule header -->
44

5-
Disallow using multiple consecutive spaces for layout purposes in templates. CSS should be used for spacing and layout instead.
5+
Formatting of text through the use of multiple whitespace is entirely visual, and therefore is incompatible with screen-reading assistive technology tools.
66

7-
## Rule Details
8-
9-
This rule discourages the use of multiple consecutive spaces (2 or more) for layout purposes in templates. CSS should be used for spacing and layout instead.
7+
The rule applies to the content of Handlebars AST TextNodes, and performs a RegExp search for two consecutive white space characters that might indicate the use of whitespace used for layout.
108

119
## Examples
1210

13-
Examples of **incorrect** code for this rule:
14-
15-
```gjs
16-
<template>
17-
<div>Hello World</div>
18-
</template>
19-
```
20-
21-
```gjs
22-
<template>
23-
<div>Text with spaces</div>
24-
</template>
25-
```
11+
This rule **forbids** the following:
2612

2713
```gjs
2814
<template>
29-
<div>Multiple spaces</div>
15+
Mon.&nbsp;&nbsp;&nbsp;&nbsp;Eggs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tomato soup&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;House salad<br>
16+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bacon&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hamburger&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fried chicken<br>
17+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Toast&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Onion rings&nbsp;&nbsp;&nbsp;&nbsp;Green beans<br>
18+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cookie&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mashed potatoes
3019
</template>
3120
```
3221

33-
Examples of **correct** code for this rule:
22+
This rule **allows** the following:
3423

3524
```gjs
3625
<template>
37-
<div>Hello World</div>
26+
<p>Start to finish</p>
3827
</template>
3928
```
4029

4130
```gjs
4231
<template>
43-
<div class="spaced-layout">Text with proper spacing</div>
32+
<p>Start&nbsp;to&nbsp;Finish</p>
4433
</template>
4534
```
4635

@@ -53,4 +42,5 @@ To fix issues caused by using whitespace for layout, the following are recommend
5342

5443
## References
5544

56-
- [eslint-plugin-ember template-no-whitespace-for-layout](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/template-no-whitespace-for-layout.md)
45+
- [F33: Using white space characters to create multiple columns in plain text content](https://www.w3.org/TR/WCAG20-TECHS/failures.html#F33)
46+
- [F34: Using white space characters to format tables in plain text content](https://www.w3.org/TR/WCAG20-TECHS/failures.html#F34)

lib/rules/template-no-whitespace-for-layout.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/** @type {import('eslint').Rule.RuleModule} */
2+
const ERROR_MESSAGE = 'Excess whitespace detected.';
3+
24
module.exports = {
35
meta: {
46
type: 'suggestion',
@@ -10,8 +12,7 @@ module.exports = {
1012
},
1113
schema: [],
1214
messages: {
13-
noWhitespaceForLayout:
14-
'Unexpected use of whitespace for layout. Use CSS for spacing instead of multiple spaces.',
15+
noWhitespaceForLayout: ERROR_MESSAGE,
1516
},
1617
originallyFrom: {
1718
name: 'ember-template-lint',

tests/lib/rules/template-no-whitespace-for-layout.js

Lines changed: 54 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,64 @@
55
const rule = require('../../../lib/rules/template-no-whitespace-for-layout');
66
const RuleTester = require('eslint').RuleTester;
77

8-
const ruleTester = new RuleTester({
9-
parser: require.resolve('ember-eslint-parser'),
10-
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
11-
});
12-
13-
ruleTester.run('template-no-whitespace-for-layout', rule, {
14-
valid: [
15-
'<template><div>Hello World</div></template>',
16-
'<template><div>Text</div></template>',
17-
18-
'<template>Start to finish</template>',
19-
'<template>Start&nbsp;to&nbsp;finish</template>',
20-
'<template>Start<br>to<br>finish</template>',
21-
`<template><div>
8+
const validHbs = [
9+
'Start to finish',
10+
'Start&nbsp;to&nbsp;finish',
11+
'Start<br>to<br>finish',
12+
`<div>
2213
example
23-
</div></template>`,
24-
`<template><div
14+
</div>`,
15+
`<div
2516
foo="bar"
2617
baz="qux"
2718
>
2819
example
29-
</div></template>`,
30-
],
31-
invalid: [
32-
{
33-
code: '<template><div>Hello World</div></template>',
34-
output: null,
35-
errors: [{ messageId: 'noWhitespaceForLayout' }],
36-
},
37-
{
38-
code: '<template><div>Text with spaces</div></template>',
39-
output: null,
40-
errors: [{ messageId: 'noWhitespaceForLayout' }],
41-
},
42-
{
43-
code: '<template><div>Multiple spaces</div></template>',
44-
output: null,
45-
errors: [{ messageId: 'noWhitespaceForLayout' }],
46-
},
20+
</div>`,
21+
];
22+
23+
const invalidHbs = [
24+
{
25+
code: 'START FINISH',
26+
output: null,
27+
errors: [{ message: 'Excess whitespace detected.' }],
28+
},
29+
{
30+
code: 'START&nbsp;&nbsp;FINISH',
31+
output: null,
32+
errors: [{ message: 'Excess whitespace detected.' }],
33+
},
34+
{
35+
code: 'START&nbsp; FINISH',
36+
output: null,
37+
errors: [{ message: 'Excess whitespace detected.' }],
38+
},
39+
{
40+
code: 'START &nbsp;FINISH',
41+
output: null,
42+
errors: [{ message: 'Excess whitespace detected.' }],
43+
},
44+
];
45+
46+
function wrapTemplate(entry) {
47+
if (typeof entry === 'string') {
48+
return `<template>${entry}</template>`;
49+
}
4750

48-
{
49-
code: '<template>START FINISH</template>',
50-
output: null,
51-
errors: [{ messageId: 'noWhitespaceForLayout' }],
52-
},
53-
{
54-
code: '<template>START&nbsp;&nbsp;FINISH</template>',
55-
output: null,
56-
errors: [{ messageId: 'noWhitespaceForLayout' }],
57-
},
58-
{
59-
code: '<template>START&nbsp; FINISH</template>',
60-
output: null,
61-
errors: [{ messageId: 'noWhitespaceForLayout' }],
62-
},
63-
{
64-
code: '<template>START &nbsp;FINISH</template>',
65-
output: null,
66-
errors: [{ messageId: 'noWhitespaceForLayout' }],
67-
},
68-
],
51+
return {
52+
...entry,
53+
code: `<template>${entry.code}</template>`,
54+
output: entry.output ? `<template>${entry.output}</template>` : entry.output,
55+
};
56+
}
57+
58+
const gjsRuleTester = new RuleTester({
59+
parser: require.resolve('ember-eslint-parser'),
60+
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
61+
});
62+
63+
gjsRuleTester.run('template-no-whitespace-for-layout', rule, {
64+
valid: validHbs.map(wrapTemplate),
65+
invalid: invalidHbs.map(wrapTemplate),
6966
});
7067

7168
const hbsRuleTester = new RuleTester({
@@ -77,60 +74,6 @@ const hbsRuleTester = new RuleTester({
7774
});
7875

7976
hbsRuleTester.run('template-no-whitespace-for-layout', rule, {
80-
valid: [
81-
'Start to finish',
82-
'Start&nbsp;to&nbsp;finish',
83-
'Start<br>to<br>finish',
84-
`<div>
85-
example
86-
</div>`,
87-
`<div
88-
foo="bar"
89-
baz="qux"
90-
>
91-
example
92-
</div>`,
93-
],
94-
invalid: [
95-
{
96-
code: 'START FINISH',
97-
output: null,
98-
errors: [
99-
{
100-
message:
101-
'Unexpected use of whitespace for layout. Use CSS for spacing instead of multiple spaces.',
102-
},
103-
],
104-
},
105-
{
106-
code: 'START&nbsp;&nbsp;FINISH',
107-
output: null,
108-
errors: [
109-
{
110-
message:
111-
'Unexpected use of whitespace for layout. Use CSS for spacing instead of multiple spaces.',
112-
},
113-
],
114-
},
115-
{
116-
code: 'START&nbsp; FINISH',
117-
output: null,
118-
errors: [
119-
{
120-
message:
121-
'Unexpected use of whitespace for layout. Use CSS for spacing instead of multiple spaces.',
122-
},
123-
],
124-
},
125-
{
126-
code: 'START &nbsp;FINISH',
127-
output: null,
128-
errors: [
129-
{
130-
message:
131-
'Unexpected use of whitespace for layout. Use CSS for spacing instead of multiple spaces.',
132-
},
133-
],
134-
},
135-
],
77+
valid: validHbs,
78+
invalid: invalidHbs,
13679
});

0 commit comments

Comments
 (0)