Skip to content

Commit 65b09a2

Browse files
committed
Update createInlineComment to format text with RegExp
1 parent 932ad2b commit 65b09a2

File tree

9 files changed

+18
-31
lines changed

9 files changed

+18
-31
lines changed

.changeset/fluffy-owls-breathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/polaris-migrator': patch
3+
---
4+
5+
Update `createInlineComment` to format text with RegExp

polaris-migrator/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
"meow": "^9.0.0",
4242
"postcss": "^8.4.14",
4343
"postcss-scss": "^4.0.4",
44-
"postcss-value-parser": "^4.2.0",
45-
"prettier": "^2.7.1"
44+
"postcss-value-parser": "^4.2.0"
4645
},
4746
"devDependencies": {
4847
"@types/is-git-clean": "^1.1.0",
@@ -54,6 +53,7 @@
5453
"@shopify/polaris": "^10.10.1",
5554
"plop": "^3.1.1",
5655
"plop-dir": "^0.0.5",
56+
"prettier": "^2.7.1",
5757
"type-fest": "^2.19.0"
5858
}
5959
}

polaris-migrator/src/migrations/scss-replace-spacing/scss-replace-spacing.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ const plugin = (options: PluginOptions = {}): Plugin => {
7777

7878
if (hasNumericOperator(parsedValue)) {
7979
// Insert comment if the declaration value contains calculations
80-
decl.before(
81-
createInlineComment(POLARIS_MIGRATOR_COMMENT, {prose: true}),
82-
);
80+
decl.before(createInlineComment(POLARIS_MIGRATOR_COMMENT));
8381
decl.before(
8482
createInlineComment(`${decl.prop}: ${parsedValue.toString()};`),
8583
);

polaris-migrator/src/migrations/scss-replace-spacing/tests/with-namespace.output.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
padding: -#{legacy-polaris-v8.spacing()};
1818
// With other functions
1919
// polaris-migrator: Unable to migrate the following expression. Please upgrade manually.
20-
// padding: calc(#{legacy-polaris-v8.rem(16px)} + var(--p-space-4));
20+
// padding: calc( #{legacy-polaris-v8.rem(16px)} + var(--p-space-4) );
2121
padding: calc(
2222
#{legacy-polaris-v8.rem(16px)} + #{legacy-polaris-v8.spacing()}
2323
);

polaris-migrator/src/migrations/styles-tokenize-font/styles-tokenize-font.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ const plugin = (options: PluginOptions = {}): Plugin => {
6363
handlers[decl.prop]();
6464

6565
if (targets.some(({replaced}) => !replaced || hasNumericOperator)) {
66-
decl.before(
67-
createInlineComment(POLARIS_MIGRATOR_COMMENT, {prose: true}),
68-
);
66+
decl.before(createInlineComment(POLARIS_MIGRATOR_COMMENT));
6967
decl.before(
7068
createInlineComment(`${decl.prop}: ${parsedValue.toString()};`),
7169
);

polaris-migrator/src/migrations/styles-tokenize-shape/styles-tokenize-shape.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ const plugin = (options: PluginOptions = {}): Plugin => {
6060

6161
if (targets.some(({replaced}) => !replaced || hasNumericOperator)) {
6262
// Insert comment if the declaration value contains calculations
63-
decl.before(
64-
createInlineComment(POLARIS_MIGRATOR_COMMENT, {prose: true}),
65-
);
63+
decl.before(createInlineComment(POLARIS_MIGRATOR_COMMENT));
6664
decl.before(
6765
createInlineComment(`${decl.prop}: ${parsedValue.toString()};`),
6866
);

polaris-migrator/src/migrations/styles-tokenize-space/tests/styles-tokenize-space.output.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
// padding: var(--p-space-4) + var(--p-space-4);
117117
padding: rem(16px) + 16px;
118118
// polaris-migrator: Unable to migrate the following expression. Please upgrade manually.
119-
// error: Non-tokenizable value '$var \* 16px'
119+
// error: Non-tokenizable value '$var * 16px'
120120
// warning: Numeric operator detected.
121121
// padding: rem($var * var(--p-space-4));
122122
padding: rem($var * 16px);

polaris-migrator/src/migrations/styles-tokenize-space/tests/with-namespace.output.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
// padding: var(--p-space-4) + var(--p-space-4);
119119
padding: legacy-polaris-v8.rem(16px) + 16px;
120120
// polaris-migrator: Unable to migrate the following expression. Please upgrade manually.
121-
// error: Non-tokenizable value '$var \* 16px'
121+
// error: Non-tokenizable value '$var * 16px'
122122
// warning: Numeric operator detected.
123123
// padding: legacy-polaris-v8.rem($var * var(--p-space-4));
124124
padding: legacy-polaris-v8.rem($var * 16px);

polaris-migrator/src/utilities/sass.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import valueParser, {
1717
Dimension,
1818
} from 'postcss-value-parser';
1919
import {toPx, getCustomPropertyNames, tokens} from '@shopify/polaris-tokens';
20-
import prettier from 'prettier';
2120

2221
import {POLARIS_MIGRATOR_COMMENT} from '../constants';
2322

@@ -328,15 +327,8 @@ export function isPolarisVar(node: Node): boolean {
328327
);
329328
}
330329

331-
export function createInlineComment(text: string, options?: {prose?: boolean}) {
332-
const formatted = prettier
333-
.format(text, {
334-
parser: options?.prose ? 'markdown' : 'scss',
335-
proseWrap: 'never',
336-
printWidth: 9999,
337-
})
338-
.trim();
339-
const comment = postcss.comment({text: formatted});
330+
export function createInlineComment(text: string) {
331+
const comment = postcss.comment({text: text.replace(/\s+/gm, ' ').trim()});
340332

341333
comment.raws.left = ' ';
342334
comment.raws.inline = true;
@@ -477,15 +469,11 @@ export function createSassMigrator(name: string, ruleFn: PolarisMigrator) {
477469
const flushReportsAsComments = () => {
478470
// @ts-expect-error No idea why TS is complaining here
479471
for (const [node, reportsForNode] of reports) {
480-
node.before(
481-
createInlineComment(POLARIS_MIGRATOR_COMMENT, {prose: true}),
482-
);
472+
node.before(createInlineComment(POLARIS_MIGRATOR_COMMENT));
483473

484474
for (const report of reportsForNode) {
485475
node.before(
486-
createInlineComment(`${report.severity}: ${report.message}`, {
487-
prose: true,
488-
}),
476+
createInlineComment(`${report.severity}: ${report.message}`),
489477
);
490478
}
491479
}
@@ -573,7 +561,7 @@ export function createSassMigrator(name: string, ruleFn: PolarisMigrator) {
573561
root.walkDecls(
574562
createWalker({
575563
walker,
576-
serialiseSuggestion: (node) => `${node.prop}: ${node.value}`,
564+
serialiseSuggestion: (node) => `${node.prop}: ${node.value};`,
577565
}),
578566
);
579567
},

0 commit comments

Comments
 (0)