Skip to content

Commit

Permalink
Integrate deprecation plugin and refactor file lookups
Browse files Browse the repository at this point in the history
- Added 'eslint-plugin-deprecation' to track and enforce deprecation rules within the codebase
- Enhanced ESLint configuration to check against deprecated code usage
- Refined file search methods in FileCache to use 'findFileByLinkText' for resolving file links
- Updated dependent components and models to utilize the new file lookup method
- Incremented package version to reflect new features and improvements
- Deprecated older file lookup methods with advisory comments and recommendations for alternatives

- Version bump to V0.0.31
  • Loading branch information
PxaMMaxP committed Jan 16, 2024
1 parent 8e166fc commit 9dd0f5b
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 18 deletions.
9 changes: 6 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
"parser": "@typescript-eslint/parser",
"env": { "node": true },
"plugins": [
"@typescript-eslint"
"@typescript-eslint",
"deprecation"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"sourceType": "module"
"sourceType": "module",
"project": "./tsconfig.json"
},
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", { "args": "none" }],
"@typescript-eslint/ban-ts-comment": "off",
"no-prototype-builtins": "off",
"@typescript-eslint/no-empty-function": "off"
"@typescript-eslint/no-empty-function": "off",
"deprecation/deprecation": "error"
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "prj",
"name": "Prj Plugin",
"version": "0.0.30",
"version": "0.0.31",
"minAppVersion": "0.15.0",
"description": "Prj Plugin - Project, Document, and Task Management",
"author": "M. Passarello",
Expand Down
163 changes: 160 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-sample-plugin",
"version": "0.0.30",
"version": "0.0.31",
"description": "Prj Plugin - Project, Document, and Task Management",
"main": "main.js",
"scripts": {
Expand All @@ -24,6 +24,7 @@
"@typescript-eslint/parser": "5.29.0",
"builtin-modules": "3.3.0",
"esbuild": "0.17.3",
"eslint-plugin-deprecation": "^2.0.0",
"obsidian": "latest",
"obsidian-dataview": "^0.4.21",
"quicktype": "^23.0.81",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export default class DocumentComponents {
.setLinkType("file")
.setFormator((value: string) => {
const baseFileData = Helper.extractDataFromWikilink(documentModel.data.file);
const baseFile = fileCache.findFileByName(baseFileData.filename ?? "");
const baseFile = fileCache.findFileByLinkText(baseFileData.filename ?? "", documentModel.file.path);
let baseFilePath = baseFileData.filename ?? "";
if (baseFile && baseFile instanceof TFile) {
baseFilePath = baseFile.path;
Expand Down
18 changes: 18 additions & 0 deletions src/libs/FileCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ export default class FileCache {
* Returns the file from the file cache
* @param fileName The name of the file to find
* @returns The file/s if found, undefined otherwise
* @deprecated Use `findFileByLinkText` instead
* @see {@link findFileByLinkText}
*/
public findFileByName(fileName: string): TFile | Array<TFile> | undefined {
const foundFile = this.fileCache?.get(fileName);
Expand All @@ -329,8 +331,11 @@ export default class FileCache {
* Returns the first file from the file cache or if no file found returns undefined
* @param fileName The name of the file to find
* @returns The file as `TFile` if found, `undefined` otherwise
* @deprecated Use `findFileByLinkText` instead
* @see {@link findFileByLinkText}
*/
public findFirstFileByName(fileName: string): TFile | undefined {
// eslint-disable-next-line deprecation/deprecation
const foundFile = this.findFileByName(fileName);
if (foundFile instanceof Array) {
return foundFile.first();
Expand All @@ -343,11 +348,24 @@ export default class FileCache {
* Returns the file from the file cache
* @param filePath The path of the file to find
* @returns The file/s if found, undefined otherwise
* @deprecated Use `findFileByLinkText` instead
* @see {@link findFileByLinkText}
*/
public findFileByPath(filePath: string): TFile | Array<TFile> | undefined {
const fileName = this.getFileNameFromPath(filePath);
if (!fileName) { this.logger.error("File name not available"); return undefined; }
// eslint-disable-next-line deprecation/deprecation
return this.findFileByName(fileName);
}

/**
* Returns the first file from the file cache or if no file found returns undefined.
* @param linkText The link text of the file to find.
* @param sourcePath The original path of the file from which the link originates.
* @returns The file as `TFile` if found, `undefined` otherwise.
*/
public findFileByLinkText(linkText: string, sourcePath = ""): TFile | undefined {
return this.app.metadataCache.getFirstLinkpathDest(linkText, sourcePath) ?? undefined;
}

}
2 changes: 1 addition & 1 deletion src/libs/Modals/CreateNewMetadataModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default class CreateNewMetadataModal extends BaseModalForm {
document.data.subType = PrjTypes.isValidFileSubType(result.data.subType);

if (!document.data.subType && result.data.file) {
const linkedFile = this.fileCache.findFirstFileByName(result.data.file as string);
const linkedFile = this.fileCache.findFileByLinkText(result.data.file as string, "");
document.setLinkedFile(linkedFile, folder);
}

Expand Down
12 changes: 5 additions & 7 deletions src/models/DocumentModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class DocumentModel extends FileModel<DocumentData> implements IPrjModel<
relatedFiles.map((relatedFile) => {
const wikilinkData = Helper.extractDataFromWikilink(relatedFile);
const mdFilename = wikilinkData.basename ? `${wikilinkData.basename}.md` : "";
const file = this.fileCache.findFileByName(mdFilename);
const file = this.fileCache.findFileByLinkText(mdFilename, this.file.path);
if (file instanceof TFile && file.path !== this.file.path) {
this._relatedFiles?.push(new DocumentModel(file));
}
Expand Down Expand Up @@ -140,15 +140,13 @@ export class DocumentModel extends FileModel<DocumentData> implements IPrjModel<
*/
public getFile(): TFile | undefined {
const fileLinkData = Helper.extractDataFromWikilink(this.data.file);
const file = this.fileCache.findFileByName(fileLinkData.filename ?? "");
const file = this.fileCache.findFileByLinkText(fileLinkData.filename ?? "", this.file.path);
if (file instanceof TFile) {
return file;
} else if (Array.isArray(file)) {
this.logger.warn(`Multiple files found for ${fileLinkData.filename}`);
return file.first() ?? this.file;
} else {
this.logger.warn(`No files found for ${fileLinkData.filename}`);
return undefined;
}
this.logger.warn(`No files found for ${fileLinkData.filename}`);
return undefined;
}


Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"0.0.27": "0.15.0",
"0.0.28": "0.15.0",
"0.0.29": "0.15.0",
"0.0.30": "0.15.0"
"0.0.30": "0.15.0",
"0.0.31": "0.15.0"
}

0 comments on commit 9dd0f5b

Please sign in to comment.