Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable validation with magic comments #3351

Merged
merged 2 commits into from
Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions site/content/docs/02-template-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ Text can also contain JavaScript expressions:
```


### Comments

---

You can use HTML comments inside components.

```html
<!-- this is a comment! -->
<h1>Hello world</h1>
```

---

Comments beginning with `svelte-ignore` disable warnings for the next block of markup. Usually these are accessibility warnings; make sure that you're disabling them for a good reason.

```html
<!-- svelte-ignore a11y-autofocus -->
<input bind:value={name} autofocus>
```


### {#if ...}

```sv
Expand Down
18 changes: 18 additions & 0 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import get_object from './utils/get_object';
import unwrap_parens from './utils/unwrap_parens';
import Slot from './nodes/Slot';
import { Node as ESTreeNode } from 'estree';
import add_to_set from './utils/add_to_set';

interface ComponentOptions {
namespace?: string;
Expand Down Expand Up @@ -70,6 +71,8 @@ function remove_node(code: MagicString, start: number, end: number, body: Node,
export default class Component {
stats: Stats;
warnings: Warning[];
ignores: Set<string>;
ignore_stack: Set<string>[] = [];

ast: Ast;
source: string;
Expand Down Expand Up @@ -442,6 +445,10 @@ export default class Component {
message: string;
}
) {
if (this.ignores && this.ignores.has(warning.code)) {
return;
}

if (!this.locator) {
this.locator = getLocator(this.source, { offsetLine: 1 });
}
Expand Down Expand Up @@ -1253,6 +1260,17 @@ export default class Component {
message
});
}

push_ignores(ignores) {
this.ignores = new Set(this.ignores || []);
add_to_set(this.ignores, ignores);
this.ignore_stack.push(this.ignores);
}

pop_ignores() {
this.ignore_stack.pop();
this.ignores = this.ignore_stack[this.ignore_stack.length - 1];
}
}

function process_component_options(component: Component, nodes) {
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/compile/nodes/Comment.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import Node from './shared/Node';

const pattern = /^\s*svelte-ignore\s+([\s\S]+)\s*$/m;

export default class Comment extends Node {
type: 'Comment';
data: string;
ignores: string[];

constructor(component, parent, scope, info) {
super(component, parent, scope, info);
this.data = info.data;

const match = pattern.exec(this.data);
this.ignores = match ? match[1].split(/[^\S]/).map(x => x.trim()).filter(Boolean) : [];
}
}
11 changes: 11 additions & 0 deletions src/compiler/compile/nodes/shared/map_children.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,20 @@ function get_constructor(type) {

export default function map_children(component, parent, scope, children: Node[]) {
let last = null;
let ignores = [];

return children.map(child => {
const constructor = get_constructor(child.type);

const use_ignores = child.type !== 'Text' && child.type !== 'Comment' && ignores.length;

if (use_ignores) component.push_ignores(ignores);
const node = new constructor(component, parent, scope, child);
if (use_ignores) component.pop_ignores(), ignores = [];

if (node.type === 'Comment' && node.ignores.length) {
ignores.push(...node.ignores);
}

if (last) last.next = node;
node.prev = last;
Expand Down
7 changes: 7 additions & 0 deletions test/validator/samples/ignore-warning/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- svelte-ignore a11y-missing-attribute -->
<div>
<img src="this-is-fine.jpg">
<marquee>but this is still discouraged</marquee>
</div>

<img src="potato.jpg">
32 changes: 32 additions & 0 deletions test/validator/samples/ignore-warning/warnings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"code": "a11y-distracting-elements",
"end": {
"character": 131,
"column": 49,
"line": 4
},
"message": "A11y: Avoid <marquee> elements",
"pos": 83,
"start": {
"character": 83,
"column": 1,
"line": 4
}
},
{
"code": "a11y-missing-attribute",
"end": {
"character": 162,
"column": 22,
"line": 7
},
"message": "A11y: <img> element should have an alt attribute",
"pos": 140,
"start": {
"character": 140,
"column": 0,
"line": 7
}
}
]
17 changes: 17 additions & 0 deletions test/validator/samples/ignore-warnings-cumulative/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- svelte-ignore a11y-structure a11y-missing-attribute -->
<div>
<figure>
<img src="potato.jpg">
<marquee>
<figcaption>potato</figcaption>
</marquee>
</figure>

<!-- svelte-ignore a11y-distracting-elements -->
<figure>
<img src="potato.jpg">
<marquee>
<figcaption>potato</figcaption>
</marquee>
</figure>
</div>
17 changes: 17 additions & 0 deletions test/validator/samples/ignore-warnings-cumulative/warnings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"code": "a11y-distracting-elements",
"end": {
"character": 161,
"column": 12,
"line": 7
},
"message": "A11y: Avoid <marquee> elements",
"pos": 104,
"start": {
"character": 104,
"column": 2,
"line": 5
}
}
]
8 changes: 8 additions & 0 deletions test/validator/samples/ignore-warnings-newline/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- svelte-ignore a11y-missing-attribute
a11y-distracting-elements -->
<div>
<img src="this-is-fine.jpg">
<marquee>but this is still discouraged</marquee>
</div>

<img src="potato.jpg">
17 changes: 17 additions & 0 deletions test/validator/samples/ignore-warnings-newline/warnings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"code": "a11y-missing-attribute",
"end": {
"character": 207,
"column": 22,
"line": 8
},
"message": "A11y: <img> element should have an alt attribute",
"pos": 185,
"start": {
"character": 185,
"column": 0,
"line": 8
}
}
]
8 changes: 8 additions & 0 deletions test/validator/samples/ignore-warnings-stacked/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- svelte-ignore a11y-missing-attribute -->
<!-- svelte-ignore a11y-distracting-elements -->
<div>
<img src="this-is-fine.jpg">
<marquee>but this is still discouraged</marquee>
</div>

<img src="potato.jpg">
17 changes: 17 additions & 0 deletions test/validator/samples/ignore-warnings-stacked/warnings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"code": "a11y-missing-attribute",
"end": {
"character": 211,
"column": 22,
"line": 8
},
"message": "A11y: <img> element should have an alt attribute",
"pos": 189,
"start": {
"character": 189,
"column": 0,
"line": 8
}
}
]
7 changes: 7 additions & 0 deletions test/validator/samples/ignore-warnings/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- svelte-ignore a11y-missing-attribute a11y-distracting-elements -->
<div>
<img src="this-is-fine.jpg">
<marquee>but this is still discouraged</marquee>
</div>

<img src="potato.jpg">
17 changes: 17 additions & 0 deletions test/validator/samples/ignore-warnings/warnings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"code": "a11y-missing-attribute",
"end": {
"character": 188,
"column": 22,
"line": 7
},
"message": "A11y: <img> element should have an alt attribute",
"pos": 166,
"start": {
"character": 166,
"column": 0,
"line": 7
}
}
]