Skip to content

Commit 7784231

Browse files
committed
Add support for loud comments
1 parent 21819c3 commit 7784231

File tree

13 files changed

+588
-9
lines changed

13 files changed

+588
-9
lines changed

lib/src/parse/sass.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ class SassParser extends StylesheetParser {
268268

269269
_readIndentation();
270270
}
271-
if (!buffer.trailingString.trimRight().endsWith("*/")) buffer.write(" */");
272271

273272
return LoudComment(buffer.interpolation(scanner.spanFrom(start)));
274273
}

lib/src/visitor/async_evaluate.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,8 +1911,10 @@ final class _EvaluateVisitor
19111911
_endOfImports++;
19121912
}
19131913

1914-
_parent.addChild(ModifiableCssComment(
1915-
await _performInterpolation(node.text), node.span));
1914+
var text = await _performInterpolation(node.text);
1915+
// Indented syntax doesn't require */
1916+
if (!text.endsWith("*/")) text += " */";
1917+
_parent.addChild(ModifiableCssComment(text, node.span));
19161918
return null;
19171919
}
19181920

lib/src/visitor/evaluate.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// DO NOT EDIT. This file was generated from async_evaluate.dart.
66
// See tool/grind/synchronize.dart for details.
77
//
8-
// Checksum: ebf292c26dcfdd7f61fd70ce3dc9e0be2b6708b3
8+
// Checksum: 2ab69d23a3b34cb54ddd74e2e854614dda582174
99
//
1010
// ignore_for_file: unused_import
1111

@@ -1903,8 +1903,10 @@ final class _EvaluateVisitor
19031903
_endOfImports++;
19041904
}
19051905

1906-
_parent.addChild(
1907-
ModifiableCssComment(_performInterpolation(node.text), node.span));
1906+
var text = _performInterpolation(node.text);
1907+
// Indented syntax doesn't require */
1908+
if (!text.endsWith("*/")) text += " */";
1909+
_parent.addChild(ModifiableCssComment(text, node.span));
19081910
return null;
19091911
}
19101912

pkg/sass-parser/jest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const config = {
33
roots: ['lib'],
44
testEnvironment: 'node',
55
setupFilesAfterEnv: ['jest-extended/all', '<rootDir>/test/setup.ts'],
6+
verbose: false,
67
};
78

89
export default config;

pkg/sass-parser/lib/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export {
3333
InterpolationRaws,
3434
NewNodeForInterpolation,
3535
} from './src/interpolation';
36+
export {
37+
CssComment,
38+
CssCommentProps,
39+
CssCommentRaws,
40+
} from './src/statement/css-comment';
3641
export {
3742
DebugRule,
3843
DebugRuleProps,

pkg/sass-parser/lib/src/sass-internal.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ declare namespace SassInternal {
101101
readonly isExclusive: boolean;
102102
}
103103

104+
class LoudComment extends Statement {
105+
readonly text: Interpolation;
106+
}
107+
104108
class Stylesheet extends ParentStatement<Statement[]> {}
105109

106110
class StyleRule extends ParentStatement<Statement[]> {
@@ -143,6 +147,7 @@ export type EachRule = SassInternal.EachRule;
143147
export type ErrorRule = SassInternal.ErrorRule;
144148
export type ExtendRule = SassInternal.ExtendRule;
145149
export type ForRule = SassInternal.ForRule;
150+
export type LoudComment = SassInternal.LoudComment;
146151
export type Stylesheet = SassInternal.Stylesheet;
147152
export type StyleRule = SassInternal.StyleRule;
148153
export type Interpolation = SassInternal.Interpolation;
@@ -158,6 +163,7 @@ export interface StatementVisitorObject<T> {
158163
visitErrorRule(node: ErrorRule): T;
159164
visitExtendRule(node: ExtendRule): T;
160165
visitForRule(node: ForRule): T;
166+
visitLoudComment(node: LoudComment): T;
161167
visitStyleRule(node: StyleRule): T;
162168
}
163169

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`a CSS-style comment toJSON 1`] = `
4+
{
5+
"inputs": [
6+
{
7+
"css": "/* foo */",
8+
"hasBOM": false,
9+
"id": "<input css _____>",
10+
},
11+
],
12+
"raws": {
13+
"closed": true,
14+
"left": " ",
15+
"right": " ",
16+
},
17+
"sassType": "comment",
18+
"source": <1:1-1:10 in 0>,
19+
"text": "foo",
20+
"textInterpolation": <foo>,
21+
"type": "comment",
22+
}
23+
`;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2024 Google Inc. Use of this source code is governed by an
2+
// MIT-style license that can be found in the LICENSE file or at
3+
// https://opensource.org/licenses/MIT.
4+
5+
import * as postcss from 'postcss';
6+
7+
import {Root} from './root';
8+
import {ChildNode, NewNode} from '.';
9+
10+
/**
11+
* A fake intermediate class to convince TypeScript to use Sass types for
12+
* various upstream methods.
13+
*
14+
* @hidden
15+
*/
16+
export class _Comment<Props> extends postcss.Comment {
17+
// Override the PostCSS types to constrain them to Sass types only.
18+
// Unfortunately, there's no way to abstract this out, because anything
19+
// mixin-like returns an intersection type which doesn't actually override
20+
// parent methods. See microsoft/TypeScript#59394.
21+
22+
after(newNode: NewNode): this;
23+
assign(overrides: Partial<Props>): this;
24+
before(newNode: NewNode): this;
25+
cloneAfter(overrides?: Partial<Props>): this;
26+
cloneBefore(overrides?: Partial<Props>): this;
27+
next(): ChildNode | undefined;
28+
prev(): ChildNode | undefined;
29+
replaceWith(...nodes: NewNode[]): this;
30+
root(): Root;
31+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright 2024 Google Inc. Use of this source code is governed by an
2+
// MIT-style license that can be found in the LICENSE file or at
3+
// https://opensource.org/licenses/MIT.
4+
5+
exports._Comment = require('postcss').Comment;

0 commit comments

Comments
 (0)