@workday/canvas-kit-styling-transform 10.3.2 → 10.3.4

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,203 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.handleCreateStencil = void 0;
7
- const typescript_1 = __importDefault(require("typescript"));
8
- const canvas_kit_styling_1 = require("@workday/canvas-kit-styling");
9
- const getVarName_1 = require("./getVarName");
10
- const makeEmotionSafe_1 = require("./makeEmotionSafe");
11
- const parseObjectToStaticValue_1 = require("./parseObjectToStaticValue");
12
- const createStyleObjectNode_1 = require("./createStyleObjectNode");
13
- const parseNodeToStaticValue_1 = require("./parseNodeToStaticValue");
14
- const isImportedFromStyling_1 = require("./isImportedFromStyling");
15
- /**
16
- * Handle all arguments of the CallExpression `createStencil()`
17
- */
18
- const handleCreateStencil = (node, context) => {
19
- const { checker, prefix, variables } = context;
20
- /**
21
- * This will match whenever a `createStencil()` call expression is encountered. It will loop
22
- * over all the config to extract variables and styles.
23
- *
24
- */
25
- if (typescript_1.default.isCallExpression(node) &&
26
- typescript_1.default.isIdentifier(node.expression) &&
27
- node.expression.text === 'createStencil' &&
28
- isImportedFromStyling_1.isImportedFromStyling(node.expression, checker)) {
29
- const config = node.arguments[0];
30
- /**
31
- * Stencils can define variables that are used in style object functions. Inside those
32
- * functions, the full variable name is not used, but rather destructured. We'll create
33
- * temporary local variables for these style object functions.
34
- *
35
- * For example:
36
- * ```ts
37
- * const myStencil = createStencil({
38
- * vars: { color: 'red' },
39
- * base: ({color}) => ({
40
- * color: color
41
- * })
42
- * })
43
- * ```
44
- */
45
- const tempVariables = {};
46
- // We need to keep track of stencil variables and values to automatically merge into the base
47
- // styles
48
- const stencilVariables = {};
49
- // Stencil name is the variable name
50
- const stencilName = canvas_kit_styling_1.slugify(getVarName_1.getVarName(node)).replace('-stencil', '');
51
- if (typescript_1.default.isObjectLiteralExpression(config)) {
52
- // get variables first
53
- const varsConfig = config.properties.find(property => {
54
- return (property.name &&
55
- typescript_1.default.isIdentifier(property.name) &&
56
- property.name.text === 'vars' &&
57
- typescript_1.default.isPropertyAssignment(property));
58
- });
59
- function extractVariables(node) {
60
- if (typescript_1.default.isPropertyAssignment(node) && typescript_1.default.isIdentifier(node.name)) {
61
- if (typescript_1.default.isObjectLiteralExpression(node.initializer)) {
62
- return node.initializer.properties.map(extractVariables);
63
- }
64
- const varName = `${stencilName}-${makeEmotionSafe_1.makeEmotionSafe(node.name.text)}`;
65
- const varValue = `--${prefix}-${varName}`;
66
- variables[`${varName}`] = varValue;
67
- variables[makeEmotionSafe_1.makeEmotionSafe(node.name.text)] = varValue;
68
- tempVariables[makeEmotionSafe_1.makeEmotionSafe(node.name.text)] = varValue;
69
- // Evaluate the variable defaults
70
- stencilVariables[varValue] = parseNodeToStaticValue_1.parseNodeToStaticValue(node.initializer, context);
71
- }
72
- }
73
- if (varsConfig && typescript_1.default.isObjectLiteralExpression(varsConfig.initializer)) {
74
- varsConfig.initializer.properties.forEach(variable => {
75
- extractVariables(variable);
76
- });
77
- }
78
- config.properties.forEach((property, index, properties) => {
79
- if (property.name && typescript_1.default.isIdentifier(property.name)) {
80
- // base config object
81
- if (property.name.text === 'base') {
82
- const styleObj = parseStyleBlock(property, context);
83
- if (styleObj) {
84
- // The `as any` are necessary because the properties are readonly, even though they
85
- // can be changed in transformers.
86
- const initializer = createStyleObjectNode_1.createStyleObjectNode({
87
- ...stencilVariables,
88
- ...styleObj,
89
- });
90
- // We cast as any because TypeScript says these are readonly, but we're in a transform
91
- initializer.parent = property;
92
- properties[index] = typescript_1.default.factory.createPropertyAssignment(property.name, initializer);
93
- }
94
- }
95
- // modifiers config object
96
- if (property.name.text === 'modifiers' &&
97
- typescript_1.default.isPropertyAssignment(property) &&
98
- typescript_1.default.isObjectLiteralExpression(property.initializer)) {
99
- property.initializer.properties.forEach(modifierProperty => {
100
- if (modifierProperty.name &&
101
- typescript_1.default.isIdentifier(modifierProperty.name) &&
102
- typescript_1.default.isPropertyAssignment(modifierProperty) &&
103
- typescript_1.default.isObjectLiteralExpression(modifierProperty.initializer)) {
104
- modifierProperty.initializer.properties.forEach((modifier, index, modifiers) => {
105
- const styleObj = parseStyleBlock(modifier, context);
106
- if (styleObj && modifier.name) {
107
- // The `as any` are necessary because the properties are readonly, even though they
108
- // can be changed in transformers.
109
- const initializer = createStyleObjectNode_1.createStyleObjectNode(styleObj);
110
- // // We cast as any because TypeScript says these are readonly, but we're in a transform
111
- initializer.parent = modifier;
112
- modifiers[index] = typescript_1.default.factory.createPropertyAssignment(modifier.name, initializer);
113
- }
114
- });
115
- }
116
- });
117
- }
118
- // compound config array
119
- if (property.name.text === 'compound' &&
120
- typescript_1.default.isPropertyAssignment(property) &&
121
- typescript_1.default.isArrayLiteralExpression(property.initializer)) {
122
- property.initializer.elements.forEach(element => {
123
- if (typescript_1.default.isObjectLiteralExpression(element)) {
124
- element.properties.forEach((compoundProperty, index, compoundProperties) => {
125
- // styles key
126
- if (compoundProperty.name &&
127
- typescript_1.default.isIdentifier(compoundProperty.name) &&
128
- compoundProperty.name.text === 'styles') {
129
- const styleObj = parseStyleBlock(compoundProperty, context);
130
- if (styleObj) {
131
- // The `as any` are necessary because the properties are readonly, even though they
132
- // can be changed in transformers.
133
- const initializer = createStyleObjectNode_1.createStyleObjectNode(styleObj);
134
- // We cast as any because TypeScript says these are readonly, but we're in a transform
135
- initializer.parent = compoundProperty;
136
- compoundProperties[index] = typescript_1.default.factory.createPropertyAssignment(compoundProperty.name, initializer);
137
- }
138
- }
139
- });
140
- }
141
- });
142
- }
143
- }
144
- });
145
- }
146
- // remove all our temp variables
147
- // eslint-disable-next-line guard-for-in
148
- for (const key in tempVariables) {
149
- delete variables[key];
150
- }
151
- // arguments are readonly, but we're in a transform
152
- node.arguments[1] = typescript_1.default.factory.createStringLiteral(stencilName);
153
- node.arguments[1].parent = node;
154
- return node;
155
- }
156
- return;
157
- };
158
- exports.handleCreateStencil = handleCreateStencil;
159
- /**
160
- * A style block is a `base`, `modifier`, or `compoundModifier` style block. It could be an ObjectLiteralExpression,
161
- * an ArrowFunction, MethodDeclaration, etc.
162
- */
163
- function parseStyleBlock(property, context) {
164
- let styleObj;
165
- if (typescript_1.default.isPropertyAssignment(property)) {
166
- if (typescript_1.default.isObjectLiteralExpression(property.initializer)) {
167
- styleObj = parseObjectToStaticValue_1.parseObjectToStaticValue(property.initializer, context);
168
- }
169
- if (isFunctionLikeDeclaration(property.initializer)) {
170
- const returnNode = getReturnStatement(property.initializer);
171
- if (returnNode) {
172
- styleObj = parseObjectToStaticValue_1.parseObjectToStaticValue(returnNode, context);
173
- }
174
- }
175
- }
176
- if (isFunctionLikeDeclaration(property)) {
177
- const returnNode = getReturnStatement(property);
178
- if (returnNode) {
179
- styleObj = parseObjectToStaticValue_1.parseObjectToStaticValue(returnNode, context);
180
- }
181
- }
182
- return styleObj;
183
- }
184
- function getReturnStatement(node) {
185
- if (node.body && typescript_1.default.isParenthesizedExpression(node.body)) {
186
- return node.body.expression;
187
- }
188
- if (node.body && typescript_1.default.isBlock(node.body)) {
189
- let returnNode;
190
- // look for the return statement. It must be a top-level statement in the block
191
- node.body.statements.forEach(statement => {
192
- // () => { return {} }
193
- if (typescript_1.default.isReturnStatement(statement)) {
194
- returnNode = statement.expression;
195
- }
196
- });
197
- return returnNode;
198
- }
199
- return undefined;
200
- }
201
- function isFunctionLikeDeclaration(node) {
202
- return node.hasOwnProperty('body');
203
- }
@@ -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,110 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.handleCreateStyles = void 0;
7
- const typescript_1 = __importDefault(require("typescript"));
8
- const parseObjectToStaticValue_1 = require("./parseObjectToStaticValue");
9
- const createStyleObjectNode_1 = require("./createStyleObjectNode");
10
- const isImportedFromStyling_1 = require("./isImportedFromStyling");
11
- const handleCreateStyles = (node, context) => {
12
- const { checker } = context;
13
- /**
14
- * Check if the node is a call expression that looks like:
15
- *
16
- * ```ts
17
- * createStyles({
18
- * // properties
19
- * })
20
- * ```
21
- *
22
- * It will also make sure the `createStyles` function was imported from
23
- * `@workday/canvas-kit-styling` to ensure we don't rewrite the AST of code we don't own.
24
- *
25
- * This transformation will pre-serialize the style objects and turn them into strings for
26
- * faster runtime processing in Emotion. The following is an example of the transformation.
27
- *
28
- * ```ts
29
- * // before transformation
30
- * const myStyles = createStyles({
31
- * fontSize: '1rem'
32
- * })
33
- *
34
- * // after transformation
35
- * const myStyles = createStyles({
36
- * name: 'abc123',
37
- * styles: 'font-size: 1rem;'
38
- * })
39
- * ```
40
- *
41
- * The after transformation already serialized the styles and goes through a shortcut process
42
- * in `@emotion/css` where only the Emotion cache is checked and styles are inserted if the
43
- * cache key wasn't found.
44
- */
45
- if (typescript_1.default.isCallExpression(node) &&
46
- typescript_1.default.isIdentifier(node.expression) &&
47
- node.expression.text === 'createStyles' &&
48
- isImportedFromStyling_1.isImportedFromStyling(node.expression, checker) &&
49
- node.arguments.length > 0) {
50
- const newArguments = [...node.arguments].map(arg => {
51
- // An `ObjectLiteralExpression` is an object like `{foo:'bar'}`:
52
- // https://ts-ast-viewer.com/#code/MYewdgzgLgBFCmBbADjAvDA3gKBjAZiCAFwwDkARgIYBOZ2AvkA
53
- if (typescript_1.default.isObjectLiteralExpression(arg)) {
54
- const styleObj = parseObjectToStaticValue_1.parseObjectToStaticValue(arg, context);
55
- return createStyleObjectNode_1.createStyleObjectNode(styleObj);
56
- }
57
- // An Identifier is a variable. It could come from anywhere - imports, earlier
58
- // assignments, etc. The easiest thing to do is to ask the TypeScript type checker what
59
- // the type representation is and go from there.
60
- if (typescript_1.default.isIdentifier(arg)) {
61
- const type = checker.getTypeAtLocation(arg);
62
- // `createStyles` accepts strings as class names. If the class name is
63
- if (type.isStringLiteral() || type.getFlags() & typescript_1.default.TypeFlags.String) {
64
- return arg;
65
- }
66
- // The type must be a object
67
- const styleObj = parseObjectToStaticValue_1.parseStyleObjFromType(type, context);
68
- return createStyleObjectNode_1.createStyleObjectNode(styleObj);
69
- }
70
- return arg;
71
- });
72
- newArguments.forEach(argument => {
73
- // TypeScript isn't expecting us to mutate arguments arguments and when emitting will
74
- // try to do something where it checks the `parent` node of the argument. Using
75
- // `ts.factory.create*`, the `parent` is `undefined` and this check will throw an error.
76
- // In order to get past this error, we manually update the `parent` node of each
77
- // argument to reference the existing call expression. This allows TypeScript to fully
78
- // type check and/or emit.
79
- argument.parent = node;
80
- });
81
- /**
82
- * We're not supposed to mutate arguments since it is supposed to be read-only. But, if I
83
- * return a new callExpression, there is no parent and it is no longer linked to the
84
- * import module. This causes incorrect code when the module export type is `commonjs`.
85
- * For example:
86
- *
87
- * ```ts
88
- * // with new callExpression
89
- * const canvas_kit_styling_1 = require(...)
90
- *
91
- * createStyles({...})
92
- *
93
- * // if we instead mutate arguments
94
- * const canvas_kit_styling_1 = require(...)
95
- *
96
- * canvas_kit_styling_1.createStyles({...})
97
- * ```
98
- *
99
- * My best guess as to why it fails when creating a new callExpression is the node's
100
- * symbol declaration link gets lost. TypeScript then has no idea `createStyles` comes
101
- * from an `ImportDeclaration` declaration node and when emitting `commonjs`, it doesn't
102
- * prefix with the `canvas_kit_styling_1`. This is hacky, but the only thing that works
103
- * correctly.
104
- */
105
- node.arguments = newArguments;
106
- return node;
107
- }
108
- return;
109
- };
110
- exports.handleCreateStyles = handleCreateStyles;
@@ -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,34 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.handleCreateVars = void 0;
7
- const typescript_1 = __importDefault(require("typescript"));
8
- const canvas_kit_styling_1 = require("@workday/canvas-kit-styling");
9
- const getVarName_1 = require("./getVarName");
10
- const makeEmotionSafe_1 = require("./makeEmotionSafe");
11
- const handleCreateVars = (node, { prefix, variables }) => {
12
- /**
13
- * This will create a variable
14
- */
15
- if (typescript_1.default.isCallExpression(node) &&
16
- typescript_1.default.isIdentifier(node.expression) &&
17
- node.expression.text === 'createVars') {
18
- const id = canvas_kit_styling_1.slugify(getVarName_1.getVarName(node)).replace('-vars', '');
19
- const vars = node.arguments
20
- .map(arg => typescript_1.default.isStringLiteral(arg) && arg.text)
21
- .filter(Boolean);
22
- vars.forEach(v => {
23
- variables[`${id}-${makeEmotionSafe_1.makeEmotionSafe(v)}`] = `--${prefix}-${id}-${makeEmotionSafe_1.makeEmotionSafe(v)}`;
24
- });
25
- return typescript_1.default.factory.createCallExpression(node.expression, [], [
26
- typescript_1.default.factory.createObjectLiteralExpression([
27
- typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createIdentifier('id'), typescript_1.default.factory.createStringLiteral(`${prefix}-${id}`)),
28
- typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createIdentifier('args'), typescript_1.default.factory.createArrayLiteralExpression(vars.map(val => typescript_1.default.factory.createStringLiteral(val)), false)),
29
- ], false),
30
- ]);
31
- }
32
- return;
33
- };
34
- exports.handleCreateVars = handleCreateVars;
@@ -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,33 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.handleCssVar = void 0;
7
- const typescript_1 = __importDefault(require("typescript"));
8
- /**
9
- * Converts `cssVar` to a `TemplateExpression` with each argument as a span
10
- *
11
- * - `cssVar('--foo')` => `\`var(${'--foo'})\``
12
- * - `cssVar('--foo', 'fallback')` => `\`var(${'--foo'}, ${'fallback'})\``
13
- *
14
- * The value parser will figure out what to do from there. We don't have access to variables at this
15
- * point, so we forward CallExpression arguments in ways the value parser understands.
16
- */
17
- const handleCssVar = node => {
18
- // cssVar(a)
19
- // cssVar(a, b)
20
- if (typescript_1.default.isCallExpression(node) &&
21
- typescript_1.default.isIdentifier(node.expression) &&
22
- node.expression.text === 'cssVar') {
23
- if (node.arguments.length === 1) {
24
- return node.arguments[0];
25
- }
26
- return typescript_1.default.factory.createTemplateExpression(typescript_1.default.factory.createTemplateHead('var('), [
27
- typescript_1.default.factory.createTemplateSpan(node.arguments[0], typescript_1.default.factory.createTemplateMiddle(', ')),
28
- typescript_1.default.factory.createTemplateSpan(node.arguments[1], typescript_1.default.factory.createTemplateTail(')')),
29
- ]);
30
- }
31
- return;
32
- };
33
- exports.handleCssVar = handleCssVar;
@@ -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"}
@@ -1,83 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.handleFocusRing = void 0;
7
- const typescript_1 = __importDefault(require("typescript"));
8
- const canvas_tokens_web_1 = require("@workday/canvas-tokens-web");
9
- const parseNodeToStaticValue_1 = require("./parseNodeToStaticValue");
10
- const handleFocusRing = (node, context) => {
11
- // { ...focusRing() }
12
- /**
13
- * A spread assignment looks like:
14
- *
15
- * ```ts
16
- * {
17
- * ...styles
18
- * }
19
- * ```
20
- *
21
- * https://ts-ast-viewer.com/#code/MYewdgzgLgBFCmBbADjAvDA3gMxCAXDAOQBGAhgE5EC+AUKJLAigEzpYB0Xzy1QA
22
- */
23
- if (typescript_1.default.isSpreadAssignment(node)) {
24
- // Detect `focusRing` calls. This is temporary until we figure out a better way to do focus
25
- // rings that doesn't require a special entry in the transform function.
26
- //
27
- // TODO: implement a fully working type resolver for CSS variables or remove support for them an
28
- // remove all uses of `focusRing` from new styling code
29
- if (typescript_1.default.isCallExpression(node.expression) &&
30
- typescript_1.default.isIdentifier(node.expression.expression) &&
31
- node.expression.expression.text === 'focusRing') {
32
- const argumentObject = node.expression.arguments[0];
33
- // defaults
34
- const defaults = {
35
- width: typescript_1.default.factory.createStringLiteral('2px'),
36
- separation: typescript_1.default.factory.createStringLiteral('0px'),
37
- inset: typescript_1.default.factory.createIdentifier('undefined'),
38
- innerColor: typescript_1.default.factory.createStringLiteral(canvas_tokens_web_1.base.frenchVanilla100),
39
- outerColor: typescript_1.default.factory.createStringLiteral(canvas_tokens_web_1.brand.common.focusOutline),
40
- };
41
- if (argumentObject && typescript_1.default.isObjectLiteralExpression(argumentObject)) {
42
- argumentObject.properties.forEach(property => {
43
- if (typescript_1.default.isPropertyAssignment(property) && typescript_1.default.isIdentifier(property.name)) {
44
- defaults[property.name.text] = property.initializer;
45
- }
46
- });
47
- }
48
- const inset = parseNodeToStaticValue_1.parseNodeToStaticValue(defaults.inset, context);
49
- let boxShadow;
50
- switch (inset) {
51
- case 'outer':
52
- boxShadow = createTemplateExpression('inset 0 0 0 ', defaults.separation, ' ', defaults.outerColor, ' inset 0 0 0 calc(', defaults.width, ' + ', defaults.separation, ') ', defaults.innerColor);
53
- break;
54
- case 'inner':
55
- boxShadow = createTemplateExpression('inset 0 0 0 ', defaults.separation, ' ', defaults.innerColor, ', 0 0 0 ', defaults.width, ' ', defaults.outerColor);
56
- break;
57
- default:
58
- boxShadow = createTemplateExpression('0 0 0 ', defaults.separation, ' ', defaults.innerColor, ', 0 0 0 calc(', defaults.width, ' + ', defaults.separation, ') ', defaults.outerColor);
59
- break;
60
- }
61
- return typescript_1.default.factory.createSpreadAssignment(typescript_1.default.factory.createObjectLiteralExpression([
62
- typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createIdentifier('boxShadow'), boxShadow),
63
- ], false));
64
- }
65
- }
66
- return;
67
- };
68
- exports.handleFocusRing = handleFocusRing;
69
- function createTemplateExpression(head, ...args) {
70
- const headNode = typescript_1.default.factory.createTemplateHead(head);
71
- const spanNodes = args.reduce((result, value, index) => {
72
- if (typeof value !== 'string') {
73
- const literal = index === args.length - 1
74
- ? typescript_1.default.factory.createTemplateTail('')
75
- : index === args.length - 2
76
- ? typescript_1.default.factory.createTemplateTail(args[index + 1])
77
- : typescript_1.default.factory.createTemplateMiddle(args[index + 1]);
78
- result.push(typescript_1.default.factory.createTemplateSpan(value, literal));
79
- }
80
- return result;
81
- }, []);
82
- return typescript_1.default.factory.createTemplateExpression(headNode, spanNodes);
83
- }
@@ -1,3 +0,0 @@
1
- import { NodeTransformer } from './types';
2
- export declare const handleKeyframes: NodeTransformer;
3
- //# sourceMappingURL=handleKeyframes.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"handleKeyframes.d.ts","sourceRoot":"","sources":["../../../../lib/utils/handleKeyframes.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,eAAe,EAAC,MAAM,SAAS,CAAC;AAOxC,eAAO,MAAM,eAAe,EAAE,eA+C7B,CAAC"}
@@ -1,47 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.handleKeyframes = void 0;
7
- const typescript_1 = __importDefault(require("typescript"));
8
- const parseNodeToStaticValue_1 = require("./parseNodeToStaticValue");
9
- const getVarName_1 = require("./getVarName");
10
- const createStyleObjectNode_1 = require("./createStyleObjectNode");
11
- const isImportedFromStyling_1 = require("./isImportedFromStyling");
12
- const parseObjectToStaticValue_1 = require("./parseObjectToStaticValue");
13
- const handleKeyframes = (node, context) => {
14
- const { checker, keyframes } = context;
15
- // keyframes`css`
16
- if (typescript_1.default.isTaggedTemplateExpression(node) &&
17
- typescript_1.default.isIdentifier(node.tag) &&
18
- node.tag.text === 'keyframes' &&
19
- isImportedFromStyling_1.isImportedFromStyling(node.tag, checker)) {
20
- // parseNodeToStaticValue can parse templates. Pass it through there to get a single static string
21
- const styleObjNode = createStyleObjectNode_1.createStyleObjectNode(parseNodeToStaticValue_1.parseNodeToStaticValue(node.template, context), '{hash}');
22
- const identifierName = getVarName_1.getVarName(node);
23
- const name = createStyleObjectNode_1.getStaticCssClassName(styleObjNode);
24
- keyframes[identifierName] = `animation-${name}`;
25
- return typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier('keyframes'), undefined, [
26
- styleObjNode,
27
- ]);
28
- }
29
- // keyframes({})
30
- if (typescript_1.default.isCallExpression(node) &&
31
- typescript_1.default.isIdentifier(node.expression) &&
32
- node.expression.text === 'keyframes' &&
33
- isImportedFromStyling_1.isImportedFromStyling(node.expression, checker)) {
34
- if (typescript_1.default.isObjectLiteralExpression(node.arguments[0])) {
35
- const styleObj = parseObjectToStaticValue_1.parseObjectToStaticValue(node.arguments[0], context);
36
- const styleObjNode = createStyleObjectNode_1.createStyleObjectNode(styleObj, '{hash}');
37
- const identifierName = getVarName_1.getVarName(node);
38
- const name = createStyleObjectNode_1.getStaticCssClassName(styleObjNode);
39
- keyframes[identifierName] = `animation-${name}`;
40
- return typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier('keyframes'), undefined, [
41
- styleObjNode,
42
- ]);
43
- }
44
- }
45
- return;
46
- };
47
- exports.handleKeyframes = handleKeyframes;
@@ -1,6 +0,0 @@
1
- import { NodeTransformer } from './types';
2
- /**
3
- * Handle the CallExpression `px2rem` to do static conversion and remove the CallExpression.
4
- */
5
- export declare const handlePx2Rem: NodeTransformer;
6
- //# sourceMappingURL=handlePx2Rem.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"handlePx2Rem.d.ts","sourceRoot":"","sources":["../../../../lib/utils/handlePx2Rem.ts"],"names":[],"mappings":"AAGA,OAAO,EAAC,eAAe,EAAC,MAAM,SAAS,CAAC;AAExC;;GAEG;AACH,eAAO,MAAM,YAAY,EAAE,eAkB1B,CAAC"}
@@ -1,26 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.handlePx2Rem = void 0;
7
- const typescript_1 = __importDefault(require("typescript"));
8
- const isImportedFromStyling_1 = require("./isImportedFromStyling");
9
- /**
10
- * Handle the CallExpression `px2rem` to do static conversion and remove the CallExpression.
11
- */
12
- const handlePx2Rem = (node, context) => {
13
- if (typescript_1.default.isCallExpression(node) &&
14
- typescript_1.default.isIdentifier(node.expression) &&
15
- node.expression.text === 'px2rem' &&
16
- isImportedFromStyling_1.isImportedFromStyling(node.expression, context.checker)) {
17
- const [pxArgument, baseArgument] = node.arguments;
18
- const base = baseArgument && typescript_1.default.isNumericLiteral(baseArgument) ? parseFloat(baseArgument.text) : 16;
19
- if (typescript_1.default.isNumericLiteral(pxArgument)) {
20
- const px = parseFloat(pxArgument.text);
21
- return typescript_1.default.factory.createStringLiteral(`${px / base}rem`);
22
- }
23
- }
24
- return;
25
- };
26
- exports.handlePx2Rem = handlePx2Rem;
@@ -1,7 +0,0 @@
1
- import ts from 'typescript';
2
- /**
3
- * Checks if the node was imported from '@workday/canvas-kit-styling'. This is useful if you want to
4
- * limit transformation to only styling imports.
5
- */
6
- export declare function isImportedFromStyling(node: ts.Node, checker: ts.TypeChecker): boolean;
7
- //# sourceMappingURL=isImportedFromStyling.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"isImportedFromStyling.d.ts","sourceRoot":"","sources":["../../../../lib/utils/isImportedFromStyling.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAE5B;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,CAAC,WAAW,WAc3E"}
@@ -1,23 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.isImportedFromStyling = void 0;
7
- const typescript_1 = __importDefault(require("typescript"));
8
- /**
9
- * Checks if the node was imported from '@workday/canvas-kit-styling'. This is useful if you want to
10
- * limit transformation to only styling imports.
11
- */
12
- function isImportedFromStyling(node, checker) {
13
- const symbol = checker.getSymbolAtLocation(node);
14
- const declaration = (symbol === null || symbol === void 0 ? void 0 : symbol.valueDeclaration) || (symbol === null || symbol === void 0 ? void 0 : symbol.declarations[0]);
15
- if (declaration &&
16
- typescript_1.default.isImportSpecifier(declaration) &&
17
- typescript_1.default.isStringLiteral(declaration.parent.parent.parent.moduleSpecifier) &&
18
- declaration.parent.parent.parent.moduleSpecifier.text === '@workday/canvas-kit-styling') {
19
- return true;
20
- }
21
- return false;
22
- }
23
- exports.isImportedFromStyling = isImportedFromStyling;