@atlaskit/eslint-plugin-design-system 4.16.4 → 4.16.5

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @atlaskit/eslint-plugin-design-system
2
2
 
3
+ ## 4.16.5
4
+
5
+ ### Patch Changes
6
+
7
+ - [`c82e6ef389c`](https://bitbucket.org/atlassian/atlassian-frontend/commits/c82e6ef389c) - Fix bug when replacing shorthand css property values with tokens, values without corresponding token won't be replaced. Also, allow styled2 alias to be matched in object styles for the spacing rule, given Jira frontend uses that alias extensively
8
+
3
9
  ## 4.16.4
4
10
 
5
11
  ### Patch Changes
@@ -68,7 +68,7 @@ var rule = (0, _createRule.createRule)({
68
68
  },
69
69
  // CSSObjectExpression
70
70
  // const styles = css({ color: 'red', margin: '4px' }), styled.div({ color: 'red', margin: '4px' })
71
- 'CallExpression[callee.name=css] > ObjectExpression, CallExpression[callee.object.name=styled] > ObjectExpression': function CallExpressionCalleeNameCssObjectExpressionCallExpressionCalleeObjectNameStyledObjectExpression(parentNode) {
71
+ 'CallExpression[callee.name=css] > ObjectExpression, CallExpression[callee.object.name=styled] > ObjectExpression, CallExpression[callee.object.name=styled2] > ObjectExpression': function CallExpressionCalleeNameCssObjectExpressionCallExpressionCalleeObjectNameStyledObjectExpressionCallExpressionCalleeObjectNameStyled2ObjectExpression(parentNode) {
72
72
  if (!(0, _eslintCodemodUtils.isNodeOfType)(parentNode, 'ObjectExpression')) {
73
73
  return;
74
74
  }
@@ -187,10 +187,34 @@ var rule = (0, _createRule.createRule)({
187
187
  if (!allResolvableValues) {
188
188
  return null;
189
189
  }
190
- return (!tokenNode && ruleConfig.applyImport ? [(0, _utils.insertTokensImport)(fixer)] : []).concat([fixer.replaceText(node.value, "`".concat(values.map(function (value) {
190
+ var valuesWithTokenReplacement = values.filter(function (value) {
191
+ return (0, _utils.findTokenNameByPropertyValue)(propertyName, value);
192
+ });
193
+ if (valuesWithTokenReplacement.length === 0) {
194
+ // all values could be replaceable but that just means they're numeric
195
+ // if none of the values have token replacement we bail
196
+ return null;
197
+ }
198
+ var originalCssString = (0, _utils.getRawExpression)(node.value, context);
199
+ if (!originalCssString) {
200
+ return null;
201
+ }
202
+ /**
203
+ * at this stage none of the values are tokens or irreplaceable values
204
+ * since we know this is shorthand, there will be multiple values
205
+ * we'll need to remove all quote chars since `getRawExpression` will return those as part of the string
206
+ * given they're part of the substring of the current node
207
+ */
208
+ var originalValues = (0, _utils.splitShorthandValues)(originalCssString.replace(/`|'|"/g, ''));
209
+ if (originalValues.length !== values.length) {
210
+ // we bail just in case original values don't correspond to the replaced values
211
+ return null;
212
+ }
213
+ return (!tokenNode && ruleConfig.applyImport ? [(0, _utils.insertTokensImport)(fixer)] : []).concat([fixer.replaceText(node.value, "`".concat(values.map(function (value, index) {
191
214
  var pixelValue = (0, _utils.emToPixels)(value, fontSize);
192
215
  var pixelValueString = "".concat(pixelValue, "px");
193
- return "${".concat((0, _utils.getTokenNodeForValue)(propertyName, pixelValueString), "}");
216
+ // if there is a token we take it, otherwise we go with the original value
217
+ return (0, _utils.findTokenNameByPropertyValue)(propertyName, value) ? "${".concat((0, _utils.getTokenNodeForValue)(propertyName, pixelValueString), "}") : originalValues[index];
194
218
  }).join(' '), "`"))]);
195
219
  } : undefined
196
220
  });
@@ -8,6 +8,7 @@ exports.cleanComments = cleanComments;
8
8
  exports.emToPixels = exports.convertHyphenatedNameToCamelCase = void 0;
9
9
  exports.findIdentifierInParentScope = findIdentifierInParentScope;
10
10
  exports.findParentNodeForLine = void 0;
11
+ exports.findTokenNameByPropertyValue = findTokenNameByPropertyValue;
11
12
  exports.getFontSizeValueInScope = getFontSizeValueInScope;
12
13
  exports.getRawExpression = void 0;
13
14
  exports.getTokenNodeForValue = getTokenNodeForValue;
@@ -491,6 +492,18 @@ function splitCssProperties(styleString) {
491
492
  function isTokenValueString(originalValue) {
492
493
  return originalValue.startsWith('${token(') && originalValue.endsWith('}');
493
494
  }
495
+ function findTokenNameByPropertyValue(propertyName, value) {
496
+ var isFontWeightOrFamily = /fontWeight|fontFamily/.test(propertyName);
497
+ var propertyValue = typeof value === 'string' ? value.trim() : value;
498
+ var pixelValue = propertyValue;
499
+ var pixelValueString = "".concat(propertyValue, "px");
500
+ var lookupValue = isFontWeightOrFamily ? pixelValue : pixelValueString;
501
+ var tokenName = isTypographyProperty(propertyName) ? typographyValueToToken[propertyName][lookupValue] : spacingValueToToken[lookupValue];
502
+ if (!tokenName) {
503
+ return undefined;
504
+ }
505
+ return tokenName;
506
+ }
494
507
 
495
508
  /**
496
509
  * Returns a string with token expression corresponding to input parameters
@@ -504,7 +517,7 @@ function getTokenReplacement(propertyName, value) {
504
517
  var pixelValue = propertyValue;
505
518
  var pixelValueString = "".concat(propertyValue, "px");
506
519
  var lookupValue = isFontWeightOrFamily ? pixelValue : pixelValueString;
507
- var tokenName = isTypographyProperty(propertyName) ? typographyValueToToken[propertyName][lookupValue] : spacingValueToToken[lookupValue];
520
+ var tokenName = findTokenNameByPropertyValue(propertyName, value);
508
521
  if (!tokenName) {
509
522
  return undefined;
510
523
  }
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/eslint-plugin-design-system",
3
- "version": "4.16.4",
3
+ "version": "4.16.5",
4
4
  "sideEffects": false
5
5
  }
@@ -3,7 +3,7 @@
3
3
  import { isNodeOfType, node as nodeFn, property } from 'eslint-codemod-utils';
4
4
  import { createRule } from '../utils/create-rule';
5
5
  import { isDecendantOfGlobalToken } from '../utils/is-node';
6
- import { convertHyphenatedNameToCamelCase, emToPixels, findParentNodeForLine, getFontSizeValueInScope, getTokenNodeForValue, getTokenReplacement, getValue, getValueFromShorthand, insertTokensImport, isTokenValueString, isTypographyProperty, isValidSpacingValue, processCssNode, shouldAnalyzeProperty, spacingValueToToken, splitShorthandValues, typographyValueToToken } from './utils';
6
+ import { convertHyphenatedNameToCamelCase, emToPixels, findParentNodeForLine, findTokenNameByPropertyValue, getFontSizeValueInScope, getRawExpression, getTokenNodeForValue, getTokenReplacement, getValue, getValueFromShorthand, insertTokensImport, isTokenValueString, isTypographyProperty, isValidSpacingValue, processCssNode, shouldAnalyzeProperty, spacingValueToToken, splitShorthandValues, typographyValueToToken } from './utils';
7
7
  const rule = createRule({
8
8
  defaultOptions: [{
9
9
  addons: ['spacing'],
@@ -59,7 +59,7 @@ const rule = createRule({
59
59
  },
60
60
  // CSSObjectExpression
61
61
  // const styles = css({ color: 'red', margin: '4px' }), styled.div({ color: 'red', margin: '4px' })
62
- 'CallExpression[callee.name=css] > ObjectExpression, CallExpression[callee.object.name=styled] > ObjectExpression': parentNode => {
62
+ 'CallExpression[callee.name=css] > ObjectExpression, CallExpression[callee.object.name=styled] > ObjectExpression, CallExpression[callee.object.name=styled2] > ObjectExpression': parentNode => {
63
63
  if (!isNodeOfType(parentNode, 'ObjectExpression')) {
64
64
  return;
65
65
  }
@@ -176,10 +176,32 @@ const rule = createRule({
176
176
  if (!allResolvableValues) {
177
177
  return null;
178
178
  }
179
- return (!tokenNode && ruleConfig.applyImport ? [insertTokensImport(fixer)] : []).concat([fixer.replaceText(node.value, `\`${values.map(value => {
179
+ const valuesWithTokenReplacement = values.filter(value => findTokenNameByPropertyValue(propertyName, value));
180
+ if (valuesWithTokenReplacement.length === 0) {
181
+ // all values could be replaceable but that just means they're numeric
182
+ // if none of the values have token replacement we bail
183
+ return null;
184
+ }
185
+ const originalCssString = getRawExpression(node.value, context);
186
+ if (!originalCssString) {
187
+ return null;
188
+ }
189
+ /**
190
+ * at this stage none of the values are tokens or irreplaceable values
191
+ * since we know this is shorthand, there will be multiple values
192
+ * we'll need to remove all quote chars since `getRawExpression` will return those as part of the string
193
+ * given they're part of the substring of the current node
194
+ */
195
+ const originalValues = splitShorthandValues(originalCssString.replace(/`|'|"/g, ''));
196
+ if (originalValues.length !== values.length) {
197
+ // we bail just in case original values don't correspond to the replaced values
198
+ return null;
199
+ }
200
+ return (!tokenNode && ruleConfig.applyImport ? [insertTokensImport(fixer)] : []).concat([fixer.replaceText(node.value, `\`${values.map((value, index) => {
180
201
  const pixelValue = emToPixels(value, fontSize);
181
202
  const pixelValueString = `${pixelValue}px`;
182
- return `\${${getTokenNodeForValue(propertyName, pixelValueString)}}`;
203
+ // if there is a token we take it, otherwise we go with the original value
204
+ return findTokenNameByPropertyValue(propertyName, value) ? `\${${getTokenNodeForValue(propertyName, pixelValueString)}}` : originalValues[index];
183
205
  }).join(' ')}\``)]);
184
206
  } : undefined
185
207
  });
@@ -408,6 +408,18 @@ export function splitCssProperties(styleString) {
408
408
  export function isTokenValueString(originalValue) {
409
409
  return originalValue.startsWith('${token(') && originalValue.endsWith('}');
410
410
  }
411
+ export function findTokenNameByPropertyValue(propertyName, value) {
412
+ const isFontWeightOrFamily = /fontWeight|fontFamily/.test(propertyName);
413
+ const propertyValue = typeof value === 'string' ? value.trim() : value;
414
+ const pixelValue = propertyValue;
415
+ const pixelValueString = `${propertyValue}px`;
416
+ const lookupValue = isFontWeightOrFamily ? pixelValue : pixelValueString;
417
+ const tokenName = isTypographyProperty(propertyName) ? typographyValueToToken[propertyName][lookupValue] : spacingValueToToken[lookupValue];
418
+ if (!tokenName) {
419
+ return undefined;
420
+ }
421
+ return tokenName;
422
+ }
411
423
 
412
424
  /**
413
425
  * Returns a string with token expression corresponding to input parameters
@@ -421,7 +433,7 @@ export function getTokenReplacement(propertyName, value) {
421
433
  const pixelValue = propertyValue;
422
434
  const pixelValueString = `${propertyValue}px`;
423
435
  const lookupValue = isFontWeightOrFamily ? pixelValue : pixelValueString;
424
- const tokenName = isTypographyProperty(propertyName) ? typographyValueToToken[propertyName][lookupValue] : spacingValueToToken[lookupValue];
436
+ const tokenName = findTokenNameByPropertyValue(propertyName, value);
425
437
  if (!tokenName) {
426
438
  return undefined;
427
439
  }
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/eslint-plugin-design-system",
3
- "version": "4.16.4",
3
+ "version": "4.16.5",
4
4
  "sideEffects": false
5
5
  }
@@ -8,7 +8,7 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
8
8
  import { isNodeOfType, node as nodeFn, property } from 'eslint-codemod-utils';
9
9
  import { createRule } from '../utils/create-rule';
10
10
  import { isDecendantOfGlobalToken } from '../utils/is-node';
11
- import { convertHyphenatedNameToCamelCase, emToPixels, findParentNodeForLine, getFontSizeValueInScope, getTokenNodeForValue, getTokenReplacement, getValue, getValueFromShorthand, insertTokensImport, isTokenValueString, isTypographyProperty, isValidSpacingValue, processCssNode, shouldAnalyzeProperty, spacingValueToToken, splitShorthandValues, typographyValueToToken } from './utils';
11
+ import { convertHyphenatedNameToCamelCase, emToPixels, findParentNodeForLine, findTokenNameByPropertyValue, getFontSizeValueInScope, getRawExpression, getTokenNodeForValue, getTokenReplacement, getValue, getValueFromShorthand, insertTokensImport, isTokenValueString, isTypographyProperty, isValidSpacingValue, processCssNode, shouldAnalyzeProperty, spacingValueToToken, splitShorthandValues, typographyValueToToken } from './utils';
12
12
  var rule = createRule({
13
13
  defaultOptions: [{
14
14
  addons: ['spacing'],
@@ -63,7 +63,7 @@ var rule = createRule({
63
63
  },
64
64
  // CSSObjectExpression
65
65
  // const styles = css({ color: 'red', margin: '4px' }), styled.div({ color: 'red', margin: '4px' })
66
- 'CallExpression[callee.name=css] > ObjectExpression, CallExpression[callee.object.name=styled] > ObjectExpression': function CallExpressionCalleeNameCssObjectExpressionCallExpressionCalleeObjectNameStyledObjectExpression(parentNode) {
66
+ 'CallExpression[callee.name=css] > ObjectExpression, CallExpression[callee.object.name=styled] > ObjectExpression, CallExpression[callee.object.name=styled2] > ObjectExpression': function CallExpressionCalleeNameCssObjectExpressionCallExpressionCalleeObjectNameStyledObjectExpressionCallExpressionCalleeObjectNameStyled2ObjectExpression(parentNode) {
67
67
  if (!isNodeOfType(parentNode, 'ObjectExpression')) {
68
68
  return;
69
69
  }
@@ -182,10 +182,34 @@ var rule = createRule({
182
182
  if (!allResolvableValues) {
183
183
  return null;
184
184
  }
185
- return (!tokenNode && ruleConfig.applyImport ? [insertTokensImport(fixer)] : []).concat([fixer.replaceText(node.value, "`".concat(values.map(function (value) {
185
+ var valuesWithTokenReplacement = values.filter(function (value) {
186
+ return findTokenNameByPropertyValue(propertyName, value);
187
+ });
188
+ if (valuesWithTokenReplacement.length === 0) {
189
+ // all values could be replaceable but that just means they're numeric
190
+ // if none of the values have token replacement we bail
191
+ return null;
192
+ }
193
+ var originalCssString = getRawExpression(node.value, context);
194
+ if (!originalCssString) {
195
+ return null;
196
+ }
197
+ /**
198
+ * at this stage none of the values are tokens or irreplaceable values
199
+ * since we know this is shorthand, there will be multiple values
200
+ * we'll need to remove all quote chars since `getRawExpression` will return those as part of the string
201
+ * given they're part of the substring of the current node
202
+ */
203
+ var originalValues = splitShorthandValues(originalCssString.replace(/`|'|"/g, ''));
204
+ if (originalValues.length !== values.length) {
205
+ // we bail just in case original values don't correspond to the replaced values
206
+ return null;
207
+ }
208
+ return (!tokenNode && ruleConfig.applyImport ? [insertTokensImport(fixer)] : []).concat([fixer.replaceText(node.value, "`".concat(values.map(function (value, index) {
186
209
  var pixelValue = emToPixels(value, fontSize);
187
210
  var pixelValueString = "".concat(pixelValue, "px");
188
- return "${".concat(getTokenNodeForValue(propertyName, pixelValueString), "}");
211
+ // if there is a token we take it, otherwise we go with the original value
212
+ return findTokenNameByPropertyValue(propertyName, value) ? "${".concat(getTokenNodeForValue(propertyName, pixelValueString), "}") : originalValues[index];
189
213
  }).join(' '), "`"))]);
