next-yak 0.0.36 → 0.0.39

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 (38) hide show
  1. package/dist/loaders/cssloader.cjs +649 -0
  2. package/dist/loaders/cssloader.cjs.map +1 -0
  3. package/dist/loaders/cssloader.d.cts +3 -0
  4. package/dist/loaders/cssloader.d.ts +3 -0
  5. package/dist/loaders/cssloader.js +616 -0
  6. package/dist/loaders/cssloader.js.map +1 -0
  7. package/dist/loaders/tsloader.cjs +844 -0
  8. package/dist/loaders/tsloader.cjs.map +1 -0
  9. package/dist/loaders/tsloader.d.cts +6 -0
  10. package/dist/loaders/tsloader.d.ts +6 -0
  11. package/dist/loaders/tsloader.js +820 -0
  12. package/dist/loaders/tsloader.js.map +1 -0
  13. package/{withYak → dist/withYak}/index.cjs +14 -7
  14. package/dist/withYak/index.cjs.map +1 -0
  15. package/{withYak → dist/withYak}/index.d.cts +2 -2
  16. package/dist/withYak/index.d.ts +37 -0
  17. package/dist/withYak/index.js +68 -0
  18. package/dist/withYak/index.js.map +1 -0
  19. package/loaders/__tests__/classifier.test.ts +99 -93
  20. package/loaders/__tests__/cssloader.test.ts +1 -1
  21. package/loaders/__tests__/getCssName.test.ts +1 -1
  22. package/loaders/__tests__/tsloader.test.ts +1 -1
  23. package/loaders/{babel-yak-plugin.cjs → babel-yak-plugin.ts} +69 -68
  24. package/loaders/{cssloader.cjs → cssloader.ts} +73 -97
  25. package/loaders/lib/{appendCssUnitToExpressionValue.cjs → appendCssUnitToExpressionValue.ts} +7 -10
  26. package/loaders/lib/{getConstantValues.cjs → getConstantValues.ts} +10 -17
  27. package/loaders/lib/{getCssName.cjs → getCssName.ts} +19 -31
  28. package/loaders/lib/{getStyledComponentName.cjs → getStyledComponentName.ts} +7 -10
  29. package/loaders/lib/{getYakImports.cjs → getYakImports.ts} +4 -10
  30. package/loaders/lib/{hash.cjs → hash.ts} +3 -5
  31. package/loaders/lib/{localIdent.cjs → localIdent.ts} +5 -7
  32. package/loaders/lib/{quasiClassifier.cjs → quasiClassifier.ts} +7 -16
  33. package/loaders/lib/{replaceQuasiExpressionTokens.cjs → replaceQuasiExpressionTokens.ts} +27 -26
  34. package/loaders/lib/{stripCssComments.cjs → stripCssComments.ts} +3 -9
  35. package/loaders/{tsloader.cjs → tsloader.ts} +11 -15
  36. package/package.json +12 -21
  37. package/withYak/index.ts +43 -34
  38. package/withYak/index.cjs.map +0 -1
@@ -1,43 +1,59 @@
1
- /// @ts-check;
2
- const quasiClassifier = require("./lib/quasiClassifier.cjs");
3
- const replaceQuasiExpressionTokens = require("./lib/replaceQuasiExpressionTokens.cjs");
4
- const murmurhash2_32_gc = require("./lib/hash.cjs");
5
- const { relative, resolve, basename } = require("path");
6
- const localIdent = require("./lib/localIdent.cjs");
7
- const getStyledComponentName = require("./lib/getStyledComponentName.cjs");
8
- const appendCssUnitToExpressionValue = require("./lib/appendCssUnitToExpressionValue.cjs");
9
- const getCssName = require("./lib/getCssName.cjs");
10
- const {
11
- getConstantName,
12
- getConstantValues,
13
- } = require("./lib/getConstantValues.cjs");
1
+ import type babelCore from "@babel/core";
2
+ import {
3
+ BabelFile,
4
+ NodePath,
5
+ PluginObj,
6
+ PluginPass,
7
+ types as babelTypes,
8
+ } from "@babel/core";
9
+ import quasiClassifier from "./lib/quasiClassifier.js";
10
+ import replaceQuasiExpressionTokens from "./lib/replaceQuasiExpressionTokens.js";
11
+ import murmurhash2_32_gc from "./lib/hash.js";
12
+ import { relative, resolve, basename } from "node:path";
13
+ import localIdent from "./lib/localIdent.js";
14
+ import getStyledComponentName from "./lib/getStyledComponentName.js";
15
+ import appendCssUnitToExpressionValue from "./lib/appendCssUnitToExpressionValue.js";
16
+ import getCssName from "./lib/getCssName.js";
17
+ import { getConstantName, getConstantValues } from "./lib/getConstantValues.js";
14
18
 
15
- /** @typedef {{replaces: Record<string, unknown>, rootContext?: string}} YakBabelPluginOptions */
16
- /** @typedef {{ css: string | undefined, styled: string | undefined, keyframes: string | undefined }} YakLocalIdentifierNames */
19
+ type YakBabelPluginOptions = {
20
+ replaces: Record<string, unknown>;
21
+ rootContext?: string;
22
+ };
23
+
24
+ type YakLocalIdentifierNames = {
25
+ css: string | undefined;
26
+ styled: string | undefined;
27
+ keyframes: string | undefined;
28
+ };
17
29
 
18
30
  /**
19
31
  * Babel plugin for typescript files that use yak - it will do things:
20
32
  * - inject the import to the css-module (with .yak.module.css extension)
21
33
  * - replace the css template literal with styles from the css-module
22
- *
23
- * @param {import("@babel/core")} babel
24
- * @param {YakBabelPluginOptions} options
25
- * @returns {babel.PluginObj<import("@babel/core").PluginPass & {
26
- * localVarNames: YakLocalIdentifierNames,
27
- * isImportedInCurrentFile: boolean,
28
- * classNameCount: number,
29
- * topLevelConstBindings: Map<string, number | string | null>,
30
- * varIndex: number,
31
- * variableNameToStyledCall: Map<string, {
32
- * wasAdded: boolean,
33
- * className: string,
34
- * astNode: import("@babel/types").CallExpression
35
- * }>
36
- * yakImportPath?: import("@babel/core").NodePath<import("@babel/core").types.ImportDeclaration>
37
- * runtimeInternalHelpers: Set<string>
38
- * }>}
39
34
  */
