Skip to content

Commit

Permalink
checks for relative imports
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabib committed Jan 10, 2025
1 parent 4785668 commit 942e8c1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ jobs:
git diff
exit 1
}
relative-imports:
runs-on: ubuntu-20.04
env:
DFX_NETWORK: mainnet
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Checks for the presence of relative imports
run: ./scripts/check-relative-imports
working-directory: ./frontend
spelling:
runs-on: ubuntu-20.04
steps:
Expand Down
57 changes: 57 additions & 0 deletions scripts/check-relative-imports
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bash
set -euo pipefail

# Checks for unauthorized relative imports in a file
# We skip two SvelteKit specific cases:
# 1. The $types import: SvelteKit generates virtual types for each page/layout that has to be imported like `./$types`
# 2. The home page route: Special case in our app that requires relative imports due to SvelteKit's routing structure
check_file_for_relative_imports() {
local file="$1"

# Skip the excepted file
if [[ "$file" == "src/routes/(app)/(home)/+page.svelte" ]]; then
return 0
fi

# Process the file line by line
in_style_block=false
while IFS= read -r line; do
# Check if we're entering or leaving a style block
if [[ $line =~ \<style ]]; then
in_style_block=true
elif [[ $line =~ \</style\> ]]; then
in_style_block=false
fi

# Skip if in style block
if [ "$in_style_block" = true ]; then
continue
fi

# Skip the $types import exception
if [[ $line =~ ^[[:space:]]*import[[:space:]]+type.*from[[:space:]]*[\"\']\.\/\$types[\"\'] ]]; then
continue
fi

# Check for relative imports
if [[ $line =~ ^[[:space:]]*import.*from[[:space:]]*[\"\'](\.\./|\./)[^\"\']+[\"\'] ]]; then
echo "Found relative import in $file:"
echo "$line"
return 1
fi
done <"$file"

return 0
}

TOP_DIR=$(git rev-parse --show-toplevel)
cd "$TOP_DIR/frontend"

has_errors=0
while IFS= read -r file; do
if ! check_file_for_relative_imports "$file"; then
has_errors=1
fi
done < <(find src -type f \( -name "*.svelte" -o -name "*.ts" \))

exit $has_errors

0 comments on commit 942e8c1

Please sign in to comment.