Skip to content

Inconsistency in getDocumentHighlights for import aliases #3688

@leeavital

Description

@leeavital

For the file:

import fs = require('fs');
import ws = fs.writeStream;

getDocumentHighlights will not find that the fs on line 1 is the same as the one on line 2.

But for the file:

import fs = require('fs');
var ws = fs.writeStream;

I wrote a small program to demonstrate the problem.

var ts = require('typescript');
var fs = require('fs');

var source = fs.readFileSync('test.ts').toString();
var sourceFile = ts.createSourceFile('test.ts', source, 1, false);

var host = {
  getCompilationSettings: function() {
      return {
          noResolve: true,
          target: ts.ScriptTarget.ES5
      };
  },
  getCurrentDirectory: function() {
    return '';
  },
  getDefaultLibFileName: function() {
    return "lib.d.ts"
  },
  getScriptFileNames: function() {
    return ['test.ts'];
  },
  getScriptIsOpen: function() {
    return true
  },
  getScriptVersion: function() {
    return "1"
  },
  getScriptSnapshot: function() {
    return {
      getLength: function() {
        return source.length;
      },
      getLineStartPositions: function () {
        return ts.computeLineStarts(source);
      },
      getText: function(start, end) {
        return source.substring(start, end);
      }
    };
  }
};

var documentRegistry = ts.createDocumentRegistry();
var languageServices = ts.createLanguageService(host, documentRegistry);

var importFS  = sourceFile.statements[0];
var highlights = languageServices.getDocumentHighlights('test.ts', importFS.name.pos + 1, ['test.ts']);
console.log(highlights[0].highlightSpans.length);

When test.ts contains the first example, it outputs 1. When test.ts contains the second example, it outputs 2.

Metadata

Metadata

Assignees

Labels

APIRelates to the public API for TypeScriptBugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions