Skip to content

chore: better ignore code handling #11606

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 1 commit into from
May 14, 2024
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filename, locator, warnings } from './state.js';
import { filename, locator, warnings, ignore_stack } from './state.js';

/** @typedef {{ start?: number, end?: number }} NodeLike */

Expand All @@ -8,8 +8,7 @@ import { filename, locator, warnings } from './state.js';
* @param {string} message
*/
function w(node, code, message) {
// @ts-expect-error
if (node?.ignores?.has(code)) return;
if (ignore_stack.at(-1)?.has(code)) return;

warnings.push({
code,
Expand Down
3 changes: 0 additions & 3 deletions packages/svelte/src/compiler/phases/2-analyze/a11y.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,6 @@ function check_element(node, state) {
for (const attribute of node.attributes) {
if (attribute.type !== 'Attribute') continue;

// @ts-expect-error gross hack
attribute.ignores = node.ignores;

const name = attribute.name.toLowerCase();
// aria-props
if (name.startsWith('aria-')) {
Expand Down
99 changes: 41 additions & 58 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { prune } from './css/css-prune.js';
import { hash } from './utils.js';
import { warn_unused } from './css/css-warn.js';
import { extract_svelte_ignore } from '../../utils/extract_svelte_ignore.js';
import { pop_ignore, push_ignore } from '../../state.js';

/**
* @param {import('#compiler').Script | null} script
Expand Down Expand Up @@ -439,8 +440,7 @@ export function analyze_component(root, source, options) {
component_slots: new Set(),
expression: null,
private_derived_state: [],
function_depth: scope.function_depth,
ignores: new Set()
function_depth: scope.function_depth
};

walk(
Expand Down Expand Up @@ -511,8 +511,7 @@ export function analyze_component(root, source, options) {
component_slots: new Set(),
expression: null,
private_derived_state: [],
function_depth: scope.function_depth,
ignores: new Set()
function_depth: scope.function_depth
};

walk(
Expand Down Expand Up @@ -1100,67 +1099,51 @@ function is_safe_identifier(expression, scope) {

/** @type {import('./types').Visitors} */
const common_visitors = {
_(node, context) {
// @ts-expect-error
const comments = /** @type {import('estree').Comment[]} */ (node.leadingComments);

if (comments) {
_(node, { state, next, path }) {
const parent = path.at(-1);
if (parent?.type === 'Fragment' && node.type !== 'Comment' && node.type !== 'Text') {
const idx = parent.nodes.indexOf(/** @type {any} */ (node));
/** @type {string[]} */
const ignores = [];

for (const comment of comments) {
const start = /** @type {any} */ (comment).start + 2;
ignores.push(...extract_svelte_ignore(start, comment.value, context.state.analysis.runes));
for (let i = idx - 1; i >= 0; i--) {
const prev = parent.nodes[i];
if (prev.type === 'Comment') {
ignores.push(
...extract_svelte_ignore(
prev.start + 2 /* '//'.length */,
prev.data,
state.analysis.runes
)
);
} else if (prev.type !== 'Text') {
break;
}
}

if (ignores.length > 0) {
// @ts-expect-error see below
node.ignores = new Set([...context.state.ignores, ...ignores]);
push_ignore(ignores);
next();
pop_ignore();
}
}

// @ts-expect-error
if (node.ignores) {
context.next({
...context.state,
// @ts-expect-error see below
ignores: node.ignores
});
} else if (context.state.ignores.size > 0) {
// @ts-expect-error
node.ignores = context.state.ignores;
}
},
Fragment(node, context) {
/** @type {string[]} */
let ignores = [];

for (const child of node.nodes) {
if (child.type === 'Text' && child.data.trim() === '') {
continue;
}

if (child.type === 'Comment') {
const start =
child.start +
(context.state.analysis.source.slice(child.start, child.start + 4) === '<!--' ? 4 : 2);

ignores.push(...extract_svelte_ignore(start, child.data, context.state.analysis.runes));
} else {
const combined_ignores = new Set(context.state.ignores);
for (const ignore of ignores) combined_ignores.add(ignore);

if (combined_ignores.size > 0) {
// TODO this is a grotesque hack that's made necessary by the fact that
// we can't call `context.visit(...)` here, because we do the convoluted
// visitor merging thing. I'm increasingly of the view that we should
// rearchitect this stuff and have a single visitor per node. It'd be
// more efficient and much simpler.
// @ts-expect-error
child.ignores = combined_ignores;
} else {
const comments = /** @type {any} */ (node).leadingComments;
if (comments) {
/** @type {string[]} */
const ignores = [];
for (const comment of comments) {
ignores.push(
...extract_svelte_ignore(
comment.start + 4 /* '<!--'.length */,
comment.value,
state.analysis.runes
)
);
}
if (ignores.length > 0) {
push_ignore(ignores);
next();
pop_ignore();
}

ignores = [];
}
}
},
Expand Down
1 change: 0 additions & 1 deletion packages/svelte/src/compiler/phases/2-analyze/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export interface AnalysisState {
expression: ExpressionTag | ClassDirective | SpreadAttribute | null;
private_derived_state: string[];
function_depth: number;
ignores: Set<string>;
}

export interface LegacyAnalysisState extends AnalysisState {
Expand Down
16 changes: 16 additions & 0 deletions packages/svelte/src/compiler/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ export let filename;

export let locator = getLocator('', { offsetLine: 1 });

/** @type {Set<string>[]} */
export let ignore_stack = [];

/**
* @param {string[]} ignores
*/
export function push_ignore(ignores) {
const next = new Set([...(ignore_stack.at(-1) || []), ...ignores]);
ignore_stack.push(next);
}

export function pop_ignore() {
ignore_stack.pop();
}

/**
* @param {{
* source: string;
Expand All @@ -20,4 +35,5 @@ export function reset(options) {
filename = options.filename;
locator = getLocator(options.source, { offsetLine: 1 });
warnings = [];
ignore_stack = [];
}
5 changes: 2 additions & 3 deletions packages/svelte/src/compiler/warnings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* This file is generated by scripts/process-messages/index.js. Do not edit! */

import { filename, locator, warnings } from './state.js';
import { filename, locator, warnings, ignore_stack } from './state.js';

/** @typedef {{ start?: number, end?: number }} NodeLike */
/**
Expand All @@ -9,8 +9,7 @@ import { filename, locator, warnings } from './state.js';
* @param {string} message
*/
function w(node, code, message) {
// @ts-expect-error
if (node?.ignores?.has(code)) return;
if (ignore_stack.at(-1)?.has(code)) return;

warnings.push({
code,
Expand Down
84 changes: 42 additions & 42 deletions packages/svelte/tests/validator/samples/unknown-code/warnings.json
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
[
{
"code": "legacy_code",
"end": {
"column": 41,
"line": 3
},
"message": "`a11y-missing-attribute` is no longer valid — please use `a11y_missing_attribute` instead",
"start": {
"column": 19,
"line": 3
"line": 3,
"column": 17
},
"end": {
"line": 3,
"column": 39
}
},
{
"code": "unknown_code",
"end": {
"column": 41,
"line": 8
},
"message": "`ally_missing_attribute` is not a recognised code (did you mean `a11y_missing_attribute`?)",
"code": "a11y_missing_attribute",
"message": "`<img>` element should have an alt attribute",
"start": {
"column": 19,
"line": 8
"line": 5,
"column": 1
},
"end": {
"line": 5,
"column": 29
}
},
{
"code": "legacy_code",
"end": {
"column": 39,
"line": 13
},
"message": "`a11y-misplaced-scope` is no longer valid — please use `a11y_misplaced_scope` instead",
"code": "unknown_code",
"message": "`ally_missing_attribute` is not a recognised code (did you mean `a11y_missing_attribute`?)",
"start": {
"column": 19,
"line": 13
"line": 8,
"column": 17
},
"end": {
"line": 8,
"column": 39
}
},
{
"code": "a11y_missing_attribute",
"end": {
"column": 29,
"line": 5
},
"message": "`<img>` element should have an alt attribute",
"start": {
"column": 1,
"line": 5
"line": 10,
"column": 1
},
"end": {
"line": 10,
"column": 29
}
},
{
"code": "a11y_missing_attribute",
"end": {
"column": 29,
"line": 10
},
"message": "`<img>` element should have an alt attribute",
"code": "legacy_code",
"message": "`a11y-misplaced-scope` is no longer valid — please use `a11y_misplaced_scope` instead",
"start": {
"column": 1,
"line": 10
"line": 13,
"column": 17
},
"end": {
"line": 13,
"column": 37
}
},
{
"code": "a11y_misplaced_scope",
"end": {
"column": 10,
"line": 14
},
"message": "The scope attribute should only be used with `<th>` elements",
"start": {
"column": 5,
"line": 14
"line": 14,
"column": 5
},
"end": {
"line": 14,
"column": 10
}
}
]
Loading