MCPcopy
hub / github.com/CesiumGS/cesium / fixTypescriptDefinitionsSource

Function fixTypescriptDefinitionsSource

gulpfile.js:957–1000  ·  view source on GitHub ↗

* Fix the typescript definitions output to match what we need. * * - Change declare => export since we are wrapping everything in a namespace * - Change CesiumMath => Math to help avoid conflicts with the native Math class * - Fix up the WebGLConstants aliasing by simply unquoting the strings.

(source)

Source from the content-addressed store, hash-verified

955 * @returns The modified source
956 */
957function fixTypescriptDefinitionsSource(source) {
958 return (
959 source
960 .replace(/^declare /gm, "export ")
961 .replace(/module "Math"/gm, "namespace Math")
962 .replace(/CesiumMath/gm, "Math")
963 .replace(/Number\[]/gm, "number[]") // Workaround https://github.com/englercj/tsd-jsdoc/issues/117
964 .replace(/String\[]/gm, "string[]")
965 .replace(/Boolean\[]/gm, "boolean[]")
966 .replace(/Object\[]/gm, "object[]")
967 .replace(/<Number>/gm, "<number>")
968 .replace(/<String>/gm, "<string>")
969 .replace(/<Boolean>/gm, "<boolean>")
970 .replace(/<Object>/gm, "<object>")
971 .replace(
972 /= "WebGLConstants\.(.+)"/gm,
973 // eslint-disable-next-line no-unused-vars
974 (match, p1) => `= WebGLConstants.${p1}`,
975 )
976 // Strip const enums which can cause errors - https://www.typescriptlang.org/docs/handbook/enums.html#const-enum-pitfalls
977 .replace(/^(\s*)(export )?const enum (\S+) {(\s*)$/gm, "$1$2enum $3 {$4")
978 // Replace JSDoc generation version of defined with an improved version using TS type predicates
979 .replace(
980 /\n?export function defined\(value: any\): boolean;/gm,
981 `\n${readFileSync("./packages/engine/Source/Core/defined.d.ts")
982 .toString()
983 .replace(/\n*\/\*.*?\*\/\n*/gms, "")
984 .replace("export default", "export")}`,
985 )
986 // Replace JSDoc generation version of Check with one that asserts the type of variables after called
987 .replace(
988 /\/\*\*[\*\s\w]*?\*\/\nexport const Check: any;/m,
989 `\n${readFileSync("./packages/engine/Source/Core/Check.d.ts")
990 .toString()
991 .replace(/export default.*\n?/, "")
992 .replace("const Check", "export const Check")}`,
993 )
994 // Fix https://github.com/CesiumGS/cesium/issues/10498 so we can use the rest parameter expand tuple
995 .replace(
996 "raiseEvent(...arguments: Parameters<Listener>[]): void;",
997 "raiseEvent(...arguments: Parameters<Listener>): void;",
998 )
999 );
1000}
1001
1002/**
1003 * Generates TypeScript definition file (.d.ts) for a package.

Callers 2

Calls 1

toStringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…