Skip to content

[fix] be able to silence more warnings #6504

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

Merged
merged 9 commits into from
Jul 14, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* Support `bind:group` in SSR ([#4621](https://github.com/sveltejs/svelte/pull/4621))
* Add sourcemaps to html elements ([#6427](https://github.com/sveltejs/svelte/pull/6427))
* Make `<script>` and `<style>` end tag parsing more robust ([#6511](https://github.com/sveltejs/svelte/pull/6511))
* Make it possible to silence more warnings ([#5954](https://github.com/sveltejs/svelte/issues/5954))

## 3.38.3

Expand Down
19 changes: 19 additions & 0 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { DecodedSourceMap, RawSourceMap } from '@ampproject/remapping/dist/types
import { clone } from '../utils/clone';
import compiler_warnings from './compiler_warnings';
import compiler_errors from './compiler_errors';
import { extract_ignores_above_position, extract_svelte_ignore_from_comments } from '../utils/extract_svelte_ignore';

interface ComponentOptions {
namespace?: string;
Expand Down Expand Up @@ -171,12 +172,17 @@ export default class Component {
}

this.walk_module_js();

this.push_ignores(this.ast.instance ? extract_ignores_above_position(this.ast.instance.start, this.ast.html.children) : []);
this.walk_instance_js_pre_template();
this.pop_ignores();

this.fragment = new Fragment(this, ast.html);
this.name = this.get_unique_name(name);

this.push_ignores(this.ast.instance ? extract_ignores_above_position(this.ast.instance.start, this.ast.html.children) : []);
this.walk_instance_js_post_template();
this.pop_ignores();

this.elements.forEach(element => this.stylesheet.apply(element));
if (!compile_options.customElement) this.stylesheet.reify();
Expand Down Expand Up @@ -476,6 +482,14 @@ export default class Component {
}

extract_exports(node) {
const ignores = extract_svelte_ignore_from_comments(node);
if (ignores.length) this.push_ignores(ignores);
const result = this._extract_exports(node);
if (ignores.length) this.pop_ignores();
return result;
}

private _extract_exports(node) {
if (node.type === 'ExportDefaultDeclaration') {
this.error(node, compiler_errors.default_export);
}
Expand Down Expand Up @@ -1169,6 +1183,9 @@ export default class Component {
}> = [];

this.ast.instance.content.body.forEach(node => {
const ignores = extract_svelte_ignore_from_comments(node);
if (ignores.length) this.push_ignores(ignores);

if (node.type === 'LabeledStatement' && node.label.name === '$') {
this.reactive_declaration_nodes.add(node);

Expand Down Expand Up @@ -1246,6 +1263,8 @@ export default class Component {
declaration
});
}

if (ignores.length) this.pop_ignores();
});

const lookup = new Map();
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/compile/css/Stylesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Component from '../Component';
import { CssNode } from './interfaces';
import hash from '../utils/hash';
import compiler_warnings from '../compiler_warnings';
import { extract_ignores_above_position } from '../../utils/extract_svelte_ignore';

function remove_css_prefix(name: string): string {
return name.replace(/^-((webkit)|(moz)|(o)|(ms))-/, '');
Expand Down Expand Up @@ -447,10 +448,13 @@ export default class Stylesheet {
}

warn_on_unused_selectors(component: Component) {
const ignores = !this.ast.css ? [] : extract_ignores_above_position(this.ast.css.start, this.ast.html.children);
component.push_ignores(ignores);
this.children.forEach(child => {
child.warn_on_unused_selector((selector: Selector) => {
component.warn(selector.node, compiler_warnings.css_unused_selector(this.source.slice(selector.node.start, selector.node.end)));
});
});
component.pop_ignores();
}
}
6 changes: 1 addition & 5 deletions src/compiler/compile/nodes/Comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import Component from '../Component';
import Node from './shared/Node';
import TemplateScope from './shared/TemplateScope';

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

export default class Comment extends Node {
type: 'Comment';
data: string;
Expand All @@ -13,8 +11,6 @@ export default class Comment extends Node {
constructor(component: Component, parent: Node, scope: TemplateScope, info: TemplateNode) {
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) : [];
this.ignores = info.ignores;
}
}
16 changes: 2 additions & 14 deletions src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Block from './Block';
import { ClassDeclaration, FunctionExpression, Node, Statement, ObjectExpression, Expression } from 'estree';
import { apply_preprocessor_sourcemap } from '../../utils/mapped_code';
import { RawSourceMap, DecodedSourceMap } from '@ampproject/remapping/dist/types/types';
import { flatten } from '../../utils/flatten';

export default function dom(
component: Component,
Expand Down Expand Up @@ -550,18 +551,5 @@ export default function dom(
body.push(declaration);
}

return { js: flatten(body, []), css };
}

function flatten(nodes: any[], target: any[]) {
for (let i = 0; i < nodes.length; i += 1) {
const node = nodes[i];
if (Array.isArray(node)) {
flatten(node, target);
} else {
target.push(node);
}
}

return target;
return { js: flatten(body), css };
}
9 changes: 8 additions & 1 deletion src/compiler/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export interface MustacheTag extends BaseNode {
expression: Node;
}

export interface Comment extends BaseNode {
type: 'Comment';
data: string;
ignores: string[];
}

export type DirectiveType = 'Action'
| 'Animation'
| 'Binding'
Expand Down Expand Up @@ -52,7 +58,8 @@ export type TemplateNode = Text
| MustacheTag
| BaseNode
| Directive
| Transition;
| Transition
| Comment;

export interface Parser {
readonly template: string;
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/parse/state/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { is_void } from '../../utils/names';
import { Parser } from '../index';
import { Directive, DirectiveType, TemplateNode, Text } from '../../interfaces';
import fuzzymatch from '../../utils/fuzzymatch';
import { extract_svelte_ignore } from '../../utils/extract_svelte_ignore';
import parser_errors from '../errors';

// eslint-disable-next-line no-useless-escape
Expand Down Expand Up @@ -64,7 +65,8 @@ export default function tag(parser: Parser) {
start,
end: parser.index,
type: 'Comment',
data
data,
ignores: extract_svelte_ignore(data)
});

return;
Expand Down
34 changes: 34 additions & 0 deletions src/compiler/utils/extract_svelte_ignore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { TemplateNode } from '../interfaces';
import { flatten } from './flatten';

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

export function extract_svelte_ignore(text: string): string[] {
const match = pattern.exec(text);
return match ? match[1].split(/[^\S]/).map(x => x.trim()).filter(Boolean) : [];
}

export function extract_svelte_ignore_from_comments<Node extends { leadingComments?: Array<{value: string}> }>(node: Node): string[] {
return flatten((node.leadingComments || []).map(comment => extract_svelte_ignore(comment.value)));
}

export function extract_ignores_above_position(position: number, template_nodes: TemplateNode[]): string[] {
const previous_node_idx = template_nodes.findIndex(child => child.end === position);
if (previous_node_idx === -1) {
return [];
}

for (let i = previous_node_idx; i >= 0; i--) {
const node = template_nodes[i];
if (node.type !== 'Comment' && node.type !== 'Text') {
return [];
}
if (node.type === 'Comment') {
if (node.ignores.length) {
return node.ignores;
}
}
}

return [];
}
14 changes: 14 additions & 0 deletions src/compiler/utils/flatten.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function flatten<T>(nodes: T[][], target?: T[]): T[];
export function flatten<T>(nodes: T[], target?: T[]): T[];
export function flatten(nodes: any[], target: any[] = []): any[] {
for (let i = 0; i < nodes.length; i += 1) {
const node = nodes[i];
if (Array.isArray(node)) {
flatten(node, target);
} else {
target.push(node);
}
}

return target;
}
1 change: 1 addition & 0 deletions test/parser/samples/comment-with-ignores/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- svelte-ignore foo bar -->
16 changes: 16 additions & 0 deletions test/parser/samples/comment-with-ignores/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"html": {
"start": 0,
"end": 30,
"type": "Fragment",
"children": [
{
"start": 0,
"end": 30,
"type": "Comment",
"data": " svelte-ignore foo bar ",
"ignores": ["foo", "bar"]
}
]
}
}
3 changes: 2 additions & 1 deletion test/parser/samples/comment/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"start": 0,
"end": 18,
"type": "Comment",
"data": " a comment "
"data": " a comment ",
"ignores": []
}
]
}
Expand Down
9 changes: 9 additions & 0 deletions test/validator/samples/silence-warnings-2/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script context="module">
let foo;
</script>

<!-- svelte-ignore unused-export-let module-script-reactive-declaration -->
<script>
export let unused;
$: reactive = foo;
</script>
1 change: 1 addition & 0 deletions test/validator/samples/silence-warnings-2/warnings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
15 changes: 15 additions & 0 deletions test/validator/samples/silence-warnings/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script context="module">
let foo;
</script>

<script>
// svelte-ignore unused-export-let
export let unused;
// svelte-ignore module-script-reactive-declaration
$: reactive = foo;
</script>

<!-- svelte-ignore css-unused-selector -->
<style>
.unused {}
</style>
1 change: 1 addition & 0 deletions test/validator/samples/silence-warnings/warnings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]