Skip to content

Commit

Permalink
Fix the string representations of the Sass AST (#1682)
Browse files Browse the repository at this point in the history
* 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 <jathak@google.com>
  • Loading branch information
stof and jathak committed Apr 25, 2022
1 parent fbd450b commit 6eed6eb
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 1.50.2

* Improve the string output of some AST nodes in error messages.

## 1.50.1

### Embedded Sass
Expand Down
4 changes: 2 additions & 2 deletions lib/src/ast/sass/argument_declaration.dart
Expand Up @@ -161,7 +161,7 @@ class ArgumentDeclaration implements SassNode {
}

String toString() => [
for (var arg in arguments) arg.toString(),
if (restArgument != null) '$restArgument...'
for (var arg in arguments) '\$$arg',
if (restArgument != null) '\$$restArgument...'
].join(', ');
}
2 changes: 1 addition & 1 deletion lib/src/ast/sass/argument_invocation.dart
Expand Up @@ -48,7 +48,7 @@ class ArgumentInvocation implements SassNode {
String toString() {
var components = [
...positional,
for (var name in named.keys) "$name: ${named[name]}",
for (var name in named.keys) "\$$name: ${named[name]}",
if (rest != null) "$rest...",
if (keywordRest != null) "$keywordRest..."
];
Expand Down
4 changes: 2 additions & 2 deletions lib/src/ast/sass/expression/list.dart
Expand Up @@ -53,8 +53,8 @@ class ListExpression implements Expression {
if (expression.contents.length < 2) return false;
if (expression.hasBrackets) return false;
return separator == ListSeparator.comma
? separator == ListSeparator.comma
: separator != ListSeparator.undecided;
? expression.separator == ListSeparator.comma
: expression.separator != ListSeparator.undecided;
}

if (separator != ListSeparator.space) return false;
Expand Down
2 changes: 0 additions & 2 deletions lib/src/ast/sass/import/static.dart
Expand Up @@ -3,7 +3,6 @@
// https://opensource.org/licenses/MIT.

import 'package:meta/meta.dart';
import 'package:charcode/charcode.dart';
import 'package:source_span/source_span.dart';

import '../import.dart';
Expand Down Expand Up @@ -36,7 +35,6 @@ class StaticImport implements Import {
var buffer = StringBuffer(url);
if (supports != null) buffer.write(" supports($supports)");
if (media != null) buffer.write(" $media");
buffer.writeCharCode($semicolon);
return buffer.toString();
}
}
17 changes: 17 additions & 0 deletions lib/src/ast/sass/statement/declaration.dart
Expand Up @@ -2,6 +2,7 @@
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'package:charcode/charcode.dart';
import 'package:meta/meta.dart';
import 'package:source_span/source_span.dart';

Expand Down Expand Up @@ -62,4 +63,20 @@ class Declaration extends ParentStatement {
}

T accept<T>(StatementVisitor<T> visitor) => visitor.visitDeclaration(this);

String toString() {
var buffer = StringBuffer();
buffer.write(name);
buffer.writeCharCode($colon);

if (value != null) {
if (!isCustomProperty) {
buffer.writeCharCode($space);
}
buffer.write("$value");
}

var children = this.children;
return children == null ? "$buffer;" : "$buffer {${children.join(" ")}}";
}
}
2 changes: 1 addition & 1 deletion lib/src/ast/sass/statement/extend_rule.dart
Expand Up @@ -32,5 +32,5 @@ class ExtendRule implements Statement {

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

String toString() => "@extend $selector";
String toString() => "@extend $selector${isOptional ? ' !optional' : ''};";
}
2 changes: 1 addition & 1 deletion lib/src/ast/sass/statement/if_rule.dart
Expand Up @@ -44,7 +44,7 @@ class IfRule implements Statement {
String toString() {
var result = clauses
.mapIndexed((index, clause) =>
"@${index == 0 ? 'if' : 'else if'} {${clause.children.join(' ')}}")
"@${index == 0 ? 'if' : 'else if'} ${clause.expression} {${clause.children.join(' ')}}")
.join(' ');

var lastClause = this.lastClause;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/ast/sass/statement/variable_declaration.dart
Expand Up @@ -90,9 +90,9 @@ class VariableDeclaration implements Statement, SassDeclaration {
visitor.visitVariableDeclaration(this);

String toString() {
var buffer = StringBuffer("\$");
var buffer = StringBuffer();
if (namespace != null) buffer.write("$namespace.");
buffer.write("$name: $expression;");
buffer.write("\$$name: $expression;");
return buffer.toString();
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
@@ -1,5 +1,5 @@
name: sass
version: 1.50.1
version: 1.50.2-dev
description: A Sass implementation in Dart.
homepage: https://github.com/sass/dart-sass

Expand Down

0 comments on commit 6eed6eb

Please sign in to comment.