-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fei5815.2.workaround] Add check for improper cross-package type defi…
…nition references
- Loading branch information
1 parent
fd44c99
commit 5f5885a
Showing
2 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env -S node -r @swc-node/register | ||
/* eslint-disable no-console */ | ||
/** | ||
* Check type definition files for anything that doesn't look right. | ||
* | ||
* If our packages don't export all the types that other packages reference, | ||
* even indirectly, then their type definitions will import them from the source | ||
* files instead. Since we don't want to ship source code in every package, | ||
* we want to guard against this. | ||
* | ||
* This script should be run after `yarn build:types`. It will scan the type | ||
* definitions of each package for any types that are being incorrectly | ||
* imported from other the source code of other packages, and flag them, | ||
* exiting with a non-zero status code if any are found. | ||
*/ | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
import * as fglob from "fast-glob"; | ||
|
||
const rootDir = path.join(__dirname, ".."); | ||
const packagesDir = path.join(rootDir, "packages"); | ||
|
||
// Find all the type definition files in the packages dist directories. | ||
const typeDefinitionFiles = fglob.sync("**/*.d.ts", { | ||
cwd: packagesDir, | ||
onlyFiles: true, | ||
}); | ||
|
||
let foundErrors = false; | ||
// Scan each one for any imports of types from source. | ||
for (const typeDefinitionFile of typeDefinitionFiles) { | ||
const regexpImportSrc = | ||
/import\(".+\/(wonder-stuff-.+)\/src\/.+"\)\.([a-zA-Z]+)/g; | ||
const content = fs.readFileSync( | ||
path.join(packagesDir, typeDefinitionFile), | ||
"utf-8", | ||
); | ||
const lines = content.split("\n"); | ||
let match; | ||
for (let line = 0; line < lines.length; line++) { | ||
while ((match = regexpImportSrc.exec(lines[line]))) { | ||
foundErrors = true; | ||
const position = match.index; | ||
const lineNo = line + 1; | ||
const refPath = path.join("packages", typeDefinitionFile); | ||
console.error(`${refPath}:${lineNo}:${position}`); | ||
console.error( | ||
` Incorrectly imported type ${match[2]} from ${match[1]} source`, | ||
); | ||
console.error( | ||
` Update the package ${match[1]} to export the type ${match[2]}\n`, | ||
); | ||
} | ||
} | ||
} | ||
|
||
if (foundErrors) { | ||
process.exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters