Merge pull request #4580 from aloisklink/refactor/replace-enums

Remove all TypeScript enums and forbid them in ESLint
This commit is contained in:
Sidharth Vinod
2023-07-03 07:26:39 +00:00
committed by GitHub
5 changed files with 28 additions and 28 deletions

View File

@@ -123,6 +123,14 @@ module.exports = {
files: ['*.{ts,tsx}'], files: ['*.{ts,tsx}'],
plugins: ['tsdoc'], plugins: ['tsdoc'],
rules: { rules: {
'no-restricted-syntax': [
'error',
{
selector: 'TSEnumDeclaration',
message:
'Prefer using TypeScript union types over TypeScript enum, since TypeScript enums have a bunch of issues, see https://dev.to/dvddpl/whats-the-problem-with-typescript-enums-2okj',
},
],
'tsdoc/syntax': 'error', 'tsdoc/syntax': 'error',
}, },
}, },

View File

@@ -226,9 +226,11 @@ export const reset = (config = siteConfig): void => {
updateCurrentConfig(config, directives); updateCurrentConfig(config, directives);
}; };
enum ConfigWarning { const ConfigWarning = {
'LAZY_LOAD_DEPRECATED' = 'The configuration options lazyLoadedDiagrams and loadExternalDiagramsAtStartup are deprecated. Please use registerExternalDiagrams instead.', LAZY_LOAD_DEPRECATED:
} 'The configuration options lazyLoadedDiagrams and loadExternalDiagramsAtStartup are deprecated. Please use registerExternalDiagrams instead.',
} as const;
type ConfigWarningStrings = keyof typeof ConfigWarning; type ConfigWarningStrings = keyof typeof ConfigWarning;
const issuedWarnings: { [key in ConfigWarningStrings]?: boolean } = {}; const issuedWarnings: { [key in ConfigWarningStrings]?: boolean } = {};
const issueWarning = (warning: ConfigWarningStrings) => { const issueWarning = (warning: ConfigWarningStrings) => {

View File

@@ -412,18 +412,8 @@ export interface FlowchartDiagramConfig extends BaseDiagramConfig {
wrappingWidth?: number; wrappingWidth?: number;
} }
export enum SankeyLinkColor { export type SankeyLinkColor = 'source' | 'target' | 'gradient';
source = 'source', export type SankeyNodeAlignment = 'left' | 'right' | 'center' | 'justify';
target = 'target',
gradient = 'gradient',
}
export enum SankeyNodeAlignment {
left = 'left',
right = 'right',
center = 'center',
justify = 'justify',
}
export interface SankeyDiagramConfig extends BaseDiagramConfig { export interface SankeyDiagramConfig extends BaseDiagramConfig {
width?: number; width?: number;

View File

@@ -1,5 +1,5 @@
import theme from './themes/index.js'; import theme from './themes/index.js';
import { MermaidConfig, SankeyLinkColor, SankeyNodeAlignment } from './config.type.js'; import { type MermaidConfig } from './config.type.js';
/** /**
* **Configuration methods in Mermaid version 8.6.0 have been updated, to learn more[[click * **Configuration methods in Mermaid version 8.6.0 have been updated, to learn more[[click
* here](8.6.0_docs.md)].** * here](8.6.0_docs.md)].**
@@ -2273,8 +2273,8 @@ const config: Partial<MermaidConfig> = {
sankey: { sankey: {
width: 800, width: 800,
height: 400, height: 400,
linkColor: SankeyLinkColor.gradient, linkColor: 'gradient',
nodeAlignment: SankeyNodeAlignment.justify, nodeAlignment: 'justify',
useMaxWidth: false, useMaxWidth: false,
}, },
fontSize: 16, fontSize: 16,

View File

@@ -18,17 +18,17 @@ import {
} from 'd3-sankey'; } from 'd3-sankey';
import { configureSvgSize } from '../../setupGraphViewbox.js'; import { configureSvgSize } from '../../setupGraphViewbox.js';
import { Uid } from '../../rendering-util/uid.js'; import { Uid } from '../../rendering-util/uid.js';
import { SankeyLinkColor, SankeyNodeAlignment } from '../../config.type.js'; import type { SankeyLinkColor, SankeyNodeAlignment } from '../../config.type.js';
// Map config options to alignment functions // Map config options to alignment functions
const alignmentsMap: Record< const alignmentsMap: Record<
SankeyNodeAlignment, SankeyNodeAlignment,
(node: d3SankeyNode<object, object>, n: number) => number (node: d3SankeyNode<object, object>, n: number) => number
> = { > = {
[SankeyNodeAlignment.left]: d3SankeyLeft, left: d3SankeyLeft,
[SankeyNodeAlignment.right]: d3SankeyRight, right: d3SankeyRight,
[SankeyNodeAlignment.center]: d3SankeyCenter, center: d3SankeyCenter,
[SankeyNodeAlignment.justify]: d3SankeyJustify, justify: d3SankeyJustify,
}; };
/** /**
@@ -157,9 +157,9 @@ export const draw = function (text: string, id: string, _version: string, diagOb
.attr('class', 'link') .attr('class', 'link')
.style('mix-blend-mode', 'multiply'); .style('mix-blend-mode', 'multiply');
const linkColor = conf?.linkColor || SankeyLinkColor.gradient; const linkColor = conf?.linkColor || 'gradient';
if (linkColor === SankeyLinkColor.gradient) { if (linkColor === 'gradient') {
const gradient = link const gradient = link
.append('linearGradient') .append('linearGradient')
.attr('id', (d: any) => (d.uid = Uid.next('linearGradient-')).id) .attr('id', (d: any) => (d.uid = Uid.next('linearGradient-')).id)
@@ -180,13 +180,13 @@ export const draw = function (text: string, id: string, _version: string, diagOb
let coloring: any; let coloring: any;
switch (linkColor) { switch (linkColor) {
case SankeyLinkColor.gradient: case 'gradient':
coloring = (d: any) => d.uid; coloring = (d: any) => d.uid;
break; break;
case SankeyLinkColor.source: case 'source':
coloring = (d: any) => colorScheme(d.source.id); coloring = (d: any) => colorScheme(d.source.id);
break; break;
case SankeyLinkColor.target: case 'target':
coloring = (d: any) => colorScheme(d.target.id); coloring = (d: any) => colorScheme(d.target.id);
break; break;
default: default: