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

Format and lint #894

Merged
merged 19 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
18 changes: 1 addition & 17 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,17 +1 @@
alphametics
connect
diamond
food-chain
forth
grade-school
grains
list-ops
nth-prime
palindrome-products
queen-attack
rational-numbers
saddle-points
secret-handshake
sublist
twelve-days
variable-length-quantity

2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto
* text=auto eol=lf
3 changes: 2 additions & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Maintainers
# Maintainers
config/maintainers.json @exercism/maintainers-admin

# Code owners
.github/CODEOWNERS @exercism/maintainers-admin
.github/workflows @exercism/javascript
6 changes: 3 additions & 3 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
name: "codeql"
name: 'codeql'

on:
push:
Expand All @@ -12,7 +12,7 @@ on:
# The branches below must be a subset of the branches above
branches: [master]
schedule:
- cron: "0 14 * * 5"
- cron: '0 14 * * 5'

jobs:
analyze:
Expand All @@ -23,7 +23,7 @@ jobs:
matrix:
# Override automatic language detection by changing the below list
# Supported options are ['csharp', 'cpp', 'go', 'java', 'javascript', 'python']
language: ["javascript"]
language: ['javascript']
# Learn more...
# https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#overriding-automatic-language-detection

Expand Down
110 changes: 110 additions & 0 deletions .github/workflows/format-code.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
name: 'Format code'

on:
issue_comment:
types: [created]

jobs:
format:
name: 'Format code'
runs-on: ubuntu-latest
if: github.event.issue.pull_request != '' && contains(github.event.comment.body, '/format')
steps:
- name: 'Post acknowledgement that it will format code'
continue-on-error: true # Never fail the build if this fails
uses: actions/github-script@6e5ee1dc1cb3740e5e5e76ad668e3f526edbfe45 # 2.0.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'The "Format code" action has started running.'
})

- name: 'Download PR data'
run: |
PR_DATA="/tmp/pr.json"

jq -r ".issue.pull_request.url" "$GITHUB_EVENT_PATH" | \
xargs curl --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' -o "$PR_DATA" --url

- name: 'Check fork status'
id: fork_status
run: |
IS_FORK="$(jq '.head.repo.fork' "/tmp/pr.json")"
echo "::set-output name=fork::$IS_FORK"

- name: 'Setup SSH deploy key'
if: steps.fork_status.outputs.fork == 'false'
run: |
mkdir ~/.ssh
echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519

- name: 'Checkout code'
run: |
PR_DATA="/tmp/pr.json"

HEAD_REF=$(jq -r ".head.ref" "$PR_DATA")

if [ ${{ steps.fork_status.outputs.fork }} == "false" ]; then
echo "::debug::Setting up repo using SSH"
HEAD_REPO=$(jq -r '.head.repo.ssh_url' "$PR_DATA")
else
echo "::debug::Setting up repo using HTTPS"
HEAD_REPO=$(jq -r '.head.repo.clone_url | sub("https://"; "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@")' "$PR_DATA")
fi

git clone $HEAD_REPO .
git checkout -b "$HEAD_REF" "origin/$HEAD_REF"

- name: 'Format code'
run: ./bin/format.sh
env:
EXERCISM_PRETTIER_VERSION: '2.1.2'

- name: 'Commit formatted code'
run: |
# Check if there is nothing to commit (i.e. no formatting changes made)
if [ -z "$(git status --porcelain)" ]; then
echo "Code is already formatted correctly"
exit 0
fi

# Setup the git user (required to commit anything)
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"

# Commit the changes made by prettier
git add .
git commit -m "[CI] Format code"
git push

- name: 'Post acknowledgement that it has formatted the code'
continue-on-error: true # Never fail the build if this fails
uses: actions/github-script@6e5ee1dc1cb3740e5e5e76ad668e3f526edbfe45 # 2.0.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'The "Format code" action has finished running.'
})

