Skip to content

Commit

Permalink
Update createInlineComment to format text with RegExp
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-b-rose committed Nov 3, 2022
1 parent 932ad2b commit 31d6b0b
Show file tree
Hide file tree
Showing 15 changed files with 24 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-owls-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/polaris-migrator': patch
---

Update `createInlineComment` to format text with RegExp
4 changes: 2 additions & 2 deletions polaris-migrator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
"meow": "^9.0.0",
"postcss": "^8.4.14",
"postcss-scss": "^4.0.4",
"postcss-value-parser": "^4.2.0",
"prettier": "^2.7.1"
"postcss-value-parser": "^4.2.0"
},
"devDependencies": {
"@types/is-git-clean": "^1.1.0",
Expand All @@ -54,6 +53,7 @@
"@shopify/polaris": "^10.10.1",
"plop": "^3.1.1",
"plop-dir": "^0.0.5",
"prettier": "^2.7.1",
"type-fest": "^2.19.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ const plugin = (options: PluginOptions = {}): Plugin => {

if (targets.some(({replaced}) => !replaced || hasNumericOperator)) {
// Insert comment if the declaration value contains calculations
decl.before(
createInlineComment(POLARIS_MIGRATOR_COMMENT, {prose: true}),
);
decl.before(createInlineComment(POLARIS_MIGRATOR_COMMENT));
decl.before(
createInlineComment(`${decl.prop}: ${parsedValue.toString()};`),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ const plugin = (options: PluginOptions = {}): Plugin => {

if (targets.some(({replaced}) => !replaced || hasNumericOperator)) {
// Insert comment if the declaration value contains calculations
decl.before(
createInlineComment(POLARIS_MIGRATOR_COMMENT, {prose: true}),
);
decl.before(createInlineComment(POLARIS_MIGRATOR_COMMENT));
decl.before(
createInlineComment(`${decl.prop}: ${parsedValue.toString()};`),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ const plugin = (options: PluginOptions = {}): Plugin => {

if (targets.some(({replaced}) => !replaced)) {
// Insert comment if the declaration value contains calculations
decl.before(
createInlineComment(POLARIS_MIGRATOR_COMMENT, {prose: true}),
);
decl.before(createInlineComment(POLARIS_MIGRATOR_COMMENT));
decl.before(
createInlineComment(`${decl.prop}: ${parsedValue.toString()};`),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ const plugin = (options: PluginOptions = {}): Plugin => {
});

if (needsComment) {
decl.before(
createInlineComment(POLARIS_MIGRATOR_COMMENT, {prose: true}),
);
decl.before(createInlineComment(POLARIS_MIGRATOR_COMMENT));
decl.before(
createInlineComment(`${decl.prop}: ${parsedValue.toString()};`),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ const plugin = (options: PluginOptions = {}): Plugin => {
});

if (needsComment) {
decl.before(
createInlineComment(POLARIS_MIGRATOR_COMMENT, {prose: true}),
);
decl.before(createInlineComment(POLARIS_MIGRATOR_COMMENT));
decl.before(
createInlineComment(`${decl.prop}: ${parsedValue.toString()};`),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ const plugin = (options: PluginOptions = {}): Plugin => {
});

if (needsComment) {
decl.before(
createInlineComment(POLARIS_MIGRATOR_COMMENT, {prose: true}),
);
decl.before(createInlineComment(POLARIS_MIGRATOR_COMMENT));
decl.before(
createInlineComment(`${decl.prop}: ${parsedValue.toString()};`),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ const plugin = (options: PluginOptions = {}): Plugin => {

if (hasNumericOperator(parsedValue)) {
// Insert comment if the declaration value contains calculations
decl.before(
createInlineComment(POLARIS_MIGRATOR_COMMENT, {prose: true}),
);
decl.before(createInlineComment(POLARIS_MIGRATOR_COMMENT));
decl.before(
createInlineComment(`${decl.prop}: ${parsedValue.toString()};`),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
padding: -#{legacy-polaris-v8.spacing()};
// With other functions
// polaris-migrator: Unable to migrate the following expression. Please upgrade manually.
// padding: calc(#{legacy-polaris-v8.rem(16px)} + var(--p-space-4));
// padding: calc( #{legacy-polaris-v8.rem(16px)} + var(--p-space-4) );
padding: calc(
#{legacy-polaris-v8.rem(16px)} + #{legacy-polaris-v8.spacing()}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ const plugin = (options: PluginOptions = {}): Plugin => {
handlers[decl.prop]();

if (targets.some(({replaced}) => !replaced || hasNumericOperator)) {
decl.before(
createInlineComment(POLARIS_MIGRATOR_COMMENT, {prose: true}),
);
decl.before(createInlineComment(POLARIS_MIGRATOR_COMMENT));
decl.before(
createInlineComment(`${decl.prop}: ${parsedValue.toString()};`),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ const plugin = (options: PluginOptions = {}): Plugin => {

if (targets.some(({replaced}) => !replaced || hasNumericOperator)) {
// Insert comment if the declaration value contains calculations
decl.before(
createInlineComment(POLARIS_MIGRATOR_COMMENT, {prose: true}),
);
decl.before(createInlineComment(POLARIS_MIGRATOR_COMMENT));
decl.before(
createInlineComment(`${decl.prop}: ${parsedValue.toString()};`),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
// padding: var(--p-space-4) + var(--p-space-4);
padding: rem(16px) + 16px;
// polaris-migrator: Unable to migrate the following expression. Please upgrade manually.
// error: Non-tokenizable value '$var \* 16px'
// error: Non-tokenizable value '$var * 16px'
// warning: Numeric operator detected.
// padding: rem($var * var(--p-space-4));
padding: rem($var * 16px);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
// padding: var(--p-space-4) + var(--p-space-4);
padding: legacy-polaris-v8.rem(16px) + 16px;
// polaris-migrator: Unable to migrate the following expression. Please upgrade manually.
// error: Non-tokenizable value '$var \* 16px'
// error: Non-tokenizable value '$var * 16px'
// warning: Numeric operator detected.
// padding: legacy-polaris-v8.rem($var * var(--p-space-4));
padding: legacy-polaris-v8.rem($var * 16px);
Expand Down
22 changes: 5 additions & 17 deletions polaris-migrator/src/utilities/sass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import valueParser, {
Dimension,
} from 'postcss-value-parser';
import {toPx, getCustomPropertyNames, tokens} from '@shopify/polaris-tokens';
import prettier from 'prettier';

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

Expand Down Expand Up @@ -328,15 +327,8 @@ export function isPolarisVar(node: Node): boolean {
);
}

export function createInlineComment(text: string, options?: {prose?: boolean}) {
const formatted = prettier
.format(text, {
parser: options?.prose ? 'markdown' : 'scss',
proseWrap: 'never',
printWidth: 9999,
})
.trim();
const comment = postcss.comment({text: formatted});
export function createInlineComment(text: string) {
const comment = postcss.comment({text: text.replace(/\s+/gm, ' ').trim()});

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

for (const report of reportsForNode) {
node.before(
createInlineComment(`${report.severity}: ${report.message}`, {
prose: true,
}),
createInlineComment(`${report.severity}: ${report.message}`),
);
}
}
Expand Down Expand Up @@ -573,7 +561,7 @@ export function createSassMigrator(name: string, ruleFn: PolarisMigrator) {
root.walkDecls(
createWalker({
walker,
serialiseSuggestion: (node) => `${node.prop}: ${node.value}`,
serialiseSuggestion: (node) => `${node.prop}: ${node.value};`,
}),
);
},
Expand Down

0 comments on commit 31d6b0b

Please sign in to comment.