Skip to content

Commit

Permalink
feat: show error paths which failed to get parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenisx committed Apr 14, 2021
1 parent de578df commit 7f4cc4d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Position,
Range,
} from "vscode";
import path from "path";
import { DEFAULT_CONFIG, FILTER_REGEX } from "./constants";
import { createCompletionItems, setup } from "./main";
import { parseFiles } from "./parser";
Expand Down Expand Up @@ -36,7 +37,16 @@ export async function activate(context: ExtensionContext): Promise<void> {
return null;
}

const cssVars = await parseFiles(config);
const [cssVars, errorPaths] = await parseFiles(config);
if (errorPaths.length > 0) {
const relativePaths = errorPaths
.map(_path => _path.split(config.workspaceFolder + path.sep))
.map(path => (path.length > 0 ? path[1] : path[0]));
window.showWarningMessage(
"Failed to parse CSS variables in files:",
`\n\n${relativePaths.join("\n\n")}`
);
}
const completionItems = createCompletionItems(cssVars);
return new CompletionList(completionItems);
},
Expand Down
7 changes: 4 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,13 @@ const parseFile = async function (path: string, config: Config) {
*/
export const parseFiles = async function (
config: Config
): Promise<CSSVarRecord> {
): Promise<[CSSVarRecord, string[]]> {
updateCacheOnFileDelete();

let cssVars: CSSVarRecord = CACHE.cssVars;
const isModified =
Object.keys(CACHE.fileMetas).length !== config.files.length;
const errorPaths: string[] = [];

for (const path of config.files) {
const cachedFileMeta = CACHE.fileMetas[path];
Expand All @@ -124,7 +125,7 @@ export const parseFiles = async function (
try {
newVars = await parseFile(path, config);
} catch (e) {
// NOOP
errorPaths.push(path);
}
cssVars = {
...cssVars,
Expand Down Expand Up @@ -153,5 +154,5 @@ export const parseFiles = async function (
}

CACHE.cssVars = cssVars;
return CACHE.cssVars;
return [CACHE.cssVars, errorPaths];
};
3 changes: 2 additions & 1 deletion src/test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,14 @@ describe("Test Parser", () => {
...EXTENSION_CONFIG,
files: [RENAMED_FILE, BROKEN_FILE],
};
await parseFiles(updatedConfig);
const [_, errorPaths] = await parseFiles(updatedConfig);
expect(Object.keys(CACHE.cssVars).length).toBeGreaterThan(0);
expect(CACHE.cssVars[RENAMED_FILE][0]).toMatchObject({
property: "--red100",
value: "#f00",
} as CSSVarDeclarations);
expect(CACHE.cssVars[BROKEN_FILE].length).toBe(0);
expect(errorPaths[0]).toBe(BROKEN_FILE);
});
})
});

0 comments on commit 7f4cc4d

Please sign in to comment.