Skip to content

Commit

Permalink
Merge pull request #11 from eps1lon/feat/add-core-utils-to-utils-comp…
Browse files Browse the repository at this point in the history
…at-self

Add compat for usage in utils itself
  • Loading branch information
mnajdova authored Oct 27, 2020
2 parents 324b3a8 + bd907da commit 11bbc63
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 9 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/packages/material-ui-icons/legacy
/packages/material-ui-icons/src
/packages/material-ui-icons/templateSvgIcon.js
/packages/material-ui-utils/macros/__fixtures__/
# Ignore fixtures
/packages/typescript-to-proptypes/test/*/*
/tmp
Expand Down
3 changes: 3 additions & 0 deletions packages/material-ui-utils/macros/MuiError.macro.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default class MuiError {
constructor(message: string);
}
41 changes: 33 additions & 8 deletions packages/material-ui-utils/macros/MuiError.macro.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { createMacro, MacroError } = require('babel-plugin-macros');
const helperModuleImports = require('@babel/helper-module-imports');
const fs = require('fs');
const path = require('path');

function invertObject(object) {
const inverted = {};
Expand All @@ -11,9 +12,13 @@ function invertObject(object) {
}

/**
* Supported imports:
* 1. bare specifier e.g. `'@material-ui/utils/macros/MuiError.macro'`
* 2. relative import from `packages/material-ui-utils/src` e.g. `'../macros/MuiError.macro'`
*
* @param {import('babel-plugin-macros').MacroParams} param0
*/
function muiError({ references, babel, config }) {
function muiError({ references, babel, config, source }) {
const { errorCodesPath = {}, missingError = 'annotate' } = config;
const errorCodes = JSON.parse(fs.readFileSync(errorCodesPath, { encoding: 'utf8' }));
const errorCodesLookup = invertObject(errorCodes);
Expand Down Expand Up @@ -125,13 +130,33 @@ function muiError({ references, babel, config }) {
errorCode = parseInt(errorCode, 10);

if (formatMuiErrorMessageIdentifier === null) {
// Outputs:
// import { formatMuiErrorMessage } from '@material-ui/utils';
formatMuiErrorMessageIdentifier = helperModuleImports.addNamed(
babelPath,
'formatMuiErrorMessage',
'@material-ui/utils',
);
const isBareImportSourceIdentifier = source.startsWith('@material-ui/utils');
if (isBareImportSourceIdentifier) {
// Input: import MuiError from '@material-ui/utils/macros/MuiError.macro'
// Outputs:
// import { formatMuiErrorMessage } from '@material-ui/utils';
formatMuiErrorMessageIdentifier = helperModuleImports.addNamed(
babelPath,
'formatMuiErrorMessage',
'@material-ui/utils',
);
} else {
const normalizedRelativeImport = path.normalize(
source.replace('../macros/MuiError.macro', './formatMuiErrorMessage'),
);
// 'formatMuiErrorMessage' implies './formatMuiErrorMessage' for fs paths but not for import specifiers.
const formatMuiErrorMessageImportSource = normalizedRelativeImport.startsWith('.')
? normalizedRelativeImport
: `./${normalizedRelativeImport}`;
// Input: import MuiError from '../macros/MuiError.macro'
// Outputs:
// import formatMuiErrorMessage from './formatMuiErrorMessage';
formatMuiErrorMessageIdentifier = helperModuleImports.addDefault(
babelPath,
formatMuiErrorMessageImportSource,
{ nameHint: 'formatMuiErrorMessage' },
);
}
}

// Outputs:
Expand Down
8 changes: 8 additions & 0 deletions packages/material-ui-utils/macros/MuiError.macro.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,13 @@ pluginTester({
},
},
},
{
title: 'relative-import',
pluginOptions: {
muiError: { errorCodesPath: path.join(fixturePath, 'relative-import', 'error-codes.json') },
},
fixture: path.join(fixturePath, 'relative-import', 'input.js'),
output: readOutputFixtureSync('relative-import', 'output.js'),
},
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"1": "Material-UI: Expected valid input target.\nDid you use `inputComponent`"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import MuiError from '../../../macros/MuiError.macro';

throw new MuiError('Material-UI: Expected valid input target.\n' + 'Did you use `inputComponent`');
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import _formatMuiErrorMessage from '../../formatMuiErrorMessage';
throw new Error(
process.env.NODE_ENV !== 'production'
? `Material-UI: Expected valid input target.
Did you use \`inputComponent\``
: _formatMuiErrorMessage(1),
);
1 change: 0 additions & 1 deletion packages/material-ui-utils/src/capitalize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-ignore
import MuiError from '../macros/MuiError.macro';
// It should to be noted that this function isn't equivalent to `text-transform: capitalize`.
//
Expand Down

0 comments on commit 11bbc63

Please sign in to comment.