Make `@import` declarations more robust against the SASS spec

This commit is contained in:
Alex Palaistras 2016-03-19 11:38:36 +00:00
parent 7257ea0902
commit a849ee6135
5 changed files with 26 additions and 12 deletions

View File

@ -10,7 +10,7 @@ Currently, the following features are implemented:
* C99/C++-style comments (i.e. `// This is a comment`)
* Variables
* Imports (i.e. `@import "colors/common`)
* Imports
A full test-suite is provided (depending only on `make` and `awk`), which should serve as a good example of the existing feature-set.

16
fawkss
View File

@ -65,8 +65,8 @@ function file_exists(filename) {
BEGIN {
# Error messages used across Fawkss.
errors["variable-undeclared"] = "ERROR: Use of undeclared variable '%s' on line %d\n"
errors["import-cyclic"] = "ERROR: Cyclic import of file '%s' in file '%s', line %d\n"
errors["import-not-found"] = "ERROR: Import file '%s' not found, defined in '%s' on line %d\n"
errors["import-cyclic"] = "ERROR: Cyclic import of file '%s' in file '%s', line %d\n"
errors["import-not-found"] = "ERROR: Import file '%s' not found, defined in '%s' on line %d\n"
# Rule definitions.
rules["comment"] = "[ ]*//.*$"
@ -75,7 +75,8 @@ BEGIN {
rules["variable-name"] = "\\$[a-zA-Z0-9_-]+"
rules["variable-define"] = "^[ ]*" rules["variable-name"] "[ ]*:"
rules["import"] = "@import"
rules["import-path"] = "['\"][^'\".]+(.scss)?[ ]*['\"]"
rules["import-define"] = "[ ]*@import[ ]+" rules["import-path"] "[ ]*;"
rules["import-variants"] = "%s%s.scss,%s_%s.scss,%s%s,%s_%s"
}
@ -105,15 +106,16 @@ BEGIN {
#
# > Match import declarations, for example:
# >
# > @import "partials/colors"
# > @import "partials/colors";
# >
# > The above declaration may match file `_colors.scss` or `colors.scss` in the
# > `partials` directory (which should exist on the same level as the calling
# > file). Imports can be nested (i.e. imported files may in turn import other
# > files), and cyclic dependancies will return a fatal error.
if ($1 ~ rules["import"]) {
# Remove quotes from path part.
path = substr($2, 2, length($2) - 2)
if ($0 ~ rules["import-define"]) {
# Extract path part from import declaration.
match($0, rules["import-path"])
path = substr($0, RSTART + 1, RLENGTH - 2)
# Extract file and directory name parts from path, and append root directory
# path for current file to directory part.

View File

@ -4,11 +4,23 @@
--- TEST ---
@import "imports/partial"
@import "imports/full.scss"
// These should be passed down to the resulting CSS file verbatim.
@import "foo.css";
@import "foo" screen;
@import "http://foo.com/bar";
@import url(foo);
// These should be imported into the resulting CSS file.
@import "imports/partial" ;
@import "imports/full.scss";
--- EXPECTED ---
@import "foo.css";
@import "foo" screen;
@import "http://foo.com/bar";
@import url(foo);
.partial {
content: 'This is a partial';
}

View File

@ -1,4 +1,4 @@
@import "other-stuff.scss"
@import "other-stuff.scss";
$col-white: #fff;

View File

@ -1,4 +1,4 @@
@import "other-stuff"
@import "other-stuff";
.full {
content: 'This is a full import';