Skip to content

Commit 11bbc63

Browse files
authored
Merge pull request #11 from eps1lon/feat/add-core-utils-to-utils-compat-self
Add compat for usage in utils itself
2 parents 324b3a8 + bd907da commit 11bbc63

File tree

8 files changed

+58
-9
lines changed

8 files changed

+58
-9
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
/packages/material-ui-icons/legacy
1919
/packages/material-ui-icons/src
2020
/packages/material-ui-icons/templateSvgIcon.js
21+
/packages/material-ui-utils/macros/__fixtures__/
2122
# Ignore fixtures
2223
/packages/typescript-to-proptypes/test/*/*
2324
/tmp
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default class MuiError {
2+
constructor(message: string);
3+
}

packages/material-ui-utils/macros/MuiError.macro.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { createMacro, MacroError } = require('babel-plugin-macros');
22
const helperModuleImports = require('@babel/helper-module-imports');
33
const fs = require('fs');
4+
const path = require('path');
45

56
function invertObject(object) {
67
const inverted = {};
@@ -11,9 +12,13 @@ function invertObject(object) {
1112
}
1213

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

127132
if (formatMuiErrorMessageIdentifier === null) {
128-
// Outputs:
129-
// import { formatMuiErrorMessage } from '@material-ui/utils';
130-
formatMuiErrorMessageIdentifier = helperModuleImports.addNamed(
131-
babelPath,
132-
'formatMuiErrorMessage',
133-
'@material-ui/utils',
134-
);
133+
const isBareImportSourceIdentifier = source.startsWith('@material-ui/utils');
134+
if (isBareImportSourceIdentifier) {
135+
// Input: import MuiError from '@material-ui/utils/macros/MuiError.macro'
136+
// Outputs:
137+
// import { formatMuiErrorMessage } from '@material-ui/utils';
138+
formatMuiErrorMessageIdentifier = helperModuleImports.addNamed(
139+
babelPath,
140+
'formatMuiErrorMessage',
141+
'@material-ui/utils',
142+
);
143+
} else {
144+
const normalizedRelativeImport = path.normalize(
145+
source.replace('../macros/MuiError.macro', './formatMuiErrorMessage'),
146+
);
147+
// 'formatMuiErrorMessage' implies './formatMuiErrorMessage' for fs paths but not for import specifiers.
148+
const formatMuiErrorMessageImportSource = normalizedRelativeImport.startsWith('.')
149+
? normalizedRelativeImport
150+
: `./${normalizedRelativeImport}`;
151+
// Input: import MuiError from '../macros/MuiError.macro'
152+
// Outputs:
153+
// import formatMuiErrorMessage from './formatMuiErrorMessage';
154+
formatMuiErrorMessageIdentifier = helperModuleImports.addDefault(
155+
babelPath,
156+
formatMuiErrorMessageImportSource,
157+
{ nameHint: 'formatMuiErrorMessage' },
158+
);
159+
}
135160
}
136161

137162
// Outputs:

packages/material-ui-utils/macros/MuiError.macro.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,13 @@ pluginTester({
9595
},
9696
},
9797
},
98+
{
99+
title: 'relative-import',
100+
pluginOptions: {
101+
muiError: { errorCodesPath: path.join(fixturePath, 'relative-import', 'error-codes.json') },
102+
},
103+
fixture: path.join(fixturePath, 'relative-import', 'input.js'),
104+
output: readOutputFixtureSync('relative-import', 'output.js'),
105+
},
98106
],
99107
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"1": "Material-UI: Expected valid input target.\nDid you use `inputComponent`"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import MuiError from '../../../macros/MuiError.macro';
2+
3+
throw new MuiError('Material-UI: Expected valid input target.\n' + 'Did you use `inputComponent`');
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import _formatMuiErrorMessage from '../../formatMuiErrorMessage';
2+
throw new Error(
3+
process.env.NODE_ENV !== 'production'
4+
? `Material-UI: Expected valid input target.
5+
Did you use \`inputComponent\``
6+
: _formatMuiErrorMessage(1),
7+
);

packages/material-ui-utils/src/capitalize.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-ignore
21
import MuiError from '../macros/MuiError.macro';
32
// It should to be noted that this function isn't equivalent to `text-transform: capitalize`.
43
//

0 commit comments

Comments
 (0)