Skip to content

Commit 6eed6eb

Browse files
stofjathak
andauthored
Fix the string representations of the Sass AST (#1682)
* Fix the string representations of the Sass AST Those are used when providing recommendations in warnings or errors, so it is better if they produce valid code. * Add changelog Co-authored-by: Jennifer Thakar <[email protected]>
1 parent fbd450b commit 6eed6eb

File tree

10 files changed

+31
-12
lines changed

10 files changed

+31
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.50.2
2+
3+
* Improve the string output of some AST nodes in error messages.
4+
15
## 1.50.1
26

37
### Embedded Sass

lib/src/ast/sass/argument_declaration.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class ArgumentDeclaration implements SassNode {
161161
}
162162

163163
String toString() => [
164-
for (var arg in arguments) arg.toString(),
165-
if (restArgument != null) '$restArgument...'
164+
for (var arg in arguments) '\$$arg',
165+
if (restArgument != null) '\$$restArgument...'
166166
].join(', ');
167167
}

lib/src/ast/sass/argument_invocation.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class ArgumentInvocation implements SassNode {
4848
String toString() {
4949
var components = [
5050
...positional,
51-
for (var name in named.keys) "$name: ${named[name]}",
51+
for (var name in named.keys) "\$$name: ${named[name]}",
5252
if (rest != null) "$rest...",
5353
if (keywordRest != null) "$keywordRest..."
5454
];

lib/src/ast/sass/expression/list.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class ListExpression implements Expression {
5353
if (expression.contents.length < 2) return false;
5454
if (expression.hasBrackets) return false;
5555
return separator == ListSeparator.comma
56-
? separator == ListSeparator.comma
57-
: separator != ListSeparator.undecided;
56+
? expression.separator == ListSeparator.comma
57+
: expression.separator != ListSeparator.undecided;
5858
}
5959

6060
if (separator != ListSeparator.space) return false;

lib/src/ast/sass/import/static.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// https://opensource.org/licenses/MIT.
44

55
import 'package:meta/meta.dart';
6-
import 'package:charcode/charcode.dart';
76
import 'package:source_span/source_span.dart';
87

98
import '../import.dart';
@@ -36,7 +35,6 @@ class StaticImport implements Import {
3635
var buffer = StringBuffer(url);
3736
if (supports != null) buffer.write(" supports($supports)");
3837
if (media != null) buffer.write(" $media");
39-
buffer.writeCharCode($semicolon);
4038
return buffer.toString();
4139
}
4240
}

lib/src/ast/sass/statement/declaration.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// MIT-style license that can be found in the LICENSE file or at
33
// https://opensource.org/licenses/MIT.
44

5+
import 'package:charcode/charcode.dart';
56
import 'package:meta/meta.dart';
67
import 'package:source_span/source_span.dart';
78

@@ -62,4 +63,20 @@ class Declaration extends ParentStatement {
6263
}
6364

6465
T accept<T>(StatementVisitor<T> visitor) => visitor.visitDeclaration(this);
66+
67+
String toString() {
68+
var buffer = StringBuffer();
69+
buffer.write(name);
70+
buffer.writeCharCode($colon);
71+
72+
if (value != null) {
73+
if (!isCustomProperty) {
74+
buffer.writeCharCode($space);
75+
}
76+
buffer.write("$value");
77+
}
78+
79+
var children = this.children;
80+
return children == null ? "$buffer;" : "$buffer {${children.join(" ")}}";
81+
}
6582
}

lib/src/ast/sass/statement/extend_rule.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ class ExtendRule implements Statement {
3232

3333
T accept<T>(StatementVisitor<T> visitor) => visitor.visitExtendRule(this);
3434

35-
String toString() => "@extend $selector";
35+
String toString() => "@extend $selector${isOptional ? ' !optional' : ''};";
3636
}

lib/src/ast/sass/statement/if_rule.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class IfRule implements Statement {
4444
String toString() {
4545
var result = clauses
4646
.mapIndexed((index, clause) =>
47-
"@${index == 0 ? 'if' : 'else if'} {${clause.children.join(' ')}}")
47+
"@${index == 0 ? 'if' : 'else if'} ${clause.expression} {${clause.children.join(' ')}}")
4848
.join(' ');
4949

5050
var lastClause = this.lastClause;

lib/src/ast/sass/statement/variable_declaration.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ class VariableDeclaration implements Statement, SassDeclaration {
9090
visitor.visitVariableDeclaration(this);
9191

9292
String toString() {
93-
var buffer = StringBuffer("\$");
93+
var buffer = StringBuffer();
9494
if (namespace != null) buffer.write("$namespace.");
95-
buffer.write("$name: $expression;");
95+
buffer.write("\$$name: $expression;");
9696
return buffer.toString();
9797
}
9898
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sass
2-
version: 1.50.1
2+
version: 1.50.2-dev
33
description: A Sass implementation in Dart.
44
homepage: https://github.com/sass/dart-sass
55

0 commit comments

Comments
 (0)