From a0ba4d1f5b6d4a4a052a88d5f3e90cf01b98d0d3 Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Thu, 11 Jul 2024 21:54:04 +0200 Subject: [PATCH] Fix types --- src/@types/index.d.ts | 8 ++++++++ src/global.d.ts | 32 ++++++++++++++++++++++++++++++++ src/utils/generateIconTokens.ts | 10 +++++++--- 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 src/global.d.ts diff --git a/src/@types/index.d.ts b/src/@types/index.d.ts index b78d2e85..b1fe21f2 100644 --- a/src/@types/index.d.ts +++ b/src/@types/index.d.ts @@ -17,3 +17,11 @@ limitations under the License. export type Theme = "light" | "light-hc" | "dark" | "dark-hc"; export type Platform = "web" | "android" | "ios"; + +declare module "prettier" { + export interface Options {} +} + +declare module "svgo" { + export interface Config {} +} diff --git a/src/global.d.ts b/src/global.d.ts new file mode 100644 index 00000000..124c2214 --- /dev/null +++ b/src/global.d.ts @@ -0,0 +1,32 @@ +/* +Copyright 2024 New Vector Ltd. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Stub declarations, as svgr types depend on these, but we don't have them installed +declare module "prettier" { + export interface Options {} +} + +declare module "svgo" { + export interface Config {} +} + +// This module doens't have a type definition +declare module "@babel/plugin-transform-react-jsx" { + import type { PluginItem } from "@babel/core"; + + const babelTransformReactJsx: PluginItem; + export default babelTransformReactJsx; +} diff --git a/src/utils/generateIconTokens.ts b/src/utils/generateIconTokens.ts index 5343d53d..b27d8827 100644 --- a/src/utils/generateIconTokens.ts +++ b/src/utils/generateIconTokens.ts @@ -19,13 +19,17 @@ import path from "node:path"; import { fileURLToPath } from "node:url"; import type { TransformOptions as BabelOptions } from "@babel/core"; -import generate from "@babel/generator"; +import generate_ from "@babel/generator"; import babelTransformReactJsx from "@babel/plugin-transform-react-jsx"; import t from "@babel/types"; import { type ConfigPlugin, transform as svgrTransform } from "@svgr/core"; import svgrPluginJsx from "@svgr/plugin-jsx"; import { camelCase, startCase } from "lodash-es"; +// Types for the default export of @babel/generator are wrong +const generate = (generate_ as unknown as { default: typeof generate_ }) + .default; + /** * Generates `icons/$icons.json` and React components off all the * SVG icons discovered in the `icons/` folder @@ -205,12 +209,12 @@ export default ${componentName}; ]); // Generate the index.cjs file - const cjsCode = generate.default(cjsProgram).code; + const cjsCode = generate(cjsProgram).code; await fs.writeFile(path.join(webOutput, "index.cjs"), cjsCode, "utf-8"); // Generate the index.js file const esmProgram = t.program(indexEsmStatements, [], "module"); - const esmCode = generate.default(esmProgram).code; + const esmCode = generate(esmProgram).code; await fs.writeFile(path.join(webOutput, "index.js"), esmCode, "utf-8"); // The index.d.ts is identical to the index.js as it only re-exports the icons