refactor: reduce ESLint warnings / better typing (#5242)

* Fix code block children type

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Add return type

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Add types

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Fix return types

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Fix details type

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Fix type

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
This commit is contained in:
Joshua Chen 2021-07-29 23:59:38 +08:00 committed by GitHub
parent a59378ba87
commit 9e615eff02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 38 additions and 21 deletions

View file

@ -62,7 +62,7 @@ const getHighlightDirectiveRegex = (
return new RegExp(`^\\s*(?:${commentPattern})\\s*$`);
};
// select comment styles based on language
const highlightDirectiveRegex = (lang) => {
const highlightDirectiveRegex = (lang: string) => {
switch (lang) {
case 'js':
case 'javascript':
@ -119,11 +119,12 @@ export default function CodeBlock({
const prismTheme = usePrismTheme();
// In case interleaved Markdown (e.g. when using CodeBlock as standalone component).
const content = Array.isArray(children) ? children.join('') : children;
const content = Array.isArray(children)
? children.join('')
: (children as string);
if (metastring && highlightLinesRangeRegex.test(metastring)) {
// Tested above
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const highlightLinesRange = metastring.match(highlightLinesRangeRegex)![1];
highlightLines = rangeParser(highlightLinesRange).filter((n) => n > 0);
}
@ -145,7 +146,7 @@ export default function CodeBlock({
const directiveRegex = highlightDirectiveRegex(language);
// go through line by line
const lines = content.replace(/\n$/, '').split('\n');
let blockStart;
let blockStart: number;
// loop through lines
for (let index = 0; index < lines.length; ) {
const line = lines[index];
@ -169,7 +170,7 @@ export default function CodeBlock({
break;
case 'highlight-end':
range += `${blockStart}-${lineNumber - 1},`;
range += `${blockStart!}-${lineNumber - 1},`;
break;
default: