Skip to content

Fails at LESS @when syntax with comma #99

Closed

Description

@thybzi

I'm using Stylelint with minimal config like the following { "rules": { "no-browser-hacks": true } }
This check uses postcss-selector-parser module

When linting the following LESS file:

.my-mixin() when (@a = 5), (@b = 3) {
    color: red;
}

I'm getting the error:

TypeError: Cannot read property 'value' of undefined
    at Parser.parentheses (C:\Users\thybzi\AppData\Roaming\npm\node_modules\stylelint\node_modules\postcss-selector-parser\dist\parser.js:305:17)
    at Parser.parse (C:\Users\thybzi\AppData\Roaming\npm\node_modules\stylelint\node_modules\postcss-selector-parser\dist\parser.js:531:22)
    at Parser.loop (C:\Users\thybzi\AppData\Roaming\npm\node_modules\stylelint\node_modules\postcss-selector-parser\dist\parser.js:517:18)
    at new Parser (C:\Users\thybzi\AppData\Roaming\npm\node_modules\stylelint\node_modules\postcss-selector-parser\dist\parser.js:104:21)
    at Processor.process (C:\Users\thybzi\AppData\Roaming\npm\node_modules\stylelint\node_modules\postcss-selector-parser\dist\processor.js:26:21)
    at Plugin.<anonymous> (C:\Users\thybzi\AppData\Roaming\npm\node_modules\stylelint\node_modules\stylehacks\dist\plugins\bodyEmpty.js:36:67)
    at Plugin.<anonymous> (C:\Users\thybzi\AppData\Roaming\npm\node_modules\stylelint\node_modules\stylehacks\dist\plugin.js:40:29)
    at C:\Users\thybzi\AppData\Roaming\npm\node_modules\stylelint\node_modules\postcss\lib\container.js:148:26
    at Root.each (C:\Users\thybzi\AppData\Roaming\npm\node_modules\stylelint\node_modules\postcss\lib\container.js:114:22)
    at Root.walk (C:\Users\thybzi\AppData\Roaming\npm\node_modules\stylelint\node_modules\postcss\lib\container.js:147:21)

Seems to be postcss-selector-parser (not stylelint) bug.

If you need any additional info, I'll try to provide it.

Activity

  1. ben-eb commented on Jul 8, 2017

    @ben-eb
    Member

    OK so I've had a chance to look at this now, the problem is that the value enclosed in parentheses doesn't neatly fit into the idea of CSS selectors as the parser understands it. You can see this if you remove the rest of the string:

    const p = require('postcss-selector-parser');
    
    p(selectors => {
        console.log(require('util').inspect(selectors, false, null));
    }).process('.my-mixin() when (@a = 5)').result;

    Produces the following tree:

    Root {
      spaces: { before: '', after: '' },
      nodes: 
       [ Selector {
           spaces: { before: '', after: '' },
           nodes: 
            [ ClassName {
                value: 'my-mixin()',
                source: { start: { line: 1, column: 1 }, end: { line: 1, column: 9 } },
                sourceIndex: 0,
                spaces: { before: '', after: '' },
                type: 'class',
                parent: [Circular] },
              Combinator {
                value: ' ',
                source: { start: { line: 1, column: 12 }, end: { line: 1, column: 12 } },
                sourceIndex: 11,
                spaces: { before: '', after: '' },
                type: 'combinator',
                parent: [Circular] },
              Tag {
                value: 'when',
                source: { start: { line: 1, column: 13 }, end: { line: 1, column: 16 } },
                sourceIndex: 12,
                spaces: { before: '', after: '' },
                type: 'tag',
                parent: [Circular] },
              Combinator {
                value: ' (@a = 5)',
                source: { start: { line: 1, column: 17 }, end: { line: 1, column: 17 } },
                sourceIndex: 16,
                spaces: { before: '', after: '' },
                type: 'combinator',
                parent: [Circular] } ],
           type: 'selector',
           parent: [Circular] } ],
      type: 'root' }
    

    You can see that (@a = 5) is being parsed as a combinator node, due to the space token. Adding proper support for this is not something that I personally am wanting to do because I am not a Less user, and my original intention for the module was to parse CSS selector strings.

    I think it might be quite a lot of work to parse Less selectors properly, but I am willing to accept code that implements this parsing, so long as it can fit neatly into the module and is well tested.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions