Using JavaScript API to define variables with hash values doesn't allow use of dot notation in stylesheets #1286

Closed
jas opened this Issue Dec 13, 2013 · 9 comments

Projects

None yet

6 participants

@jas
jas commented Dec 13, 2013

I'm really enjoying the new hash support in Stylus. Thank you @Panya for all the work you did to make it happen.

I ran into an issue tonight with using stylus.define to set a variable in JavaScript — I might be overlooking something.

style.define('testhash', {
  a: 'value1',
  b: 'value2',
  c: {
    d: 'value 3'
  }
});

I would like to be able to access these values in my Stylus files using dot notation (ie testhash.a), but get the following error:

testhash has no property .a

When I inspect the value of testhash with the p function this is the output:

(a 'value1') (b 'value2') (c ((d 'value 3')))

Using stylus.nodes or stylus.utils is there currently a way to do this? I tried utils.coerce but it doesn't appear it's been updated for the new hashes.

@Panya
Member
Panya commented Dec 13, 2013

Thanks for the report. There is some backwards compatible thing with define. Currently if you'll pass an object as an argument it'll be coerced to the list with pairs (for example { foo: 'bar', bar: 'baz' } would become the expression (foo 'bar') (bar 'baz')). But i think it could be fixed with optional parameter or so.

@simshanith

#1358 has been closed, but looks like a simple solution.

Can we have a hash-objects option or something that uses @martinheidegger coerceObject implementation renamed to coerceObjectToHash, with the backwards-compatabile current coerceObject implementation renamed to coerceObjectToList and used as the default?

@Panya
Member
Panya commented Mar 22, 2014

I've added optional hash object support through raw parameter of the define function. For example:

style.define('testhash', {
  a: 'value1',
  b: 'value2',
  c: {
    d: 'value 3'
  }
}, true);
body
  content: testhash.c.d // 'value 3'

Fix will be in the next minor release.

@kizu kizu added a commit that referenced this issue Apr 5, 2014
@kizu kizu Merge branch 'pr/1444' into rc-0.43
* pr/1444:
  Added tests for `define`
  Support the hash objects in `define`. Fixes #1286
666bfd2
@Panya Panya closed this in 16fa016 Apr 5, 2014
@Sawtaytoes

Is the fix in version 0.44, or it's going into 0.45?

@Panya
Member
Panya commented Apr 27, 2014

@Saturn2888 this fix already in 0.44.

@Sawtaytoes

Looks like I was a version behind in the project I was using to test. This is such a recent fix I didn't realize I was missing it.

Awesome! Glad to see this as an available feature now 👍!

@simshanith simshanith referenced this issue in gruntjs/grunt-contrib-stylus Apr 27, 2014
Closed

Allow use of "raw" define #100

@acidjazz

This seems to only mainly work when specifying a colin in stylus, for instance:

          &.c_red1
            color white
            background-color d.color.red1

Will give me an error: expected "indent", got "outdent", where as this

          &.c_red1
            color white
            background-color: d.color.red1

compiles just fine.

@vendethiel

That's another thing entirely, though.

@acidjazz

Yes @vendethiel after I wrote this I dug through issues and found issue #1405 , It seems like its still outstanding and undecided. I can see how the parser can be confused but also how this puts a dent in the sugar of stylus (like not requiring colons). Where do you think this should land?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment