Skip to content

Commit

Permalink
[core] Enforce curly braces for block statements (#27946)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Aug 25, 2021
1 parent 5608c83 commit c7a3739
Show file tree
Hide file tree
Showing 27 changed files with 214 additions and 71 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module.exports = {
*/
rules: {
'consistent-this': ['error', 'self'],
curly: ['error', 'all'],
// Just as bad as "max components per file"
'max-classes-per-file': 'off',
// Too interruptive
Expand Down
8 changes: 6 additions & 2 deletions docs/src/components/header/HeaderNavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ export default function HeaderNavBar() {
setSubMenuOpen(true);
}
setSubMenuIndex((prevValue) => {
if (prevValue === null) return 0;
if (prevValue === null) {
return 0;
}
if (prevValue === PRODUCT_IDS.length - 1) {
return 0;
}
Expand All @@ -131,7 +133,9 @@ export default function HeaderNavBar() {
if (event.key === 'ArrowUp') {
event.preventDefault();
setSubMenuIndex((prevValue) => {
if (prevValue === null) return 0;
if (prevValue === null) {
return 0;
}
if (prevValue === 0) {
return PRODUCT_IDS.length - 1;
}
Expand Down
4 changes: 3 additions & 1 deletion docs/src/components/home/DesignKits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ export function DesignKitImagesSet2({

export function DesignKitTools({ disableLink, ...props }: { disableLink?: boolean } & BoxProps) {
function renderTool(brand: 'figma' | 'sketch' | 'xd') {
if (disableLink) return <DesignToolLogo brand={brand} />;
if (disableLink) {
return <DesignToolLogo brand={brand} />;
}
return (
<DesignToolLink brand={brand}>
<DesignToolLogo brand={brand} />
Expand Down
4 changes: 3 additions & 1 deletion docs/src/components/home/ElementPointer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ export default function PointerContainer({
[],
);
React.useEffect(() => {
if (onElementChange) onElementChange(data);
if (onElementChange) {
onElementChange(data);
}
}, [data, onElementChange]);
return (
<PointerContext.Provider value={handleMouseOver}>
Expand Down
8 changes: 6 additions & 2 deletions docs/src/components/home/StoreTemplatesBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ export function StoreTemplatesSet1({
...props
}: { disableLink?: boolean; keyframes?: Record<string, object> } & BoxProps) {
function renderTemplate(brand: TemplateBrand) {
if (disableLink) return <StoreTemplateImage brand={brand} />;
if (disableLink) {
return <StoreTemplateImage brand={brand} />;
}
return (
<StoreTemplateLink brand={brand}>
<StoreTemplateImage brand={brand} />
Expand Down Expand Up @@ -185,7 +187,9 @@ export function StoreTemplatesSet2({
...props
}: { disableLink?: boolean; keyframes?: Record<string, object> } & BoxProps) {
function renderTemplate(brand: TemplateBrand) {
if (disableLink) return <StoreTemplateImage brand={brand} />;
if (disableLink) {
return <StoreTemplateImage brand={brand} />;
}
return (
<StoreTemplateLink brand={brand}>
<StoreTemplateImage brand={brand} />
Expand Down
8 changes: 6 additions & 2 deletions docs/src/components/showcase/FolderTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@ function stableSort<T>(array: readonly T[], comparator: (a: T, b: T) => number)
const stabilizedThis = array.map((el, index) => [el, index] as [T, number]);
stabilizedThis.sort((a, b) => {
const order = comparator(a[0], b[0]);
if (order !== 0) return order;
if (order !== 0) {
return order;
}
return a[1] - b[1];
});
return stabilizedThis.map((el) => el[0]);
}

function formatSize(size: number) {
const kb = size / 1000;
if (kb < 1000) return `${kb.toFixed(1)} KB`;
if (kb < 1000) {
return `${kb.toFixed(1)} KB`;
}
return `${(kb / 1000).toFixed(0)} MB`;
}

Expand Down
8 changes: 6 additions & 2 deletions docs/src/modules/utils/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,12 @@ function findPages(
pages.sort((a, b) => {
const pathnameA = a.pathname.replace(/-/g, '');
const pathnameB = b.pathname.replace(/-/g, '');
if (pathnameA < pathnameB) return -1;
if (pathnameA > pathnameB) return 1;
if (pathnameA < pathnameB) {
return -1;
}
if (pathnameA > pathnameB) {
return 1;
}
return 0;
});

Expand Down
4 changes: 3 additions & 1 deletion docs/src/pages/components/tables/EnhancedTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ function stableSort(array, comparator) {
const stabilizedThis = array.map((el, index) => [el, index]);
stabilizedThis.sort((a, b) => {
const order = comparator(a[0], b[0]);
if (order !== 0) return order;
if (order !== 0) {
return order;
}
return a[1] - b[1];
});
return stabilizedThis.map((el) => el[0]);
Expand Down
4 changes: 3 additions & 1 deletion docs/src/pages/components/tables/EnhancedTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ function stableSort<T>(array: readonly T[], comparator: (a: T, b: T) => number)
const stabilizedThis = array.map((el, index) => [el, index] as [T, number]);
stabilizedThis.sort((a, b) => {
const order = comparator(a[0], b[0]);
if (order !== 0) return order;
if (order !== 0) {
return order;
}
return a[1] - b[1];
});
return stabilizedThis.map((el) => el[0]);
Expand Down
4 changes: 3 additions & 1 deletion docs/src/pages/discover-more/showcase/Showcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ function stableSort(array, cmp) {
const stabilizedThis = array.map((el, index) => [el, index]);
stabilizedThis.sort((a, b) => {
const order = cmp(a[0], b[0]);
if (order !== 0) return order;
if (order !== 0) {
return order;
}
return a[1] - b[1];
});
return stabilizedThis.map((el) => el[0]);
Expand Down
4 changes: 3 additions & 1 deletion packages/material-ui-codemod/src/util/memoize.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ const memoize = (func, resolver = (a) => a) => {
const cache = new Map();
return (...args) => {
const key = resolver(...args);
if (cache.has(key)) return cache.get(key);
if (cache.has(key)) {
return cache.get(key);
}
const value = func(...args);
cache.set(key, value);
return value;
Expand Down
32 changes: 24 additions & 8 deletions packages/material-ui-codemod/src/v4.0.0/optimal-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@ export default function transformer(fileInfo, api, options) {
};

root.find(j.ImportDeclaration).forEach((path) => {
if (path.value.importKind && path.value.importKind !== 'value') return;
if (path.value.importKind && path.value.importKind !== 'value') {
return;
}
const importPath = path.value.source.value.replace(/(index)?(\.js)?$/, '');
const match = importPath.match(importRegExp);
if (!match) return;
if (!match) {
return;
}

const subpath = match[1].replace(/\/$/, '');

if (/^(internal)/.test(subpath)) return;
if (/^(internal)/.test(subpath)) {
return;
}
const targetImportPath = `${targetModule}/${subpath}`;

const whitelist = getJSExports(
Expand All @@ -48,10 +54,16 @@ export default function transformer(fileInfo, api, options) {
);

path.node.specifiers.forEach((specifier, index) => {
if (!path.node.specifiers.length) return;
if (!path.node.specifiers.length) {
return;
}

if (specifier.importKind && specifier.importKind !== 'value') return;
if (specifier.type === 'ImportNamespaceSpecifier') return;
if (specifier.importKind && specifier.importKind !== 'value') {
return;
}
if (specifier.type === 'ImportNamespaceSpecifier') {
return;
}
const localName = specifier.local.name;
switch (specifier.type) {
case 'ImportNamespaceSpecifier':
Expand All @@ -69,7 +81,9 @@ export default function transformer(fileInfo, api, options) {
break;
}
case 'ImportSpecifier':
if (!whitelist.has(specifier.imported.name)) return;
if (!whitelist.has(specifier.imported.name)) {
return;
}
addSpecifier(targetImportPath, specifier);
path.get('specifiers', index).prune();
break;
Expand All @@ -78,7 +92,9 @@ export default function transformer(fileInfo, api, options) {
}
});

if (!path.node.specifiers.length) path.prune();
if (!path.node.specifiers.length) {
path.prune();
}
});

addImports(
Expand Down
32 changes: 24 additions & 8 deletions packages/material-ui-codemod/src/v4.0.0/top-level-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,38 @@ export default function transformer(fileInfo, api, options) {
const resultSpecifiers = [];

root.find(j.ImportDeclaration).forEach((path) => {
if (!path.node.specifiers.length) return;
if (!path.node.specifiers.length) {
return;
}

if (path.value.importKind && path.value.importKind !== 'value') return;
if (path.value.importKind && path.value.importKind !== 'value') {
return;
}
const importPath = path.value.source.value;
const match = importPath.match(importRegExp);
if (!match) return;
if (!match) {
return;
}

if (importPath.includes('internal/')) return;
if (importPath.includes('internal/')) {
return;
}

path.node.specifiers.forEach((specifier, index) => {
if (specifier.importKind && specifier.importKind !== 'value') return;
if (specifier.type === 'ImportNamespaceSpecifier') return;
if (specifier.importKind && specifier.importKind !== 'value') {
return;
}
if (specifier.type === 'ImportNamespaceSpecifier') {
return;
}

switch (specifier.type) {
case 'ImportDefaultSpecifier': {
const localName = specifier.local.name;
const moduleName = match[1];
if (whitelist[moduleName] == null) return;
if (whitelist[moduleName] == null) {
return;
}
resultSpecifiers.push(
j.importSpecifier(j.identifier(moduleName), j.identifier(localName)),
);
Expand All @@ -64,7 +78,9 @@ export default function transformer(fileInfo, api, options) {
}
});

if (!path.node.specifiers.length) path.prune();
if (!path.node.specifiers.length) {
path.prune();
}
});

if (resultSpecifiers.length) {
Expand Down
32 changes: 24 additions & 8 deletions packages/material-ui-codemod/src/v5.0.0/optimal-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@ export default function transformer(fileInfo, api, options) {
};

root.find(j.ImportDeclaration).forEach((path) => {
if (path.value.importKind && path.value.importKind !== 'value') return;
if (path.value.importKind && path.value.importKind !== 'value') {
return;
}
const importPath = path.value.source.value.replace(/(index)?(\.js)?$/, '');
const match = importPath.match(importRegExp);
if (!match) return;
if (!match) {
return;
}

const subpath = match[1].replace(/\/$/, '');

if (/^(internal)/.test(subpath)) return;
if (/^(internal)/.test(subpath)) {
return;
}
const targetImportPath = `${targetModule}/${subpath}`;

const whitelist = getJSExports(
Expand All @@ -48,10 +54,16 @@ export default function transformer(fileInfo, api, options) {
);

path.node.specifiers.forEach((specifier, index) => {
if (!path.node.specifiers.length) return;
if (!path.node.specifiers.length) {
return;
}

if (specifier.importKind && specifier.importKind !== 'value') return;
if (specifier.type === 'ImportNamespaceSpecifier') return;
if (specifier.importKind && specifier.importKind !== 'value') {
return;
}
if (specifier.type === 'ImportNamespaceSpecifier') {
return;
}
const localName = specifier.local.name;
switch (specifier.type) {
case 'ImportNamespaceSpecifier':
Expand All @@ -69,7 +81,9 @@ export default function transformer(fileInfo, api, options) {
break;
}
case 'ImportSpecifier':
if (!whitelist.has(specifier.imported.name)) return;
if (!whitelist.has(specifier.imported.name)) {
return;
}
addSpecifier(targetImportPath, specifier);
path.get('specifiers', index).prune();
break;
Expand All @@ -78,7 +92,9 @@ export default function transformer(fileInfo, api, options) {
}
});

if (!path.node.specifiers.length) path.prune();
if (!path.node.specifiers.length) {
path.prune();
}
});

addImports(
Expand Down
4 changes: 3 additions & 1 deletion packages/material-ui-lab/src/PickersDay/PickersDay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ const PickersDay = React.forwardRef(function PickersDay<TDate>(
};

const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
if (!allowSameDateSelection && selected) return;
if (!allowSameDateSelection && selected) {
return;
}

if (!disabled) {
onDaySelect(day, 'finish');
Expand Down
12 changes: 9 additions & 3 deletions packages/material-ui-lab/src/TabContext/TabContext.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ describe('<TabContext />', () => {
*/
function Tabs({ value }) {
const context = useTabContext();
if (context === null) throw new TypeError();
if (context === null) {
throw new TypeError();
}

return (
<React.Fragment>
Expand Down Expand Up @@ -59,7 +61,9 @@ describe('<TabContext />', () => {
*/
function Tabs({ value }) {
const context = useTabContext();
if (context === null) throw new TypeError();
if (context === null) {
throw new TypeError();
}

return (
<React.Fragment>
Expand Down Expand Up @@ -87,7 +91,9 @@ describe('<TabContext />', () => {
*/
function Tabs({ value }) {
const context = useTabContext();
if (context === null) throw new TypeError();
if (context === null) {
throw new TypeError();
}

return (
<React.Fragment>
Expand Down
Loading

0 comments on commit c7a3739

Please sign in to comment.