Skip to content

Conversation

@esr360
Copy link

@esr360 esr360 commented Jul 5, 2018

Addresses the discussion from #5

Copy link
Owner

@pmowrer pmowrer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting some thought into this @esr360 and sorry about the delayed response!

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's $foo? I think this needs some explanation... what's the idea behind shouldBeStringified? What are the criteria?

Copy link
Author

@esr360 esr360 Jul 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pmowrer if you haven't already please read my comments starting from here: #5 (comment) - you can follow the process that led me to this solution.

The idea behind the shouldBeStringified function is that it attempts to determine whether a value passed to parseValue should treated as a string and returned as "${value}".

$foo is just a placeholder variable we attempt to assign the non-stringified value to. If this breaks the Sass compiler, we know that Sass needs it to be a string to compile, hence we return shouldBeStringified as true.

Been using this branch on my side project since opening it and I've had no issues - and I have a plethora of various JSON values of different types.

I'm happy to make whatever improvements you need.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pmowrer it's probably also worth you checking this out sasstools/json-importer#3

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a bit crude to just do a try/catch... do we know it's backwards compatible? Would you mind summarizing your reasoning from your discussions in these other tickets?

Copy link
Author

@esr360 esr360 Jul 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate on why using a try/catch here seems crude? It seems like an appropriate use of it to me. The only way we can know for sure if a value will compile in Sass is to simply "try" it, and "catch" whether it errors or not; that's what makes this solution so simple. We don't have to try and preempt whether a value should be returned as "${value}" based off some logic, we can literally just try it, and catch the error before it actually happens, and then manipulate the value when needed.

As far as compatibility is concerned, I am not aware of any compatibility issues with try/catch, at least for basic usage such as this case.

I can't really summarize any more than I already have:

The idea behind the shouldBeStringified function is that it attempts to determine whether a value passed to parseValue should treated as a string and returned as "${value}".

$foo is just a placeholder variable we attempt to assign the non-stringified value to. If this breaks the Sass compiler, we know that Sass needs it to be a string to compile, hence we return shouldBeStringified as true.

@xyfer said (in this thread sasstools/json-importer#3) "The inefficiency is executing Node Sass for each value in shouldBeStringified. What I was suggesting is I think there is why to achieve the same behaviour without this overhead."

So whilst the implementation of the solution may not be as efficient as it could be, the solution itself seems good, and I can't say I've noticed any significant time overhead since using it.

To be clear, my experience from this solution is that I'm able to import any valid JSON file into my Sass without it ever erroring and without any unexpected behaviour in my Sass, and without having to format my JSON in a special way.

@pmowrer
Copy link
Owner

pmowrer commented Jul 27, 2018

Can you elaborate on why using a try/catch here seems crude? It seems like an appropriate use of it to me.

I wasn't commenting on the use of try/catch specifically, but rather on the "appropriate use". It all sounds reasonable to me, but its not clear from looking at the code what cases this is meant to support. Do we know it won't break people? Could we be introducing false positives?

I'm cool with merging this change, but the README needs to be updated to reflect this change (there's a section on strings) and I think shouldBeStringified needs a comment explaining what/why it is doing.

@pmowrer
Copy link
Owner

pmowrer commented Sep 6, 2018

Closing due to inactivity

@pmowrer pmowrer closed this Sep 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants