Skip to content

Commit f3d0c31

Browse files
pndewit43081j
andauthored
fix: allow disable comment to be the directly above the statement (#61)
Allows disable comments to occur in any parent of the target node rather than only at the statement level --------- Co-authored-by: 43081j <43081j@users.noreply.github.com>
1 parent b196730 commit f3d0c31

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

src/parse.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ export const parse: Parser<Root | Document> = (
134134
}) as Root;
135135
} catch (err) {
136136
if (err instanceof CssSyntaxError) {
137-
const line = node.loc ? ` (${opts?.from ?? 'unknown'}:${node.loc.start.line})` : opts?.from;
137+
const line = node.loc
138+
? ` (${opts?.from ?? 'unknown'}:${node.loc.start.line})`
139+
: opts?.from;
138140

139141
console.warn(
140142
'[postcss-lit]',

src/test/parse_test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,47 @@ describe('parse', () => {
363363
assert.equal(ast.nodes.length, 0);
364364
});
365365

366+
it('should ignore a child node', () => {
367+
const {ast} = createTestAst(`
368+
class MyClass {
369+
// postcss-lit-disable-next-line
370+
static style = css\`
371+
.foo { color: hotpink; }
372+
\`;
373+
}
374+
`);
375+
376+
assert.equal(ast.nodes.length, 0);
377+
});
378+
379+
it('should ignore a child node from an array', () => {
380+
const {ast} = createTestAst(`
381+
class MyClass {
382+
static style = [
383+
// postcss-lit-disable-next-line
384+
css\`
385+
.foo { color: hotpink; }
386+
\`,
387+
];
388+
}
389+
`);
390+
391+
assert.equal(ast.nodes.length, 0);
392+
});
393+
394+
it('should ignore everything inside a disabled parent node', () => {
395+
const {ast} = createTestAst(`
396+
// postcss-lit-disable-next-line
397+
class MyClass {
398+
static style = css\`
399+
.foo { color: hotpink; }
400+
\`;
401+
}
402+
`);
403+
404+
assert.equal(ast.nodes.length, 0);
405+
});
406+
366407
it('should ignore deeply disabled lines', () => {
367408
const {ast} = createTestAst(`
368409
// postcss-lit-disable-next-line

src/util.ts

+8-12
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,14 @@ export function isDisableComment(node: Comment): boolean {
2020
export function hasDisableComment(
2121
path: NodePath<TaggedTemplateExpression>
2222
): boolean {
23-
const statement = path.getStatementParent();
24-
25-
if (statement && statement.node.leadingComments) {
26-
const comment =
27-
statement.node.leadingComments[statement.node.leadingComments.length - 1];
28-
29-
if (comment !== undefined && isDisableComment(comment)) {
30-
return true;
31-
}
32-
}
33-
34-
return false;
23+
// The comment could be directly above the node or above any of its parents
24+
return (
25+
path.find(
26+
(p) =>
27+
// There could be multiple preceding the comments
28+
p.node.leadingComments?.some(isDisableComment) === true
29+
) !== null
30+
);
3531
}
3632

3733
export type Position =

0 commit comments

Comments
 (0)