@workday/canvas-kit-styling-transform 10.3.1 → 10.3.3

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 (123) hide show
  1. package/index.ts +5 -0
  2. package/package.json +6 -14
  3. package/testing.ts +3 -0
  4. package/dist/commonjs/index.d.ts +0 -3
  5. package/dist/commonjs/index.d.ts.map +0 -1
  6. package/dist/commonjs/index.js +0 -7
  7. package/dist/commonjs/lib/styleTransform.d.ts +0 -23
  8. package/dist/commonjs/lib/styleTransform.d.ts.map +0 -1
  9. package/dist/commonjs/lib/styleTransform.js +0 -103
  10. package/dist/commonjs/lib/utils/createStyleObjectNode.d.ts +0 -18
  11. package/dist/commonjs/lib/utils/createStyleObjectNode.d.ts.map +0 -1
  12. package/dist/commonjs/lib/utils/createStyleObjectNode.js +0 -47
  13. package/dist/commonjs/lib/utils/getCssVariables.d.ts +0 -4
  14. package/dist/commonjs/lib/utils/getCssVariables.d.ts.map +0 -1
  15. package/dist/commonjs/lib/utils/getCssVariables.js +0 -31
  16. package/dist/commonjs/lib/utils/getErrorMessage.d.ts +0 -15
  17. package/dist/commonjs/lib/utils/getErrorMessage.d.ts.map +0 -1
  18. package/dist/commonjs/lib/utils/getErrorMessage.js +0 -45
  19. package/dist/commonjs/lib/utils/getFallbackVariable.d.ts +0 -9
  20. package/dist/commonjs/lib/utils/getFallbackVariable.d.ts.map +0 -1
  21. package/dist/commonjs/lib/utils/getFallbackVariable.js +0 -30
  22. package/dist/commonjs/lib/utils/getVarName.d.ts +0 -17
  23. package/dist/commonjs/lib/utils/getVarName.d.ts.map +0 -1
  24. package/dist/commonjs/lib/utils/getVarName.js +0 -33
  25. package/dist/commonjs/lib/utils/handleCalc.d.ts +0 -19
  26. package/dist/commonjs/lib/utils/handleCalc.d.ts.map +0 -1
  27. package/dist/commonjs/lib/utils/handleCalc.js +0 -62
  28. package/dist/commonjs/lib/utils/handleCreateStencil.d.ts +0 -6
  29. package/dist/commonjs/lib/utils/handleCreateStencil.d.ts.map +0 -1
  30. package/dist/commonjs/lib/utils/handleCreateStencil.js +0 -203
  31. package/dist/commonjs/lib/utils/handleCreateStyles.d.ts +0 -3
  32. package/dist/commonjs/lib/utils/handleCreateStyles.d.ts.map +0 -1
  33. package/dist/commonjs/lib/utils/handleCreateStyles.js +0 -110
  34. package/dist/commonjs/lib/utils/handleCreateVars.d.ts +0 -3
  35. package/dist/commonjs/lib/utils/handleCreateVars.d.ts.map +0 -1
  36. package/dist/commonjs/lib/utils/handleCreateVars.js +0 -34
  37. package/dist/commonjs/lib/utils/handleCssVar.d.ts +0 -12
  38. package/dist/commonjs/lib/utils/handleCssVar.d.ts.map +0 -1
  39. package/dist/commonjs/lib/utils/handleCssVar.js +0 -33
  40. package/dist/commonjs/lib/utils/handleFocusRing.d.ts +0 -3
  41. package/dist/commonjs/lib/utils/handleFocusRing.d.ts.map +0 -1
  42. package/dist/commonjs/lib/utils/handleFocusRing.js +0 -83
  43. package/dist/commonjs/lib/utils/handleKeyframes.d.ts +0 -3
  44. package/dist/commonjs/lib/utils/handleKeyframes.d.ts.map +0 -1
  45. package/dist/commonjs/lib/utils/handleKeyframes.js +0 -47
  46. package/dist/commonjs/lib/utils/handlePx2Rem.d.ts +0 -6
  47. package/dist/commonjs/lib/utils/handlePx2Rem.d.ts.map +0 -1
  48. package/dist/commonjs/lib/utils/handlePx2Rem.js +0 -26
  49. package/dist/commonjs/lib/utils/isImportedFromStyling.d.ts +0 -7
  50. package/dist/commonjs/lib/utils/isImportedFromStyling.d.ts.map +0 -1
  51. package/dist/commonjs/lib/utils/isImportedFromStyling.js +0 -23
  52. package/dist/commonjs/lib/utils/makeEmotionSafe.d.ts +0 -7
  53. package/dist/commonjs/lib/utils/makeEmotionSafe.d.ts.map +0 -1
  54. package/dist/commonjs/lib/utils/makeEmotionSafe.js +0 -15
  55. package/dist/commonjs/lib/utils/parseNodeToStaticValue.d.ts +0 -7
  56. package/dist/commonjs/lib/utils/parseNodeToStaticValue.d.ts.map +0 -1
  57. package/dist/commonjs/lib/utils/parseNodeToStaticValue.js +0 -149
  58. package/dist/commonjs/lib/utils/parseObjectToStaticValue.d.ts +0 -13
  59. package/dist/commonjs/lib/utils/parseObjectToStaticValue.d.ts.map +0 -1
  60. package/dist/commonjs/lib/utils/parseObjectToStaticValue.js +0 -118
  61. package/dist/commonjs/lib/utils/types.d.ts +0 -16
  62. package/dist/commonjs/lib/utils/types.d.ts.map +0 -1
  63. package/dist/commonjs/lib/utils/types.js +0 -2
  64. package/dist/es6/index.d.ts +0 -3
  65. package/dist/es6/index.d.ts.map +0 -1
  66. package/dist/es6/index.js +0 -2
  67. package/dist/es6/lib/styleTransform.d.ts +0 -23
  68. package/dist/es6/lib/styleTransform.d.ts.map +0 -1
  69. package/dist/es6/lib/styleTransform.js +0 -94
  70. package/dist/es6/lib/utils/createStyleObjectNode.d.ts +0 -18
  71. package/dist/es6/lib/utils/createStyleObjectNode.d.ts.map +0 -1
  72. package/dist/es6/lib/utils/createStyleObjectNode.js +0 -39
  73. package/dist/es6/lib/utils/getCssVariables.d.ts +0 -4
  74. package/dist/es6/lib/utils/getCssVariables.d.ts.map +0 -1
  75. package/dist/es6/lib/utils/getCssVariables.js +0 -26
  76. package/dist/es6/lib/utils/getErrorMessage.d.ts +0 -15
  77. package/dist/es6/lib/utils/getErrorMessage.d.ts.map +0 -1
  78. package/dist/es6/lib/utils/getErrorMessage.js +0 -41
  79. package/dist/es6/lib/utils/getFallbackVariable.d.ts +0 -9
  80. package/dist/es6/lib/utils/getFallbackVariable.d.ts.map +0 -1
  81. package/dist/es6/lib/utils/getFallbackVariable.js +0 -26
  82. package/dist/es6/lib/utils/getVarName.d.ts +0 -17
  83. package/dist/es6/lib/utils/getVarName.d.ts.map +0 -1
  84. package/dist/es6/lib/utils/getVarName.js +0 -26
  85. package/dist/es6/lib/utils/handleCalc.d.ts +0 -19
  86. package/dist/es6/lib/utils/handleCalc.d.ts.map +0 -1
  87. package/dist/es6/lib/utils/handleCalc.js +0 -55
  88. package/dist/es6/lib/utils/handleCreateStencil.d.ts +0 -6
  89. package/dist/es6/lib/utils/handleCreateStencil.d.ts.map +0 -1
  90. package/dist/es6/lib/utils/handleCreateStencil.js +0 -196
  91. package/dist/es6/lib/utils/handleCreateStyles.d.ts +0 -3
  92. package/dist/es6/lib/utils/handleCreateStyles.d.ts.map +0 -1
  93. package/dist/es6/lib/utils/handleCreateStyles.js +0 -103
  94. package/dist/es6/lib/utils/handleCreateVars.d.ts +0 -3
  95. package/dist/es6/lib/utils/handleCreateVars.d.ts.map +0 -1
  96. package/dist/es6/lib/utils/handleCreateVars.js +0 -27
  97. package/dist/es6/lib/utils/handleCssVar.d.ts +0 -12
  98. package/dist/es6/lib/utils/handleCssVar.d.ts.map +0 -1
  99. package/dist/es6/lib/utils/handleCssVar.js +0 -26
  100. package/dist/es6/lib/utils/handleFocusRing.d.ts +0 -3
  101. package/dist/es6/lib/utils/handleFocusRing.d.ts.map +0 -1
  102. package/dist/es6/lib/utils/handleFocusRing.js +0 -76
  103. package/dist/es6/lib/utils/handleKeyframes.d.ts +0 -3
  104. package/dist/es6/lib/utils/handleKeyframes.d.ts.map +0 -1
  105. package/dist/es6/lib/utils/handleKeyframes.js +0 -40
  106. package/dist/es6/lib/utils/handlePx2Rem.d.ts +0 -6
  107. package/dist/es6/lib/utils/handlePx2Rem.d.ts.map +0 -1
  108. package/dist/es6/lib/utils/handlePx2Rem.js +0 -19
  109. package/dist/es6/lib/utils/isImportedFromStyling.d.ts +0 -7
  110. package/dist/es6/lib/utils/isImportedFromStyling.d.ts.map +0 -1
  111. package/dist/es6/lib/utils/isImportedFromStyling.js +0 -16
  112. package/dist/es6/lib/utils/makeEmotionSafe.d.ts +0 -7
  113. package/dist/es6/lib/utils/makeEmotionSafe.d.ts.map +0 -1
  114. package/dist/es6/lib/utils/makeEmotionSafe.js +0 -11
  115. package/dist/es6/lib/utils/parseNodeToStaticValue.d.ts +0 -7
  116. package/dist/es6/lib/utils/parseNodeToStaticValue.d.ts.map +0 -1
  117. package/dist/es6/lib/utils/parseNodeToStaticValue.js +0 -142
  118. package/dist/es6/lib/utils/parseObjectToStaticValue.d.ts +0 -13
  119. package/dist/es6/lib/utils/parseObjectToStaticValue.d.ts.map +0 -1
  120. package/dist/es6/lib/utils/parseObjectToStaticValue.js +0 -110
  121. package/dist/es6/lib/utils/types.d.ts +0 -16
  122. package/dist/es6/lib/utils/types.d.ts.map +0 -1
  123. package/dist/es6/lib/utils/types.js +0 -1
