Skip to content

Commit

Permalink
fix(ts-types): add type guards to any type (#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabertazimi authored May 8, 2022
1 parent 5f341f0 commit 66acbaa
Show file tree
Hide file tree
Showing 17 changed files with 309 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"instanceof",
"katex",
"keyof",
"mdast",
"monokai",
"nocopy",
"noline",
Expand All @@ -17,6 +18,7 @@
"stylelint",
"tailwindcss",
"typeof",
"unist",
"vercel"
]
}
4 changes: 2 additions & 2 deletions components/CodeBlocks/BlockCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import theme from './monokai';
interface Props {
enableLine: boolean;
lines: Set<number>;
language?: string;
language: Language;
children: string;
}

Expand All @@ -20,7 +20,7 @@ const BlockCode = ({
<Highlight
{...defaultProps}
code={children}
language={language as Language}
language={language}
theme={theme}
>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
Expand Down
2 changes: 1 addition & 1 deletion components/CodeBlocks/LiveCode.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import LiveCode from './LiveCode';
describe('LiveCode', () => {
test('should render live code correctly (snapshot)', () => {
const { container } = render(
<LiveCode language="ts">const foo = bar();</LiveCode>
<LiveCode language="typescript">const foo = bar();</LiveCode>
);

expect(container).toMatchSnapshot();
Expand Down
4 changes: 2 additions & 2 deletions components/CodeBlocks/LiveCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import styles from './LiveCode.module.css';
import theme from './monokai';

interface Props {
language?: string;
language?: Language;
children?: string;
}

Expand All @@ -23,7 +23,7 @@ const LiveCode = ({ language, children }: Props): JSX.Element => (
<LiveProvider
scope={scope}
code={children}
language={language as Language}
language={language}
theme={theme}
>
<LiveEditor padding="1.25rem" className={styles.editor} />
Expand Down
20 changes: 15 additions & 5 deletions components/CodeBlocks/Pre.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { classNames, dynamic } from '@components/utils';
import type { HTMLProps, ReactElement } from 'react';
import BlockCode from './BlockCode';
import styles from './Pre.module.css';
import { normalizeCode, normalizeLanguage, normalizeLines } from './utils';
import {
normalizeCode,
normalizeLanguage,
normalizeLanguageName,
normalizeLines,
} from './utils';

const LiveCode = dynamic(() => import('./LiveCode'));

Expand All @@ -15,6 +20,11 @@ interface Props extends HTMLProps<HTMLPreElement> {
lines?: string;
}

interface CodeProps {
children?: string;
className?: string;
}

const Pre = ({
live = false,
noline = false,
Expand All @@ -24,11 +34,11 @@ const Pre = ({
children,
className,
}: Props): JSX.Element => {
const codeElement = children as ReactElement;
const codeElement = children as ReactElement<CodeProps>;
const code = normalizeCode(codeElement?.props?.children);
const languageClass = codeElement?.props?.className as string;
const language = languageClass?.replace('language-', '');
const languageName = normalizeLanguage(language);
const languageClass = codeElement?.props?.className;
const language = normalizeLanguage(languageClass);
const languageName = normalizeLanguageName(language);
const highlightLines = normalizeLines(lines);

return (
Expand Down
Loading

1 comment on commit 66acbaa

@vercel
Copy link

@vercel vercel bot commented on 66acbaa May 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

blog – ./

blog-git-main-sabertaz.vercel.app
blog.tazimi.dev
blog-sabertaz.vercel.app

Please sign in to comment.