40
- module.exports = function (babel, options) {
35
+ export default function (
36
+ babel: typeof babelCore,
37
+ options: YakBabelPluginOptions
38
+ ): PluginObj<
39
+ PluginPass & {
40
+ localVarNames: YakLocalIdentifierNames;
41
+ isImportedInCurrentFile: boolean;
42
+ classNameCount: number;
43
+ topLevelConstBindings: Map<string, number | string | null>;
44
+ varIndex: number;
45
+ variableNameToStyledCall: Map<
46
+ string,
47
+ {
48
+ wasAdded: boolean;
49
+ className: string;
50
+ astNode: babelTypes.CallExpression;
51
+ }
52
+ >;
53
+ yakImportPath?: NodePath<babelTypes.ImportDeclaration>;
54
+ runtimeInternalHelpers: Set<string>;
55
+ }
56
+ > {
41
57
  const { replaces } = options;
42
58
  const rootContext = options.rootContext || process.cwd();
43
59
  const { types: t } = babel;
@@ -45,13 +61,9 @@ module.exports = function (babel, options) {
45
61
  /**
46
62
  * A unique prefix for each file to avoid collisions
47
63
  * (generated on first use by hashing the relative file path)
48
- * @type {WeakMap<import("@babel/core").BabelFile, string>}
49
- */
50
- const hashedFilePaths = new WeakMap();
51
- /**
52
- * @param {import("@babel/core").BabelFile} file
53
64
  */
54
- const getHashedFilePath = (file) => {
65
+ const hashedFilePaths = new WeakMap<BabelFile, string>();
66
+ const getHashedFilePath = (file: BabelFile) => {
55
67
  const fromCache = hashedFilePaths.get(file);
56
68
  if (fromCache) {
57
69
  return fromCache;
@@ -78,12 +90,11 @@ module.exports = function (babel, options) {
78
90
  * - styled(Component)`...` -> styledCall
79
91
  * - styled.div.attrs({})`...` -> attrsCall
80
92
  * - keyframes`...` -> keyframesLiteral
81
- *
82
- * @param {babel.types.Expression} tag
83
- * @param {YakLocalIdentifierNames} localVarNames
84
- * @returns {"cssLiteral" | "keyframesLiteral" | "styledLiteral" | "styledCall" | "attrsCall" | "unknown"}
85
93
  */
86
- const getYakExpressionType = (tag, localVarNames) => {
94
+ const getYakExpressionType = (
95
+ tag: babelTypes.Expression,
96
+ localVarNames: YakLocalIdentifierNames
97
+ ) => {
87
98
  if (t.isIdentifier(tag)) {
88
99
  if (tag.name === localVarNames.css) {
89
100
  return "cssLiteral";
@@ -157,9 +168,6 @@ module.exports = function (babel, options) {
157
168
  *
158
169
  * Inject the import to the css-module (with .yak.module.css extension)
159
170
  * e.g. `import './App.yak.module.css!=!./App?./App.yak.module.css'`
160
- *
161
- * @param {import("@babel/core").NodePath<import("@babel/types").ImportDeclaration>} path
162
- * @param {babel.PluginPass & {localVarNames: YakLocalIdentifierNames, isImportedInCurrentFile: boolean, classNameCount: number, varIndex: number}} state
163
171
  */
164
172
  ImportDeclaration(path, state) {
165
173
  const node = path.node;
@@ -193,9 +201,7 @@ module.exports = function (babel, options) {
193
201
  return;
194
202
  }
195
203
 
196
- const importSpecifier = /** @type {babel.types.Identifier} */ (
197
- specifier.imported
198
- );
204
+ const importSpecifier = specifier.imported;
199
205
  const localSpecifier = specifier.local || importSpecifier;
200
206
 
201
207
  if (
@@ -215,9 +221,6 @@ module.exports = function (babel, options) {
215
221
  * - styled(Component)`...`
216
222
  * - styled.div.attrs({})`...`
217
223
  * - keyframes`...`
218
- *
219
- * @param {import("@babel/core").NodePath<import("@babel/core").types.TaggedTemplateExpression>} path
220
- * @param {babel.PluginPass & {localVarNames: YakLocalIdentifierNames, isImportedInCurrentFile: boolean, classNameCount: number, varIndex: number}} state
221
224
  */
222
225
  TaggedTemplateExpression(path, state) {
223
226
  if (!this.isImportedInCurrentFile) {
@@ -300,10 +303,9 @@ module.exports = function (babel, options) {
300
303
  * e.g. css`` -> css(...)
301
304
  * newArguments is a set of all arguments that will be passed to the function
302
305
  */
303
- const newArguments = new Set();
306
+ const newArguments = new Set<babelTypes.Expression>();
304
307
  const quasis = path.node.quasi.quasis;
305
- /** @type {string[]} */
306
- let currentNestingScopes = [];
308
+ let currentNestingScopes: string[] = [];
307
309
  const quasiTypes = quasis.map((quasi) => {
308
310
  const classification = quasiClassifier(
309
311
  quasi.value.raw,
@@ -314,8 +316,8 @@ module.exports = function (babel, options) {
314
316
  });
315
317
 
316
318
  const expressions = path.node.quasi.expressions.filter(
317
- /** @type {(expression: babel.types.Expression | babel.types.TSType) => expression is babel.types.Expression} */
318
- (expression) => t.isExpression(expression)
319
+ (expression): expression is babelTypes.Expression =>
320
+ t.isExpression(expression)
319
321
  );
320
322
 
321
323
  let cssVariablesInlineStyle;
@@ -397,7 +399,7 @@ module.exports = function (babel, options) {
397
399
  cssVariablesInlineStyle.properties.push(
398
400
  t.objectProperty(
399
401
  t.stringLiteral(cssVariableName),
400
- /** @type {babel.types.Expression} */ (transformedExpression)
402
+ transformedExpression
401
403
  )
402
404
  );
403
405
  }
@@ -455,18 +457,19 @@ module.exports = function (babel, options) {
455
457
  },
456
458
  },
457
459
  };
458
- };
460
+ }
459
461
 
460
- class InvalidPositionError extends Error {
462
+ export class InvalidPositionError extends Error {
461
463
  /**
462
464
  * Add the expression code that caused the error to the message
463
465
  * for better debugging
464
- * @param {string} message
465
- * @param {import("@babel/types").Expression} expression
466
- * @param {import("@babel/core").BabelFile} file
467
- * @param {string} [recommendedFix]
468
466
  */
469
- constructor(message, expression, file, recommendedFix) {
467
+ constructor(
468
+ message: string,
469
+ expression: babelTypes.Expression,
470
+ file: BabelFile,
471
+ recommendedFix?: string
472
+ ) {
470
473
  let errorText = message;
471
474
  const line = expression.loc?.start.line ?? -1;
472
475
  if (line !== -1) {
@@ -484,5 +487,3 @@ class InvalidPositionError extends Error {
484
487
  super(errorText);
485
488
  }
486
489
  }
487
-
488
- module.exports.InvalidPositionError = InvalidPositionError;
@@ -1,29 +1,27 @@
1
- /// @ts-check
2
- const getYakImports = require("./lib/getYakImports.cjs");
3
- const babel = require("@babel/core");
4
- const quasiClassifier = require("./lib/quasiClassifier.cjs");
5
- const localIdent = require("./lib/localIdent.cjs");
6
- const replaceQuasiExpressionTokens = require("./lib/replaceQuasiExpressionTokens.cjs");
7
- const getStyledComponentName = require("./lib/getStyledComponentName.cjs");
8
- const getCssName = require("./lib/getCssName.cjs");
9
- const murmurhash2_32_gc = require("./lib/hash.cjs");
10
- const { relative } = require("path");
11
- const {
12
- getConstantName,
13
- getConstantValues,
14
- } = require("./lib/getConstantValues.cjs");
15
-
16
- /**
17
- * @typedef {{ selector: string, hasParent: boolean, quasiCode: Array<{ insideCssValue: boolean, code: string }>, cssPartExpressions: { [key: number]: CssPartExpression[]} }} CssPartExpression
18
- * A CssPartExpression is the css code block for each tagged template expression
19
- */
1
+ import getYakImports from "./lib/getYakImports.js";
2
+ import babel, { NodePath } from "@babel/core";
3
+ import quasiClassifier from "./lib/quasiClassifier.js";
4
+ import localIdent from "./lib/localIdent.js";
5
+ import replaceQuasiExpressionTokens from "./lib/replaceQuasiExpressionTokens.js";
6
+ import getStyledComponentName from "./lib/getStyledComponentName.js";
7
+ import getCssName from "./lib/getCssName.js";
8
+ import murmurhash2_32_gc from "./lib/hash.js";
9
+ import { relative } from "path";
10
+ import { getConstantName, getConstantValues } from "./lib/getConstantValues.js";
11
+ import { Identifier, TaggedTemplateExpression } from "@babel/types";
12
+
13
+ // A CssPartExpression is the css code block for each tagged template expression
14
+ type CssPartExpression = {
15
+ selector: string;
16
+ hasParent: boolean;
17
+ quasiCode: Array<{ insideCssValue: boolean; code: string }>;
18
+ cssPartExpressions: { [key: number]: CssPartExpression[] };
19
+ };
20
20
 
21
- /**
22
- * @param {string} source
23
- * @this {any}
24
- * @returns {Promise<string>}
25
- */
26
- module.exports = async function cssLoader(source) {
21
+ export default async function cssLoader(
22
+ this: any,
23
+ source: string
24
+ ): Promise<string> {
27
25
  const { rootContext, resourcePath } = this;
28
26
 
29
27
  /** .yak files are constant definition files */
@@ -34,8 +32,7 @@ module.exports = async function cssLoader(source) {
34
32
  // However .yak files inside .yak files are not be compiled
35
33
  // to avoid performance overhead
36
34
  const importedYakConstants = isYakFile ? [] : getYakImports(source);
37
- /** @type {Record<string, unknown>} */
38
- const replaces = {};
35
+ const replaces: Record<string, unknown> = {};
39
36
  await Promise.all(
40
37
  importedYakConstants.map(async ({ imports, from }) => {
41
38
  const constantValues = await this.importModule(from, {
@@ -65,8 +62,12 @@ module.exports = async function cssLoader(source) {
65
62
 
66
63
  const { types: t } = babel;
67
64
 
68
- /** @type {{css?: string, styled?: string, attrs?: "attrs", keyframes?: string}} */
69
- const localVarNames = {
65
+ const localVarNames: {
66
+ css: string | undefined;
67
+ styled: string | undefined;
68
+ attrs: string;
69
+ keyframes: string | undefined;
70
+ } = {
70
71
  css: undefined,
71
72
  styled: undefined,
72
73
  attrs: "attrs",
@@ -78,28 +79,23 @@ module.exports = async function cssLoader(source) {
78
79
  *
79
80
  * Babel iterates over the full TaggedLiteralExpression before it iterates over their children
80
81
  * To keep the order as written in the original code the code fragments are stored in an ordered map
81
- * @type {Map<import("@babel/core").NodePath<import("@babel/types").TaggedTemplateExpression>, CssPartExpression>}
82
82
  */
83
- const cssParts = new Map();
83
+ const cssParts = new Map<
84
+ NodePath<TaggedTemplateExpression>,
85
+ CssPartExpression
86
+ >();
84
87
 
85
88
  let index = 0;
86
89
  let varIndex = 0;
87
- /** @type {string | null} */
88
- let hashedFile = null;
89
-
90
- /** @type {Map<string, string>} */
91
- const variableNameToStyledClassName = new Map();
90
+ let hashedFile: string | null = null;
92
91
 
93
- /** @type {Map<string, number | string | null>} */
94
- let topLevelConstBindings = new Map();
92
+ const variableNameToStyledClassName = new Map<string, string>();
93
+ let topLevelConstBindings = new Map<string, number | string | null>();
95
94
 
96
95
  babel.traverse(ast, {
97
96
  Program(path) {
98
97
  topLevelConstBindings = getConstantValues(path, t);
99
98
  },
100
- /**
101
- * @param {import("@babel/core").NodePath<import("@babel/types").ImportDeclaration>} path
102
- */
103
99
  ImportDeclaration(path) {
104
100
  const node = path.node;
105
101
  if (node.source.value !== "next-yak") {
@@ -115,9 +111,8 @@ module.exports = async function cssLoader(source) {
115
111
  return;
116
112
  }
117
113
 
118
- const importSpecifier = /** @type {babel.types.Identifier} */ (
119
- specifier.imported
120
- );
114
+ const importSpecifier =
115
+ /** @type {babel.types.Identifier} */ specifier.imported;
121
116
  const localSpecifier = specifier.local || importSpecifier;
122
117
  if (
123
118
  importSpecifier.name === "styled" ||
@@ -137,36 +132,31 @@ module.exports = async function cssLoader(source) {
137
132
 
138
133
  const isCssLiteral =
139
134
  t.isIdentifier(tag) &&
140
- /** @type {babel.types.Identifier} */ (tag).name === localVarNames.css;
135
+ /** @type {babel.types.Identifier} */ tag.name === localVarNames.css;
141
136
 
142
137
  const isKeyFrameLiteral =
143
138
  t.isIdentifier(tag) &&
144
- /** @type {babel.types.Identifier} */ (tag).name ===
139
+ /** @type {babel.types.Identifier} */ tag.name ===
145
140
  localVarNames.keyframes;
146
141
 
147
142
  const isStyledLiteral =
148
143
  t.isMemberExpression(tag) &&
149
144
  t.isIdentifier(
150
- /** @type {babel.types.MemberExpression} */ (tag).object
145
+ /** @type {babel.types.MemberExpression} */ tag.object
151
146
  ) &&
152
- /** @type {babel.types.Identifier} */ (
153
- /** @type {babel.types.MemberExpression} */ (tag).object
154
- ).name === localVarNames.styled;
147
+ /** @type {babel.types.Identifier} */ /** @type {babel.types.MemberExpression} */ tag
148
+ .object.name === localVarNames.styled;
155
149
 
156
150
  const isStyledCall =
157
151
  t.isCallExpression(tag) &&
158
- t.isIdentifier(
159
- /** @type {babel.types.CallExpression} */ (tag).callee
160
- ) &&
161
- /** @type {babel.types.Identifier} */ (
162
- /** @type {babel.types.CallExpression} */ (tag).callee
163
- ).name === localVarNames.styled;
152
+ t.isIdentifier(/** @type {babel.types.CallExpression} */ tag.callee) &&
153
+ /** @type {babel.types.Identifier} */ /** @type {babel.types.CallExpression} */ tag
154
+ .callee.name === localVarNames.styled;
164
155
 
165
156
  const isAttrsCall =
166
157
  t.isCallExpression(tag) &&
167
158
  t.isMemberExpression(tag.callee) &&
168
- /** @type {babel.types.Identifier} */ (tag.callee.property).name ===
169
- "attrs";
159
+ (tag.callee.property as Identifier).name === "attrs";
170
160
 
171
161
  if (
172
162
  !isCssLiteral &&
@@ -213,8 +203,7 @@ module.exports = async function cssLoader(source) {
213
203
  isKeyFrameLiteral ? "keyframes" : "selector"
214
204
  );
215
205
 
216
- /** @type {CssPartExpression} */
217
- const currentCssParts = {
206
+ const currentCssParts: CssPartExpression = {
218
207
  quasiCode: [],
219
208
  cssPartExpressions: [],
220
209
  selector: !parentLocation
@@ -223,7 +212,10 @@ module.exports = async function cssLoader(source) {
223
212
  hasParent: Boolean(parentLocation),
224
213
  };
225
214
  const parentCssParts =
226
- parentLocation && cssParts.get(parentLocation.parent);
215
+ parentLocation &&
216
+ cssParts.get(
217
+ parentLocation.parent as NodePath<TaggedTemplateExpression>
218
+ );
227
219
  cssParts.set(path, currentCssParts);
228
220
  if (parentCssParts) {
229
221
  parentCssParts.cssPartExpressions[parentLocation.currentIndex] ||= [];
@@ -313,31 +305,25 @@ module.exports = async function cssLoader(source) {
313
305
  );
314
306
 
315
307
  return mergeCssPartExpression(rootCssParts).trim();
316
- };
308
+ }
317
309
 
318
310
  /**
319
311
  * In jscode slashes are escaped however in css code they are not
320
312
  * e.g. in javascript `:before { content: "\\f0c9"; }` would be `:before { content: "\f0c9"; }` in css
321
313
  * slashes are still possible with `:before { content: "\\\\"; }`
322
- * @param {string} code
323
314
  */
324
- const unEscapeCssCode = (code) => code.replace(/\\\\/gi, "\\");
315
+ const unEscapeCssCode = (code: string) => code.replace(/\\\\/gi, "\\");
325
316
 
326
317
  /**
327
318
  * Searches the closest parent TaggedTemplateExpression using a name from localNames
328
319
  * Returns the location inside this parent
329
- *
330
- * @param {import("@babel/core").NodePath<import("@babel/types").TaggedTemplateExpression>} path
331
- * @param {{ css?: string , styled?: string }} localNames
332
320
  */
333
321
  const getClosestTemplateLiteralExpressionParentPath = (
334
- path,
335
- { css, styled }
322
+ path: NodePath<TaggedTemplateExpression>,
323
+ { css, styled }: { css?: string; styled?: string }
336
324
  ) => {
337
- /** @type {import("@babel/core").NodePath} */
338
- let grandChild = path;
339
- /** @type {import("@babel/core").NodePath} */
340
- let child = path;
325
+ let grandChild: NodePath = path;
326
+ let child: NodePath = path;
341
327
  let parent = path.parentPath;
342
328
  const t = babel.types;
343
329
  while (parent) {
@@ -345,28 +331,23 @@ const getClosestTemplateLiteralExpressionParentPath = (
345
331
  const tag = parent.node.tag;
346
332
  const isCssLiteral =
347
333
  t.isIdentifier(tag) &&
348
- /** @type {babel.types.Identifier} */ (tag).name === css;
334
+ /** @type {babel.types.Identifier} */ tag.name === css;
349
335
  const isStyledLiteral =
350
336
  t.isMemberExpression(tag) &&
351
337
  t.isIdentifier(
352
- /** @type {babel.types.MemberExpression} */ (tag).object
338
+ /** @type {babel.types.MemberExpression} */ tag.object
353
339
  ) &&
354
- /** @type {babel.types.Identifier} */ (
355
- /** @type {babel.types.MemberExpression} */ (tag).object
356
- ).name === styled;
340
+ /** @type {babel.types.Identifier} */ /** @type {babel.types.MemberExpression} */ tag
341
+ .object.name === styled;
357
342
  const isStyledCall =
358
343
  t.isCallExpression(tag) &&
359
- t.isIdentifier(
360
- /** @type {babel.types.CallExpression} */ (tag).callee
361
- ) &&
362
- /** @type {babel.types.Identifier} */ (
363
- /** @type {babel.types.CallExpression} */ (tag).callee
364
- ).name === styled;
344
+ t.isIdentifier(/** @type {babel.types.CallExpression} */ tag.callee) &&
345
+ /** @type {babel.types.Identifier} */ /** @type {babel.types.CallExpression} */ tag
346
+ .callee.name === styled;
365
347
  const isAttrsCall =
366
348
  t.isCallExpression(tag) &&
367
349
  t.isMemberExpression(tag.callee) &&
368
- /** @type {babel.types.Identifier} */ (tag.callee.property).name ===
369
- "attrs";
350
+ (tag.callee.property as Identifier).name === "attrs";
370
351
  if (isCssLiteral || isStyledLiteral || isStyledCall || isAttrsCall) {
371
352
  if (
372
353
  !t.isTemplateLiteral(child.node) ||
@@ -377,9 +358,7 @@ const getClosestTemplateLiteralExpressionParentPath = (
377
358
  const currentIndex = child.node.expressions.indexOf(grandChild.node);
378
359
  return {
379
360
  parent:
380
- /** @type {import("@babel/core").NodePath<import("@babel/types").TaggedTemplateExpression>} */ (
381
- parent
382
- ),
361
+ /** @type {import("@babel/core").NodePath<import("@babel/types").TaggedTemplateExpression>} */ parent,
383
362
  currentIndex,
384
363
  };
385
364
  }
@@ -396,11 +375,11 @@ const getClosestTemplateLiteralExpressionParentPath = (
396
375
 
397
376
  /**
398
377
  * depthFirst traversal of the css parts
399
- * @param {CssPartExpression[]} cssPartExpression
400
- * @param {number} [level]
401
- * @returns
402
378
  */
403
- const mergeCssPartExpression = (cssPartExpression, level = 0) => {
379
+ const mergeCssPartExpression = (
380
+ cssPartExpression: CssPartExpression[],
381
+ level = 0
382
+ ) => {
404
383
  let css = "";
405
384
  for (const { quasiCode, cssPartExpressions, selector } of cssPartExpression) {
406
385
  let cssPart = "";
@@ -426,7 +405,4 @@ const mergeCssPartExpression = (cssPartExpression, level = 0) => {
426
405
  return css;
427
406
  };
428
407
 
429
- /**
430
- * @param {string} str
431
- */
432
- const trimNewLines = (str) => str.replace(/^\s*\n+|\s+$/g, "");
408
+ const trimNewLines = (str: string) => str.replace(/^\s*\n+|\s+$/g, "");
@@ -1,16 +1,13 @@
1
+ import type { types as babelTypes } from "@babel/core";
2
+
1
3
  /**
2
4
  * Extracts the css unit from a css string and checks if it is a valid CSS unit
3
- *
4
- * @param {string} cssUnit
5
- * @param {babel.types.Expression} expression
6
- * @param {Set<string>} runtimeInternalHelpers
7
- * @param {import("@babel/types")} t
8
5
  */
9
6
  const appendCssUnitToExpressionValue = (
10
- cssUnit,
11
- expression,
12
- runtimeInternalHelpers,
13
- t
7
+ cssUnit: string,
8
+ expression: babelTypes.Expression,
9
+ runtimeInternalHelpers: Set<string>,
10
+ t: typeof babelTypes
14
11
  ) => {
15
12
  if (expression.type === "ArrowFunctionExpression") {
16
13
  if (expression.body.type !== "BlockStatement") {
@@ -48,4 +45,4 @@ const appendCssUnitToExpressionValue = (
48
45
  return callExpression;
49
46
  };
50
47
 
51
- module.exports = appendCssUnitToExpressionValue;
48
+ export default appendCssUnitToExpressionValue;
@@ -1,13 +1,12 @@
1
- // @ts-check
2
-
3
- /** @typedef {import("@babel/types")} babel */
1
+ import type { NodePath, types as babelTypes } from "@babel/core";
4
2
 
5
3
  /**
6
4
  * Returns the name of the expression
7
- * @param {babel.types.Expression} expression
8
- * @param {import("@babel/types")} t
9
5
  */
10
- const getConstantName = (expression, t) => {
6
+ export const getConstantName = (
7
+ expression: babelTypes.Expression,
8
+ t: typeof babelTypes
9
+ ) => {
11
10
  // e.g. styled.div`color: ${x};`
12
11
  if (t.isIdentifier(expression)) {
13
12
  // e.g. x
@@ -38,13 +37,12 @@ const getConstantName = (expression, t) => {
38
37
 
39
38
  /**
40
39
  * Extracts all top level constant values from a program path
41
- *
42
- * @param {babel.NodePath<babel.types.Program>} path
43
- * @param {import("@babel/types")} t
44
40
  */
45
- function getConstantValues(path, t) {
46
- /** @type {Map<string, string | number | null>} */
47
- const topLevelConstBindings = new Map();
41
+ export function getConstantValues(
42
+ path: NodePath<babelTypes.Program>,
43
+ t: typeof babelTypes
44
+ ) {
45
+ const topLevelConstBindings = new Map<string, string | number | null>();
48
46
  const bindings = Object.entries(path.scope.bindings);
49
47
  for (const [name, binding] of bindings) {
50
48
  if (binding.kind === "module") {
@@ -76,8 +74,3 @@ function getConstantValues(path, t) {
76
74
  }
77
75
  return topLevelConstBindings;
78
76
  }
79
-
80
- module.exports = {
81
- getConstantValues,
82
- getConstantName,
83
- };