@@ -1,9 +0,0 @@
1
- /**
2
- * Looks for a variable value that doesn't include a fallback and automatically adds one if found in
3
- * the current cache of variables. This allows fallbacks to be automatically included in
4
- * environments without the variables defined. This is most useful for Storybook or other sandboxes
5
- * that may not have CSS Variables defined. The fallbacks will allow the UI to look correct without
6
- * additional setup. Fallbacks come from the `fallbackFiles` transform configuration.
7
- */
8
- export declare function getFallbackVariable(variableName: string, variables: Record<string, string>): string | undefined;
9
- //# sourceMappingURL=getFallbackVariable.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getFallbackVariable.d.ts","sourceRoot":"","sources":["../../../../lib/utils/getFallbackVariable.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAChC,MAAM,GAAG,SAAS,CAuBpB"}
@@ -1,26 +0,0 @@
1
- /**
2
- * Looks for a variable value that doesn't include a fallback and automatically adds one if found in
3
- * the current cache of variables. This allows fallbacks to be automatically included in
4
- * environments without the variables defined. This is most useful for Storybook or other sandboxes
5
- * that may not have CSS Variables defined. The fallbacks will allow the UI to look correct without
6
- * additional setup. Fallbacks come from the `fallbackFiles` transform configuration.
7
- */
8
- export function getFallbackVariable(variableName, variables) {
9
- const variable = variableName.includes('var(') ? variableName : variables[variableName];
10
- if (variable && variable.includes('var(')) {
11
- return variable.replace(/(var\(([A-Za-z0-9\-_]+)\))/, (
12
- /** matched substring "var(--var-name)" */ _,
13
- /** the full match of the first group "var(--var-name)" */ varMatch,
14
- /** the variable name - match of the second group "--var-name" */ cssVarName) => {
15
- const value = variables[cssVarName];
16
- if (value && value.startsWith('var')) {
17
- return getFallbackVariable(value, variables);
18
- }
19
- return value || varMatch;
20
- });
21
- }
22
- if (variable) {
23
- return variable;
24
- }
25
- return;
26
- }
@@ -1,17 +0,0 @@
1
- import ts from 'typescript';
2
- /**
3
- * This function returns a calculated name of a node by walking up the AST and looking for nodes
4
- * with a `name` property that has an `Identifier` node type. The result is dash-case. This is
5
- * useful for automatically generating variable names based on a TS file.
6
- *
7
- * In the following example, the `baz` node would have a name of `foo-bar-baz`.
8
- * ```ts
9
- * const foo = {
10
- * bar: {
11
- * baz: ''
12
- * }
13
- * }
14
- * ```
15
- */
16
- export declare function getVarName(node: ts.Node, parts?: string[]): string;
17
- //# sourceMappingURL=getVarName.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getVarName.d.ts","sourceRoot":"","sources":["../../../../lib/utils/getVarName.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAE5B;;;;;;;;;;;;;GAaG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,KAAK,GAAE,MAAM,EAAO,GAAG,MAAM,CAYtE"}
@@ -1,26 +0,0 @@
1
- import ts from 'typescript';
2
- /**
3
- * This function returns a calculated name of a node by walking up the AST and looking for nodes
4
- * with a `name` property that has an `Identifier` node type. The result is dash-case. This is
5
- * useful for automatically generating variable names based on a TS file.
6
- *
7
- * In the following example, the `baz` node would have a name of `foo-bar-baz`.
8
- * ```ts
9
- * const foo = {
10
- * bar: {
11
- * baz: ''
12
- * }
13
- * }
14
- * ```
15
- */
16
- export function getVarName(node, parts = []) {
17
- // base case. Join all the parts
18
- if (!node.parent || node.kind === ts.SyntaxKind.VariableStatement) {
19
- return parts.join('-');
20
- }
21
- // Any node with a `name` property that is an identifier can add to the var name
22
- if (node.name && ts.isIdentifier(node.name)) {
23
- return getVarName(node.parent, [node.name.text, ...parts]);
24
- }
25
- return getVarName(node.parent, parts);
26
- }
@@ -1,19 +0,0 @@
1
- import { NodeTransformer } from './types';
2
- /**
3
- * Handle all instances of `calc.*()` statically. It converts `calc.*` call expressions to template
4
- * string literals. The transformer will then transform the template literal into static values.
5
- *
6
- * ```ts
7
- * // in
8
- * calc.add('80%', '2px')
9
- *
10
- * // out
11
- * `calc(${'80%'} + ${'2px'})`
12
- * ```
13
- *
14
- * A template literal is used because the values might be Identifiers, PropertyAccessExpressions,
15
- * etc. The transform can handle template string literals with different spans, so we'll convert to
16
- * those as an intermediate step.
17
- */
18
- export declare const handleCalc: NodeTransformer;
19
- //# sourceMappingURL=handleCalc.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"handleCalc.d.ts","sourceRoot":"","sources":["../../../../lib/utils/handleCalc.ts"],"names":[],"mappings":"AAGA,OAAO,EAAC,eAAe,EAAC,MAAM,SAAS,CAAC;AAExC;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,UAAU,EAAE,eA+BxB,CAAC"}
@@ -1,55 +0,0 @@
1
- import ts from 'typescript';
2
- import { isImportedFromStyling } from './isImportedFromStyling';
3
- /**
4
- * Handle all instances of `calc.*()` statically. It converts `calc.*` call expressions to template
5
- * string literals. The transformer will then transform the template literal into static values.
6
- *
7
- * ```ts
8
- * // in
9
- * calc.add('80%', '2px')
10
- *
11
- * // out
12
- * `calc(${'80%'} + ${'2px'})`
13
- * ```
14
- *
15
- * A template literal is used because the values might be Identifiers, PropertyAccessExpressions,
16
- * etc. The transform can handle template string literals with different spans, so we'll convert to
17
- * those as an intermediate step.
18
- */
19
- export const handleCalc = (node, context) => {
20
- if (ts.isCallExpression(node) &&
21
- ts.isPropertyAccessExpression(node.expression) &&
22
- ts.isIdentifier(node.expression.expression) &&
23
- node.expression.expression.text === 'calc' &&
24
- ts.isIdentifier(node.expression.name) &&
25
- isImportedFromStyling(node.expression.expression, context.checker)) {
26
- if (node.expression.name.text === 'add') {
27
- return replaceWithTemplateString(node.arguments[0], node.arguments[1], ' + ');
28
- }
29
- if (node.expression.name.text === 'subtract') {
30
- return replaceWithTemplateString(node.arguments[0], node.arguments[1], ' - ');
31
- }
32
- if (node.expression.name.text === 'multiply') {
33
- return replaceWithTemplateString(node.arguments[0], node.arguments[1], ' * ');
34
- }
35
- if (node.expression.name.text === 'divide') {
36
- return replaceWithTemplateString(node.arguments[0], node.arguments[1], ' / ');
37
- }
38
- if (node.expression.name.text === 'negate') {
39
- return replaceWithTemplateString(node.arguments[0], ts.factory.createStringLiteral('-1'), ' * ');
40
- }
41
- }
42
- return;
43
- };
44
- /**
45
- * Creates a template literal of the calculation result.
46
- */
47
- function replaceWithTemplateString(value1, value2, binder) {
48
- if (ts.isStringLiteral(value1) && ts.isStringLiteral(value2)) {
49
- return ts.factory.createStringLiteral(`calc(${value1.text}${binder}${value2.text})`);
50
- }
51
- return ts.factory.createTemplateExpression(ts.factory.createTemplateHead('calc('), [
52
- ts.factory.createTemplateSpan(value1, ts.factory.createTemplateMiddle(binder)),
53
- ts.factory.createTemplateSpan(value2, ts.factory.createTemplateTail(')')),
54
- ]);
55
- }
@@ -1,6 +0,0 @@
1
- import { NodeTransformer } from './types';
2
- /**
3
- * Handle all arguments of the CallExpression `createStencil()`
4
- */
5
- export declare const handleCreateStencil: NodeTransformer;
6
- //# sourceMappingURL=handleCreateStencil.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"handleCreateStencil.d.ts","sourceRoot":"","sources":["../../../../lib/utils/handleCreateStencil.ts"],"names":[],"mappings":"AASA,OAAO,EAAC,eAAe,EAAqB,MAAM,SAAS,CAAC;AAG5D;;GAEG;AACH,eAAO,MAAM,mBAAmB,EAAE,eAqLjC,CAAC"}
@@ -1,196 +0,0 @@
1
- import ts from 'typescript';
2
- import { slugify } from '@workday/canvas-kit-styling';
3
- import { getVarName } from './getVarName';
4
- import { makeEmotionSafe } from './makeEmotionSafe';
5
- import { parseObjectToStaticValue } from './parseObjectToStaticValue';
6
- import { createStyleObjectNode } from './createStyleObjectNode';
7
- import { parseNodeToStaticValue } from './parseNodeToStaticValue';
8
- import { isImportedFromStyling } from './isImportedFromStyling';
9
- /**
10
- * Handle all arguments of the CallExpression `createStencil()`
11
- */
12
- export const handleCreateStencil = (node, context) => {
13
- const { checker, prefix, variables } = context;
14
- /**
15
- * This will match whenever a `createStencil()` call expression is encountered. It will loop
16
- * over all the config to extract variables and styles.
17
- *
18
- */
19
- if (ts.isCallExpression(node) &&
20
- ts.isIdentifier(node.expression) &&
21
- node.expression.text === 'createStencil' &&
22
- isImportedFromStyling(node.expression, checker)) {
23
- const config = node.arguments[0];
24
- /**
25
- * Stencils can define variables that are used in style object functions. Inside those
26
- * functions, the full variable name is not used, but rather destructured. We'll create
27
- * temporary local variables for these style object functions.
28
- *
29
- * For example:
30
- * ```ts
31
- * const myStencil = createStencil({
32
- * vars: { color: 'red' },
33
- * base: ({color}) => ({
34
- * color: color
35
- * })
36
- * })
37
- * ```
38
- */
39
- const tempVariables = {};
40
- // We need to keep track of stencil variables and values to automatically merge into the base
41
- // styles
42
- const stencilVariables = {};
43
- // Stencil name is the variable name
44
- const stencilName = slugify(getVarName(node)).replace('-stencil', '');
45
- if (ts.isObjectLiteralExpression(config)) {
46
- // get variables first
47
- const varsConfig = config.properties.find(property => {
48
- return (property.name &&
49
- ts.isIdentifier(property.name) &&
50
- property.name.text === 'vars' &&
51
- ts.isPropertyAssignment(property));
52
- });
53
- function extractVariables(node) {
54
- if (ts.isPropertyAssignment(node) && ts.isIdentifier(node.name)) {
55
- if (ts.isObjectLiteralExpression(node.initializer)) {
56
- return node.initializer.properties.map(extractVariables);
57
- }
58
- const varName = `${stencilName}-${makeEmotionSafe(node.name.text)}`;
59
- const varValue = `--${prefix}-${varName}`;
60
- variables[`${varName}`] = varValue;
61
- variables[makeEmotionSafe(node.name.text)] = varValue;
62
- tempVariables[makeEmotionSafe(node.name.text)] = varValue;
63
- // Evaluate the variable defaults
64
- stencilVariables[varValue] = parseNodeToStaticValue(node.initializer, context);
65
- }
66
- }
67
- if (varsConfig && ts.isObjectLiteralExpression(varsConfig.initializer)) {
68
- varsConfig.initializer.properties.forEach(variable => {
69
- extractVariables(variable);
70
- });
71
- }
72
- config.properties.forEach((property, index, properties) => {
73
- if (property.name && ts.isIdentifier(property.name)) {
74
- // base config object
75
- if (property.name.text === 'base') {
76
- const styleObj = parseStyleBlock(property, context);
77
- if (styleObj) {
78
- // The `as any` are necessary because the properties are readonly, even though they
79
- // can be changed in transformers.
80
- const initializer = createStyleObjectNode({
81
- ...stencilVariables,
82
- ...styleObj,
83
- });
84
- // We cast as any because TypeScript says these are readonly, but we're in a transform
85
- initializer.parent = property;
86
- properties[index] = ts.factory.createPropertyAssignment(property.name, initializer);
87
- }
88
- }
89
- // modifiers config object
90
- if (property.name.text === 'modifiers' &&
91
- ts.isPropertyAssignment(property) &&
92
- ts.isObjectLiteralExpression(property.initializer)) {
93
- property.initializer.properties.forEach(modifierProperty => {
94
- if (modifierProperty.name &&
95
- ts.isIdentifier(modifierProperty.name) &&
96
- ts.isPropertyAssignment(modifierProperty) &&
97
- ts.isObjectLiteralExpression(modifierProperty.initializer)) {
98
- modifierProperty.initializer.properties.forEach((modifier, index, modifiers) => {
99
- const styleObj = parseStyleBlock(modifier, context);
100
- if (styleObj && modifier.name) {
101
- // The `as any` are necessary because the properties are readonly, even though they
102
- // can be changed in transformers.
103
- const initializer = createStyleObjectNode(styleObj);
104
- // // We cast as any because TypeScript says these are readonly, but we're in a transform
105
- initializer.parent = modifier;
106
- modifiers[index] = ts.factory.createPropertyAssignment(modifier.name, initializer);
107
- }
108
- });
109
- }
110
- });
111
- }
112
- // compound config array
113
- if (property.name.text === 'compound' &&
114
- ts.isPropertyAssignment(property) &&
115
- ts.isArrayLiteralExpression(property.initializer)) {
116
- property.initializer.elements.forEach(element => {
117
- if (ts.isObjectLiteralExpression(element)) {
118
- element.properties.forEach((compoundProperty, index, compoundProperties) => {
119
- // styles key
120
- if (compoundProperty.name &&
121
- ts.isIdentifier(compoundProperty.name) &&
122
- compoundProperty.name.text === 'styles') {
123
- const styleObj = parseStyleBlock(compoundProperty, context);
124
- if (styleObj) {
125
- // The `as any` are necessary because the properties are readonly, even though they
126
- // can be changed in transformers.
127
- const initializer = createStyleObjectNode(styleObj);
128
- // We cast as any because TypeScript says these are readonly, but we're in a transform
129
- initializer.parent = compoundProperty;
130
- compoundProperties[index] = ts.factory.createPropertyAssignment(compoundProperty.name, initializer);
131
- }
132
- }
133
- });
134
- }
135
- });
136
- }
137
- }
138
- });
139
- }
140
- // remove all our temp variables
141
- // eslint-disable-next-line guard-for-in
142
- for (const key in tempVariables) {
143
- delete variables[key];
144
- }
145
- // arguments are readonly, but we're in a transform
146
- node.arguments[1] = ts.factory.createStringLiteral(stencilName);
147
- node.arguments[1].parent = node;
148
- return node;
149
- }
150
- return;
151
- };
152
- /**
153
- * A style block is a `base`, `modifier`, or `compoundModifier` style block. It could be an ObjectLiteralExpression,
154
- * an ArrowFunction, MethodDeclaration, etc.
155
- */
156
- function parseStyleBlock(property, context) {
157
- let styleObj;
158
- if (ts.isPropertyAssignment(property)) {
159
- if (ts.isObjectLiteralExpression(property.initializer)) {
160
- styleObj = parseObjectToStaticValue(property.initializer, context);
161
- }
162
- if (isFunctionLikeDeclaration(property.initializer)) {
163
- const returnNode = getReturnStatement(property.initializer);
164
- if (returnNode) {
165
- styleObj = parseObjectToStaticValue(returnNode, context);
166
- }
167
- }
168
- }
169
- if (isFunctionLikeDeclaration(property)) {
170
- const returnNode = getReturnStatement(property);
171
- if (returnNode) {
172
- styleObj = parseObjectToStaticValue(returnNode, context);
173
- }
174
- }
175
- return styleObj;
176
- }
177
- function getReturnStatement(node) {
178
- if (node.body && ts.isParenthesizedExpression(node.body)) {
179
- return node.body.expression;
180
- }
181
- if (node.body && ts.isBlock(node.body)) {
182
- let returnNode;
183
- // look for the return statement. It must be a top-level statement in the block
184
- node.body.statements.forEach(statement => {
185
- // () => { return {} }
186
- if (ts.isReturnStatement(statement)) {
187
- returnNode = statement.expression;
188
- }
189
- });
190
- return returnNode;
191
- }
192
- return undefined;
193
- }
194
- function isFunctionLikeDeclaration(node) {
195
- return node.hasOwnProperty('body');
196
- }
@@ -1,3 +0,0 @@
1
- import { NodeTransformer } from './types';
2
- export declare const handleCreateStyles: NodeTransformer;
3
- //# sourceMappingURL=handleCreateStyles.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"handleCreateStyles.d.ts","sourceRoot":"","sources":["../../../../lib/utils/handleCreateStyles.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,eAAe,EAAC,MAAM,SAAS,CAAC;AAGxC,eAAO,MAAM,kBAAkB,EAAE,eA4GhC,CAAC"}
@@ -1,103 +0,0 @@
1
- import ts from 'typescript';
2
- import { parseObjectToStaticValue, parseStyleObjFromType } from './parseObjectToStaticValue';
3
- import { createStyleObjectNode } from './createStyleObjectNode';
4
- import { isImportedFromStyling } from './isImportedFromStyling';
5
- export const handleCreateStyles = (node, context) => {
6
- const { checker } = context;
7
- /**
8
- * Check if the node is a call expression that looks like:
9
- *
10
- * ```ts
11
- * createStyles({
12
- * // properties
13
- * })
14
- * ```
15
- *
16
- * It will also make sure the `createStyles` function was imported from
17
- * `@workday/canvas-kit-styling` to ensure we don't rewrite the AST of code we don't own.
18
- *
19
- * This transformation will pre-serialize the style objects and turn them into strings for
20
- * faster runtime processing in Emotion. The following is an example of the transformation.
21
- *
22
- * ```ts
23
- * // before transformation
24
- * const myStyles = createStyles({
25
- * fontSize: '1rem'
26
- * })
27
- *
28
- * // after transformation
29
- * const myStyles = createStyles({
30
- * name: 'abc123',
31
- * styles: 'font-size: 1rem;'
32
- * })
33
- * ```
34
- *
35
- * The after transformation already serialized the styles and goes through a shortcut process
36
- * in `@emotion/css` where only the Emotion cache is checked and styles are inserted if the
37
- * cache key wasn't found.
38
- */
39
- if (ts.isCallExpression(node) &&
40
- ts.isIdentifier(node.expression) &&
41
- node.expression.text === 'createStyles' &&
42
- isImportedFromStyling(node.expression, checker) &&
43
- node.arguments.length > 0) {
44
- const newArguments = [...node.arguments].map(arg => {
45
- // An `ObjectLiteralExpression` is an object like `{foo:'bar'}`:
46
- // https://ts-ast-viewer.com/#code/MYewdgzgLgBFCmBbADjAvDA3gKBjAZiCAFwwDkARgIYBOZ2AvkA
47
- if (ts.isObjectLiteralExpression(arg)) {
48
- const styleObj = parseObjectToStaticValue(arg, context);
49
- return createStyleObjectNode(styleObj);
50
- }
51
- // An Identifier is a variable. It could come from anywhere - imports, earlier
52
- // assignments, etc. The easiest thing to do is to ask the TypeScript type checker what
53
- // the type representation is and go from there.
54
- if (ts.isIdentifier(arg)) {
55
- const type = checker.getTypeAtLocation(arg);
56
- // `createStyles` accepts strings as class names. If the class name is
57
- if (type.isStringLiteral() || type.getFlags() & ts.TypeFlags.String) {
58
- return arg;
59
- }
60
- // The type must be a object
61
- const styleObj = parseStyleObjFromType(type, context);
62
- return createStyleObjectNode(styleObj);
63
- }
64
- return arg;
65
- });
66
- newArguments.forEach(argument => {
67
- // TypeScript isn't expecting us to mutate arguments arguments and when emitting will
68
- // try to do something where it checks the `parent` node of the argument. Using
69
- // `ts.factory.create*`, the `parent` is `undefined` and this check will throw an error.
70
- // In order to get past this error, we manually update the `parent` node of each
71
- // argument to reference the existing call expression. This allows TypeScript to fully
72
- // type check and/or emit.
73
- argument.parent = node;
74
- });
75
- /**
76
- * We're not supposed to mutate arguments since it is supposed to be read-only. But, if I
77
- * return a new callExpression, there is no parent and it is no longer linked to the
78
- * import module. This causes incorrect code when the module export type is `commonjs`.
79
- * For example:
80
- *
81
- * ```ts
82
- * // with new callExpression
83
- * const canvas_kit_styling_1 = require(...)
84
- *
85
- * createStyles({...})
86
- *
87
- * // if we instead mutate arguments
88
- * const canvas_kit_styling_1 = require(...)
89
- *
90
- * canvas_kit_styling_1.createStyles({...})
91
- * ```
92
- *
93
- * My best guess as to why it fails when creating a new callExpression is the node's
94
- * symbol declaration link gets lost. TypeScript then has no idea `createStyles` comes
95
- * from an `ImportDeclaration` declaration node and when emitting `commonjs`, it doesn't
96
- * prefix with the `canvas_kit_styling_1`. This is hacky, but the only thing that works
97
- * correctly.
98
- */
99
- node.arguments = newArguments;
100
- return node;
101
- }
102
- return;
103
- };
@@ -1,3 +0,0 @@
1
- import { NodeTransformer } from './types';
2
- export declare const handleCreateVars: NodeTransformer;
3
- //# sourceMappingURL=handleCreateVars.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"handleCreateVars.d.ts","sourceRoot":"","sources":["../../../../lib/utils/handleCreateVars.ts"],"names":[],"mappings":"AAMA,OAAO,EAAC,eAAe,EAAC,MAAM,SAAS,CAAC;AAExC,eAAO,MAAM,gBAAgB,EAAE,eA2C9B,CAAC"}
@@ -1,27 +0,0 @@
1
- import ts from 'typescript';
2
- import { slugify } from '@workday/canvas-kit-styling';
3
- import { getVarName } from './getVarName';
4
- import { makeEmotionSafe } from './makeEmotionSafe';
5
- export const handleCreateVars = (node, { prefix, variables }) => {
6
- /**
7
- * This will create a variable
8
- */
9
- if (ts.isCallExpression(node) &&
10
- ts.isIdentifier(node.expression) &&
11
- node.expression.text === 'createVars') {
12
- const id = slugify(getVarName(node)).replace('-vars', '');
13
- const vars = node.arguments
14
- .map(arg => ts.isStringLiteral(arg) && arg.text)
15
- .filter(Boolean);
16
- vars.forEach(v => {
17
- variables[`${id}-${makeEmotionSafe(v)}`] = `--${prefix}-${id}-${makeEmotionSafe(v)}`;
18
- });
19
- return ts.factory.createCallExpression(node.expression, [], [
20
- ts.factory.createObjectLiteralExpression([
21
- ts.factory.createPropertyAssignment(ts.factory.createIdentifier('id'), ts.factory.createStringLiteral(`${prefix}-${id}`)),
22
- ts.factory.createPropertyAssignment(ts.factory.createIdentifier('args'), ts.factory.createArrayLiteralExpression(vars.map(val => ts.factory.createStringLiteral(val)), false)),
23
- ], false),
24
- ]);
25
- }
26
- return;
27
- };
@@ -1,12 +0,0 @@
1
- import { NodeTransformer } from './types';
2
- /**
3
- * Converts `cssVar` to a `TemplateExpression` with each argument as a span
4
- *
5
- * - `cssVar('--foo')` => `\`var(${'--foo'})\``
6
- * - `cssVar('--foo', 'fallback')` => `\`var(${'--foo'}, ${'fallback'})\``
7
- *
8
- * The value parser will figure out what to do from there. We don't have access to variables at this
9
- * point, so we forward CallExpression arguments in ways the value parser understands.
10
- */
11
- export declare const handleCssVar: NodeTransformer;
12
- //# sourceMappingURL=handleCssVar.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"handleCssVar.d.ts","sourceRoot":"","sources":["../../../../lib/utils/handleCssVar.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,eAAe,EAAC,MAAM,SAAS,CAAC;AAExC;;;;;;;;GAQG;AACH,eAAO,MAAM,YAAY,EAAE,eAoB1B,CAAC"}
@@ -1,26 +0,0 @@
1
- import ts from 'typescript';
2
- /**
3
- * Converts `cssVar` to a `TemplateExpression` with each argument as a span
4
- *
5
- * - `cssVar('--foo')` => `\`var(${'--foo'})\``
6
- * - `cssVar('--foo', 'fallback')` => `\`var(${'--foo'}, ${'fallback'})\``
7
- *
8
- * The value parser will figure out what to do from there. We don't have access to variables at this
9
- * point, so we forward CallExpression arguments in ways the value parser understands.
10
- */
11
- export const handleCssVar = node => {
12
- // cssVar(a)
13
- // cssVar(a, b)
14
- if (ts.isCallExpression(node) &&
15
- ts.isIdentifier(node.expression) &&
16
- node.expression.text === 'cssVar') {
17
- if (node.arguments.length === 1) {
18
- return node.arguments[0];
19
- }
20
- return ts.factory.createTemplateExpression(ts.factory.createTemplateHead('var('), [
21
- ts.factory.createTemplateSpan(node.arguments[0], ts.factory.createTemplateMiddle(', ')),
22
- ts.factory.createTemplateSpan(node.arguments[1], ts.factory.createTemplateTail(')')),
23
- ]);
24
- }
25
- return;
26
- };
@@ -1,3 +0,0 @@
1
- import { NodeTransformer } from './types';
2
- export declare const handleFocusRing: NodeTransformer;
3
- //# sourceMappingURL=handleFocusRing.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"handleFocusRing.d.ts","sourceRoot":"","sources":["../../../../lib/utils/handleFocusRing.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,eAAe,EAAC,MAAM,SAAS,CAAC;AAGxC,eAAO,MAAM,eAAe,EAAE,eAwG7B,CAAC"}