@mui/internal-babel-plugin-minify-errors 2.0.8-canary.1 → 2.0.8-canary.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +70 -48
  2. package/index.test.js +1 -1
  3. package/package.json +5 -5
package/index.js CHANGED
@@ -102,6 +102,68 @@ function handleUnminifyableError(missingError, path) {
102
102
  }
103
103
  }
104
104
 
105
+ /**
106
+ * @param {babel.types} t
107
+ * @param {babel.NodePath<babel.types.NewExpression>} newExpressionPath
108
+ * @param {{ detection: Options['detection']; missingError: MissingError}} param2
109
+ * @returns {null | { messageNode: babel.types.Expression; messagePath: babel.NodePath<babel.types.ArgumentPlaceholder | babel.types.SpreadElement | babel.types.Expression>; message: { message: string; expressions: babel.types.Expression[] } }}
110
+ */
111
+ function findMessageNode(t, newExpressionPath, { detection, missingError }) {
112
+ if (!newExpressionPath.get('callee').isIdentifier({ name: 'Error' })) {
113
+ return null;
114
+ }
115
+
116
+ switch (detection) {
117
+ case 'opt-in': {
118
+ if (
119
+ !newExpressionPath.node.leadingComments?.some((comment) =>
120
+ comment.value.includes(COMMENT_OPT_IN_MARKER),
121
+ )
122
+ ) {
123
+ return null;
124
+ }
125
+ newExpressionPath.node.leadingComments = newExpressionPath.node.leadingComments.filter(
126
+ (comment) => !comment.value.includes(COMMENT_OPT_IN_MARKER),
127
+ );
128
+ break;
129
+ }
130
+ case 'opt-out': {
131
+ if (
132
+ newExpressionPath.node.leadingComments?.some((comment) =>
133
+ comment.value.includes(COMMENT_OPT_OUT_MARKER),
134
+ )
135
+ ) {
136
+ newExpressionPath.node.leadingComments = newExpressionPath.node.leadingComments.filter(
137
+ (comment) => !comment.value.includes(COMMENT_OPT_OUT_MARKER),
138
+ );
139
+ return null;
140
+ }
141
+
142
+ break;
143
+ }
144
+ default: {
145
+ throw new Error(`Unknown detection option: ${detection}`);
146
+ }
147
+ }
148
+
149
+ const messagePath = newExpressionPath.get('arguments')[0];
150
+ if (!messagePath) {
151
+ return null;
152
+ }
153
+
154
+ const messageNode = messagePath.node;
155
+ if (t.isSpreadElement(messageNode) || t.isArgumentPlaceholder(messageNode)) {
156
+ handleUnminifyableError(missingError, newExpressionPath);
157
+ return null;
158
+ }
159
+ const message = extractMessage(t, messageNode);
160
+ if (!message) {
161
+ handleUnminifyableError(missingError, newExpressionPath);
162
+ return null;
163
+ }
164
+ return { messagePath, messageNode, message };
165
+ }
166
+
105
167
  /**
106
168
  * Transforms the error message node.
107
169
  * @param {babel.types} t
@@ -261,59 +323,15 @@ module.exports = function plugin(
261
323
  name: '@mui/internal-babel-plugin-minify-errors',
262
324
  visitor: {
263
325
  NewExpression(newExpressionPath, state) {
264
- if (!newExpressionPath.get('callee').isIdentifier({ name: 'Error' })) {
265
- return;
266
- }
267
-
268
- switch (detection) {
269
- case 'opt-in': {
270
- if (
271
- !newExpressionPath.node.leadingComments?.some((comment) =>
272
- comment.value.includes(COMMENT_OPT_IN_MARKER),
273
- )
274
- ) {
275
- return;
276
- }
277
- newExpressionPath.node.leadingComments = newExpressionPath.node.leadingComments.filter(
278
- (comment) => !comment.value.includes(COMMENT_OPT_IN_MARKER),
279
- );
280
- break;
281
- }
282
- case 'opt-out': {
283
- if (
284
- newExpressionPath.node.leadingComments?.some((comment) =>
285
- comment.value.includes(COMMENT_OPT_OUT_MARKER),
286
- )
287
- ) {
288
- newExpressionPath.node.leadingComments =
289
- newExpressionPath.node.leadingComments.filter(
290
- (comment) => !comment.value.includes(COMMENT_OPT_OUT_MARKER),
291
- );
292
- return;
293
- }
294
-
295
- break;
296
- }
297
- default: {
298
- throw new Error(`Unknown detection option: ${detection}`);
299
- }
300
- }
301
-
302
- const messagePath = newExpressionPath.get('arguments')[0];
303
- if (!messagePath) {
304
- return;
305
- }
306
-
307
- const messageNode = messagePath.node;
308
- if (t.isSpreadElement(messageNode) || t.isArgumentPlaceholder(messageNode)) {
309
- handleUnminifyableError(missingError, newExpressionPath);
326
+ const message = findMessageNode(t, newExpressionPath, { detection, missingError });
327
+ if (!message) {
310
328
  return;
311
329
  }
312
330
 
313
331
  const transformedMessage = transformMessage(
314
332
  t,
315
333
  newExpressionPath,
316
- messageNode,
334
+ message.messageNode,
317
335
  state,
318
336
  errorCodesLookup,
319
337
  missingError,
@@ -322,7 +340,7 @@ module.exports = function plugin(
322
340
  );
323
341
 
324
342
  if (transformedMessage) {
325
- messagePath.replaceWith(transformedMessage);
343
+ message.messagePath.replaceWith(transformedMessage);
326
344
  }
327
345
  },
328
346
  },
@@ -336,3 +354,7 @@ module.exports = function plugin(
336
354
  },
337
355
  };
338
356
  };
357
+
358
+ module.exports.findMessageNode = findMessageNode;
359
+
360
+ exports.findMessageNode = findMessageNode;
package/index.test.js CHANGED
@@ -2,7 +2,7 @@ import * as fs from 'fs';
2
2
  import * as path from 'path';
3
3
  import * as os from 'os';
4
4
  import { pluginTester } from 'babel-plugin-tester';
5
- import { expect } from 'chai';
5
+ import { expect } from 'vitest';
6
6
  import plugin from './index';
7
7
 
8
8
  const temporaryErrorCodesPath = path.join(os.tmpdir(), 'error-codes.json');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mui/internal-babel-plugin-minify-errors",
3
- "version": "2.0.8-canary.1",
3
+ "version": "2.0.8-canary.10",
4
4
  "author": "MUI Team",
5
5
  "description": "This is an internal package not meant for general use.",
6
6
  "repository": {
@@ -21,18 +21,18 @@
21
21
  "find-package-json": "^1.2.0"
22
22
  },
23
23
  "devDependencies": {
24
- "@babel/core": "^7.22.0",
24
+ "@babel/core": "^7.28.4",
25
25
  "@types/babel__core": "^7.20.5",
26
26
  "@types/babel__helper-module-imports": "^7.18.3",
27
27
  "@types/find-package-json": "^1.2.6",
28
- "babel-plugin-tester": "^11.0.4",
29
- "chai": "^4.5.0"
28
+ "babel-plugin-tester": "^12.0.0"
30
29
  },
31
30
  "peerDependencies": {
32
31
  "@babel/core": "7"
33
32
  },
34
33
  "sideEffects": false,
35
34
  "type": "commonjs",
35
+ "main": "./index.js",
36
36
  "exports": {
37
37
  ".": "./index.js"
38
38
  },
@@ -42,6 +42,6 @@
42
42
  "publishConfig": {
43
43
  "access": "public"
44
44
  },
45
- "gitSha": "89c91764b62f0af7ce6ee4ec52c8e45b47e3c8aa",
45
+ "gitSha": "a120f1e30da4263d125f97e1b3431f2b09b11b73",
46
46
  "scripts": {}
47
47
  }