Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: ⬆️ Upgrade Prettier to v3 #1845

Merged
merged 13 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
node_modules
**/*.d.ts*
dist/
coverage/
storybook-static/
packages/tokens/brand/**/*
packages/theme/brand/**/*
packages/react-old/**/*
tsc-build/
.storybook
File renamed without changes.
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
packages/tokens/brand/**/*
packages/theme/brand/**/*
apps/storefront/tokens
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
"html-css-class-completion.excludeGlobPattern": "**/react-css-modules.css",
"cSpell.words": ["altinn", "brreg", "designsystemet", "digdir"],
"cSpell.language": "en,nb",
"cSpell.ignorePaths": ["**/node_modules/**", "**/package.json"],
"cSpell.ignorePaths": ["**/node_modules/**", "**/package.json", "yarn.lock"],
"cSpell.enabledLanguageIds": ["markdown", "plaintext"]
}
131 changes: 73 additions & 58 deletions apps/storefront/components/CodeSnippet/CodeSnippet.tsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,60 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { FilesIcon } from '@navikt/aksel-icons';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import prettier from 'prettier/standalone.js';
import parserJs from 'prettier/parser-flow.js';
import parserHtml from 'prettier/parser-markdown.js';
import parserCss from 'prettier/parser-postcss.js';
import parserTs from 'prettier/parser-typescript';
import type { Options } from 'prettier';

Check failure on line 4 in apps/storefront/components/CodeSnippet/CodeSnippet.tsx

View workflow job for this annotation

GitHub Actions / Builds, lints and tests code

'Options' is defined but never used

Check failure on line 4 in apps/storefront/components/CodeSnippet/CodeSnippet.tsx

View workflow job for this annotation

GitHub Actions / Builds, lints and tests code

'Options' is defined but never used
import { format } from 'prettier/standalone.js';
import * as prettierMarkdown from 'prettier/plugins/markdown.js';
import * as prettierHtml from 'prettier/plugins/html.js';
import * as prettierCSS from 'prettier/plugins/postcss.js';
import * as prettierTypescript from 'prettier/plugins/typescript.js';
import * as prettierEstree from 'prettier/plugins/estree';
import { nightOwl } from 'react-syntax-highlighter/dist/cjs/styles/prism';
import { Tooltip } from '@digdir/designsystemet-react';

import classes from './CodeSnippet.module.css';

const CodeSnippet = ({ language = 'markup', children = '' }) => {
const plugins = [
prettierTypescript,
prettierEstree,
prettierCSS,
prettierMarkdown,
prettierHtml,
];

type CodeSnippetProps = {
language?: 'css' | 'html' | 'ts' | 'markdown';
children?: string;
};

const CodeSnippet = ({
language = 'markdown',
children = '',
}: CodeSnippetProps) => {
const [toolTipText, setToolTipText] = useState('Kopier');
const [snippet, setSnippet] = useState('');

useEffect(() => {
async function formatSnippet(
children: string,
language: CodeSnippetProps['language'],
) {
try {
const formatted = await format(children, {
parser: language === 'ts' ? 'typescript' : language,
plugins,
});
setSnippet(formatted);
} catch (error) {
console.error('Failed formatting code snippet:', error);
}
}
void formatSnippet(children, language);

return () => {
setSnippet('');
};
}, [children, language]);

if (language === 'css' || language === 'scss') {
// eslint-disable-next-line import/no-named-as-default-member
children = prettier.format(children, {
parser: 'css',
plugins: [parserCss],
});
}
if (language === 'javascript') {
// eslint-disable-next-line import/no-named-as-default-member
children = prettier.format(children, {
parser: 'flow',
plugins: [parserJs],
tabWidth: 2,
semi: true,
});
}
if (language === 'markup') {
// eslint-disable-next-line import/no-named-as-default-member
children = prettier.format(children, {
parser: 'markdown',
plugins: [parserHtml],
});
}
if (language === 'ts') {
// eslint-disable-next-line import/no-named-as-default-member
children = prettier.format(children, {
parser: 'typescript',
plugins: [parserTs],
});
}
const onButtonClick = () => {
setToolTipText('Kopiert!');
navigator.clipboard.writeText(children).catch((reason) => {
Expand All @@ -53,27 +64,31 @@

return (
<div className={classes.codeSnippet}>
<Tooltip content={toolTipText}>
<button
onMouseEnter={() => setToolTipText('Kopier')}
onClick={() => onButtonClick()}
className={classes.icon}
title='Kopier'
>
<FilesIcon fontSize={20} />
</button>
</Tooltip>
<SyntaxHighlighter
style={nightOwl}
language='jsx'
customStyle={{
fontSize: '15px',
margin: 0,
padding: '18px',
}}
>
{children}
</SyntaxHighlighter>
{snippet && (
<>
<Tooltip content={toolTipText}>
<button
onMouseEnter={() => setToolTipText('Kopier')}
onClick={() => onButtonClick()}
className={classes.icon}
title='Kopier'
>
<FilesIcon fontSize={20} />
</button>
</Tooltip>
<SyntaxHighlighter
style={nightOwl}
language='jsx'
customStyle={{
fontSize: '15px',
margin: 0,
padding: '18px',
}}
>
{snippet}
</SyntaxHighlighter>
</>
)}
</div>
);
};
Expand Down
Loading
Loading