Skip to content

Classifier returning Identifier for whitespace #1997

@basarat

Description

@basarat

For the following code:

var foo = 123;

class Awesome {

}

var bar: Awesome;

I get the new lines classified as Identifier.

 Object {length: 3, classification: 1} "keyword" "var"
 Object {length: 1, classification: 4} "" " "
 Object {length: 3, classification: 5} "identifier" "foo"
 Object {length: 1, classification: 4} "" " "
 Object {length: 1, classification: 2} "operator" "="
 Object {length: 1, classification: 4} "" " "
 Object {length: 3, classification: 6} "number" "123"
 Object {length: 1, classification: 0} "punctuation" ";"
 Object {length: 1, classification: 5} "identifier" "
"
 Object {length: 1, classification: 5} "identifier" "
"
 Object {length: 5, classification: 1} "keyword" "class"
 Object {length: 1, classification: 4} "" " "
.... truncated ....

The classification code I am using is

    var classifier: ts.Classifier = ts.createClassifier({ log: () => undefined });
    var classificationResult = classifier.getClassificationsForLine(textForTest, ts.EndOfLineState.Start).entries;
    var totalLength = 0;
    classificationResult.forEach((info) => {
        var str = textForTest.substr(totalLength, info.length);
        console.log(info, getStyleForToken(info, ''), str); // nevermind the empty '' for now as I want to get "keyword"
        totalLength = totalLength + info.length;
    });

with getStyleForToken:

function getStyleForToken(token: ts.ClassificationInfo, str: string): string {
    switch (token.classification) {
        case TokenClass.Punctuation:
            return 'punctuation';
        case TokenClass.Keyword:
            switch (str) {
                case 'static':
                case 'public':
                case 'private':
                case 'export':
                case 'get':
                case 'set':
                    return 'qualifier';
                case 'class':
                case 'function':
                case 'module':
                case 'var':
                    return 'definition';
                case 'string':
                case 'number':
                case 'void':
                case 'boolean':
                    return 'keyword';
                default:
                    return 'keyword';
            }
        case TokenClass.Operator:
            return 'operator';
        case TokenClass.Comment:
            return 'comment';
        case TokenClass.Whitespace:
            return '';
        case TokenClass.Identifier:
            return 'identifier';
        case TokenClass.NumberLiteral:
            return 'number';
        case TokenClass.StringLiteral:
            return 'string';
        case TokenClass.RegExpLiteral:
            return 'regexp';
        default:
            return null; // This should not happen
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions