Skip to content

Commit

Permalink
feat: Support jump to definition from parent to child app and tests (l…
Browse files Browse the repository at this point in the history
…ifeart#234)

* failing parent defintion case

* failing parent to child defintion case

* update snapshot

* add support for test definitions and parent to child imports

* address comments

* address comments
  • Loading branch information
suchitadoshi1987 authored Apr 12, 2021
1 parent f29b112 commit 24f33e5
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/builtin-addons/core/script-definition-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ class PathResolvers {

return joinPaths(...appParams);
}

resolveTestScopeImport(root: string, pathName: string) {
return joinPaths(path.join(root, pathName));
}

muImportPaths(root: string, pathName: string) {
const pathParts = pathName.split('/');

Expand Down Expand Up @@ -185,12 +190,30 @@ export default class CoreScriptDefinitionProvider {
} else if (isImportSpecifier(astPath)) {
logInfo(`Handle script import for Project "${project.name}"`);
const pathName: string = ((astPath.parentFromLevel(2) as unknown) as t.ImportDeclaration).source.value;
const pathParts = pathName.split('/');
let maybeAppName = pathParts.shift();

project.roots.forEach((projectRoot) => {
const potentialPaths = this.guessPathForImport(projectRoot, uri, pathName) || [];
if (maybeAppName && maybeAppName.startsWith('@')) {
maybeAppName = maybeAppName + '/' + pathParts.shift();
}

definitions = definitions.concat(potentialPaths);
});
let potentialPaths: Location[];
const addonInfo = project.addonsMeta.find(({ name }) => pathName.startsWith(name + '/tests'));

// If the start of the pathname is same as the project name, then use that as the root.
if (project.name === maybeAppName && pathName.startsWith(project.name + '/tests')) {
const importPaths = this.resolvers.resolveTestScopeImport(project.root, pathParts.join(path.sep));

potentialPaths = pathsToLocations(...importPaths);
} else if (addonInfo) {
const importPaths = this.resolvers.resolveTestScopeImport(addonInfo.root, pathName);

potentialPaths = pathsToLocations(...importPaths);
} else {
potentialPaths = this.guessPathForImport(project.root, uri, pathName) || [];
}

definitions = definitions.concat(potentialPaths);
} else if (isServiceInjection(astPath)) {
let serviceName = ((astPath.node as unknown) as t.Identifier).name;
const args = astPath.parent.value.arguments;
Expand Down
48 changes: 48 additions & 0 deletions test/__snapshots__/integration-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2798,6 +2798,54 @@ Object {
}
`;
exports[`integration Project class resolution, based on fs path and file structure support parent project addon calling child project 1`] = `
Object {
"addonsMeta": Array [
Object {
"name": "biz",
"root": "lib/biz",
},
Object {
"name": "foo",
"root": "../lib/foo",
},
],
"registry": Object {
"component": Object {
"bar": Array [
"../lib/foo/addon/components/bar.js",
"lib/biz/addon/components/bar.js",
],
},
"helper": Object {
"blah": Array [
"tests/helpers/blah.js",
],
},
"routePath": Object {
"hello": Array [
"app/templates/hello.hbs",
],
},
},
"response": Array [
Object {
"range": Object {
"end": Object {
"character": 0,
"line": 0,
},
"start": Object {
"character": 0,
"line": 0,
},
},
"uri": "/tests/helpers/blah.js",
},
],
}
`;
exports[`integration autocomplete works for angle component slots 1`] = `
Array [
Object {
Expand Down
52 changes: 52 additions & 0 deletions test/integration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,58 @@ describe('integration', function () {
expect(result.length).toBe(2);
expect(result[0].response.length).toBe(3);
});

it('support parent project addon calling child project', async () => {
const result = await getResult(
DefinitionRequest.method,
connection,
{
'full-project': {
'app/templates/hello.hbs': '',
'tests/helpers/blah.js': '',
lib: {
biz: {
'addon/components/bar.js': '',
'package.json': JSON.stringify({
name: 'biz',
keywords: ['ember-addon'],
dependencies: {},
'ember-addon': {
paths: ['../../../lib/foo'],
},
}),
'index.js': '',
},
},
'package.json': JSON.stringify({
name: 'full-project',
dependencies: { 'ember-holy-futuristic-template-namespacing-batman': '^1.0.2' },
'ember-addon': {
paths: ['lib/biz'],
},
}),
},
lib: {
foo: {
addon: {
'components/bar.js': 'import Blah from "full-project/tests/helpers/blah"',
},
'package.json': JSON.stringify({
name: 'foo',
keywords: ['ember-addon'],
dependencies: {},
}),
'index.js': '',
},
},
},
'lib/foo/addon/components/bar.js',
{ line: 0, character: 8 },
'full-project'
);

expect(result).toMatchSnapshot();
});
});

describe('Autocomplete works for broken templates', () => {
Expand Down

0 comments on commit 24f33e5

Please # to comment.