Skip to content

Commit

Permalink
[charts] Fix eslint for react compiler (mui#13444)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfauquette authored and thomasmoon committed Sep 6, 2024
1 parent c0f5607 commit 8437d65
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const path = require('path');
const ENABLE_REACT_COMPILER_PLUGIN = process.env.ENABLE_REACT_COMPILER_PLUGIN ?? false;

// Enable React Compiler Plugin rules per package
const ENABLE_REACT_COMPILER_PLUGIN_CHARTS =
process.env.ENABLE_REACT_COMPILER_PLUGIN_CHARTS ?? false;
const ENABLE_REACT_COMPILER_PLUGIN_CHARTS = process.env.ENABLE_REACT_COMPILER_PLUGIN_CHARTS ?? true;
const ENABLE_REACT_COMPILER_PLUGIN_DATA_GRID =
process.env.ENABLE_REACT_COMPILER_PLUGIN_DATA_GRID ?? false;
const ENABLE_REACT_COMPILER_PLUGIN_DATE_PICKERS =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ChartsReferenceLineClasses,
getReferenceLineUtilityClass,
} from './chartsReferenceLineClasses';
import { buildWarning } from '../internals/warning';

export type ChartsXReferenceLineProps<
TValue extends string | number | Date = string | number | Date,
Expand Down Expand Up @@ -72,7 +73,11 @@ export function getXReferenceLineClasses(classes?: Partial<ChartsReferenceLineCl
);
}

let warnedOnce = false;
const valueError = buildWarning(
(value, id) =>
`MUI X Charts: the value ${value} does not exist in the data of x axis with id ${id}.`,
'error',
);

function ChartsXReferenceLine(props: ChartsXReferenceLineProps) {
const {
Expand All @@ -93,12 +98,7 @@ function ChartsXReferenceLine(props: ChartsXReferenceLineProps) {

if (xPosition === undefined) {
if (process.env.NODE_ENV !== 'production') {
if (!warnedOnce) {
warnedOnce = true;
console.error(
`MUI X Charts: the value ${x} does not exist in the data of x axis with id ${axisId}.`,
);
}
valueError(x, axisId);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ChartsReferenceLineClasses,
getReferenceLineUtilityClass,
} from './chartsReferenceLineClasses';
import { buildWarning } from '../internals/warning';

export type ChartsYReferenceLineProps<
TValue extends string | number | Date = string | number | Date,
Expand Down Expand Up @@ -60,8 +61,6 @@ const getTextParams = ({
}
};

let warnedOnce = false;

export function getYReferenceLineClasses(classes?: Partial<ChartsReferenceLineClasses>) {
return composeClasses(
{
Expand All @@ -74,6 +73,12 @@ export function getYReferenceLineClasses(classes?: Partial<ChartsReferenceLineCl
);
}

const valueError = buildWarning(
(value, id) =>
`MUI X Charts: the value ${value} does not exist in the data of y axis with id ${id}.`,
'error',
);

function ChartsYReferenceLine(props: ChartsYReferenceLineProps) {
const {
y,
Expand All @@ -93,12 +98,7 @@ function ChartsYReferenceLine(props: ChartsYReferenceLineProps) {

if (yPosition === undefined) {
if (process.env.NODE_ENV !== 'production') {
if (!warnedOnce) {
warnedOnce = true;
console.error(
`MUI X Charts: the value ${y} does not exist in the data of y axis with id ${axisId}.`,
);
}
valueError(y, axisId);
}
return null;
}
Expand Down
4 changes: 1 addition & 3 deletions packages/x-charts/src/ChartsText/ChartsText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ function ChartsText(props: ChartsTextProps) {
if (angle) {
transforms.push(`rotate(${angle}, ${x}, ${y})`);
}
if (transforms.length) {
textProps.transform = transforms.join(' ');
}

return (
<text
{...textProps}
transform={transforms.length > 0 ? transforms.join(' ') : undefined}
x={x}
y={y}
textAnchor={textAnchor}
Expand Down
17 changes: 17 additions & 0 deletions packages/x-charts/src/internals/warning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function buildWarning(
message: (...args: any) => string,
gravity: 'warning' | 'error' = 'warning',
) {
let alreadyWarned = false;

return (...args: any) => {
if (!alreadyWarned) {
alreadyWarned = true;
if (gravity === 'error') {
console.error(message(...args));
} else {
console.warn(message(...args));
}
}
};
}

0 comments on commit 8437d65

Please sign in to comment.