From 7ca989a4f7dfa4afe269bf3a7aec887e9fa93900 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Mon, 4 Feb 2019 13:14:25 -0800 Subject: [PATCH] Produce a better error for invalid function contents (#587) Closes #584 --- CHANGELOG.md | 3 +++ lib/src/parse/stylesheet.dart | 21 +++++++++++++++++++-- pubspec.yaml | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 201738ff..f829f18e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ * Produce a better error message for style rules that are missing the closing `}`. +* Produce a better error message for style rules and property declarations + within `@function` rules. + ### Command-Line Interface * Passing a directory on the command line now compiles all Sass source files in diff --git a/lib/src/parse/stylesheet.dart b/lib/src/parse/stylesheet.dart index 388beb88..d6d88def 100644 --- a/lib/src/parse/stylesheet.dart +++ b/lib/src/parse/stylesheet.dart @@ -451,7 +451,7 @@ abstract class StylesheetParser extends Parser { // here should be mirrored there. var start = scanner.state; - scanner.expectChar($at); + scanner.expectChar($at, name: "@-rule"); var name = interpolatedIdentifier(); whitespace(); @@ -536,6 +536,23 @@ abstract class StylesheetParser extends Parser { /// Consumes an at-rule allowed within a function. Statement _functionAtRule() { + if (scanner.peekChar() != $at) { + var position = scanner.position; + Statement statement; + try { + statement = _declarationOrStyleRule(); + } on SourceSpanFormatException catch (_) { + // If we can't parse a valid declaration or style rule, throw a more + // generic error message. + scanner.error("expected @-rule", position: position); + } + + error( + "@function rules may not contain " + "${statement is StyleRule ? "style rules" : "declarations"}.", + statement.span); + } + var start = scanner.state; switch (_plainAtRuleName()) { case "debug": @@ -563,7 +580,7 @@ abstract class StylesheetParser extends Parser { /// Consumes an at-rule's name, with interpolation disallowed. String _plainAtRuleName() { - scanner.expectChar($at); + scanner.expectChar($at, name: "@-rule"); var name = identifier(); whitespace(); return name; diff --git a/pubspec.yaml b/pubspec.yaml index 77d6401b..2bd3bcb6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: sass -version: 1.17.0-dev +version: 1.17.0 description: A Sass implementation in Dart. author: Dart Team homepage: https://github.com/sass/dart-sass