@workday/canvas-kit-styling-transform 10.3.0-649-next.0 → 10.3.0-655-next.0

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 (53) hide show
  1. package/dist/commonjs/lib/styleTransform.d.ts +5 -4
  2. package/dist/commonjs/lib/styleTransform.d.ts.map +1 -1
  3. package/dist/commonjs/lib/styleTransform.js +17 -6
  4. package/dist/commonjs/lib/utils/createStyleObjectNode.d.ts +9 -1
  5. package/dist/commonjs/lib/utils/createStyleObjectNode.d.ts.map +1 -1
  6. package/dist/commonjs/lib/utils/createStyleObjectNode.js +19 -3
  7. package/dist/commonjs/lib/utils/handleCalc.js +2 -2
  8. package/dist/commonjs/lib/utils/handleCreateStencil.d.ts.map +1 -1
  9. package/dist/commonjs/lib/utils/handleCreateStencil.js +10 -9
  10. package/dist/commonjs/lib/utils/handleCreateStyles.d.ts.map +1 -1
  11. package/dist/commonjs/lib/utils/handleCreateStyles.js +4 -3
  12. package/dist/commonjs/lib/utils/handleCreateVars.js +5 -5
  13. package/dist/commonjs/lib/utils/handleCssVar.js +1 -1
  14. package/dist/commonjs/lib/utils/handleFocusRing.js +2 -2
  15. package/dist/commonjs/lib/utils/handleKeyframes.d.ts +3 -0
  16. package/dist/commonjs/lib/utils/handleKeyframes.d.ts.map +1 -0
  17. package/dist/commonjs/lib/utils/handleKeyframes.js +47 -0
  18. package/dist/commonjs/lib/utils/handlePx2Rem.js +2 -2
  19. package/dist/commonjs/lib/utils/parseNodeToStaticValue.d.ts +2 -1
  20. package/dist/commonjs/lib/utils/parseNodeToStaticValue.d.ts.map +1 -1
  21. package/dist/commonjs/lib/utils/parseNodeToStaticValue.js +24 -10
  22. package/dist/commonjs/lib/utils/parseObjectToStaticValue.d.ts +3 -2
  23. package/dist/commonjs/lib/utils/parseObjectToStaticValue.d.ts.map +1 -1
  24. package/dist/commonjs/lib/utils/parseObjectToStaticValue.js +18 -17
  25. package/dist/commonjs/lib/utils/types.d.ts +7 -1
  26. package/dist/commonjs/lib/utils/types.d.ts.map +1 -1
  27. package/dist/es6/lib/styleTransform.d.ts +5 -4
  28. package/dist/es6/lib/styleTransform.d.ts.map +1 -1
  29. package/dist/es6/lib/styleTransform.js +17 -6
  30. package/dist/es6/lib/utils/createStyleObjectNode.d.ts +9 -1
  31. package/dist/es6/lib/utils/createStyleObjectNode.d.ts.map +1 -1
  32. package/dist/es6/lib/utils/createStyleObjectNode.js +17 -2
  33. package/dist/es6/lib/utils/handleCalc.js +2 -2
  34. package/dist/es6/lib/utils/handleCreateStencil.d.ts.map +1 -1
  35. package/dist/es6/lib/utils/handleCreateStencil.js +10 -9
  36. package/dist/es6/lib/utils/handleCreateStyles.d.ts.map +1 -1
  37. package/dist/es6/lib/utils/handleCreateStyles.js +4 -3
  38. package/dist/es6/lib/utils/handleCreateVars.js +5 -5
  39. package/dist/es6/lib/utils/handleCssVar.js +1 -1
  40. package/dist/es6/lib/utils/handleFocusRing.js +2 -2
  41. package/dist/es6/lib/utils/handleKeyframes.d.ts +3 -0
  42. package/dist/es6/lib/utils/handleKeyframes.d.ts.map +1 -0
  43. package/dist/es6/lib/utils/handleKeyframes.js +40 -0
  44. package/dist/es6/lib/utils/handlePx2Rem.js +2 -2
  45. package/dist/es6/lib/utils/parseNodeToStaticValue.d.ts +2 -1
  46. package/dist/es6/lib/utils/parseNodeToStaticValue.d.ts.map +1 -1
  47. package/dist/es6/lib/utils/parseNodeToStaticValue.js +24 -10
  48. package/dist/es6/lib/utils/parseObjectToStaticValue.d.ts +3 -2
  49. package/dist/es6/lib/utils/parseObjectToStaticValue.d.ts.map +1 -1
  50. package/dist/es6/lib/utils/parseObjectToStaticValue.js +18 -17
  51. package/dist/es6/lib/utils/types.d.ts +7 -1
  52. package/dist/es6/lib/utils/types.d.ts.map +1 -1
  53. package/package.json +3 -3
@@ -1,11 +1,12 @@
1
1
  import ts from 'typescript';
2
- import { NodeTransformer } from './utils/types';
2
+ import { NodeTransformer, TransformerContext } from './utils/types';
3
3
  export declare type NestedStyleObject = {
4
4
  [key: string]: string | NestedStyleObject;
5
5
  };
