Skip to content

Commit

Permalink
Upgrade formatter tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Sep 4, 2024
1 parent 5355ff8 commit 01ae4c0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
28 changes: 10 additions & 18 deletions app/src/specs/validateFrontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Plugin } from 'unified';
import { parse as parseYaml, YAMLParseError } from 'yaml';
import { z, ZodError } from 'zod';

import { TracedError } from '../util/error';
import { ENSNameRegex, GithubUsernameRegex } from '../util/regex';

export type UnparsedFrontmatter = {
Expand Down Expand Up @@ -51,7 +52,6 @@ export const validateFrontmatter = (
return FrontMatterZod.parse(parsed);
} catch (error) {
if (error instanceof YAMLParseError) {
console.log(error.name);
const line =
frontmatter!.position.start.line +
(error.linePos?.[0].line || 0);
Expand All @@ -62,26 +62,18 @@ export const validateFrontmatter = (
frontmatter!.position.start.column +
(error.linePos?.[0].col || 0);

console.log(
'::error file=' +
directPath +
',line=' +
line +
',col=' +
column +
',endColumn=' +
endColumn +
'::' +
error.message
);
throw new TracedError(error, directPath, line, column, endColumn);
} else if (error instanceof ZodError) {
console.log(error.issues, frontmatter, directPath);
} else {
console.log(error);
throw new TracedError(
error,
directPath,
frontmatter!.position.start.line,
frontmatter!.position.start.column,
frontmatter!.position.end.column
);
}

// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
throw error;
}
};

Expand Down
17 changes: 9 additions & 8 deletions app/src/specs/validateTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,14 @@ export const extractTitle =
(node) => node.type === 'heading' && node.depth === 1
) as TitleNode[];

if (titleCount.length > 1) {
console.log(titleCount[1]);

if (titleCount.length > 1)
throw new TracedError(
'More then one h1 (#) heading found, please use h2 (##) or h3 (###) headings',
directPath,
titleCount[1]!.position.start.line,
titleCount[1]!.position.start.column,
titleCount[1]!.position.end.column
);
}

const first = (
(tree as any)['children'] as [
Expand All @@ -65,9 +62,9 @@ export const extractTitle =
]
).shift();

const x = TitleZod.parse(first);
const titleNode = TitleZod.parse(first);

const title = x.children
const title = titleNode.children
.reduce(
(accumulator, current) => accumulator + ' ' + current.value,
''
Expand All @@ -76,8 +73,12 @@ export const extractTitle =

// title must match regex
if (!titleRegex.test(title)) {
throw new Error(
'Invalid title format, please format title as "ENSIP-X: Title" (PR\'s) or "ENSIP-123: Title" (after merge)'
throw new TracedError(
'Invalid title format, please format title as "ENSIP-X: Title" (PR\'s) or "ENSIP-123: Title" (after merge)',
directPath,
titleNode.position.start.line,
titleNode.position.start.column,
titleNode.position.end.column
);
}

Expand Down

0 comments on commit 01ae4c0

Please sign in to comment.