Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encapsulate LS file-system acces logic into FS provider #303

Merged
merged 4 commits into from
Oct 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,15 @@
"dependencies": {
"@glimmer/syntax": "^0.73.1",
"@lifeart/ember-extract-inline-templates": "^2.0.0",
"@types/estree": "^0.0.46",
"@types/fuzzaldrin": "^2.1.3",
"@types/lodash": "^4.14.167",
"@types/memoizee": "^0.4.5",
"ast-types": "^0.14.2",
"cross-spawn": "^7.0.3",
"dag-map": "^2.0.2",
"ember-meta-explorer": "^0.2.1",
"ember-template-recast": "^5.0.1",
"find-up": "^5.0.0",
"fs-extra": "^8.1.0",
"fuzzaldrin": "^2.1.0",
"i": "^0.3.6",
"lodash": "^4.17.20",
"memoizee": "^0.4.15",
"rimraf": "^3.0.2",
"semver": "^7.3.5",
"util": "^0.12.3",
"vscode-jsonrpc": "6.0.0",
"vscode-languageserver": "^7.0.0",
"vscode-languageserver-protocol": "^3.16.0",
Expand All @@ -54,6 +45,13 @@
"walk-sync": "^2.2.0"
},
"devDependencies": {
"rimraf": "^3.0.2",
"cross-spawn": "^7.0.3",
"@types/cross-spawn": "^6.0.2",
"@types/estree": "^0.0.46",
"@types/fuzzaldrin": "^2.1.3",
"@types/lodash": "^4.14.167",
"@types/memoizee": "^0.4.5",
"@babel/types": "^7.12.12",
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@semantic-release/changelog": "^5.0.1",
Expand Down
10 changes: 5 additions & 5 deletions src/builtin-addons/core/script-completion-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default class ScriptCompletionProvider {
}

if (!this.meta.projectAddonsInfoInitialized) {
mGetProjectAddonsInfo(root);
await mGetProjectAddonsInfo(root);
this.enableRegistryCache('projectAddonsInfoInitialized');
this.project.invalidateRegistry();
}
Expand All @@ -102,12 +102,12 @@ export default class ScriptCompletionProvider {
log('isRouteLookup');

if (!this.meta.routesRegistryInitialized) {
mListRoutes(this.project);
await mListRoutes(this.project);
this.enableRegistryCache('routesRegistryInitialized');
}

if (!this.meta.projectAddonsInfoInitialized) {
mGetProjectAddonsInfo(root);
await mGetProjectAddonsInfo(root);
this.enableRegistryCache('projectAddonsInfoInitialized');
this.project.invalidateRegistry();
}
Expand All @@ -130,7 +130,7 @@ export default class ScriptCompletionProvider {
}

if (!this.meta.projectAddonsInfoInitialized) {
mGetProjectAddonsInfo(root);
await mGetProjectAddonsInfo(root);
this.enableRegistryCache('projectAddonsInfoInitialized');
this.project.invalidateRegistry();
}
Expand Down Expand Up @@ -185,7 +185,7 @@ export default class ScriptCompletionProvider {
}

if (!this.meta.projectAddonsInfoInitialized) {
mGetProjectAddonsInfo(root);
await mGetProjectAddonsInfo(root);
this.enableRegistryCache('projectAddonsInfoInitialized');
this.project.invalidateRegistry();
}
Expand Down
85 changes: 42 additions & 43 deletions src/builtin-addons/core/script-definition-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
isImportSpecifier,
} from './../../utils/ast-helpers';
import { normalizeServiceName } from '../../utils/normalizers';
import { isModuleUnificationApp, podModulePrefixForRoot } from './../../utils/layout-helpers';
import { asyncFilter, podModulePrefixForRoot } from './../../utils/layout-helpers';
import { provideRouteDefinition } from './template-definition-provider';
import { logInfo } from '../../utils/logger';
import { Project } from '../../project';
Expand All @@ -26,6 +26,7 @@ type ItemType = 'Model' | 'Transform' | 'Service';
// barking on 'LayoutCollectorFn' is defined but never used @typescript-eslint/no-unused-vars
// eslint-disable-line
type LayoutCollectorFn = (root: string, itemName: string, podModulePrefix?: string) => string[];
type AsyncLayoutCollectorFn = (root: string, itemName: string, podModulePrefix?: string) => Promise<string[]>;

function joinPaths(...args: string[]) {
return ['.ts', '.js'].map((extName: string) => {
Expand All @@ -37,7 +38,7 @@ function joinPaths(...args: string[]) {
}

class PathResolvers {
[key: string]: LayoutCollectorFn;
[key: string]: LayoutCollectorFn | AsyncLayoutCollectorFn;
muModelPaths(root: string, modelName: string) {
return joinPaths(root, 'src', 'data', 'models', modelName, 'model');
}
Expand Down Expand Up @@ -65,11 +66,11 @@ class PathResolvers {
podServicePaths(root: string, modelName: string, podPrefix: string) {
return joinPaths(root, 'app', podPrefix, modelName, 'service');
}
addonServicePaths(root: string, serviceName: string) {
return getAddonPathsForType(root, 'services', serviceName);
async addonServicePaths(root: string, serviceName: string): Promise<string[]> {
return await getAddonPathsForType(root, 'services', serviceName);
}
addonImportPaths(root: string, pathName: string) {
return getAddonImport(root, pathName);
async addonImportPaths(root: string, pathName: string) {
return await getAddonImport(root, pathName);
}
classicImportPaths(root: string, pathName: string) {
const pathParts = pathName.split('/');
Expand Down Expand Up @@ -108,57 +109,53 @@ export default class CoreScriptDefinitionProvider {
this.server = server;
this.project = project;
}
guessPathForImport(root: string, uri: string, importPath: string) {
async guessPathForImport(root: string, uri: string, importPath: string) {
if (!uri) {
return null;
}

const guessedPaths: string[] = [];
const fnName = 'Import';

if (isModuleUnificationApp(root)) {
this.resolvers[`mu${fnName}Paths`](root, importPath).forEach((pathLocation: string) => {
guessedPaths.push(pathLocation);
});
} else {
this.resolvers[`classic${fnName}Paths`](root, importPath).forEach((pathLocation: string) => {
guessedPaths.push(pathLocation);
});
}
(await this.resolvers[`classic${fnName}Paths`](root, importPath)).forEach((pathLocation: string) => {
guessedPaths.push(pathLocation);
});

this.resolvers.addonImportPaths(root, importPath).forEach((pathLocation: string) => {
const addonImports = await this.resolvers.addonImportPaths(root, importPath);

addonImports.forEach((pathLocation: string) => {
guessedPaths.push(pathLocation);
});

return pathsToLocations(...guessedPaths);
const existingPaths = await asyncFilter(guessedPaths, this.server.fs.exists);

return pathsToLocations(...existingPaths);
}
guessPathsForType(root: string, fnName: ItemType, typeName: string) {
async guessPathsForType(root: string, fnName: ItemType, typeName: string) {
const guessedPaths: string[] = [];

if (isModuleUnificationApp(root)) {
this.resolvers[`mu${fnName}Paths`](root, typeName).forEach((pathLocation: string) => {
guessedPaths.push(pathLocation);
});
} else {
this.resolvers[`classic${fnName}Paths`](root, typeName).forEach((pathLocation: string) => {
(await this.resolvers[`classic${fnName}Paths`](root, typeName)).forEach((pathLocation: string) => {
guessedPaths.push(pathLocation);
});
const podPrefix = podModulePrefixForRoot(root);

if (podPrefix) {
(await this.resolvers[`pod${fnName}Paths`](root, typeName, podPrefix)).forEach((pathLocation: string) => {
guessedPaths.push(pathLocation);
});
const podPrefix = podModulePrefixForRoot(root);

if (podPrefix) {
this.resolvers[`pod${fnName}Paths`](root, typeName, podPrefix).forEach((pathLocation: string) => {
guessedPaths.push(pathLocation);
});
}
}

if (fnName === 'Service') {
this.resolvers.addonServicePaths(root, typeName).forEach((item: string) => {
const paths = await this.resolvers.addonServicePaths(root, typeName);

paths.forEach((item: string) => {
guessedPaths.push(item);
});
}

return pathsToLocations(...guessedPaths);
const existingPaths = await asyncFilter(guessedPaths, this.server.fs.exists);

return pathsToLocations(...existingPaths);
}
async onDefinition(root: string, params: DefinitionFunctionParams): Promise<Definition | null> {
const { textDocument, focusPath, type, results, server, position } = params;
Expand Down Expand Up @@ -192,13 +189,13 @@ export default class CoreScriptDefinitionProvider {
} else if (isModelReference(astPath)) {
const modelName = ((astPath.node as unknown) as t.StringLiteral).value;

definitions = this.guessPathsForType(root, 'Model', modelName);
definitions = await this.guessPathsForType(root, 'Model', modelName);
} else if (isTransformReference(astPath)) {
const transformName = ((astPath.node as unknown) as t.StringLiteral).value;

definitions = this.guessPathsForType(root, 'Transform', transformName);
definitions = await this.guessPathsForType(root, 'Transform', transformName);
} else if (isImportPathDeclaration(astPath)) {
definitions = this.guessPathForImport(root, uri, ((astPath.node as unknown) as t.StringLiteral).value) || [];
definitions = (await this.guessPathForImport(root, uri, ((astPath.node as unknown) as t.StringLiteral).value)) || [];
} 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;
Expand All @@ -215,14 +212,16 @@ export default class CoreScriptDefinitionProvider {
// 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));
const existingPaths = await asyncFilter(importPaths, this.server.fs.exists);

potentialPaths = pathsToLocations(...importPaths);
potentialPaths = pathsToLocations(...existingPaths);
} else if (addonInfo) {
const importPaths = this.resolvers.resolveTestScopeImport(addonInfo.root, pathName);
const existingPaths = await asyncFilter(importPaths, this.server.fs.exists);

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

definitions = definitions.concat(potentialPaths);
Expand All @@ -234,15 +233,15 @@ export default class CoreScriptDefinitionProvider {
serviceName = args[0].value;
}

definitions = this.guessPathsForType(root, 'Service', normalizeServiceName(serviceName));
definitions = await this.guessPathsForType(root, 'Service', normalizeServiceName(serviceName));
} else if (isNamedServiceInjection(astPath)) {
const serviceName = ((astPath.node as unknown) as t.StringLiteral).value;

definitions = this.guessPathsForType(root, 'Service', normalizeServiceName(serviceName));
definitions = await this.guessPathsForType(root, 'Service', normalizeServiceName(serviceName));
} else if (isRouteLookup(astPath)) {
const routePath = ((astPath.node as unknown) as t.StringLiteral).value;

definitions = provideRouteDefinition(this.registry, routePath);
definitions = await provideRouteDefinition(this.registry, routePath, this.server.fs);
}

return definitions || [];
Expand Down
Loading