- name: 'Post reminder to trigger build manually'
continue-on-error: true # Never fail the build if this fails
if: steps.fork_status.outputs.fork == 'true'
uses: actions/github-script@6e5ee1dc1cb3740e5e5e76ad668e3f526edbfe45 # 2.0.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'For security reasons, `/format` does not trigger CI builds when the PR has been submitted from a fork. If checks were not passing due to code format, trigger a build to make the required checks pass, through one of the following ways:\n\n- Push an empty commit to this branch: `git commit --allow-empty -m "Trigger builds"`.\n- Close and reopen the PR.\n- Push a regular commit to this branch.'
})
18 changes: 18 additions & 0 deletions .github/workflows/verify-code-formatting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: javascript / format

on:
push:
pull_request:
workflow_dispatch:

jobs:
verify:
runs-on: ubuntu-latest
steps:
- name: 'Checkout code'
uses: actions/checkout@v2

- name: 'Verify formatting of all files'
run: ./bin/check-formatting.sh
env:
EXERCISM_PRETTIER_VERSION: '2.2.1'
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exercises/**/README.md
!/README.md
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": true,
"singleQuote": true,
"endOfLine": "lf"
}
60 changes: 39 additions & 21 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@ We welcome contributions of all sorts and sizes, from reporting issues to submit

---

