Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add extension check for relative imports #2055

Merged
merged 6 commits into from
Apr 8, 2024
24 changes: 22 additions & 2 deletions dangerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const RENDER_COMMENT = `// -- Render -------------------------------------------
const STATE_PROPERTIES_COMMENT = `// -- State & Properties -------------------------------- //`
const PRIVATE_COMMENT = `// -- Private ------------------------------------------- //`
const PACKAGE_VERSION = ConstantsUtil.VERSION
const RELATIVE_IMPORT_SAME_DIR = `'./`
const RELATIVE_IMPORT_PARENT_DIR = `'../`
const RELATIVE_IMPORT_EXTENSION = `.js'`

// -- Data --------------------------------------------------------------------
const { modified_files, created_files, deleted_files, diffForFile } = danger.git
Expand Down Expand Up @@ -265,10 +268,23 @@ async function checkScaffoldHtmlPackage() {
checkScaffoldHtmlPackage()

// -- Client(s) Package Checks ----------------------------------------------------
// -- Helper functions
const isRelativeImport = (addition: string | undefined) => {
const sameDir = addition?.includes(RELATIVE_IMPORT_SAME_DIR)
const parentDir = addition?.includes(RELATIVE_IMPORT_PARENT_DIR)
return sameDir || parentDir
}
const containsRelativeImportWithoutJSExtension = (addition: string | undefined) => {
const hasImportStatement = addition?.includes('import')
const lacksJSExtension = !addition?.includes(RELATIVE_IMPORT_EXTENSION)
const hasRelativePath = isRelativeImport(addition)

return hasImportStatement && lacksJSExtension && hasRelativePath
}
async function checkClientPackages() {
const wagmi_files = modified_files.filter(f => f.includes('/wagmi/'))
const client_files = modified_files.filter(f => /\/(wagmi|solana|ethers|ethers5)\//.test(f))

for (const f of wagmi_files) {
for (const f of client_files) {
const diff = await diffForFile(f)

if (diff?.added.includes("from '@web3modal/core")) {
Expand All @@ -278,6 +294,10 @@ async function checkClientPackages() {
if (diff?.added.includes("from '@web3modal/ui")) {
fail(`${f} is not allowed to import from @web3modal/ui`)
}

if (containsRelativeImportWithoutJSExtension(diff?.added)) {
fail(`${f} contains relative imports without .js extension`)
}
}
}
checkClientPackages()
Expand Down