Skip to content

Commit

Permalink
refactor: reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
KamiKillertO committed Sep 2, 2020
1 parent e430c94 commit 5ca9be3
Show file tree
Hide file tree
Showing 4 changed files with 429 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/messages.js
Expand Up @@ -70,6 +70,7 @@ const errors = {
E061: (/* data */) => "WCAG rule 78: Each button element must contain content.",
E062: (/* data */) => "WCAG rule 74: The label element should not encapsulate select and textarea elements.",
E063: (/* data */) => "WCAG rule 73: Each fieldset element should contain a legend element.",
E064: (data) => `Unexpected space ${data.is_before ? "before" : "after"} text.`,

INLINE_01: ({ instruction }) => `unrecognized linthtml instruction: \`linthtml-${instruction}\``,
INLINE_02: ({ rule_name }) => `unrecognized rule name \`${rule_name}\` in linthtml-configure instruction`,
Expand Down
76 changes: 76 additions & 0 deletions lib/rules/no-surrounding-whitespace/README.md
@@ -0,0 +1,76 @@
# no-surrounding-space

Disallow extra spacing just after a tag is opened and just before a tag is closed.


## The following patterns are considered violations

```html
<h1> Lorem ipsum</h1>
```

```html
<h1>Lorem ipsum </h1>
```

```html
<h1> Lorem ipsum </h1>
```

```html
<p> <strong>Lorem</strong> ipsum</p>
```

```html
<p> <!-- comment --><strong>Lorem</strong> ipsum</p>
```

```html
<div> </div>
```

## The following patterns are not considered violations

```html
<h1>Lorem ipsum</h1>
```

```html
<p><strong>Lorem</strong> ipsum</p>
```

```html
<p><!-- comment --><strong>Lorem</strong> ipsum</p>
```
```html
<p>Lorem <strong>ipsum</strong></p>
```

```html
<div>
<p>Lorem ipsum dolor sit amet...</p>
</div>
```

```html
<div>
<p>
Lorem ipsum dolor sit amet...
</p>
</div>
```

```html
<div>
<p>
<!-- comment -->
Lorem ipsum dolor sit amet...
<!-- comment -->
</p>
</div>
```

```html
<div></div>
```

222 changes: 222 additions & 0 deletions lib/rules/no-surrounding-whitespace/__tests__/index.js
@@ -0,0 +1,222 @@
const { expect } = require("chai");
const linthtml = require("../../../index");
const none = require("../../../presets").presets.none;

describe("legacy linter | no-surrounding-whitespace", function() {
function createLinter(config) {
return new linthtml.LegacyLinter(linthtml.rules, none, config);
}

it("Should report an error when there's whitespaces at the start of a text node", async function() {
const linter = createLinter({ "no-surrounding-whitespace": true });
const html = "<p> foo</p>";

const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
expect(issues[0].position)
.to
.deep
.equal({
start: {
line: 1,
column: 4
},
end: {
line: 1,
column: 6
}
});
});

it("Should report an error when there's whitespaces at the end of a text node", async function() {
const linter = createLinter({ "no-surrounding-whitespace": true });
const html = "<p>foo </p>";

const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
expect(issues[0].position)
.to
.deep
.equal({
start: {
line: 1,
column: 7
},
end: {
line: 1,
column: 9
}
});
});

it("Should not report an error when there's whitespaces before a sibling", async function() {
const linter = createLinter({ "no-surrounding-whitespace": true });
const html = "<p>foo <strong>bar</strong></p>";

const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should not report an error when there's whitespaces after a sibling", async function() {
const linter = createLinter({ "no-surrounding-whitespace": true });
const html = "<p><strong>foo</strong> bar</p>";

const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("should not report an error for indentation text node", async function() {
const linter = createLinter({ "no-surrounding-whitespace": true });
const html = `
<div>
<p>foo</p>
bar
</div>
`;

const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("should report an error", async function() {
const linter = createLinter({ "no-surrounding-whitespace": true });
const html = "<p> <strong>Lorem</strong> ipsum</p>";

const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
expect(issues[0].position)
.to
.deep
.equal({
start: {
line: 1,
column: 4
},
end: {
line: 1,
column: 5
}
});
});

it("Should no report an error", async function() {
const linter = createLinter({ "no-surrounding-whitespace": true });
const html = [
"<div>",
" <p>Lorem ipsum</p>",
"</div>"
].join("\n");

const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});
});
describe("no-surrounding-whitespace", function() {
function createLinter(rules) {
return linthtml.fromConfig({ rules });
}

it("Should report an error when there's whitespaces at the start of a text node", async function() {
const linter = createLinter({ "no-surrounding-whitespace": true });
const html = "<p> foo</p>";

const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
expect(issues[0].position)
.to
.deep
.equal({
start: {
line: 1,
column: 4
},
end: {
line: 1,
column: 6
}
});
});

it("Should report an error when there's whitespaces at the end of a text node", async function() {
const linter = createLinter({ "no-surrounding-whitespace": true });
const html = "<p>foo </p>";

const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
expect(issues[0].position)
.to
.deep
.equal({
start: {
line: 1,
column: 7
},
end: {
line: 1,
column: 9
}
});
});

it("Should not report an error when there's whitespaces before a sibling", async function() {
const linter = createLinter({ "no-surrounding-whitespace": true });
const html = "<p>foo <strong>bar</strong></p>";

const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should not report an error when there's whitespaces after a sibling", async function() {
const linter = createLinter({ "no-surrounding-whitespace": true });
const html = "<p><strong>foo</strong> bar</p>";

const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("should not report an error for indentation text node", async function() {
const linter = createLinter({ "no-surrounding-whitespace": true });
const html = `
<div>
<p>foo</p>
bar
</div>
`;

const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("should report an error", async function() {
const linter = createLinter({ "no-surrounding-whitespace": true });
const html = "<p> <strong>Lorem</strong> ipsum</p>";

const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
expect(issues[0].position)
.to
.deep
.equal({
start: {
line: 1,
column: 4
},
end: {
line: 1,
column: 5
}
});
});

it("Should no report an error", async function() {
const linter = createLinter({ "no-surrounding-whitespace": true });
const html = [
"<div>",
" <p>Lorem ipsum</p>",
"</div>"
].join("\n");

const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});
});

0 comments on commit 5ca9be3

Please sign in to comment.