190
214
  } : undefined
191
215
  });
@@ -452,6 +452,18 @@ export function splitCssProperties(styleString) {
452
452
  export function isTokenValueString(originalValue) {
453
453
  return originalValue.startsWith('${token(') && originalValue.endsWith('}');
454
454
  }
455
+ export function findTokenNameByPropertyValue(propertyName, value) {
456
+ var isFontWeightOrFamily = /fontWeight|fontFamily/.test(propertyName);
457
+ var propertyValue = typeof value === 'string' ? value.trim() : value;
458
+ var pixelValue = propertyValue;
459
+ var pixelValueString = "".concat(propertyValue, "px");
460
+ var lookupValue = isFontWeightOrFamily ? pixelValue : pixelValueString;
461
+ var tokenName = isTypographyProperty(propertyName) ? typographyValueToToken[propertyName][lookupValue] : spacingValueToToken[lookupValue];
462
+ if (!tokenName) {
463
+ return undefined;
464
+ }
465
+ return tokenName;
466
+ }
455
467
 
456
468
  /**
457
469
  * Returns a string with token expression corresponding to input parameters
@@ -465,7 +477,7 @@ export function getTokenReplacement(propertyName, value) {
465
477
  var pixelValue = propertyValue;
466
478
  var pixelValueString = "".concat(propertyValue, "px");
467
479
  var lookupValue = isFontWeightOrFamily ? pixelValue : pixelValueString;
468
- var tokenName = isTypographyProperty(propertyName) ? typographyValueToToken[propertyName][lookupValue] : spacingValueToToken[lookupValue];
480
+ var tokenName = findTokenNameByPropertyValue(propertyName, value);
469
481
  if (!tokenName) {
470
482
  return undefined;
471
483
  }
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/eslint-plugin-design-system",
3
- "version": "4.16.4",
3
+ "version": "4.16.5",
4
4
  "sideEffects": false
5
5
  }
@@ -150,6 +150,7 @@ export declare function splitCssProperties(styleString: string): string[];
150
150
  * @param originalVaue string representing a css property value e.g 1em, 12px
151
151
  */
152
152
  export declare function isTokenValueString(originalValue: string): boolean;
153
+ export declare function findTokenNameByPropertyValue(propertyName: string, value: string): string | undefined;
153
154
  /**
154
155
  * Returns a string with token expression corresponding to input parameters
155
156
  * if no token found for the pair the function returns undefined
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@atlaskit/eslint-plugin-design-system",
3
3
  "description": "The essential plugin for use with the Atlassian Design System.",
4
- "version": "4.16.4",
4
+ "version": "4.16.5",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "publishConfig": {
7
7
  "registry": "https://registry.npmjs.org/"