Skip to content

Commit

Permalink
feat(language-service): Enable go to definition of styleUrl (#51746)
Browse files Browse the repository at this point in the history
This commit enables 'go to definition' action for the new 'styleUrl' property

PR Close #51746
  • Loading branch information
atscott authored and pkozlowski-opensource committed Sep 18, 2023
1 parent 0c5b34b commit e2416a2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/language-service/src/language_service.ts
Expand Up @@ -608,10 +608,13 @@ function isInAngularContext(program: ts.Program, fileName: string, position: num
return false;
}

const asgn = getPropertyAssignmentFromValue(node, 'template') ??
const assignment = getPropertyAssignmentFromValue(node, 'template') ??
getPropertyAssignmentFromValue(node, 'templateUrl') ??
getPropertyAssignmentFromValue(node.parent, 'styleUrls');
return asgn !== null && getClassDeclFromDecoratorProp(asgn) !== null;
// `node.parent` is used because the string is a child of an array element and we want to get
// the property name
getPropertyAssignmentFromValue(node.parent, 'styleUrls') ??
getPropertyAssignmentFromValue(node, 'styleUrl');
return assignment !== null && getClassDeclFromDecoratorProp(assignment) !== null;
}

function findTightestNodeAtPosition(program: ts.Program, fileName: string, position: number) {
Expand Down
24 changes: 24 additions & 0 deletions packages/language-service/test/legacy/definitions_spec.ts
Expand Up @@ -532,6 +532,30 @@ describe('definitions', () => {
expect(def.textSpan).toEqual({start: 0, length: 0});
});

it('should be able to find a stylesheet from a style url', () => {
const {position, text} = service.overwrite(APP_COMPONENT, `
import {Component} from '@angular/core';
@Component({
template: 'empty',
styleUrl: './te¦st.css'
})
export class AppComponent {}`);
const result = ngLS.getDefinitionAndBoundSpan(APP_COMPONENT, position);


expect(result).toBeDefined();
const {textSpan, definitions} = result!;

expect(text.substring(textSpan.start, textSpan.start + textSpan.length))
.toEqual('./test.css');

expect(definitions).toBeDefined();
expect(definitions!.length).toBe(1);
const [def] = definitions!;
expect(def.fileName).toContain('/app/test.css');
expect(def.textSpan).toEqual({start: 0, length: 0});
});

xit('should be able to find a resource url with malformed component meta', () => {
const {position, text} = service.overwrite(APP_COMPONENT, `
import {Component} from '@angular/core';
Expand Down

0 comments on commit e2416a2

Please sign in to comment.