Skip to content

Commit

Permalink
Add support for silent comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Sep 6, 2024
1 parent cf918bc commit d9e854a
Show file tree
Hide file tree
Showing 9 changed files with 753 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/src/js/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ void _updateAstPrototypes() {
var file = SourceFile.fromString('');
getJSClass(file).defineMethod('getText',
(SourceFile self, int start, [int? end]) => self.getText(start, end));
getJSClass(file)
.defineGetter('codeUnits', (SourceFile self) => self.codeUnits);
var interpolation = Interpolation(const [], bogusSpan);
getJSClass(interpolation)
.defineGetter('asPlain', (Interpolation self) => self.asPlain);
Expand Down
5 changes: 5 additions & 0 deletions pkg/sass-parser/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export {
} from './src/statement/generic-at-rule';
export {Root, RootProps, RootRaws} from './src/statement/root';
export {Rule, RuleProps, RuleRaws} from './src/statement/rule';
export {
SassComment,
SassCommentProps,
SassCommentRaws,
} from './src/statement/sass-comment';
export {
AnyStatement,
AtRule,
Expand Down
8 changes: 8 additions & 0 deletions pkg/sass-parser/lib/src/sass-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface SourceFile {
/** Node-only extension that we use to avoid re-creating inputs. */
_postcssInput?: postcss.Input;

readonly codeUnits: number[];

getText(start: number, end?: number): string;
}

Expand Down Expand Up @@ -109,6 +111,10 @@ declare namespace SassInternal {
readonly query: Interpolation;
}

class SilentComment extends Statement {
readonly text: string;
}

class Stylesheet extends ParentStatement<Statement[]> {}

class StyleRule extends ParentStatement<Statement[]> {
Expand Down Expand Up @@ -153,6 +159,7 @@ export type ExtendRule = SassInternal.ExtendRule;
export type ForRule = SassInternal.ForRule;
export type LoudComment = SassInternal.LoudComment;
export type MediaRule = SassInternal.MediaRule;
export type SilentComment = SassInternal.SilentComment;
export type Stylesheet = SassInternal.Stylesheet;
export type StyleRule = SassInternal.StyleRule;
export type Interpolation = SassInternal.Interpolation;
Expand All @@ -170,6 +177,7 @@ export interface StatementVisitorObject<T> {
visitForRule(node: ForRule): T;
visitLoudComment(node: LoudComment): T;
visitMediaRule(node: MediaRule): T;
visitSilentComment(node: SilentComment): T;
visitStyleRule(node: StyleRule): T;
}

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

exports[`a Sass-style comment toJSON 1`] = `
{
"inputs": [
{
"css": "// foo",
"hasBOM": false,
"id": "<input css _____>",
},
],
"raws": {
"before": "",
"beforeLines": [
"",
],
"left": " ",
},
"sassType": "sass-comment",
"source": <1:1-1:7 in 0>,
"text": "foo",
"type": "comment",
}
`;
12 changes: 9 additions & 3 deletions pkg/sass-parser/lib/src/statement/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {LazySource} from '../lazy-source';
import {Node, NodeProps} from '../node';
import * as sassInternal from '../sass-internal';
import {CssComment, CssCommentProps} from './css-comment';
import {SassComment, SassCommentChildProps} from './sass-comment';
import {GenericAtRule, GenericAtRuleProps} from './generic-at-rule';
import {DebugRule, DebugRuleProps} from './debug-rule';
import {EachRule, EachRuleProps} from './each-rule';
Expand Down Expand Up @@ -45,7 +46,8 @@ export type StatementType =
| 'debug-rule'
| 'each-rule'
| 'for-rule'
| 'error-rule';
| 'error-rule'
| 'sass-comment';

/**
* All Sass statements that are also at-rules.
Expand All @@ -59,7 +61,7 @@ export type AtRule = DebugRule | EachRule | ErrorRule | ForRule | GenericAtRule;
*
* @category Statement
*/
export type Comment = CssComment;
export type Comment = CssComment | SassComment;

/**
* All Sass statements that are valid children of other statements.
Expand All @@ -85,7 +87,8 @@ export type ChildProps =
| ErrorRuleProps
| ForRuleProps
| GenericAtRuleProps
| RuleProps;
| RuleProps
| SassCommentChildProps;

/**
* The Sass eqivalent of PostCSS's `ContainerProps`.
Expand Down Expand Up @@ -158,6 +161,7 @@ const visitor = sassInternal.createStatementVisitor<Statement>({
appendInternalChildren(rule, inner.children);
return rule;
},
visitSilentComment: inner => new SassComment(undefined, inner),
visitStyleRule: inner => new Rule(undefined, inner),
});

Expand Down Expand Up @@ -271,6 +275,8 @@ export function normalize(
result.push(new ErrorRule(node));
} else if ('text' in node || 'textInterpolation' in node) {
result.push(new CssComment(node as CssCommentProps));
} else if ('silentText' in node) {
result.push(new SassComment(node));
} else {
result.push(...postcssNormalizeAndConvertToSass(self, node, sample));
}
Expand Down
Loading

0 comments on commit d9e854a

Please sign in to comment.