Skip to content

Commit

Permalink
Add support for loud comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Sep 4, 2024
1 parent 21819c3 commit 7784231
Show file tree
Hide file tree
Showing 13 changed files with 588 additions and 9 deletions.
1 change: 0 additions & 1 deletion lib/src/parse/sass.dart
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ class SassParser extends StylesheetParser {

_readIndentation();
}
if (!buffer.trailingString.trimRight().endsWith("*/")) buffer.write(" */");

return LoudComment(buffer.interpolation(scanner.spanFrom(start)));
}
Expand Down
6 changes: 4 additions & 2 deletions lib/src/visitor/async_evaluate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1911,8 +1911,10 @@ final class _EvaluateVisitor
_endOfImports++;
}

_parent.addChild(ModifiableCssComment(
await _performInterpolation(node.text), node.span));
var text = await _performInterpolation(node.text);
// Indented syntax doesn't require */
if (!text.endsWith("*/")) text += " */";
_parent.addChild(ModifiableCssComment(text, node.span));
return null;
}

Expand Down
8 changes: 5 additions & 3 deletions lib/src/visitor/evaluate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// DO NOT EDIT. This file was generated from async_evaluate.dart.
// See tool/grind/synchronize.dart for details.
//
// Checksum: ebf292c26dcfdd7f61fd70ce3dc9e0be2b6708b3
// Checksum: 2ab69d23a3b34cb54ddd74e2e854614dda582174
//
// ignore_for_file: unused_import

Expand Down Expand Up @@ -1903,8 +1903,10 @@ final class _EvaluateVisitor
_endOfImports++;
}

_parent.addChild(
ModifiableCssComment(_performInterpolation(node.text), node.span));
var text = _performInterpolation(node.text);
// Indented syntax doesn't require */
if (!text.endsWith("*/")) text += " */";
_parent.addChild(ModifiableCssComment(text, node.span));
return null;
}

Expand Down
1 change: 1 addition & 0 deletions pkg/sass-parser/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const config = {
roots: ['lib'],
testEnvironment: 'node',
setupFilesAfterEnv: ['jest-extended/all', '<rootDir>/test/setup.ts'],
verbose: false,
};

export default config;
5 changes: 5 additions & 0 deletions pkg/sass-parser/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export {
InterpolationRaws,
NewNodeForInterpolation,
} from './src/interpolation';
export {
CssComment,
CssCommentProps,
CssCommentRaws,
} from './src/statement/css-comment';
export {
DebugRule,
DebugRuleProps,
Expand Down
6 changes: 6 additions & 0 deletions pkg/sass-parser/lib/src/sass-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ declare namespace SassInternal {
readonly isExclusive: boolean;
}

class LoudComment extends Statement {
readonly text: Interpolation;
}

class Stylesheet extends ParentStatement<Statement[]> {}

class StyleRule extends ParentStatement<Statement[]> {
Expand Down Expand Up @@ -143,6 +147,7 @@ export type EachRule = SassInternal.EachRule;
export type ErrorRule = SassInternal.ErrorRule;
export type ExtendRule = SassInternal.ExtendRule;
export type ForRule = SassInternal.ForRule;
export type LoudComment = SassInternal.LoudComment;
export type Stylesheet = SassInternal.Stylesheet;
export type StyleRule = SassInternal.StyleRule;
export type Interpolation = SassInternal.Interpolation;
Expand All @@ -158,6 +163,7 @@ export interface StatementVisitorObject<T> {
visitErrorRule(node: ErrorRule): T;
visitExtendRule(node: ExtendRule): T;
visitForRule(node: ForRule): T;
visitLoudComment(node: LoudComment): T;
visitStyleRule(node: StyleRule): T;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`a CSS-style comment toJSON 1`] = `
{
"inputs": [
{
"css": "/* foo */",
"hasBOM": false,
"id": "<input css _____>",
},
],
"raws": {
"closed": true,
"left": " ",
"right": " ",
},
"sassType": "comment",
"source": <1:1-1:10 in 0>,
"text": "foo",
"textInterpolation": <foo>,
"type": "comment",
}
`;
31 changes: 31 additions & 0 deletions pkg/sass-parser/lib/src/statement/comment-internal.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import * as postcss from 'postcss';

import {Root} from './root';
import {ChildNode, NewNode} from '.';

/**
* A fake intermediate class to convince TypeScript to use Sass types for
* various upstream methods.
*
* @hidden
*/
export class _Comment<Props> extends postcss.Comment {
// Override the PostCSS types to constrain them to Sass types only.
// Unfortunately, there's no way to abstract this out, because anything
// mixin-like returns an intersection type which doesn't actually override
// parent methods. See microsoft/TypeScript#59394.

after(newNode: NewNode): this;
assign(overrides: Partial<Props>): this;
before(newNode: NewNode): this;
cloneAfter(overrides?: Partial<Props>): this;
cloneBefore(overrides?: Partial<Props>): this;
next(): ChildNode | undefined;
prev(): ChildNode | undefined;
replaceWith(...nodes: NewNode[]): this;
root(): Root;
}
5 changes: 5 additions & 0 deletions pkg/sass-parser/lib/src/statement/comment-internal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2024 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

exports._Comment = require('postcss').Comment;
Loading

0 comments on commit 7784231

Please sign in to comment.