forked from angular/angular
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(language-service): TS references from template items
Keen and I were talking about what it would take to support getting references at a position in the current language service, since it's unclear when more investment in the Ivy LS will be available. Getting TS references from a template is trivial -- we simply need to get the definition of a symbol, which is already handled by the language service, and ask the TS language service to give us the references for that definition. This doesn't handle references in templates, but that could be done in a subsequent pass. Part of angular/vscode-ng-language-service#29
- Loading branch information
Showing
5 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import * as ts from 'typescript'; | ||
|
||
import {createLanguageService} from '../src/language_service'; | ||
import {TypeScriptServiceHost} from '../src/typescript_host'; | ||
|
||
import {MockTypescriptHost} from './test_utils'; | ||
|
||
describe('references', () => { | ||
const mockHost = new MockTypescriptHost(['/app/main.ts']); | ||
const tsLS = ts.createLanguageService(mockHost); | ||
const ngHost = new TypeScriptServiceHost(mockHost, tsLS); | ||
const ngLS = createLanguageService(ngHost); | ||
|
||
beforeEach(() => { | ||
mockHost.reset(); | ||
}); | ||
|
||
describe('component members', () => { | ||
it('should get TS references for a member in template', () => { | ||
const fileName = mockHost.addCode(` | ||
@Component({ | ||
template: '{{«title»}}' | ||
}) | ||
export class MyComponent { | ||
/*1*/title: string; | ||
setTitle(newTitle: string) { | ||
this./*2*/title = newTitle; | ||
} | ||
}`); | ||
const content = mockHost.readFile(fileName)!; | ||
|
||
const varName = 'title'; | ||
const marker = mockHost.getReferenceMarkerFor(fileName, varName); | ||
|
||
const references = ngLS.getReferencesAtPosition(fileName, marker.start)!; | ||
expect(references).toBeDefined(); | ||
expect(references.length).toBe(2); | ||
|
||
for (let i = 0; i < references.length; ++i) { | ||
const comment = `/*${i + 1}*/`; | ||
const start = content.indexOf(comment) + comment.length; | ||
expect(references[i].fileName).toBe(fileName); | ||
expect(references[i].textSpan.start).toBe(start); | ||
expect(references[i].textSpan.length).toBe(varName.length); | ||
} | ||
}); | ||
}); | ||
}); |