- [Code of Conduct](#code-of-conduct)
- [Exercises](#exercises)
- [New exercise](#new-exercise)
- [Implementing existing exercise](#implementing-existing-exercise)
- [ Creating a track-specific exercise](#creating-a-track-specific-exercise)
- [Existing exercises](#existing-exercises)
- [Improving the README.md](#improving-the-readmemd)
- [Syncing the exercise](#syncing-the-exercise)
- [Improving or adding mentor notes](#improving-or-adding-mentor-notes)
- [Improving or adding automated test analyzers](#improving-or-adding-automated-test-analyzers)
- [Documentation](#documentation)
- [Tools](#tools)
- [Fetch `configlet`](#fetch-configlet)
- [Fetch `canonical_data_syncer`](#fetch-canonical-data-syncer)
- [Scripts](#scripts)
- [`lint`](#lint)
- [`test`](#test)
- [`sync`](#sync)
- [`checksum`](#checksum)
- [`ci-check`](#ci-check)
- [`ci`](#ci)
- [Contributing](#contributing)
- [Code of Conduct](#code-of-conduct)
- [Exercises](#exercises)
- [New exercise](#new-exercise)
- [Implementing existing exercise](#implementing-existing-exercise)
- [Creating a track-specific exercise](#creating-a-track-specific-exercise)
- [Existing exercises](#existing-exercises)
- [Improving the README.md](#improving-the-readmemd)
- [Syncing the exercise](#syncing-the-exercise)
- [Improving or adding mentor notes](#improving-or-adding-mentor-notes)
- [Improving or adding automated test analyzers](#improving-or-adding-automated-test-analyzers)
- [Documentation](#documentation)
- [Tools](#tools)
- [Fetch configlet](#fetch-configlet)
- [Fetch canonical data syncer](#fetch-canonical-data-syncer)
- [Scripts](#scripts)
- [`format`](#format)
- [`lint`](#lint)
- [`test`](#test)
- [`sync`](#sync)
- [`checksum`](#checksum)
- [`ci-check`](#ci-check)
- [`ci`](#ci)

---

Expand Down Expand Up @@ -70,6 +72,7 @@ If there is no such issue, you may open one. The baseline of work is as follows:
1. Run the tests locally, using `scripts/test`: `ASSIGNMENT=slug npx babel-node scripts/test`.
1. Run the linter locally, using `scripts/lint`: `ASSIGNMENT=slug npx babel-node scripts/lint`.
1. Create an entry in `config.json`: a unique _new_ UUID (you can use the `configlet uuid` tool to generate one, scroll down to [tools](#tools) to see how you can get it), give it a difficulty (should be similar to similar exercises), and make sure the _order_ of the file is sane. Currently the file is ordered first on core - non core, then on difficulty low to high, and finally lexographically.
1. Format the files, using `scripts/format`: `npx babel-node scripts/format`.

The final step is opening a Pull Request, with these items all checked off. Make sure the tests run and the linter is happy. It will run automatically on your PR.

Expand Down Expand Up @@ -128,6 +131,8 @@ You'll need LTS or higher NodeJS in order to contribute to the _code_ in this re
- `jest` to run all the test files on all example implementations
- `babel` to transpile everything so it works _regardless of your version of NodeJS_.

We also use `prettier` to format the files. **Prettier is _NOT_ installed when using `npm install`**, because the CI will enforce a certain version. Instead use `npx babel-node scripts/format` to run prettier. If you want to auto-format using your editor, match the version in the GitHub Workflow `verify-code-formatting.yml`.

### Fetch configlet

If you'd like to download [configlet][configlet], you can use the [`fetch-configlet`][bin-fetch-configlet] binary. It will run on Linux, Mac OSX and Windows, and download `configlet` to your local drive. Find more information about [configlet][configlet] [here][configlet].
Expand Down Expand Up @@ -155,6 +160,19 @@ If you'd like to download [`canonical_data_syncer`][canonical-data-syncer], you

We have various `scripts` for you in order to aid with maintaining and contributing to this repository.

#### `format`

```js
/*
* Run this script (from root directory): npx babel-node scripts/format
*
* This runs `prettier` on all applicable files, FORCES using the same version
* as the CI uses to check if the files have been formatted.
*/
```

Use this action to format all the files using the correct version of prettier. If you want your editor to do this automatically, make sure you install `prettier` (e.g. `npm install [email protected]`), where the version matches `.github/workflows/verify-code-formatting.yml`.

#### `lint`

```js
Expand Down
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contribute to this track. It also has a list of tools you can use, of which the

## Running the test suite

This runs `jest` tests for all sample solutions. This *does not* use the regular way to run `jest`, because the example solution files must be renamed to be imported correctly into the test files.
This runs `jest` tests for all sample solutions. This _does not_ use the regular way to run `jest`, because the example solution files must be renamed to be imported correctly into the test files.

```shell
npx babel-node scripts/test
Expand All @@ -36,18 +36,17 @@ ASSIGNMENT=two-fer npx babel-node scripts/test

## Related repositories

* [Website Copy][git-website-copy] (Mentor Notes)
* [The JavaScript Analyzer][git-javascript-analyzer] (Automated Code Analysis)
* [The JavaScript Test Runner][git-javascript-test-runner]
* [The TypeScript track][git-typescript]
* [The TypeScript Analyzer][git-typescript-analyzer]
- [Website Copy][git-website-copy] (Mentor Notes)
- [The JavaScript Analyzer][git-javascript-analyzer] (Automated Code Analysis)
- [The JavaScript Test Runner][git-javascript-test-runner]
- [The TypeScript track][git-typescript]
- [The TypeScript Analyzer][git-typescript-analyzer]

[web-exercism]: https://exercism.io
[git-configlet]: https://github.com/exercism/docs/blob/master/language-tracks/configuration/configlet.md
[bin-fetch-configlet]: https://github.com/exercism/javascript/blob/master/bin/fetch-configlet
[file-config]: https://github.com/exercism/javascript/blob/master/config.json
[file-contributing]: https://github.com/exercism/javascript/blob/master/CONTRIBUTING.md

[git-javascript]: https://github.com/exercism/javascript
[git-javascript-analyzer]: https://github.com/exercism/javascript-analyzer
[git-javascript-test-runner]: https://github.com/exercism/javascript-test-runner
Expand Down
8 changes: 4 additions & 4 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module.exports = {
presets: [
[
"@babel/preset-env",
'@babel/preset-env',
{
targets: {
node: "current",
node: 'current',
},
useBuiltIns: "entry",
useBuiltIns: 'entry',
corejs: 3,
},
],
],
plugins: ["@babel/plugin-syntax-bigint"],
plugins: ['@babel/plugin-syntax-bigint'],
};
3 changes: 3 additions & 0 deletions bin/check-formatting.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

npx "prettier@$EXERCISM_PRETTIER_VERSION" --check "**/*.{js,jsx,ts,tsx,css,sass,scss,html,json,md,yml}"
9 changes: 9 additions & 0 deletions bin/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

if [ -z "$EXERCISM_PRETTIER_VERSION" ]; then
echo "This script requires the EXERCISM_PRETTIER_VERSION variable to work."
echo "Please see https://github.com/exercism/v3/blob/master/docs/maintainers/style-guide.md for guidance."
exit 1
fi

npx "prettier@$EXERCISM_PRETTIER_VERSION" --write "**/*.{js,jsx,ts,tsx,css,sass,scss,html,json,md,yml}"
Loading