Skip to content

Commit

Permalink
Feat: class based addon api (ember-tooling#131)
Browse files Browse the repository at this point in the history
* feat: class-based addon api

* remove some any
  • Loading branch information
lifeart authored May 30, 2020
1 parent 8fcac46 commit e74f11f
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/utils/addon-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface PublicAddonAPI {
onCodeAction?: CodeActionResolveFunction;
onInit?: InitFunction;
}

interface HandlerObject {
handler: PublicAddonAPI;
updateHandler: () => void;
Expand Down Expand Up @@ -113,12 +114,47 @@ export function initBuiltinProviders(): ProjectProviders {
};
}

export function isConstructor(obj: any) {
return !!obj.prototype && !!obj.prototype.constructor.name;
}

function create<T>(model: new () => T): T {
return new model();
}

function requireUncached(module: string) {
delete require.cache[require.resolve(module)];
let result = {};

try {
result = require(module);

if (isConstructor(result)) {
const instance: PublicAddonAPI = create(result as any);
const instanceResult: PublicAddonAPI = {};

if (typeof instance.onInit === 'function') {
instanceResult.onInit = instance.onInit.bind(instance);
}

if (typeof instance.onCodeAction === 'function') {
instanceResult.onCodeAction = instance.onCodeAction.bind(instance);
}

if (typeof instance.onComplete === 'function') {
instanceResult.onComplete = instance.onComplete.bind(instance);
}

if (typeof instance.onDefinition === 'function') {
instanceResult.onDefinition = instance.onDefinition.bind(instance);
}

if (typeof instance.onReference === 'function') {
instanceResult.onReference = instance.onReference.bind(instance);
}

return instance;
}
} catch (e) {
logError(e);
}
Expand All @@ -129,8 +165,8 @@ function requireUncached(module: string) {
export function collectProjectProviders(root: string, addons: string[]): ProjectProviders {
const roots = addons
.concat([root])
.concat(getProjectAddonsRoots(root) as any, getProjectInRepoAddonsRoots(root) as any)
.filter((pathItem: any) => typeof pathItem === 'string');
.concat(getProjectAddonsRoots(root), getProjectInRepoAddonsRoots(root))
.filter((pathItem) => typeof pathItem === 'string');
const dagMap: DAGMap<HandlerObject> = new DAGMap();

roots.forEach((packagePath: string) => {
Expand Down
27 changes: 27 additions & 0 deletions test/__snapshots__/integration-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,33 @@ Object {
}
`;

exports[`integration Able to use classes for API support dummy class-based addon definition:template 1`] = `
Object {
"registry": Object {
"component": Object {
"hello": Array [
"app/components/hello/index.hbs",
],
},
},
"response": Array [
Object {
"range": Object {
"end": Object {
"character": 0,
"line": 0,
},
"start": Object {
"character": 0,
"line": 0,
},
},
"uri": "/app/components/hello/index.hbs",
},
],
}
`;

exports[`integration Autocomplete works for broken templates autocomplete information for component #1 {{ 1`] = `
Array [
Object {
Expand Down
60 changes: 60 additions & 0 deletions test/integration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,66 @@ describe('integration', function () {
});
});

describe('Able to use classes for API', () => {
it('support dummy class-based addon definition:template', async () => {
const result = await getResult(
DefinitionRequest.type,
connection,
{
node_modules: {
provider: {
lib: {
'langserver.js': `
module.exports = class Boo { onDefinition(root) {
let path = require("path");
let filePath = path.resolve(path.normalize(path.join(__dirname, "./../../../app/components/hello/index.hbs")));
return [ {
"range": {
"end": {
"character": 0,
"line": 0,
},
"start": {
"character": 0,
"line": 0,
}
},
"uri": filePath
} ];
}}
`,
},
'package.json': JSON.stringify({
name: 'provider',
'ember-language-server': {
entry: './lib/langserver',
capabilities: {
definitionProvider: true,
},
},
}),
},
},
'package.json': JSON.stringify({
dependencies: {
provider: '*',
},
}),
app: {
components: {
hello: {
'index.hbs': '{{this}}',
},
},
},
},
'app/components/hello/index.hbs',
{ line: 0, character: 3 }
);

expect(result).toMatchSnapshot();
});
});
describe('Able to provide API:Definition', () => {
it('support dummy addon definition:template', async () => {
const result = await getResult(
Expand Down

0 comments on commit e74f11f

Please sign in to comment.