Skip to content

Commit

Permalink
feat(ast-utils.ts): add support for resolving import URIs with variou…
Browse files Browse the repository at this point in the history
…s conditions to handle different scenarios and improve flexibility in importing modules
  • Loading branch information
TGTGamer committed Feb 20, 2024
1 parent b3262db commit 663e247
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/schema/src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { isFromStdlib } from '@zenstackhq/sdk';
import { AstNode, getDocument, LangiumDocuments, Mutable } from 'langium';
import { URI, Utils } from 'vscode-uri';
import { findNodeModulesFile } from './pkg-utils';

export function extractDataModelsWithAllowRules(model: Model): DataModel[] {
return model.declarations.filter(
Expand Down Expand Up @@ -94,15 +95,23 @@ export function getDataModelFieldReference(expr: Expression): DataModelField | u
}

export function resolveImportUri(imp: ModelImport): URI | undefined {
if (imp.path === undefined || imp.path.length === 0) {
return undefined;
if (!imp.path) return undefined; // This will return true if imp.path is undefined, null, or an empty string ("").

if (!imp.path.endsWith('.zmodel')) {
imp.path += '.zmodel';
}

if (
!imp.path.startsWith('.') // Respect relative paths
&& !imp.path.startsWith('/') // Respect absolute paths (Unix)
&& !/^[a-zA-Z]:\\/.test(imp.path) // Respect absolute paths (Windows)
) {
imp.path = findNodeModulesFile(imp.path) ?? '';
if (!imp.path) return undefined; // If the path is empty, return undefined
}

const dirUri = Utils.dirname(getDocument(imp).uri);
let grammarPath = imp.path;
if (!grammarPath.endsWith('.zmodel')) {
grammarPath += '.zmodel';
}
return Utils.resolvePath(dirUri, grammarPath);
return Utils.resolvePath(dirUri, imp.path);
}

export function resolveTransitiveImports(documents: LangiumDocuments, model: Model): Model[] {
Expand Down

0 comments on commit 663e247

Please sign in to comment.