Skip to content

Intelligently determine if value should be a string #3

Open
@esr360

Description

@esr360

I'm not sure if you're familiar with node-sass-json-importer, but one of the caveats of that tool is that you must double quote strings in your JSON, which essentially means you cannot guarantee that a valid JSON file will not break the Sass compiler.

I much prefer the implementation here which treats everything as a string and has the user selectively use unquote as desired; I had tried to get the same behaviour implemented into node-sass-json-importer to no avail thus far (pmowrer/node-sass-json-importer#5 (comment)).

However, I have a current open pull request which attempts to intelligently determine if a value should be treated as a CSS value or a string: pmowrer/node-sass-json-importer#70

It's quite a simple and effective solution which is working great in my rather large project so far (with over 40 JSON files), which ultimately boils down to attempting to render the value with Sass and seeing if it works or not - if it doesn't, then it is treated as a string:

export function shouldBeStringified(value) {
  try {
    sass.renderSync({
      data: `$foo: ${value};`
    });

    return false;
  } catch(error) {
    return true
  }
}

...would something similar work with this project, and is it a safe solution?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions