@workday/canvas-kit-styling-transform 13.2.0 → 13.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commonjs/index.d.ts +14 -0
- package/dist/commonjs/index.d.ts.map +1 -0
- package/dist/commonjs/index.js +28 -0
- package/dist/commonjs/lib/createObjectTransform.d.ts +10 -0
- package/dist/commonjs/lib/createObjectTransform.d.ts.map +1 -0
- package/dist/commonjs/lib/createObjectTransform.js +14 -0
- package/dist/commonjs/lib/createPropertyTransform.d.ts +7 -0
- package/dist/commonjs/lib/createPropertyTransform.d.ts.map +1 -0
- package/dist/commonjs/lib/createPropertyTransform.js +11 -0
- package/dist/commonjs/lib/createTypeScriptWatchProgram.d.ts +16 -0
- package/dist/commonjs/lib/createTypeScriptWatchProgram.d.ts.map +1 -0
- package/dist/commonjs/lib/createTypeScriptWatchProgram.js +45 -0
- package/dist/commonjs/lib/styleTransform.d.ts +23 -0
- package/dist/commonjs/lib/styleTransform.d.ts.map +1 -0
- package/dist/commonjs/lib/styleTransform.js +131 -0
- package/dist/commonjs/lib/utils/createStyleObjectNode.d.ts +35 -0
- package/dist/commonjs/lib/utils/createStyleObjectNode.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/createStyleObjectNode.js +84 -0
- package/dist/commonjs/lib/utils/getCssVariables.d.ts +4 -0
- package/dist/commonjs/lib/utils/getCssVariables.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/getCssVariables.js +31 -0
- package/dist/commonjs/lib/utils/getErrorMessage.d.ts +16 -0
- package/dist/commonjs/lib/utils/getErrorMessage.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/getErrorMessage.js +45 -0
- package/dist/commonjs/lib/utils/getFallbackVariable.d.ts +9 -0
- package/dist/commonjs/lib/utils/getFallbackVariable.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/getFallbackVariable.js +30 -0
- package/dist/commonjs/lib/utils/getHash.d.ts +4 -0
- package/dist/commonjs/lib/utils/getHash.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/getHash.js +16 -0
- package/dist/commonjs/lib/utils/getVarName.d.ts +17 -0
- package/dist/commonjs/lib/utils/getVarName.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/getVarName.js +34 -0
- package/dist/commonjs/lib/utils/handleCalc.d.ts +2 -0
- package/dist/commonjs/lib/utils/handleCalc.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handleCalc.js +33 -0
- package/dist/commonjs/lib/utils/handleCreateStencil.d.ts +7 -0
- package/dist/commonjs/lib/utils/handleCreateStencil.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handleCreateStencil.js +309 -0
- package/dist/commonjs/lib/utils/handleCreateStyles.d.ts +3 -0
- package/dist/commonjs/lib/utils/handleCreateStyles.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handleCreateStyles.js +86 -0
- package/dist/commonjs/lib/utils/handleCreateVars.d.ts +3 -0
- package/dist/commonjs/lib/utils/handleCreateVars.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handleCreateVars.js +65 -0
- package/dist/commonjs/lib/utils/handleCssVar.d.ts +2 -0
- package/dist/commonjs/lib/utils/handleCssVar.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handleCssVar.js +19 -0
- package/dist/commonjs/lib/utils/handleInjectGlobal.d.ts +3 -0
- package/dist/commonjs/lib/utils/handleInjectGlobal.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handleInjectGlobal.js +40 -0
- package/dist/commonjs/lib/utils/handleKeyframes.d.ts +3 -0
- package/dist/commonjs/lib/utils/handleKeyframes.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handleKeyframes.js +48 -0
- package/dist/commonjs/lib/utils/handleParentModifier.d.ts +2 -0
- package/dist/commonjs/lib/utils/handleParentModifier.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handleParentModifier.js +26 -0
- package/dist/commonjs/lib/utils/handlePx2Rem.d.ts +2 -0
- package/dist/commonjs/lib/utils/handlePx2Rem.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/handlePx2Rem.js +19 -0
- package/dist/commonjs/lib/utils/injectStyles.d.ts +3 -0
- package/dist/commonjs/lib/utils/injectStyles.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/injectStyles.js +11 -0
- package/dist/commonjs/lib/utils/isImportedFromStyling.d.ts +7 -0
- package/dist/commonjs/lib/utils/isImportedFromStyling.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/isImportedFromStyling.js +24 -0
- package/dist/commonjs/lib/utils/parseNodeToStaticValue.d.ts +11 -0
- package/dist/commonjs/lib/utils/parseNodeToStaticValue.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/parseNodeToStaticValue.js +217 -0
- package/dist/commonjs/lib/utils/parseObjectToStaticValue.d.ts +15 -0
- package/dist/commonjs/lib/utils/parseObjectToStaticValue.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/parseObjectToStaticValue.js +168 -0
- package/dist/commonjs/lib/utils/stylisFns.d.ts +3 -0
- package/dist/commonjs/lib/utils/stylisFns.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/stylisFns.js +45 -0
- package/dist/commonjs/lib/utils/types.d.ts +210 -0
- package/dist/commonjs/lib/utils/types.d.ts.map +1 -0
- package/dist/commonjs/lib/utils/types.js +2 -0
- package/dist/commonjs/lib/webpack-loader.d.ts +7 -0
- package/dist/commonjs/lib/webpack-loader.d.ts.map +1 -0
- package/dist/commonjs/lib/webpack-loader.js +21 -0
- package/dist/commonjs/lib/webpackPlugin.d.ts +31 -0
- package/dist/commonjs/lib/webpackPlugin.d.ts.map +1 -0
- package/dist/commonjs/lib/webpackPlugin.js +38 -0
- package/dist/commonjs/spec/createProgramFromSource.d.ts +8 -0
- package/dist/commonjs/spec/createProgramFromSource.d.ts.map +1 -0
- package/dist/commonjs/spec/createProgramFromSource.js +111 -0
- package/dist/commonjs/spec/findNodes.d.ts +3 -0
- package/dist/commonjs/spec/findNodes.d.ts.map +1 -0
- package/dist/commonjs/spec/findNodes.js +33 -0
- package/dist/commonjs/testing.d.ts +4 -0
- package/dist/commonjs/testing.d.ts.map +1 -0
- package/dist/commonjs/testing.js +11 -0
- package/dist/es6/index.d.ts +14 -0
- package/dist/es6/index.d.ts.map +1 -0
- package/dist/es6/index.js +14 -0
- package/dist/es6/lib/createObjectTransform.d.ts +10 -0
- package/dist/es6/lib/createObjectTransform.d.ts.map +1 -0
- package/dist/es6/lib/createObjectTransform.js +10 -0
- package/dist/es6/lib/createPropertyTransform.d.ts +7 -0
- package/dist/es6/lib/createPropertyTransform.d.ts.map +1 -0
- package/dist/es6/lib/createPropertyTransform.js +7 -0
- package/dist/es6/lib/createTypeScriptWatchProgram.d.ts +16 -0
- package/dist/es6/lib/createTypeScriptWatchProgram.d.ts.map +1 -0
- package/dist/es6/lib/createTypeScriptWatchProgram.js +36 -0
- package/dist/es6/lib/styleTransform.d.ts +23 -0
- package/dist/es6/lib/styleTransform.d.ts.map +1 -0
- package/dist/es6/lib/styleTransform.js +120 -0
- package/dist/es6/lib/utils/createStyleObjectNode.d.ts +35 -0
- package/dist/es6/lib/utils/createStyleObjectNode.d.ts.map +1 -0
- package/dist/es6/lib/utils/createStyleObjectNode.js +74 -0
- package/dist/es6/lib/utils/getCssVariables.d.ts +4 -0
- package/dist/es6/lib/utils/getCssVariables.d.ts.map +1 -0
- package/dist/es6/lib/utils/getCssVariables.js +26 -0
- package/dist/es6/lib/utils/getErrorMessage.d.ts +16 -0
- package/dist/es6/lib/utils/getErrorMessage.d.ts.map +1 -0
- package/dist/es6/lib/utils/getErrorMessage.js +41 -0
- package/dist/es6/lib/utils/getFallbackVariable.d.ts +9 -0
- package/dist/es6/lib/utils/getFallbackVariable.d.ts.map +1 -0
- package/dist/es6/lib/utils/getFallbackVariable.js +26 -0
- package/dist/es6/lib/utils/getHash.d.ts +4 -0
- package/dist/es6/lib/utils/getHash.d.ts.map +1 -0
- package/dist/es6/lib/utils/getHash.js +9 -0
- package/dist/es6/lib/utils/getVarName.d.ts +17 -0
- package/dist/es6/lib/utils/getVarName.d.ts.map +1 -0
- package/dist/es6/lib/utils/getVarName.js +27 -0
- package/dist/es6/lib/utils/handleCalc.d.ts +2 -0
- package/dist/es6/lib/utils/handleCalc.d.ts.map +1 -0
- package/dist/es6/lib/utils/handleCalc.js +27 -0
- package/dist/es6/lib/utils/handleCreateStencil.d.ts +7 -0
- package/dist/es6/lib/utils/handleCreateStencil.d.ts.map +1 -0
- package/dist/es6/lib/utils/handleCreateStencil.js +301 -0
- package/dist/es6/lib/utils/handleCreateStyles.d.ts +3 -0
- package/dist/es6/lib/utils/handleCreateStyles.d.ts.map +1 -0
- package/dist/es6/lib/utils/handleCreateStyles.js +79 -0
- package/dist/es6/lib/utils/handleCreateVars.d.ts +3 -0
- package/dist/es6/lib/utils/handleCreateVars.d.ts.map +1 -0
- package/dist/es6/lib/utils/handleCreateVars.js +58 -0
- package/dist/es6/lib/utils/handleCssVar.d.ts +2 -0
- package/dist/es6/lib/utils/handleCssVar.d.ts.map +1 -0
- package/dist/es6/lib/utils/handleCssVar.js +13 -0
- package/dist/es6/lib/utils/handleInjectGlobal.d.ts +3 -0
- package/dist/es6/lib/utils/handleInjectGlobal.d.ts.map +1 -0
- package/dist/es6/lib/utils/handleInjectGlobal.js +33 -0
- package/dist/es6/lib/utils/handleKeyframes.d.ts +3 -0
- package/dist/es6/lib/utils/handleKeyframes.d.ts.map +1 -0
- package/dist/es6/lib/utils/handleKeyframes.js +41 -0
- package/dist/es6/lib/utils/handleParentModifier.d.ts +2 -0
- package/dist/es6/lib/utils/handleParentModifier.d.ts.map +1 -0
- package/dist/es6/lib/utils/handleParentModifier.js +20 -0
- package/dist/es6/lib/utils/handlePx2Rem.d.ts +2 -0
- package/dist/es6/lib/utils/handlePx2Rem.d.ts.map +1 -0
- package/dist/es6/lib/utils/handlePx2Rem.js +13 -0
- package/dist/es6/lib/utils/injectStyles.d.ts +3 -0
- package/dist/es6/lib/utils/injectStyles.d.ts.map +1 -0
- package/dist/es6/lib/utils/injectStyles.js +7 -0
- package/dist/es6/lib/utils/isImportedFromStyling.d.ts +7 -0
- package/dist/es6/lib/utils/isImportedFromStyling.d.ts.map +1 -0
- package/dist/es6/lib/utils/isImportedFromStyling.js +17 -0
- package/dist/es6/lib/utils/parseNodeToStaticValue.d.ts +11 -0
- package/dist/es6/lib/utils/parseNodeToStaticValue.d.ts.map +1 -0
- package/dist/es6/lib/utils/parseNodeToStaticValue.js +209 -0
- package/dist/es6/lib/utils/parseObjectToStaticValue.d.ts +15 -0
- package/dist/es6/lib/utils/parseObjectToStaticValue.d.ts.map +1 -0
- package/dist/es6/lib/utils/parseObjectToStaticValue.js +159 -0
- package/dist/es6/lib/utils/stylisFns.d.ts +3 -0
- package/dist/es6/lib/utils/stylisFns.d.ts.map +1 -0
- package/dist/es6/lib/utils/stylisFns.js +41 -0
- package/dist/es6/lib/utils/types.d.ts +210 -0
- package/dist/es6/lib/utils/types.d.ts.map +1 -0
- package/dist/es6/lib/utils/types.js +1 -0
- package/dist/es6/lib/webpack-loader.d.ts +7 -0
- package/dist/es6/lib/webpack-loader.d.ts.map +1 -0
- package/dist/es6/lib/webpack-loader.js +15 -0
- package/dist/es6/lib/webpackPlugin.d.ts +31 -0
- package/dist/es6/lib/webpackPlugin.d.ts.map +1 -0
- package/dist/es6/lib/webpackPlugin.js +31 -0
- package/dist/es6/spec/createProgramFromSource.d.ts +8 -0
- package/dist/es6/spec/createProgramFromSource.d.ts.map +1 -0
- package/dist/es6/spec/createProgramFromSource.js +81 -0
- package/dist/es6/spec/findNodes.d.ts +3 -0
- package/dist/es6/spec/findNodes.d.ts.map +1 -0
- package/dist/es6/spec/findNodes.js +26 -0
- package/dist/es6/testing.d.ts +4 -0
- package/dist/es6/testing.d.ts.map +1 -0
- package/dist/es6/testing.js +3 -0
- package/index.ts +19 -0
- package/lib/createObjectTransform.ts +12 -0
- package/lib/createPropertyTransform.ts +9 -0
- package/lib/createTypeScriptWatchProgram.ts +52 -0
- package/lib/styleTransform.ts +169 -0
- package/lib/utils/createStyleObjectNode.ts +105 -0
- package/lib/utils/getCssVariables.ts +36 -0
- package/lib/utils/getErrorMessage.ts +57 -0
- package/lib/utils/getFallbackVariable.ts +34 -0
- package/lib/utils/getHash.ts +13 -0
- package/lib/utils/getVarName.ts +32 -0
- package/lib/utils/handleCalc.ts +33 -0
- package/lib/utils/handleCreateStencil.ts +442 -0
- package/lib/utils/handleCreateStyles.ts +97 -0
- package/lib/utils/handleCreateVars.ts +90 -0
- package/lib/utils/handleCssVar.ts +19 -0
- package/lib/utils/handleInjectGlobal.ts +51 -0
- package/lib/utils/handleKeyframes.ts +60 -0
- package/lib/utils/handleParentModifier.ts +29 -0
- package/lib/utils/handlePx2Rem.ts +22 -0
- package/lib/utils/injectStyles.ts +14 -0
- package/lib/utils/isImportedFromStyling.ts +21 -0
- package/lib/utils/parseNodeToStaticValue.ts +281 -0
- package/lib/utils/parseObjectToStaticValue.ts +201 -0
- package/lib/utils/stylisFns.ts +56 -0
- package/lib/utils/types.ts +223 -0
- package/lib/webpack-loader.ts +28 -0
- package/lib/webpackPlugin.ts +69 -0
- package/package.json +19 -14
- package/index.js +0 -22
|
@@ -0,0 +1,7 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,17 @@
|
|
|
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 function isImportedFromStyling(node, checker) {
|
|
7
|
+
var _a;
|
|
8
|
+
const symbol = checker.getSymbolAtLocation(node);
|
|
9
|
+
const declaration = (symbol === null || symbol === void 0 ? void 0 : symbol.valueDeclaration) || ((_a = symbol === null || symbol === void 0 ? void 0 : symbol.declarations) === null || _a === void 0 ? void 0 : _a[0]);
|
|
10
|
+
if (declaration &&
|
|
11
|
+
ts.isImportSpecifier(declaration) &&
|
|
12
|
+
ts.isStringLiteral(declaration.parent.parent.parent.moduleSpecifier) &&
|
|
13
|
+
declaration.parent.parent.parent.moduleSpecifier.text === '@workday/canvas-kit-styling') {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { TransformerContext } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* This is the workhorse of statically analyzing style values
|
|
5
|
+
*/
|
|
6
|
+
export declare function parseNodeToStaticValue(node: ts.Node, context: TransformerContext): string | number;
|
|
7
|
+
/**
|
|
8
|
+
* Get a value from an aliased symbol, if it exists. An aliased symbol comes from
|
|
9
|
+
*/
|
|
10
|
+
export declare function getValueFromAliasedSymbol(node: ts.Node, varName: string, context: TransformerContext): string | void;
|
|
11
|
+
//# sourceMappingURL=parseNodeToStaticValue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseNodeToStaticValue.d.ts","sourceRoot":"","sources":["../../../../lib/utils/parseNodeToStaticValue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAG5B,OAAO,EAAC,kBAAkB,EAAC,MAAM,SAAS,CAAC;AAQ3C;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,EAAE,CAAC,IAAI,EACb,OAAO,EAAE,kBAAkB,GAC1B,MAAM,GAAG,MAAM,CAyIjB;AA4DD;;GAEG;AACH,wBAAgB,yBAAyB,CACvC,IAAI,EAAE,EAAE,CAAC,IAAI,EACb,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,kBAAkB,GAC1B,MAAM,GAAG,IAAI,CA2Cf"}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { getErrorMessage } from './getErrorMessage';
|
|
3
|
+
function handlePropertyTransforms(node, context) {
|
|
4
|
+
return context.propertyTransforms.reduce((result, transformer) => {
|
|
5
|
+
return result || transformer(node, context) || undefined;
|
|
6
|
+
}, undefined);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* This is the workhorse of statically analyzing style values
|
|
10
|
+
*/
|
|
11
|
+
export function parseNodeToStaticValue(node, context) {
|
|
12
|
+
const { checker } = context;
|
|
13
|
+
const value = handlePropertyTransforms(node, context);
|
|
14
|
+
if (value) {
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* String literals like 'red' or empty Template Expressions like `red`
|
|
19
|
+
*/
|
|
20
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
|
|
21
|
+
return node.text;
|
|
22
|
+
}
|
|
23
|
+
// 12
|
|
24
|
+
if (ts.isNumericLiteral(node)) {
|
|
25
|
+
return parseFloat(node.text);
|
|
26
|
+
}
|
|
27
|
+
// -12
|
|
28
|
+
if (ts.isPrefixUnaryExpression(node) &&
|
|
29
|
+
node.operator === ts.SyntaxKind.MinusToken &&
|
|
30
|
+
ts.isNumericLiteral(node.operand)) {
|
|
31
|
+
return -parseFloat(node.operand.text);
|
|
32
|
+
}
|
|
33
|
+
// undefined
|
|
34
|
+
if (ts.isIdentifier(node) && node.text === 'undefined') {
|
|
35
|
+
return 'undefined';
|
|
36
|
+
}
|
|
37
|
+
// a.b
|
|
38
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
39
|
+
getPropertyAccessExpressionText(node);
|
|
40
|
+
const varName = getPropertyAccessExpressionText(node);
|
|
41
|
+
const value = getValueFromProcessedNodes(varName, context) ||
|
|
42
|
+
getValueFromAliasedSymbol(node, varName, context);
|
|
43
|
+
if (value) {
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (ts.isComputedPropertyName(node) && ts.isIdentifier(node.expression)) {
|
|
48
|
+
const varName = node.expression.text;
|
|
49
|
+
const value = getValueFromProcessedNodes(varName, context) ||
|
|
50
|
+
getValueFromAliasedSymbol(node, varName, context);
|
|
51
|
+
if (value) {
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// [a.b]
|
|
56
|
+
if (ts.isComputedPropertyName(node) && ts.isPropertyAccessExpression(node.expression)) {
|
|
57
|
+
const varName = getPropertyAccessExpressionText(node.expression);
|
|
58
|
+
const value = getValueFromProcessedNodes(varName, context) ||
|
|
59
|
+
getValueFromAliasedSymbol(node, varName, context);
|
|
60
|
+
if (value) {
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// [`${a.b} &`]
|
|
65
|
+
if (ts.isComputedPropertyName(node) && ts.isTemplateExpression(node.expression)) {
|
|
66
|
+
return getStyleValueFromTemplateExpression(node.expression, context);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* ```ts
|
|
70
|
+
* `border 1px ${myVars.colors.border}`
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
if (ts.isTemplateExpression(node)) {
|
|
74
|
+
return getStyleValueFromTemplateExpression(node, context);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* An Identifier is a simple variable. It may represent a variable, so we'll check it before
|
|
78
|
+
* moving on. This typically happens in stencils.
|
|
79
|
+
*/
|
|
80
|
+
if (ts.isIdentifier(node)) {
|
|
81
|
+
const value = getValueFromProcessedNodes(node.text, context) ||
|
|
82
|
+
getValueFromAliasedSymbol(node, node.text, context);
|
|
83
|
+
if (value) {
|
|
84
|
+
return value;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (ts.isElementAccessExpression(node)) {
|
|
88
|
+
const value = parseTypeToStaticValue(checker.getTypeAtLocation(node));
|
|
89
|
+
if (value) {
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// If we got here, we cannot statically analyze by the AST alone. We have to check the type of the
|
|
94
|
+
// correct AST Node
|
|
95
|
+
if (ts.isIdentifier(node) || ts.isPropertyAccessExpression(node)) {
|
|
96
|
+
const value = parseTypeToStaticValue(checker.getTypeAtLocation(node));
|
|
97
|
+
if (value) {
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (ts.isComputedPropertyName(node)) {
|
|
102
|
+
const value = parseTypeToStaticValue(checker.getTypeAtLocation(node.expression));
|
|
103
|
+
if (value) {
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// we don't know what this is, we need to throw an error
|
|
108
|
+
const type = checker.getTypeAtLocation(node);
|
|
109
|
+
const typeValue = checker.typeToString(type);
|
|
110
|
+
throw new Error(`Unknown type at: "${node.getText()}". Received "${typeValue}"\n${getErrorMessage(node, context)}\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.`);
|
|
111
|
+
}
|
|
112
|
+
function parseTypeToStaticValue(type) {
|
|
113
|
+
if (type.isStringLiteral()) {
|
|
114
|
+
return type.value;
|
|
115
|
+
}
|
|
116
|
+
if (type.isNumberLiteral()) {
|
|
117
|
+
return type.value;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* A `PropertyExpression` is an expression with a dot in it. Like `a.b.c`. It may be nested. This
|
|
122
|
+
* function will walk the AST and create a string like `a.b.c` to be passed on to variable name
|
|
123
|
+
* generation. This will be used for CSS variable lookups.
|
|
124
|
+
*/
|
|
125
|
+
function getPropertyAccessExpressionText(node) {
|
|
126
|
+
if (ts.isIdentifier(node.name)) {
|
|
127
|
+
if (ts.isIdentifier(node.expression)) {
|
|
128
|
+
return `${node.expression.text}.${node.name.text}`;
|
|
129
|
+
}
|
|
130
|
+
if (ts.isPropertyAccessExpression(node.expression)) {
|
|
131
|
+
return `${getPropertyAccessExpressionText(node.expression)}.${node.name.text}`;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return '';
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Gets a static string value from a template expression. It could recurse.
|
|
138
|
+
*/
|
|
139
|
+
function getStyleValueFromTemplateExpression(node, context) {
|
|
140
|
+
if (!node) {
|
|
141
|
+
return '';
|
|
142
|
+
}
|
|
143
|
+
if (ts.isTemplateExpression(node)) {
|
|
144
|
+
return (getStyleValueFromTemplateExpression(node.head, context) +
|
|
145
|
+
node.templateSpans.map(value => getStyleValueFromTemplateExpression(value, context)).join(''));
|
|
146
|
+
}
|
|
147
|
+
if (ts.isTemplateHead(node) || ts.isTemplateTail(node) || ts.isTemplateMiddle(node)) {
|
|
148
|
+
return node.text;
|
|
149
|
+
}
|
|
150
|
+
if (ts.isTemplateSpan(node)) {
|
|
151
|
+
return (parseNodeToStaticValue(node.expression, context) +
|
|
152
|
+
getStyleValueFromTemplateExpression(node.literal, context));
|
|
153
|
+
}
|
|
154
|
+
return '';
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Get a value from an aliased symbol, if it exists. An aliased symbol comes from
|
|
158
|
+
*/
|
|
159
|
+
export function getValueFromAliasedSymbol(node, varName, context) {
|
|
160
|
+
const { checker, transform } = context;
|
|
161
|
+
let symbolNode;
|
|
162
|
+
if (ts.isIdentifier(node)) {
|
|
163
|
+
// base case is the node is an identifier
|
|
164
|
+
symbolNode = node;
|
|
165
|
+
}
|
|
166
|
+
if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.name)) {
|
|
167
|
+
return getValueFromAliasedSymbol(node.expression, varName, context);
|
|
168
|
+
}
|
|
169
|
+
if (ts.isComputedPropertyName(node)) {
|
|
170
|
+
return getValueFromAliasedSymbol(node.expression, varName, context);
|
|
171
|
+
}
|
|
172
|
+
if (symbolNode) {
|
|
173
|
+
const symbol = checker.getSymbolAtLocation(symbolNode);
|
|
174
|
+
let declaration;
|
|
175
|
+
if (symbol === null || symbol === void 0 ? void 0 : symbol.valueDeclaration) {
|
|
176
|
+
// If the symbol has a value declaration, we'll use that declaration
|
|
177
|
+
declaration = symbol.valueDeclaration;
|
|
178
|
+
}
|
|
179
|
+
else if (symbol && symbol.getFlags() & ts.SymbolFlags.Alias) {
|
|
180
|
+
// If the symbol does not have a value declaration, we'll check if there's a aliased symbol
|
|
181
|
+
// linking to the value declaration.
|
|
182
|
+
declaration = checker.getAliasedSymbol(symbol).valueDeclaration;
|
|
183
|
+
}
|
|
184
|
+
// If there is an aliased symbol and it is a variable declaration, try to resolve the
|
|
185
|
+
if (declaration && hasExpression(declaration)) {
|
|
186
|
+
if (declaration.initializer) {
|
|
187
|
+
// Update the `prefix` to the alias declaration's source file.
|
|
188
|
+
const aliasFileContext = {
|
|
189
|
+
...context,
|
|
190
|
+
prefix: context.getPrefix(declaration.getSourceFile().fileName),
|
|
191
|
+
};
|
|
192
|
+
transform(declaration.initializer, aliasFileContext);
|
|
193
|
+
return getValueFromProcessedNodes(varName, aliasFileContext);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function getValueFromProcessedNodes(varName, context) {
|
|
199
|
+
const { names } = context;
|
|
200
|
+
if (context.nameScope && names[`${context.nameScope}${varName}`]) {
|
|
201
|
+
return names[`${context.nameScope}${varName}`];
|
|
202
|
+
}
|
|
203
|
+
if (names[varName]) {
|
|
204
|
+
return names[varName];
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function hasExpression(node) {
|
|
208
|
+
return 'initializer' in node;
|
|
209
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { NestedStyleObject, TransformerContext } from './types';
|
|
3
|
+
export declare function parseObjectToStaticValue(node: ts.Node, context: TransformerContext): NestedStyleObject;
|
|
4
|
+
/**
|
|
5
|
+
* If we're here, we have a `ts.Type` that represents a style object. We try to parse a style object
|
|
6
|
+
* from the AST, but we might have something that is more complicated like a function call or an
|
|
7
|
+
* identifier that represents an object. It could be imported from another file.
|
|
8
|
+
*/
|
|
9
|
+
export declare function parseStyleObjFromType(type: ts.Type, context: TransformerContext): Record<string, any>;
|
|
10
|
+
/**
|
|
11
|
+
* Wrap all unwrapped CSS Variables. For example, `{padding: '--foo'}` will be replaced with
|
|
12
|
+
* `{padding: 'var(--foo)'}`. It also works on variables in the middle of the property.
|
|
13
|
+
*/
|
|
14
|
+
export declare function maybeWrapCSSVariables(input: string | number, names: Record<string, string>): string | number;
|
|
15
|
+
//# sourceMappingURL=parseObjectToStaticValue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseObjectToStaticValue.d.ts","sourceRoot":"","sources":["../../../../lib/utils/parseObjectToStaticValue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAI5B,OAAO,EAAC,iBAAiB,EAAE,kBAAkB,EAAC,MAAM,SAAS,CAAC;AAE9D,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,EAAE,CAAC,IAAI,EACb,OAAO,EAAE,kBAAkB,GAC1B,iBAAiB,CAUnB;AA4GD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,kBAAkB,uBA+B/E;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,MAAM,GAAG,MAAM,EACtB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAC5B,MAAM,GAAG,MAAM,CA4BjB"}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { getFallbackVariable } from './getFallbackVariable';
|
|
3
|
+
import { parseNodeToStaticValue } from './parseNodeToStaticValue';
|
|
4
|
+
export function parseObjectToStaticValue(node, context) {
|
|
5
|
+
let styleObj = {};
|
|
6
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
7
|
+
node.properties.forEach(property => {
|
|
8
|
+
styleObj = { ...styleObj, ...parsePropertyToStaticValue(property, context) };
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
return wrapStyleObj(styleObj, context);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Wrap all values and nested object values with a maybeWrapCSSVariables call
|
|
15
|
+
*/
|
|
16
|
+
function wrapStyleObj(styleObj, context) {
|
|
17
|
+
for (const key in styleObj) {
|
|
18
|
+
if (styleObj.hasOwnProperty(key)) {
|
|
19
|
+
const value = styleObj[key];
|
|
20
|
+
if (typeof value === 'object') {
|
|
21
|
+
styleObj[key] = wrapStyleObj(value, context);
|
|
22
|
+
}
|
|
23
|
+
if (typeof value === 'string') {
|
|
24
|
+
styleObj[key] = maybeWrapCSSVariables(value, context.names);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return styleObj;
|
|
29
|
+
}
|
|
30
|
+
function handleObjectTransforms(node, context) {
|
|
31
|
+
return context.objectTransforms.reduce((result, transformer) => {
|
|
32
|
+
return result || transformer(node, context) || undefined;
|
|
33
|
+
}, undefined);
|
|
34
|
+
}
|
|
35
|
+
function parsePropertyToStaticValue(node, context) {
|
|
36
|
+
const styleObj = {};
|
|
37
|
+
// check to see if there's an object transform for this node
|
|
38
|
+
const obj = handleObjectTransforms(node, context);
|
|
39
|
+
if (obj) {
|
|
40
|
+
return obj;
|
|
41
|
+
}
|
|
42
|
+
// If there's a spread, we're expecting an object to be returned. We'll see if there's an object transform for this node
|
|
43
|
+
if (ts.isSpreadAssignment(node)) {
|
|
44
|
+
const obj = handleObjectTransforms(node.expression, context);
|
|
45
|
+
if (obj) {
|
|
46
|
+
return obj;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// {...{key: 'value'}}
|
|
50
|
+
if (ts.isSpreadAssignment(node) && ts.isObjectLiteralExpression(node.expression)) {
|
|
51
|
+
// recurse to parse a nested ObjectLiteralExpression
|
|
52
|
+
return parseObjectToStaticValue(node.expression, context);
|
|
53
|
+
}
|
|
54
|
+
// { name: value }
|
|
55
|
+
if (ts.isPropertyAssignment(node)) {
|
|
56
|
+
const key = ts.isIdentifier(node.name)
|
|
57
|
+
? node.name.text
|
|
58
|
+
: parseNodeToStaticValue(node.name, context);
|
|
59
|
+
if (key) {
|
|
60
|
+
if (ts.isObjectLiteralExpression(node.initializer)) {
|
|
61
|
+
// nested
|
|
62
|
+
styleObj[key] = parseObjectToStaticValue(node.initializer, context);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
styleObj[key] = parseNodeToStaticValue(node.initializer, context);
|
|
66
|
+
}
|
|
67
|
+
return styleObj;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// { name: value } (types)
|
|
71
|
+
if (ts.isPropertySignature(node)) {
|
|
72
|
+
const key = ts.isIdentifier(node.name)
|
|
73
|
+
? node.name.text
|
|
74
|
+
: parseNodeToStaticValue(node.name, context).toString();
|
|
75
|
+
if (key) {
|
|
76
|
+
if (key.includes('&') || key.startsWith(':')) {
|
|
77
|
+
// nested
|
|
78
|
+
styleObj[key] = parseObjectToStaticValue(node.type, context);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
styleObj[key] = parseNodeToStaticValue(node.type, context).toString() || '';
|
|
82
|
+
}
|
|
83
|
+
return styleObj;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// { name }
|
|
87
|
+
if (ts.isShorthandPropertyAssignment(node)) {
|
|
88
|
+
const key = node.name.text;
|
|
89
|
+
styleObj[key] = parseNodeToStaticValue(node.name, context).toString() || '';
|
|
90
|
+
return styleObj;
|
|
91
|
+
}
|
|
92
|
+
// { ...value }
|
|
93
|
+
if (ts.isSpreadAssignment(node)) {
|
|
94
|
+
// Spread assignments are a bit complicated to use the AST to figure out, so we'll ask the
|
|
95
|
+
// TypeScript type checker.
|
|
96
|
+
const type = context.checker.getTypeAtLocation(node.expression);
|
|
97
|
+
return parseStyleObjFromType(type, context);
|
|
98
|
+
}
|
|
99
|
+
return styleObj;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* If we're here, we have a `ts.Type` that represents a style object. We try to parse a style object
|
|
103
|
+
* from the AST, but we might have something that is more complicated like a function call or an
|
|
104
|
+
* identifier that represents an object. It could be imported from another file.
|
|
105
|
+
*/
|
|
106
|
+
export function parseStyleObjFromType(type, context) {
|
|
107
|
+
const styleObj = {};
|
|
108
|
+
// Gets all the properties of the type object
|
|
109
|
+
return type.getProperties().reduce((result, property) => {
|
|
110
|
+
const declaration = property.valueDeclaration;
|
|
111
|
+
if (!declaration) {
|
|
112
|
+
return result;
|
|
113
|
+
}
|
|
114
|
+
// we might have generics, so we'll use the type of the symbol instead of the type at the
|
|
115
|
+
// declaration. This resolves generics like `T` into literal values if they exist.
|
|
116
|
+
const propType = context.checker.getTypeOfSymbolAtLocation(property, declaration);
|
|
117
|
+
if (propType.isStringLiteral()) {
|
|
118
|
+
// This isn't a component variable, it is a static CSS variable
|
|
119
|
+
result[property.name] = propType.value;
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
if (propType.isNumberLiteral()) {
|
|
123
|
+
result[property.name] = propType.value;
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
...result,
|
|
128
|
+
...parsePropertyToStaticValue(declaration, context),
|
|
129
|
+
};
|
|
130
|
+
}, styleObj);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Wrap all unwrapped CSS Variables. For example, `{padding: '--foo'}` will be replaced with
|
|
134
|
+
* `{padding: 'var(--foo)'}`. It also works on variables in the middle of the property.
|
|
135
|
+
*/
|
|
136
|
+
export function maybeWrapCSSVariables(input, names) {
|
|
137
|
+
if (typeof input === 'number') {
|
|
138
|
+
return input;
|
|
139
|
+
}
|
|
140
|
+
// matches an string starting with `--` that isn't already wrapped in a `var()`. It tries to match
|
|
141
|
+
// any character that isn't a valid separator in CSS
|
|
142
|
+
return input.replace(/([a-z]*[ (]*)(--[^\s;,'})]+)(\){0,1})/gi, (match, prefix, variable, postfix) => {
|
|
143
|
+
// shortcut for var() wrappers that already have a fallback
|
|
144
|
+
if (prefix.startsWith('var(') && postfix !== ')') {
|
|
145
|
+
return match;
|
|
146
|
+
}
|
|
147
|
+
// find a possible fallback
|
|
148
|
+
const fallbackVariable = getFallbackVariable(variable, names);
|
|
149
|
+
const fallback = fallbackVariable
|
|
150
|
+
? `, ${maybeWrapCSSVariables(fallbackVariable, names)}`
|
|
151
|
+
: '';
|
|
152
|
+
// if this a var wrapper without a fallback
|
|
153
|
+
if (prefix.startsWith('var(') && postfix === ')') {
|
|
154
|
+
return `${prefix}${variable}${fallback}${postfix}`;
|
|
155
|
+
}
|
|
156
|
+
// if this is not a var wrapper at all
|
|
157
|
+
return `${prefix}var(${variable}${fallback})${postfix}`;
|
|
158
|
+
});
|
|
159
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stylisFns.d.ts","sourceRoot":"","sources":["../../../../lib/utils/stylisFns.ts"],"names":[],"mappings":"AAGA,OAAO,EAML,UAAU,EAIX,MAAM,QAAQ,CAAC;AAMhB,eAAO,MAAM,eAAe,EAAE,UAoC7B,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
/* eslint-disable no-fallthrough */
|
|
3
|
+
/* eslint-disable default-case */
|
|
4
|
+
import { COMMENT, DECLARATION, IMPORT, KEYFRAMES, LAYER, RULESET, serialize, } from 'stylis';
|
|
5
|
+
function strlen(input) {
|
|
6
|
+
return input.length;
|
|
7
|
+
}
|
|
8
|
+
export const prettyStringify = (element, index, children, callback) => {
|
|
9
|
+
switch (element.type) {
|
|
10
|
+
case LAYER:
|
|
11
|
+
if (element.children.length) {
|
|
12
|
+
break;
|
|
13
|
+
}
|
|
14
|
+
case IMPORT:
|
|
15
|
+
case DECLARATION:
|
|
16
|
+
return (element.return =
|
|
17
|
+
element.return ||
|
|
18
|
+
' ' + element.value.replace(':', ': ') + (index === children.length - 1 ? '' : '\n'));
|
|
19
|
+
case COMMENT:
|
|
20
|
+
return `${element.value}\n`;
|
|
21
|
+
case KEYFRAMES:
|
|
22
|
+
return ((element.return =
|
|
23
|
+
element.value +
|
|
24
|
+
' {\n' +
|
|
25
|
+
serialize(element.children, callback)
|
|
26
|
+
.split('\n')
|
|
27
|
+
.map(i => ` ${i}`)
|
|
28
|
+
.join('\n') +
|
|
29
|
+
'}').replace(/\s\s\}$/, '}') + '\n');
|
|
30
|
+
case RULESET:
|
|
31
|
+
if (!strlen((element.value = element.props.join(', ')))) {
|
|
32
|
+
return '';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const childStr = serialize(element.children, callback);
|
|
36
|
+
if (childStr) {
|
|
37
|
+
element.return = `${element.value} {\n${childStr}\n}\n\n`;
|
|
38
|
+
return element.return;
|
|
39
|
+
}
|
|
40
|
+
return '';
|
|
41
|
+
};
|