Skip to content

Commit ffaedfc

Browse files
authored
Black, Prettier fixes (streamlit#187)
Some quality of life improvements for code formatting. - Added new make rules `make pyformat` and `make jsformat`, which run Black/Prettier on all source files. (These aren't used in any precommit hooks or CircleCI jobs, but I've found myself wanting to run the commands manually sometimes.) - `scripts/format.sh` no longer tries to format deleted Python files (previously, if you had a deleted file in staged files, format.sh would fail).
1 parent 97e8e0f commit ffaedfc

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

Makefile

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ else
5959
endif
6060

6161
.PHONY: pylint
62-
# Run Python linter.
62+
# Run "black", our Python formatter, to verify that our source files
63+
# are properly formatted. Does not modify any files. Returns with a non-zero
64+
# status if anything is not properly formatted. (This isn't really
65+
# "linting"; we're not checking anything but code style.)
6366
pylint:
64-
# It requires Python 3.6.0+ to run but you can reformat
65-
# Python 2 code with it, too.
67+
@# Black requires Python 3.6+ to run (but you can reformat
68+
@# Python 2 code with it, too).
6669
if command -v "black" > /dev/null; then \
6770
black --check docs/ ; \
6871
black --check examples/ ; \
@@ -71,6 +74,20 @@ pylint:
7174
black --check e2e/scripts/ ; \
7275
fi
7376

77+
.PHONY: pyformat
78+
# Run "black", our Python formatter, to fix any source files that are not
79+
# properly formatted.
80+
pyformat:
81+
@# Black requires Python 3.6+ to run (but you can reformat
82+
@# Python 2 code with it, too).
83+
if command -v "black" > /dev/null; then \
84+
black docs/ ; \
85+
black examples/ ; \
86+
black lib/streamlit/ --exclude=/*_pb2.py$/ ; \
87+
black lib/tests/ --exclude=compile_error.py ; \
88+
black e2e/scripts/ ; \
89+
fi
90+
7491
.PHONY: pytest
7592
# Run Python unit tests.
7693
pytest:
@@ -234,10 +251,11 @@ scssvars: react-init
234251
) > src/autogen/scssVariables.ts
235252

236253
.PHONY: jslint
237-
# Lint the JS code.
254+
# Lint the JS code. Saves results to test-reports/eslint/eslint.xml.
238255
jslint:
239256
@# max-warnings 0 means we'll exit with a non-zero status on any lint warning
240-
@# HK: I'm removing `max-warnings 0` out, until we convert all our JavaScript files to TypeScript
257+
@# HK: I'm removing `max-warnings 0` until we convert all our JavaScript
258+
@# files to TypeScript.
241259
cd frontend; \
242260
./node_modules/.bin/eslint \
243261
--ext .js \
@@ -249,6 +267,13 @@ jslint:
249267
--output-file test-reports/eslint/eslint.xml \
250268
./src
251269

270+
.PHONY: jsformat
271+
# Runs "Prettier" on our JavaScript and TypeScript code to fix formatting
272+
# issues.
273+
jsformat:
274+
yarn --cwd "frontend" pretty-quick \
275+
--pattern "**/*.*(js|jsx|ts|tsx)"
276+
252277
.PHONY: jstest
253278
# Run JS unit tests.
254279
jstest:

scripts/format.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ until [ -d .git ]; do cd ..; done
44
# Run Prettier on the staged files
55
yarn --cwd "frontend" pretty-quick --staged
66

7-
# Run Black on the staged files (only if it exists)
8-
# It requires Python 3.6.0+ to run but you can reformat
9-
# Python 2 code with it, too.
7+
# If Black is installed, run it on the staged files. (Black requires
8+
# Python 3.6+, but you can reformat Python 2 code with it).
9+
# "--diff-filter=ACMR" only lists files that are [A]dded, [C]opied, [M]odified,
10+
# or [R]enamed; we don't want to try to format files that have been deleted.
1011
if command -v "black" > /dev/null; then
11-
git diff --name-only --cached | grep -E "\.pyi?$" | xargs black
12+
git diff --diff-filter=ACMR --name-only --cached | grep -E "\.pyi?$" | xargs black
1213
fi

0 commit comments

Comments
 (0)