Skip to content

Commit

Permalink
add solhint and prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
anajuliabit committed Jun 3, 2024
1 parent f5fb3ed commit c69dd6b
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 2 deletions.
1 change: 0 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ env:

on:
workflow_dispatch:
pull_request:
push:
branches:
- "main"
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: test

on: workflow_dispatch
on:
workflow_dispatch:
push:
branches:
- "main"

env:
FOUNDRY_PROFILE: ci
Expand Down
52 changes: 52 additions & 0 deletions .husky/.pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash

# Temporary file to hold list of staged .sol files
STAGED_SOL_FILES=$(mktemp)
# Temporary file to hold list of all staged files for prettier
STAGED_FILES=$(mktemp)

# List staged .sol files ignoring deleted files
git diff --cached --name-status -- '*.sol' | grep -v '^D' | cut -f2- > "$STAGED_SOL_FILES"
# List all staged files ignoring deleted files
git diff --cached --name-status | grep -v '^D' | cut -f2- > "$STAGED_FILES"

# Run Solhint on staged .sol files, if any
if [ -s "$STAGED_SOL_FILES" ]; then
# If there are staged .sol files, run Solhint on them
SOLHINT_OUTPUT=$(cat "$STAGED_SOL_FILES" | xargs npx solhint --config ./.solhintrc)
SOLHINT_EXIT_CODE=$?

if [ $SOLHINT_EXIT_CODE -ne 0 ]; then
echo "Solhint errors detected:"
echo "$SOLHINT_OUTPUT"
rm "$STAGED_SOL_FILES" "$STAGED_FILES"
exit $SOLHINT_EXIT_CODE
else
# Re-add the .sol files to include any automatic fixes by Solhint
cat "$STAGED_SOL_FILES" | xargs git add
fi
fi

# Run Prettier and check for errors on staged files
if [ -s "$STAGED_FILES" ]; then
# Note: Using `--write` with Prettier to automatically fix formatting
PRETTIER_OUTPUT=$(cat "$STAGED_FILES" | xargs npx prettier --ignore-path .prettierignore --write)
PRETTIER_EXIT_CODE=$?

if [ $PRETTIER_EXIT_CODE -ne 0 ]; then
echo "Prettier formatting errors detected:"
echo "$PRETTIER_OUTPUT"
rm "$STAGED_SOL_FILES" "$STAGED_FILES"
exit $PRETTIER_EXIT_CODE
else
# Re-add the files to include any formatting changes by Prettier
cat "$STAGED_FILES" | xargs git add
fi
fi

# Clean up
rm "$STAGED_SOL_FILES" "$STAGED_FILES"

# If we reach this point, either there were no issues or only warnings.
# Warnings are allowed, so we exit successfully.
exit 0
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

./.husky/.pre-commit.sh
26 changes: 26 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# directories
broadcast
cache
coverage
lib
node_modules
out
.husky

# files
*.env
*.log
.DS_Store
lcov.info
package-lock.json
*.txt
*.toml
LICENSE
.solhintrc
.solhintignore
.prettierrc
.prettierignore
*.svg
.gitmodules
.gitignore
*.sh
21 changes: 21 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"plugins": ["prettier-plugin-solidity"],
"overrides": [
{
"files": "*.sol",
"options": {
"parser": "solidity-parse",
"printWidth": 80,
"tabWidth": 4,
"useTabs": false,
"singleQuote": false,
"bracketSpacing": false
}
}
],
"tabWidth": 4,
"printWidth": 80,
"trailingComma": "all",
"singleQuote": false,
"semi": true
}
2 changes: 2 additions & 0 deletions .solhintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib/
out/
13 changes: 13 additions & 0 deletions .solhintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{"extends": "solhint:recommended",
"rules": {
"compiler-version": ["error", ">=0.8.0"],
"func-visibility": ["error", { "ignoreConstructors": true }],
"named-parameters-mapping": "warn",
"no-console": "off",
"one-contract-per-file": "off",
"no-inline-assembly": "off",
"no-empty-blocks": "off",
"var-name-mixedcase": "off",
"no-global-import": "off"
}
}
44 changes: 44 additions & 0 deletions lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: "Lint"

env:
FOUNDRY_PROFILE: "ci"

on:
workflow_dispatch:
pull_request:
push:
branches:
- "main"

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: "Check out the repo"
uses: actions/checkout@v3
with:
submodules: recursive
- name: "Install Node.js"
uses: actions/setup-node@v3
with:
node-version: lts/*

- name: "Install the Node.js dependencies"
run: npm install

- name: Run linter and check for errors
id: lint
run: |
LINT_OUTCOME=$(npm run lint 2>&1 || true) # Prevent the step from failing immediately
echo "$LINT_OUTCOME"
echo "LINT_OUTCOME<<EOF" >> $GITHUB_ENV
echo "$LINT_OUTCOME" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
if echo "$LINT_OUTCOME" | grep -q " error "; then
echo "## Lint result" >> $GITHUB_STEP_SUMMARY
echo "❌ Failed due to errors" >> $GITHUB_STEP_SUMMARY
exit 1
else
echo "## Lint result" >> $GITHUB_STEP_SUMMARY
echo "✅ Passed or warnings found" >> $GITHUB_STEP_SUMMARY
fi
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "shutter-staking",
"description": "Shutter Staking and Delegating Smart Contracts",
"version": "1.0.0",
"devDependencies": {
"husky": "9.0.10",
"prettier": "3.0.0",
"prettier-plugin-solidity": "1.2.0",
"solhint": "4.5.2"
},
"scripts": {
"prepare": "husky",
"clean": "rm -rf cache out",
"build": "forge build",
"lint": "solhint --config ./.solhintrc --ignore-path .solhintignore '**/*.sol'",
"lint:write": "solhint --config ./.solhintrc --fix '**/*.sol'",
"prettier": "prettier --ignore-path .prettierignore --list-different './**/*'",
"prettier:write": "prettier --ignore-path .prettierignore --write './**/*'",
"test": "forge test",
"test:coverage": "forge coverage",
"test:coverage:report": "forge coverage --report lcov && genhtml lcov.info --branch-coverage --output-dir coverage"
}
}

0 comments on commit c69dd6b

Please sign in to comment.