dart-sass icon indicating copy to clipboard operation
dart-sass copied to clipboard

node-sass compat: creating sass values without use of `new` operator

Open chriseppstein opened this issue 6 years ago • 3 comments

In node-sass, you can create a sass value without the use of new. For example:

let sass = require("node-sass");
let assert = require("assert");
let sassString = sass.types.String("my-string");
assert(sassString instanceof sass.types.String); // passes

This is true for all the Sass types Null, Boolean, Color, String, Number, List, and Map.

chriseppstein avatar Feb 11 '19 23:02 chriseppstein

Note that although node-sass forbids new nodeSass.types.Null(), it allows functional invocation of Null and Boolean and it will return the appropriate singleton value:

let nodeSass = require("node-sass");
let aNull = nodeSass.types.Null();
aNull === nodeSass.NULL; // true
aNull === nodeSass.types.Null.NULL; // true
let t = nodeSass.types.Boolean(true);
let f = nodeSass.types.Boolean(false);
f === nodeSass.TRUE; // true
t === nodeSass.FALSE; // true

chriseppstein avatar Feb 11 '19 23:02 chriseppstein

I don't know of a way to address this without using new.target, so this is blocked on https://github.com/dart-lang/sdk/issues/36240. Even then, I'm not 100% sure it'll be possible to implement this without harming the efficiency of the standard constructor case. I won't close it out until it gets unblocked and I'm able to measure the difference, but it's probably a good idea to always construct with new just in case.

nex3 avatar Mar 16 '19 01:03 nex3

The actual behavior is not backward-compatible with node-sass but it's definitely an improvment. I've been migrating our projects to dart-sass and it's the only things that I had to change to replace node-sass and it's for the better if you ask me: construct with new is much more elegant and in line with all other parts of our project.

ericmorand avatar Jul 14 '20 10:07 ericmorand