6
6
  export interface StyleTransformerOptions {
7
7
  prefix: string;
8
- variables: Record<string, string>;
8
+ variables: TransformerContext['variables'];
9
+ keyframes: TransformerContext['keyframes'];
9
10
  fallbackFiles?: string[];
10
11
  transformers?: NodeTransformer[];
11
12
  }
@@ -13,10 +14,10 @@ export interface StyleTransformerOptions {
13
14
  * The reset is used in tests and should not be called normally.
14
15
  */
15
16
  export declare function _reset(): void;
16
- export default function styleTransformer(program: ts.Program, { prefix, variables, fallbackFiles, transformers, }?: Partial<StyleTransformerOptions>): ts.TransformerFactory<ts.SourceFile>;
17
+ export default function styleTransformer(program: ts.Program, { variables, fallbackFiles, transformers, ...transformContext }?: Partial<StyleTransformerOptions>): ts.TransformerFactory<ts.SourceFile>;
17
18
  /**
18
19
  * This function is useful for tests or a custom build. The `styleTransformer` function is used by
19
20
  * the https://www.npmjs.com/package/ttypescript package.
20
21
  */
21
- export declare function transform(program: ts.Program, fileName: string, options?: Partial<StyleTransformerOptions>, transformers?: NodeTransformer[]): string;
22
+ export declare function transform(program: ts.Program, fileName: string, options?: Partial<StyleTransformerOptions>): string;
22
23
  //# sourceMappingURL=styleTransform.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"styleTransform.d.ts","sourceRoot":"","sources":["../../../lib/styleTransform.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,YAAY,CAAC;AAW5B,OAAO,EAAC,eAAe,EAAC,MAAM,eAAe,CAAC;AAE9C,oBAAY,iBAAiB,GAAG;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,iBAAiB,CAAA;CAAC,CAAC;AAE5E,MAAM,WAAW,uBAAuB;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAClC,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,YAAY,CAAC,EAAE,eAAe,EAAE,CAAC;CAClC;AAKD;;GAEG;AACH,wBAAgB,MAAM,SAGrB;AAeD,MAAM,CAAC,OAAO,UAAU,gBAAgB,CACtC,OAAO,EAAE,EAAE,CAAC,OAAO,EACnB,EACE,MAAc,EACd,SAAc,EACd,aAAkB,EAClB,YAAkC,GACnC,GAAE,OAAO,CAAC,uBAAuB,CAIjC,GACA,EAAE,CAAC,kBAAkB,CAAC,EAAE,CAAC,UAAU,CAAC,CAgCtC;AAED;;;GAGG;AACH,wBAAgB,SAAS,CACvB,OAAO,EAAE,EAAE,CAAC,OAAO,EACnB,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,OAAO,CAAC,uBAAuB,CAAC,EAC1C,YAAY,CAAC,EAAE,eAAe,EAAE,UAYjC"}
1
+ {"version":3,"file":"styleTransform.d.ts","sourceRoot":"","sources":["../../../lib/styleTransform.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,YAAY,CAAC;AAW5B,OAAO,EAAC,eAAe,EAAE,kBAAkB,EAAC,MAAM,eAAe,CAAC;AAGlE,oBAAY,iBAAiB,GAAG;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,iBAAiB,CAAA;CAAC,CAAC;AAE5E,MAAM,WAAW,uBAAuB;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,kBAAkB,CAAC,WAAW,CAAC,CAAC;IAC3C,SAAS,EAAE,kBAAkB,CAAC,WAAW,CAAC,CAAC;IAC3C,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,YAAY,CAAC,EAAE,eAAe,EAAE,CAAC;CAClC;AAMD;;GAEG;AACH,wBAAgB,MAAM,SAIrB;AAgBD,MAAM,CAAC,OAAO,UAAU,gBAAgB,CACtC,OAAO,EAAE,EAAE,CAAC,OAAO,EACnB,EACE,SAAc,EACd,aAAkB,EAClB,YAAkC,EAClC,GAAG,gBAAgB,EACpB,GAAE,OAAO,CAAC,uBAAuB,CAKjC,GACA,EAAE,CAAC,kBAAkB,CAAC,EAAE,CAAC,UAAU,CAAC,CAsCtC;AAED;;;GAGG;AACH,wBAAgB,SAAS,CACvB,OAAO,EAAE,EAAE,CAAC,OAAO,EACnB,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,OAAO,CAAC,uBAAuB,CAAC,UAY3C"}
@@ -15,13 +15,16 @@ const handleCalc_1 = require("./utils/handleCalc");
15
15
  const handlePx2Rem_1 = require("./utils/handlePx2Rem");
16
16
  const handleFocusRing_1 = require("./utils/handleFocusRing");
17
17
  const handleCssVar_1 = require("./utils/handleCssVar");
18
+ const handleKeyframes_1 = require("./utils/handleKeyframes");
18
19
  let vars = {};
20
+ let keyframes = {};
19
21
  let loadedFallbacks = false;
20
22
  /**
21
23
  * The reset is used in tests and should not be called normally.
22
24
  */
23
25
  function _reset() {
24
26
  vars = {};
27
+ keyframes = {};
25
28
  loadedFallbacks = false;
26
29
  }
27
30
  exports._reset = _reset;
@@ -33,13 +36,15 @@ const defaultTransformers = [
33
36
  handleFocusRing_1.handleFocusRing,
34
37
  handleCalc_1.handleCalc,
35
38
  handlePx2Rem_1.handlePx2Rem,
39
+ handleKeyframes_1.handleKeyframes,
36
40
  handleCreateVars_1.handleCreateVars,
37
41
  handleCreateStyles_1.handleCreateStyles,
38
42
  handleCreateStencil_1.handleCreateStencil,
39
43
  ];
40
- function styleTransformer(program, { prefix = 'css', variables = {}, fallbackFiles = [], transformers = defaultTransformers, } = {
44
+ function styleTransformer(program, { variables = {}, fallbackFiles = [], transformers = defaultTransformers, ...transformContext } = {
41
45
  prefix: 'css',
42
46
  variables: {},
47
+ keyframes: {},
43
48
  transformers: defaultTransformers,
44
49
  }) {
45
50
  if (!loadedFallbacks) {
@@ -64,7 +69,13 @@ function styleTransformer(program, { prefix = 'css', variables = {}, fallbackFil
64
69
  const visit = node => {
65
70
  // eslint-disable-next-line no-param-reassign
66
71
  node = typescript_1.default.visitEachChild(node, visit, context);
67
- return handleTransformers(node, checker, prefix, vars)(transformers);
72
+ return handleTransformers(node, {
73
+ checker,
74
+ prefix: 'css',
75
+ variables: vars,
76
+ keyframes,
77
+ ...transformContext,
78
+ })(transformers);
68
79
  };
69
80
  return node => typescript_1.default.visitNode(node, visit);
70
81
  };
@@ -74,19 +85,19 @@ exports.default = styleTransformer;
74
85
  * This function is useful for tests or a custom build. The `styleTransformer` function is used by
75
86
  * the https://www.npmjs.com/package/ttypescript package.
76
87
  */
77
- function transform(program, fileName, options, transformers) {
88
+ function transform(program, fileName, options) {
78
89
  const source = program.getSourceFile(fileName) || typescript_1.default.createSourceFile(fileName, '', typescript_1.default.ScriptTarget.ES2019);
79
90
  const printer = typescript_1.default.createPrinter();
80
91
  return printer.printFile(typescript_1.default
81
- .transform(source, [styleTransformer(program, { ...options, transformers })])
92
+ .transform(source, [styleTransformer(program, options)])
82
93
  .transformed.find(s => s.fileName === fileName) || source);
83
94
  }
84
95
  exports.transform = transform;
85
- const handleTransformers = (node, checker, prefix, variables) => (transformers) => {
96
+ const handleTransformers = (node, context) => (transformers) => {
86
97
  return (transformers.reduce((result, transformer) => {
87
98
  if (result) {
88
99
  return result;
89
100
  }
90
- return transformer(node, checker, prefix, variables);
101
+ return transformer(node, context);
91
102
  }, undefined) || node);
92
103
  };
@@ -5,6 +5,14 @@ import { NestedStyleObject } from './parseObjectToStaticValue';
5
5
  * string, styles: serializedStyles}`. The `name` is hard-coded here to work with both server-side
6
6
  * and client-side style injection. This results in a stable style key for Emotion while also
7
7
  * optimizing style serialization.
8
+ *
9
+ * If `name` is provided, the name will be whatever `name` is, replacing "{hash}" with the hash
10
+ * created via `serializeStyles`. For example: 'animation-{hash}' will be converted into
11
+ * 'animation-abc123'
8
12
  */
9
- export declare function createStyleObjectNode(styleObj: NestedStyleObject): ts.ObjectLiteralExpression;
13
+ export declare function createStyleObjectNode(styleObj: NestedStyleObject | string, name?: string): ts.ObjectLiteralExpression;
14
+ /**
15
+ * Returns the name created by `createStyleObjectNode`
16
+ */
17
+ export declare function getStaticCssClassName(node: ts.Node): string;
10
18
  //# sourceMappingURL=createStyleObjectNode.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createStyleObjectNode.d.ts","sourceRoot":"","sources":["../../../../lib/utils/createStyleObjectNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAK5B,OAAO,EAAC,iBAAiB,EAAC,MAAM,4BAA4B,CAAC;AAE7D;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,iBAAiB,8BAqBhE"}
1
+ {"version":3,"file":"createStyleObjectNode.d.ts","sourceRoot":"","sources":["../../../../lib/utils/createStyleObjectNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAK5B,OAAO,EAAC,iBAAiB,EAAC,MAAM,4BAA4B,CAAC;AAE7D;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,iBAAiB,GAAG,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,8BAuBxF;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,GAAG,MAAM,CAS3D"}
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createStyleObjectNode = void 0;
6
+ exports.getStaticCssClassName = exports.createStyleObjectNode = void 0;
7
7
  const typescript_1 = __importDefault(require("typescript"));
8
8
  const serialize_1 = require("@emotion/serialize");
9
9
  const canvas_kit_styling_1 = require("@workday/canvas-kit-styling");
@@ -12,8 +12,12 @@ const canvas_kit_styling_1 = require("@workday/canvas-kit-styling");
12
12
  * string, styles: serializedStyles}`. The `name` is hard-coded here to work with both server-side
13
13
  * and client-side style injection. This results in a stable style key for Emotion while also
14
14
  * optimizing style serialization.
15
+ *
16
+ * If `name` is provided, the name will be whatever `name` is, replacing "{hash}" with the hash
17
+ * created via `serializeStyles`. For example: 'animation-{hash}' will be converted into
18
+ * 'animation-abc123'
15
19
  */
16
- function createStyleObjectNode(styleObj) {
20
+ function createStyleObjectNode(styleObj, name) {
17
21
  const serialized = serialize_1.serializeStyles([styleObj]);
18
22
  const styleText = serialized.styles;
19
23
  const styleExpression = typescript_1.default.factory.createStringLiteral(styleText);
@@ -22,10 +26,22 @@ function createStyleObjectNode(styleObj) {
22
26
  return typescript_1.default.factory.createObjectLiteralExpression([
23
27
  typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createIdentifier('name'),
24
28
  // TODO - we may need this to be a static variable for the CSS package
25
- typescript_1.default.factory.createStringLiteral(canvas_kit_styling_1.generateUniqueId()) // We might be using values that are resolved at runtime, but should still be static. We're only supporting the `cs` function running once per file, so a stable id based on a hash is not necessary
29
+ typescript_1.default.factory.createStringLiteral(name ? name.replace('{hash}', serialized.name) : canvas_kit_styling_1.generateUniqueId()) // We might be using values that are resolved at runtime, but should still be static. We're only supporting the `cs` function running once per file, so a stable id based on a hash is not necessary
26
30
  ),
27
31
  typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createIdentifier('styles'), styleExpression // In the future we can extract CSS from here by running the `stylis` compiler directly. Emotion does this here: https://github.com/emotion-js/emotion/blob/f3b268f7c52103979402da919c9c0dd3f9e0e189/packages/cache/src/index.js#L188-L245
28
32
  ),
29
33
  ], false);
30
34
  }
31
35
  exports.createStyleObjectNode = createStyleObjectNode;
36
+ /**
37
+ * Returns the name created by `createStyleObjectNode`
38
+ */
39
+ function getStaticCssClassName(node) {
40
+ if (typescript_1.default.isObjectLiteralExpression(node) &&
41
+ typescript_1.default.isPropertyAssignment(node.properties[0]) &&
42
+ typescript_1.default.isStringLiteral(node.properties[0].initializer)) {
43
+ return node.properties[0].initializer.text;
44
+ }
45
+ return '';
46
+ }
47
+ exports.getStaticCssClassName = getStaticCssClassName;
@@ -22,13 +22,13 @@ const isImportedFromStyling_1 = require("./isImportedFromStyling");
22
22
  * etc. The transform can handle template string literals with different spans, so we'll convert to
23
23
  * those as an intermediate step.
24
24
  */
25
- const handleCalc = (node, checker) => {
25
+ const handleCalc = (node, context) => {
26
26
  if (typescript_1.default.isCallExpression(node) &&
27
27
  typescript_1.default.isPropertyAccessExpression(node.expression) &&
28
28
  typescript_1.default.isIdentifier(node.expression.expression) &&
29
29
  node.expression.expression.text === 'calc' &&
30
30
  typescript_1.default.isIdentifier(node.expression.name) &&
31
- isImportedFromStyling_1.isImportedFromStyling(node.expression.expression, checker)) {
31
+ isImportedFromStyling_1.isImportedFromStyling(node.expression.expression, context.checker)) {
32
32
  if (node.expression.name.text === 'add') {
33
33
  return replaceWithTemplateString(node.arguments[0], node.arguments[1], ' + ');
34
34
  }
@@ -1 +1 @@
1
- {"version":3,"file":"handleCreateStencil.d.ts","sourceRoot":"","sources":["../../../../lib/utils/handleCreateStencil.ts"],"names":[],"mappings":"AASA,OAAO,EAAC,eAAe,EAAC,MAAM,SAAS,CAAC;AAGxC;;GAEG;AACH,eAAO,MAAM,mBAAmB,EAAE,eA8LjC,CAAC"}
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"}
@@ -15,7 +15,8 @@ const isImportedFromStyling_1 = require("./isImportedFromStyling");
15
15
  /**
16
16
  * Handle all arguments of the CallExpression `createStencil()`
17
17
  */
18
- const handleCreateStencil = (node, checker, prefix = 'css', variables = {}) => {
18
+ const handleCreateStencil = (node, context) => {
19
+ const { checker, prefix, variables } = context;
19
20
  /**
20
21
  * This will match whenever a `createStencil()` call expression is encountered. It will loop
21
22
  * over all the config to extract variables and styles.
@@ -66,7 +67,7 @@ const handleCreateStencil = (node, checker, prefix = 'css', variables = {}) => {
66
67
  variables[makeEmotionSafe_1.makeEmotionSafe(node.name.text)] = varValue;
67
68
  tempVariables[makeEmotionSafe_1.makeEmotionSafe(node.name.text)] = varValue;
68
69
  // Evaluate the variable defaults
69
- stencilVariables[varValue] = parseNodeToStaticValue_1.parseNodeToStaticValue(node.initializer, checker, prefix, variables);
70
+ stencilVariables[varValue] = parseNodeToStaticValue_1.parseNodeToStaticValue(node.initializer, context);
70
71
  }
71
72
  }
72
73
  if (varsConfig && typescript_1.default.isObjectLiteralExpression(varsConfig.initializer)) {
@@ -78,7 +79,7 @@ const handleCreateStencil = (node, checker, prefix = 'css', variables = {}) => {
78
79
  if (property.name && typescript_1.default.isIdentifier(property.name)) {
79
80
  // base config object
80
81
  if (property.name.text === 'base') {
81
- const styleObj = parseStyleBlock(property, checker, prefix, variables);
82
+ const styleObj = parseStyleBlock(property, context);
82
83
  if (styleObj) {
83
84
  // The `as any` are necessary because the properties are readonly, even though they
84
85
  // can be changed in transformers.
@@ -101,7 +102,7 @@ const handleCreateStencil = (node, checker, prefix = 'css', variables = {}) => {
101
102
  typescript_1.default.isPropertyAssignment(modifierProperty) &&
102
103
  typescript_1.default.isObjectLiteralExpression(modifierProperty.initializer)) {
103
104
  modifierProperty.initializer.properties.forEach((modifier, index, modifiers) => {
104
- const styleObj = parseStyleBlock(modifier, checker, prefix, variables);
105
+ const styleObj = parseStyleBlock(modifier, context);
105
106
  if (styleObj && modifier.name) {
106
107
  // The `as any` are necessary because the properties are readonly, even though they
107
108
  // can be changed in transformers.
@@ -125,7 +126,7 @@ const handleCreateStencil = (node, checker, prefix = 'css', variables = {}) => {
125
126
  if (compoundProperty.name &&
126
127
  typescript_1.default.isIdentifier(compoundProperty.name) &&
127
128
  compoundProperty.name.text === 'styles') {
128
- const styleObj = parseStyleBlock(compoundProperty, checker, prefix, variables);
129
+ const styleObj = parseStyleBlock(compoundProperty, context);
129
130
  if (styleObj) {
130
131
  // The `as any` are necessary because the properties are readonly, even though they
131
132
  // can be changed in transformers.
@@ -159,23 +160,23 @@ exports.handleCreateStencil = handleCreateStencil;
159
160
  * A style block is a `base`, `modifier`, or `compoundModifier` style block. It could be an ObjectLiteralExpression,
160
161
  * an ArrowFunction, MethodDeclaration, etc.
161
162
  */
162
- function parseStyleBlock(property, checker, prefix, variables) {
163
+ function parseStyleBlock(property, context) {
163
164
  let styleObj;
164
165
  if (typescript_1.default.isPropertyAssignment(property)) {
165
166
  if (typescript_1.default.isObjectLiteralExpression(property.initializer)) {
166
- styleObj = parseObjectToStaticValue_1.parseObjectToStaticValue(property.initializer, checker, prefix, variables);
167
+ styleObj = parseObjectToStaticValue_1.parseObjectToStaticValue(property.initializer, context);
167
168
  }
168
169
  if (isFunctionLikeDeclaration(property.initializer)) {
169
170
  const returnNode = getReturnStatement(property.initializer);
170
171
  if (returnNode) {
171
- styleObj = parseObjectToStaticValue_1.parseObjectToStaticValue(returnNode, checker, prefix, variables);
172
+ styleObj = parseObjectToStaticValue_1.parseObjectToStaticValue(returnNode, context);
172
173
  }
173
174
  }
174
175
  }
175
176
  if (isFunctionLikeDeclaration(property)) {
176
177
  const returnNode = getReturnStatement(property);
177
178
  if (returnNode) {
178
- styleObj = parseObjectToStaticValue_1.parseObjectToStaticValue(returnNode, checker, prefix, variables);
179
+ styleObj = parseObjectToStaticValue_1.parseObjectToStaticValue(returnNode, context);
179
180
  }
180
181
  }
181
182
  return styleObj;
@@ -1 +1 @@
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,eA2GhC,CAAC"}
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"}
@@ -8,7 +8,8 @@ const typescript_1 = __importDefault(require("typescript"));
8
8
  const parseObjectToStaticValue_1 = require("./parseObjectToStaticValue");
9
9
  const createStyleObjectNode_1 = require("./createStyleObjectNode");
10
10
  const isImportedFromStyling_1 = require("./isImportedFromStyling");
11
- const handleCreateStyles = (node, checker, prefix, variables) => {
11
+ const handleCreateStyles = (node, context) => {
12
+ const { checker } = context;
12
13
  /**
13
14
  * Check if the node is a call expression that looks like:
14
15
  *
@@ -50,7 +51,7 @@ const handleCreateStyles = (node, checker, prefix, variables) => {
50
51
  // An `ObjectLiteralExpression` is an object like `{foo:'bar'}`:
51
52
  // https://ts-ast-viewer.com/#code/MYewdgzgLgBFCmBbADjAvDA3gKBjAZiCAFwwDkARgIYBOZ2AvkA
52
53
  if (typescript_1.default.isObjectLiteralExpression(arg)) {
53
- const styleObj = parseObjectToStaticValue_1.parseObjectToStaticValue(arg, checker, prefix, variables);
54
+ const styleObj = parseObjectToStaticValue_1.parseObjectToStaticValue(arg, context);
54
55
  return createStyleObjectNode_1.createStyleObjectNode(styleObj);
55
56
  }
56
57
  // An Identifier is a variable. It could come from anywhere - imports, earlier
@@ -63,7 +64,7 @@ const handleCreateStyles = (node, checker, prefix, variables) => {
63
64
  return arg;
64
65
  }
65
66
  // The type must be a object
66
- const styleObj = parseObjectToStaticValue_1.parseStyleObjFromType(type, checker, prefix, variables);
67
+ const styleObj = parseObjectToStaticValue_1.parseStyleObjFromType(type, context);
67
68
  return createStyleObjectNode_1.createStyleObjectNode(styleObj);
68
69
  }
69
70
  return arg;
@@ -8,7 +8,7 @@ const typescript_1 = __importDefault(require("typescript"));
8
8
  const canvas_kit_styling_1 = require("@workday/canvas-kit-styling");
9
9
  const getVarName_1 = require("./getVarName");
10
10
  const makeEmotionSafe_1 = require("./makeEmotionSafe");
11
- const handleCreateVars = (node, _checker, prefix, vars) => {
11
+ const handleCreateVars = (node, { prefix, variables }) => {
12
12
  /**
13
13
  * This will create a variable
14
14
  */
@@ -16,16 +16,16 @@ const handleCreateVars = (node, _checker, prefix, vars) => {
16
16
  typescript_1.default.isIdentifier(node.expression) &&
17
17
  node.expression.text === 'createVars') {
18
18
  const id = canvas_kit_styling_1.slugify(getVarName_1.getVarName(node)).replace('-vars', '');
19
- const variables = node.arguments
19
+ const vars = node.arguments
20
20
  .map(arg => typescript_1.default.isStringLiteral(arg) && arg.text)
21
21
  .filter(Boolean);
22
- variables.forEach(v => {
23
- vars[`${id}-${makeEmotionSafe_1.makeEmotionSafe(v)}`] = `--${prefix}-${id}-${makeEmotionSafe_1.makeEmotionSafe(v)}`;
22
+ vars.forEach(v => {
23
+ variables[`${id}-${makeEmotionSafe_1.makeEmotionSafe(v)}`] = `--${prefix}-${id}-${makeEmotionSafe_1.makeEmotionSafe(v)}`;
24
24
  });
25
25
  return typescript_1.default.factory.createCallExpression(node.expression, [], [
26
26
  typescript_1.default.factory.createObjectLiteralExpression([
27
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(variables.map(val => typescript_1.default.factory.createStringLiteral(val)), false)),
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
29
  ], false),
30
30
  ]);
31
31
  }
@@ -14,7 +14,7 @@ const typescript_1 = __importDefault(require("typescript"));
14
14
  * The value parser will figure out what to do from there. We don't have access to variables at this
15
15
  * point, so we forward CallExpression arguments in ways the value parser understands.
16
16
  */
17
- const handleCssVar = (node, checker, prefix, vars) => {
17
+ const handleCssVar = node => {
18
18
  // cssVar(a)
19
19
  // cssVar(a, b)
20
20
  if (typescript_1.default.isCallExpression(node) &&
@@ -7,7 +7,7 @@ exports.handleFocusRing = void 0;
7
7
  const typescript_1 = __importDefault(require("typescript"));
8
8
  const canvas_tokens_web_1 = require("@workday/canvas-tokens-web");
9
9
  const parseNodeToStaticValue_1 = require("./parseNodeToStaticValue");
10
- const handleFocusRing = (node, checker, prefix, vars) => {
10
+ const handleFocusRing = (node, context) => {
11
11
  // { ...focusRing() }
12
12
  /**
13
13
  * A spread assignment looks like:
@@ -45,7 +45,7 @@ const handleFocusRing = (node, checker, prefix, vars) => {
45
45
  }
46
46
  });
47
47
  }
48
- const inset = parseNodeToStaticValue_1.parseNodeToStaticValue(defaults.inset, checker, prefix, vars);
48
+ const inset = parseNodeToStaticValue_1.parseNodeToStaticValue(defaults.inset, context);
49
49
  let boxShadow;
50
50
  switch (inset) {
51
51
  case 'outer':
@@ -0,0 +1,3 @@
1
+ import { NodeTransformer } from './types';
2
+ export declare const handleKeyframes: NodeTransformer;
3
+ //# sourceMappingURL=handleKeyframes.d.ts.map
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,47 @@
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;
@@ -9,11 +9,11 @@ const isImportedFromStyling_1 = require("./isImportedFromStyling");
9
9
  /**
10
10
  * Handle the CallExpression `px2rem` to do static conversion and remove the CallExpression.
11
11
  */
12
- const handlePx2Rem = (node, checker) => {
12
+ const handlePx2Rem = (node, context) => {
13
13
  if (typescript_1.default.isCallExpression(node) &&
14
14
  typescript_1.default.isIdentifier(node.expression) &&
15
15
  node.expression.text === 'px2rem' &&
16
- isImportedFromStyling_1.isImportedFromStyling(node.expression, checker)) {
16
+ isImportedFromStyling_1.isImportedFromStyling(node.expression, context.checker)) {
17
17
  const [pxArgument, baseArgument] = node.arguments;
18
18
  const base = baseArgument && typescript_1.default.isNumericLiteral(baseArgument) ? parseFloat(baseArgument.text) : 16;
19
19
  if (typescript_1.default.isNumericLiteral(pxArgument)) {
@@ -1,6 +1,7 @@
1
1
  import ts from 'typescript';
2
+ import { TransformerContext } from './types';
2
3
  /**
3
4
  * This is the workhorse of statically analyzing style values
4
5
  */
5
- export declare function parseNodeToStaticValue(node: ts.Node, checker: ts.TypeChecker, prefix?: string, variables?: Record<string, string>): string;
6
+ export declare function parseNodeToStaticValue(node: ts.Node, context: TransformerContext): string;
6
7
  //# sourceMappingURL=parseNodeToStaticValue.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"parseNodeToStaticValue.d.ts","sourceRoot":"","sources":["../../../../lib/utils/parseNodeToStaticValue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAO5B;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,EAAE,CAAC,IAAI,EACb,OAAO,EAAE,EAAE,CAAC,WAAW,EACvB,MAAM,GAAE,MAAc,EACtB,SAAS,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM,GACrC,MAAM,CAsFR"}
1
+ {"version":3,"file":"parseNodeToStaticValue.d.ts","sourceRoot":"","sources":["../../../../lib/utils/parseNodeToStaticValue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAM5B,OAAO,EAAC,kBAAkB,EAAC,MAAM,SAAS,CAAC;AAE3C;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,kBAAkB,GAAG,MAAM,CAuGzF"}
@@ -11,7 +11,8 @@ const getErrorMessage_1 = require("./getErrorMessage");
11
11
  /**
12
12
  * This is the workhorse of statically analyzing style values
13
13
  */
14
- function parseNodeToStaticValue(node, checker, prefix = 'css', variables = {}) {
14
+ function parseNodeToStaticValue(node, context) {
15
+ const { checker, variables, keyframes } = context;
15
16
  /**
16
17
  * String literals like 'red' or empty Template Expressions like `red`
17
18
  */
@@ -32,6 +33,9 @@ function parseNodeToStaticValue(node, checker, prefix = 'css', variables = {}) {
32
33
  if (variables[varName]) {
33
34
  return variables[varName];
34
35
  }
36
+ if (keyframes[varName]) {
37
+ return keyframes[varName];
38
+ }
35
39
  }
36
40
  // [a.b]
37
41
  if (typescript_1.default.isComputedPropertyName(node) && typescript_1.default.isPropertyAccessExpression(node.expression)) {
@@ -39,6 +43,9 @@ function parseNodeToStaticValue(node, checker, prefix = 'css', variables = {}) {
39
43
  if (variables[varName]) {
40
44
  return variables[varName];
41
45
  }
46
+ if (keyframes[varName]) {
47
+ return keyframes[varName];
48
+ }
42
49
  }
43
50
  /**
44
51
  * ```ts
@@ -46,7 +53,7 @@ function parseNodeToStaticValue(node, checker, prefix = 'css', variables = {}) {
46
53
  * ```
47
54
  */
48
55
  if (typescript_1.default.isTemplateExpression(node)) {
49
- return getStyleValueFromTemplateExpression(node, checker, prefix, variables);
56
+ return getStyleValueFromTemplateExpression(node, context);
50
57
  }
51
58
  /**
52
59
  * An Identifier is a simple variable. It may represent a variable, so we'll check it before
@@ -56,6 +63,15 @@ function parseNodeToStaticValue(node, checker, prefix = 'css', variables = {}) {
56
63
  if (variables[node.text]) {
57
64
  return variables[node.text];
58
65
  }
66
+ if (keyframes[node.text]) {
67
+ return keyframes[node.text];
68
+ }
69
+ }
70
+ if (typescript_1.default.isElementAccessExpression(node)) {
71
+ const value = parseTypeToStaticValue(checker.getTypeAtLocation(node));
72
+ if (value) {
73
+ return value;
74
+ }
59
75
  }
60
76
  // If we got here, we cannot statically analyze by the AST alone. We have to check the type of the
61
77
  // correct AST Node
@@ -74,7 +90,7 @@ function parseNodeToStaticValue(node, checker, prefix = 'css', variables = {}) {
74
90
  // we don't know what this is, we need to throw an error
75
91
  const type = checker.getTypeAtLocation(node);
76
92
  const typeValue = checker.typeToString(type);
77
- throw new Error(`Unknown type at: "${node.getText()}". Received "${typeValue}"\n${getErrorMessage_1.getErrorMessage(node)}\nFor static analysis of styles, please make sure all types resolve to string or numeric literals. Please use 'const' instead of 'let'. If using an object, cast using "as const" or use an interface with string or numeric literals. Variables: ${JSON.stringify(variables, null, ' ')}`);
93
+ throw new Error(`Unknown type at: "${node.getText()}". Received "${typeValue}"\n${getErrorMessage_1.getErrorMessage(node)}\nFor static analysis of styles, please make sure all types resolve to string or numeric literals. Please use 'const' instead of 'let'. If using an object, cast using "as const" or use an interface with string or numeric literals.`);
78
94
  }
79
95
  exports.parseNodeToStaticValue = parseNodeToStaticValue;
80
96
  function parseTypeToStaticValue(type) {
@@ -114,22 +130,20 @@ function getVariableNameParts(input) {
114
130
  /**
115
131
  * Gets a static string value from a template expression. It could recurse.
116
132
  */
117
- function getStyleValueFromTemplateExpression(node, checker, prefix = 'css', variables = {}) {
133
+ function getStyleValueFromTemplateExpression(node, context) {
118
134
  if (!node) {
119
135
  return '';
120
136
  }
121
137
  if (typescript_1.default.isTemplateExpression(node)) {
122
- return (getStyleValueFromTemplateExpression(node.head, checker, prefix, variables) +
123
- node.templateSpans
124
- .map(value => getStyleValueFromTemplateExpression(value, checker, prefix, variables))
125
- .join(''));
138
+ return (getStyleValueFromTemplateExpression(node.head, context) +
139
+ node.templateSpans.map(value => getStyleValueFromTemplateExpression(value, context)).join(''));
126
140
  }
127
141
  if (typescript_1.default.isTemplateHead(node) || typescript_1.default.isTemplateTail(node) || typescript_1.default.isTemplateMiddle(node)) {
128
142
  return node.text;
129
143
  }
130
144
  if (typescript_1.default.isTemplateSpan(node)) {
131
- return (parseNodeToStaticValue(node.expression, checker, prefix, variables) +
132
- getStyleValueFromTemplateExpression(node.literal, checker, prefix, variables));
145
+ return (parseNodeToStaticValue(node.expression, context) +
146
+ getStyleValueFromTemplateExpression(node.literal, context));
133
147
  }
134
148
  return '';
135
149
  }
@@ -1,12 +1,13 @@
1
1
  import ts from 'typescript';
2
+ import { TransformerContext } from './types';
2
3
  export declare type NestedStyleObject = {
3
4
  [key: string]: string | NestedStyleObject;
4
5
  };
5
- export declare function parseObjectToStaticValue(node: ts.Node, checker: ts.TypeChecker, prefix?: string, variables?: Record<string, string>): NestedStyleObject;
6
+ export declare function parseObjectToStaticValue(node: ts.Node, context: TransformerContext): NestedStyleObject;
6
7
  /**
7
8
  * If we're here, we have a `ts.Type` that represents a style object. We try to parse a style object
8
9
  * from the AST, but we might have something that is more complicated like a function call or an
9
10
  * identifier that represents an object. It could be imported from another file.
10
11
  */
11
- export declare function parseStyleObjFromType(type: ts.Type, checker: ts.TypeChecker, prefix: string, variables: Record<string, string>): Record<string, any>;
12
+ export declare function parseStyleObjFromType(type: ts.Type, context: TransformerContext): Record<string, any>;
12
13
  //# sourceMappingURL=parseObjectToStaticValue.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"parseObjectToStaticValue.d.ts","sourceRoot":"","sources":["../../../../lib/utils/parseObjectToStaticValue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAK5B,oBAAY,iBAAiB,GAAG;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,iBAAiB,CAAA;CAAC,CAAC;AAE5E,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,EAAE,CAAC,IAAI,EACb,OAAO,EAAE,EAAE,CAAC,WAAW,EACvB,MAAM,SAAQ,EACd,SAAS,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM,GACrC,iBAAiB,CAUnB;AAsED;;;;GAIG;AACH,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,EAAE,CAAC,IAAI,EACb,OAAO,EAAE,EAAE,CAAC,WAAW,EACvB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,uBA2BlC"}
1
+ {"version":3,"file":"parseObjectToStaticValue.d.ts","sourceRoot":"","sources":["../../../../lib/utils/parseObjectToStaticValue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAI5B,OAAO,EAAC,kBAAkB,EAAC,MAAM,SAAS,CAAC;AAE3C,oBAAY,iBAAiB,GAAG;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,iBAAiB,CAAA;CAAC,CAAC;AAE5E,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,EAAE,CAAC,IAAI,EACb,OAAO,EAAE,kBAAkB,GAC1B,iBAAiB,CAUnB;AA+DD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,kBAAkB,uBA0B/E"}