Skip to content

Commit

Permalink
Merge branch 'component-context-info-origin' into suchita/failedcase
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart authored Apr 11, 2021
2 parents 2514238 + 7cfc987 commit fe9ff51
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 40 deletions.
31 changes: 22 additions & 9 deletions src/project-roots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default class ProjectRoots {
projects = new Map<string, Project>();

localAddons: string[] = [];
ignoredProjects: string[] = [];

reloadProjects() {
Array.from(this.projects).forEach(([root]) => {
Expand Down Expand Up @@ -52,6 +53,10 @@ export default class ProjectRoots {
});
}

setIgnoredProjects(ignoredProjects: string[]) {
this.ignoredProjects = ignoredProjects;
}

findProjectsInsideRoot(workspaceRoot: string) {
const roots = walkSync(workspaceRoot, {
directories: false,
Expand Down Expand Up @@ -138,16 +143,24 @@ export default class ProjectRoots {
*/
const rootMap: { [key: string]: string } = {};

const projectRoots = (Array.from(this.projects.keys()) || []).map((root) => {
const lowerName = root.toLowerCase();
const projectRoots = (Array.from(this.projects.keys()) || [])
.map((root) => {
const projectName = this.projects.get(root)?.name;

if (projectName && this.ignoredProjects.includes(projectName)) {
return;
}

rootMap[lowerName] = root;
const lowerName = root.toLowerCase();

return lowerName;
});
rootMap[lowerName] = root;

return lowerName;
})
.filter((item) => item !== undefined) as string[];

const rawRoot = projectRoots
.filter((root) => isRootStartingWithFilePath(filePath, root))
.filter((root) => isRootStartingWithFilePath(root, filePath))
.reduce((a, b) => {
return a.length > b.length ? a : b;
}, '');
Expand All @@ -167,9 +180,9 @@ export default class ProjectRoots {
====================
it's safe to do, because root will be non empty if addon already registered as Project
*/
const fistSubRoot = Array.from(this.projects.values()).find((project) => {
return project.roots.some((subRoot) => isRootStartingWithFilePath(subRoot.toLocaleLowerCase(), filePath));
});
const fistSubRoot = Array.from(this.projects.values())
.filter((project) => project.name && !this.ignoredProjects.includes(project.name))
.find((project) => project.roots.some((subRoot) => isRootStartingWithFilePath(subRoot.toLocaleLowerCase(), filePath)));

if (fistSubRoot) {
return fistSubRoot;
Expand Down
3 changes: 2 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ export default class Server {
this.connection.workspace.onDidChangeWorkspaceFolders(this.onDidChangeWorkspaceFolders.bind(this));
}

this.executors['els.setConfig'] = async (_, __, [config]: [{ local: { addons: string[] } }]) => {
this.executors['els.setConfig'] = async (_, __, [config]: [{ local: { addons: string[]; ignoredProjects: string[] } }]) => {
this.projectRoots.setLocalAddons(config.local.addons);
this.projectRoots.setIgnoredProjects(config.local.ignoredProjects);

if (this.lazyInit) {
this.executeInitializers();
Expand Down
107 changes: 106 additions & 1 deletion test/integration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,7 @@ describe('integration', function () {
dependencies: { 'ember-holy-futuristic-template-namespacing-batman': '^1.0.2' },
}),
},
'lib/biz/addon/templates/components/bar.hbs',
'full-project/lib/biz/addon/templates/components/bar.hbs',
{ line: 0, character: 2 },
'full-project'
);
Expand Down Expand Up @@ -1577,6 +1577,111 @@ describe('integration', function () {
expect(result.response).toMatchSnapshot();
});

describe('Project class resolution, based on fs path and file structure', () => {
it('able to resolve main project if top-level addon is registered', async () => {
const files = {
'full-project/app/components': {
'foo.hbs': '',
'bar.hbs': '',
},
'full-project/package.json': JSON.stringify({
name: 'full-project',
'ember-addon': {
paths: ['../lib'],
},
}),
lib: {
'package.json': JSON.stringify({
name: 'my-addon',
keywords: ['ember-addon'],
}),
'index.js': '',
'addon/components/item.hbs': '<',
},
};

const result = await getResult(CompletionRequest.method, connection, files, 'lib/addon/components/item.hbs', { line: 0, character: 1 }, 'full-project');

expect(result.response.length).toBe(3);
});

it('without ignoring main project returns one only top level result', async () => {
const files = {
'child-project/app/components': {
'foo.hbs': '',
'bar.hbs': '',
},
'child-project/package.json': JSON.stringify({
name: 'child-project',
'ember-addon': {
paths: ['../lib'],
},
}),
lib: {
'package.json': JSON.stringify({
name: 'my-addon',
keywords: ['ember-addon'],
}),
'index.js': '',
'addon/components/item.hbs': '<',
},
'package.json': JSON.stringify({
name: 'parent-project',
'ember-addon': {
paths: ['lib'],
},
}),
};

const result = await getResult(CompletionRequest.method, connection, files, 'lib/addon/components/item.hbs', { line: 0, character: 1 }, [
'',
'child-project',
]);

expect(result.length).toBe(2);
expect(result[0].response.length).toBe(1);
});

it('able to ignore main project in favor of child project', async () => {
const files = {
'child-project/app/components': {
'foo.hbs': '',
'bar.hbs': '',
},
'child-project/package.json': JSON.stringify({
name: 'child-project',
'ember-addon': {
paths: ['../lib'],
},
}),
lib: {
'package.json': JSON.stringify({
name: 'my-addon',
keywords: ['ember-addon'],
}),
'index.js': '',
'addon/components/item.hbs': '<',
},
'package.json': JSON.stringify({
name: 'parent-project',
}),
};

const result = await getResult(
CompletionRequest.method,
connection,
files,
'lib/addon/components/item.hbs',
{ line: 0, character: 1 },
['', 'child-project'],
{ local: { addons: [], ignoredProjects: ['parent-project'] } }
);

expect(result.length).toBe(2);
expect(result[0].response.length).toBe(3);
});
});

describe('Autocomplete works for broken templates', () => {
it('autocomplete information for component #1 {{', async () => {
const result = await getResult(
Expand Down
24 changes: 24 additions & 0 deletions test/test_helpers/integration-helpers-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ describe('normalizeToFs', () => {

expect(normalizeToFs(files)).toStrictEqual(expectedObj);
});

it('support corner case', () => {
const files = {
'full-project/app/components': {
'foo.hbs': '',
'bar.hbs': '',
},
'full-project/package.json': '',
};

const expectedObj = {
'full-project': {
'package.json': '',
app: {
components: {
'foo.hbs': '',
'bar.hbs': '',
},
},
},
};

expect(normalizeToFs(files)).toStrictEqual(expectedObj);
});
});

describe('flattenFsProject', () => {
Expand Down
Loading

0 comments on commit fe9ff51

Please # to comment.