Skip to content

Line comment's text value not returned in the AST #2861

@vecerek

Description

@vecerek

Hi Guys!

I'd like to ask you, what happens to the text value of line comments after parsing? When I get the AST, their value is an empty string. Block comment values are returned correctly.
Here I see, that the parser saves the comment's text value regardless its type.
Should I pass some extra option to the parser, or is it a bug?

Thanks in advance :)

This is my code for parsing:

var less = require('/usr/local/lib/node_modules/less/lib/less-node')
    , fs = require('fs')
    , path = require('path');

module.exports = {
    parse: function(src, isStdin) {
        // Parses a Less file and outputs its AST

        var visit = function(o) {
            // Attaches the node type to the necessary objects
            for (var p in o) {
                if (typeof o[p] == 'object') visit(o[p]);
            }
            if (o != null && o != "") {
                if ("type" in o) o["class"] = o.type;
            }
        };

        var code = src;
        var options = {};

        if (!isStdin) {
            code = fs.readFileSync(src, 'utf8').toString();
            options = {
                filename: path.resolve(src)
            }
        }

        return less.parse(code, options, function(e, tree) {
            if (!e) {
                visit(tree);
                console.log(JSON.stringify(tree, null, 2));
            } else {
                e.class = "error"
                console.log(JSON.stringify(e));
            }
        });
    }
};

And an extract from its output:

{
          "value": "/* Color Properties */",
          "isLineComment": false,
          "currentFileInfo": {
            "filename": "/Users/attila/Projects/OpenSource/less2sass-test/less2sass/tmp/less/test_rules.less",
            "rootpath": "",
            "currentDirectory": "/Users/attila/Projects/OpenSource/less2sass-test/less2sass/tmp/less/",
            "entryPath": "/Users/attila/Projects/OpenSource/less2sass-test/less2sass/tmp/less/",
            "rootFilename": "/Users/attila/Projects/OpenSource/less2sass-test/less2sass/tmp/less/test_rules.less"
          },
          "class": "Comment"
},
{
          "value": "",
          "isLineComment": true,
          "currentFileInfo": {
            "filename": "/Users/attila/Projects/OpenSource/less2sass-test/less2sass/tmp/less/test_rules.less",
            "rootpath": "",
            "currentDirectory": "/Users/attila/Projects/OpenSource/less2sass-test/less2sass/tmp/less/",
            "entryPath": "/Users/attila/Projects/OpenSource/less2sass-test/less2sass/tmp/less/",
            "rootFilename": "/Users/attila/Projects/OpenSource/less2sass-test/less2sass/tmp/less/test_rules.less"
          },
          "class": "Comment"
}

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