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

Chore: Improve linting & auto-alphabetize translations #2294

Merged
merged 34 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
e3cb0c9
cleanup: lint codebase, update eslint recommended
Dec 3, 2021
20bb560
test: unecessary space
Dec 3, 2021
895f3e3
add eslint prettier
Dec 3, 2021
84965e7
another test
Dec 3, 2021
8cec420
bad commit format
Dec 3, 2021
49ed15c
bad commit format
Dec 3, 2021
87a7ecd
testing pre-commit hook and
Dec 3, 2021
8af6581
commit message
Dec 3, 2021
22a136d
trying again
Dec 3, 2021
6c511a6
chore: fixed bug by clearing node_modules and reinstalling
Dec 3, 2021
affef56
chore: autosort translation on save
Dec 4, 2021
19f4d20
docs: more info when a bad commit format is used
Dec 4, 2021
9c3dd93
chore: lint pattern match cleanup
Dec 4, 2021
fb5941c
chore: undo file update
Dec 4, 2021
8440503
chore: revert file update
Dec 4, 2021
f4ec97e
docs: add linting and formatting docs
Dec 5, 2021
573be50
docs: add git hooks link
Dec 6, 2021
7bb0bbc
chore: remove prettier from project
Dec 6, 2021
36be00b
chore: run e2e on precommit
Dec 17, 2021
5bd6c7b
chore: Merge remote-tracking branch 'origin' into chore/auto-alphabet…
Dec 21, 2021
66b5fa1
chore: switch to prepush
Dec 21, 2021
548b4ba
chore: update commit msg regex and docs
Dec 22, 2021
00640e8
chore: update commit msg
Dec 22, 2021
bef66d1
chore: localize image reference
Dec 22, 2021
e3b2b2e
chore: Merge branch 'master' of https://github.com/worldclassdev/near…
Dec 22, 2021
3b7b326
chore: remove commit msg length check
Dec 22, 2021
b00de19
chore: remove lint-staged, run commands on prepush
Dec 22, 2021
358cd3b
chore: update prepush command
Dec 22, 2021
5507fe7
chore(docs): remove reference to lint-staged
Dec 22, 2021
8b365df
chore: revert prettier formatting
Dec 23, 2021
f946e19
chore: revert prettier formatting
Dec 23, 2021
2555b94
chore: turn off eslint boolean cast rule
Dec 23, 2021
bbbd21a
chore: revert prettier formatting
Dec 23, 2021
b158169
chore: more eslint config refinement
Dec 23, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
# This script checks if the commit message follows a consistent format(usually useful for consistency and devops)
if ! head -1 "$1" | grep -qE "^(feat|fix|ci|chore|docs|test|style|refactor|perf|build|revert)(\(.+?\))?(!)?: .{1,}$"; then
echo "Aborting commit. Your commit message is invalid. See accepted format here: https://commitizen.github.io/cz-cli/" >&2
exit 1
fi
5 changes: 5 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
# This runs the prepush script on any modified packages
. "$(dirname "$0")/_/husky.sh"

npx lerna run --concurrency 1 --stream prepush --exclude-dependents
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
"scripts": {
"lint": "cd packages/frontend && yarn install --frozen-lockfile && yarn lint",
"build": "cd packages/frontend && yarn install --frozen-lockfile && yarn build",
"test": "cd packages/frontend && yarn install --frozen-lockfile && yarn test"
"test": "cd packages/frontend && yarn install --frozen-lockfile && yarn test",
"prepare": "husky install"
},
"devDependencies": {
"lerna": "^4.0.0"
"lerna": "^4.0.0",
"husky": "^7.0.0"
},
"dependencies": {
"@near-wallet/feature-flags": "^0.0.4"
Expand Down
3 changes: 3 additions & 0 deletions packages/frontend/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
dist/
build
node_modules
.github

6 changes: 5 additions & 1 deletion packages/frontend/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
module.exports = {
extends: 'react-app',
extends: ['react-app', 'eslint:recommended'],
rules: {
'jsx-a11y/no-access-key': 'off',
'no-useless-escape': 'off',
'semi': ['error', 'always'],
'no-console': 'off',
'no-extra-boolean-cast':'off',
'no-extra-semi':'off',
'no-irregular-whitespace':'off',
'import/order': [
'error',
{
Expand Down
56 changes: 56 additions & 0 deletions packages/frontend/docs/Linting-and-formatting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Linting and Formatting
gutsyphilip marked this conversation as resolved.
Show resolved Hide resolved
For consistency and predictable organization of code, this project uses [Eslint](https://eslint.org/) for linting.

## The Setup
The configuration for Eslint can be found at the root of this project:
- Eslint - `.eslintrc.js`

VSCode is configured to automatically fix fixable linting errors on save. See `.vscode/settings.json`.

Within `package.json`, there scripts that you can run to lint and autofix the entire codebase.

```
"lint": "eslint --ext .js --ext .jsx .",
"fix": "eslint --ext .js --ext .jsx . --fix",
```

## Git Commit hooks
Git commit hooks are a provision that allows us run custom scripts when specific events occur in the git workflow. [See more here](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks).

For this project, we are using the clientside commit hooks to automatically lint and alphabetize staged files before they get pushed.

## Auto-alphabetization of translation files
This is meant to help ensure consistency and ease of reference within the translation files. For this, we are making use of git hooks via a package called [husky](https://typicode.github.io/husky/#/). Husky allows us to configure a defined pre-push command which triggers the `prepush` script within modified packages. The `prepush` script in `packages/frontend/package.json` is configured to run a group of commands that lint and autofix, sort translation files as well as runs our unit tests.

Within `package.json`, the block below defines this.

```
"scripts": {
...,
"prepush": "concurrently \"yarn run fix\" \"cd src/translations && npx sort-json * --ignore-case true\" \"yarn run test\""
}
```

Two main things are ensured through this configuration:
- Staged translation files `src/translations/*.json"` get sorted in alphabetical order automatically using a package called [sort-json](https://github.com/kesla/sort-json)
- Staged `.js` and `.jsx` files get linted for any major linting errors and committing will fail until linting errors are resolved e.g unused vars. We can make the rules as tight or loose as we want through the configuration in `.eslintrc.js`.
- All unit tests must past before a developer can successfully push to remote.

## Proposal: Commit messages
Within `.husky/commit-msg`, there's a script that ensures consistent formatting with commit messages using the `commit-msg` git hook.

The proposed format is:
```
git commit -m "{commit type goes here}: {commit information}"
```

Example:
```
git commit -m "feat: linting support using eslint"
```

The image below provides more information on the commit types.

![image](./assets/conventional-commit-types.png)

Learn more about this conventional commit format [here](https://www.conventionalcommits.org/en/v1.0.0/#commit-message-with--to-draw-attention-to-breaking-change).
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@
"build": "NODE_ENV=production node ci/runBundler.js && node ci/sentry-send-release.js",
"test": "jest",
"lint": "eslint --ext .js --ext .jsx .",
"fix": "eslint --ext .js --ext .jsx . --fix"
"fix": "eslint --ext .js --ext .jsx . --fix",
"storybook": "start-storybook -p 6006",
"build-storybook": "build-storybook",
"prepush": "concurrently \"yarn run fix\" \"cd src/translations && npx sort-json * --ignore-case true\" \"yarn run test\""
},
"browserslist": [
">0.2%",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,4 @@ const mapStateToProps = (state, { match }) => {
};
};

export const SetupSeedPhraseWithRouter = connect(mapStateToProps, mapDispatchToProps)(withRouter(SetupSeedPhrase));
export const SetupSeedPhraseWithRouter = connect(mapStateToProps, mapDispatchToProps)(withRouter(SetupSeedPhrase));
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,4 @@ const ActivityDetailModal = ({
);
};

export default ActivityDetailModal;
export default ActivityDetailModal;
2 changes: 1 addition & 1 deletion packages/frontend/src/hooks/allAccounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ export function useAccount(accountId) {
return isOwner
? account
: allAccounts[accountId] || {};
}
}
2 changes: 1 addition & 1 deletion packages/frontend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ ReactDOM.render(
</Provider>
</GoogleReCaptchaProvider>,
document.getElementById('root')
);
);
2 changes: 1 addition & 1 deletion packages/frontend/src/redux/slices/tokens/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,4 @@ export const selectTokensLoading = createSelector(
const selectOneTokenLoading = createSelector(
[selectOneTokenFromOwnedTokens],
(token) => token.loading
);
);
2 changes: 1 addition & 1 deletion packages/frontend/src/redux/slices/transactions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,4 @@ export const selectOneTransactionByIdentity = createSelector(
export const selectTransactionsLoading = createSelector(
[selectTransactionsObjectByAccountId],
(transactions) => transactions.status.loading || false
);
);
4 changes: 2 additions & 2 deletions packages/frontend/src/translations/en.global.json
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,7 @@
"error": "Public key access was rejected. No accounts were recovered.<br/>To continue, you must allow NEAR Wallet to view your public key. Please try again.",
"success": ""
},
"LackBalanceForState": "Your available balance is too low to perform any actions on your account. Please send NEAR to your account and then try again.",
"NotEnoughBalance": "Your account does not have enough balance for this operation.",
"PROMPT_TWO_FACTOR": {
"invalidCode": "Invalid 2FA code. Please try again."
Expand Down Expand Up @@ -893,8 +894,7 @@
"VERIFY_TWO_FACTOR": {
"error": "Two Factor Authentication setup failed. Please try again.",
"success": "Two Factor Authentication successfully setup!"
},
"LackBalanceForState": "Your available balance is too low to perform any actions on your account. Please send NEAR to your account and then try again."
}
},
"releaseNotesModal": {
"desc": "You asked, we listened! The latest update brings adjustments to how balances are displayed in the wallet, and an easier way to create new accounts.",
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/translations/pt.global.json
Original file line number Diff line number Diff line change
Expand Up @@ -1317,4 +1317,4 @@
}
},
"warning": "Atenção"
}
}
2 changes: 1 addition & 1 deletion packages/frontend/src/translations/ru.global.json
Original file line number Diff line number Diff line change
Expand Up @@ -1242,4 +1242,4 @@
}
},
"warning": "Предупреждение"
}
}
2 changes: 1 addition & 1 deletion packages/frontend/src/translations/vi.global.json
Original file line number Diff line number Diff line change
Expand Up @@ -1324,4 +1324,4 @@
}
},
"warning": "Cảnh báo"
}
}
2 changes: 1 addition & 1 deletion packages/frontend/src/translations/zh-hans.global.json
Original file line number Diff line number Diff line change
Expand Up @@ -1467,4 +1467,4 @@
}
},
"warning": "警告"
}
}
2 changes: 1 addition & 1 deletion packages/frontend/src/translations/zh-hant.global.json
Original file line number Diff line number Diff line change
Expand Up @@ -1467,4 +1467,4 @@
}
},
"warning": "警告"
}
}
2 changes: 1 addition & 1 deletion packages/frontend/src/utils/account-with-lockup.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ async function viewLockupState(connection, lockupAccountId) {
transferInformation = {
transfer_poll_account_id: reader.readString()
};
};
}
let vestingType = reader.readU8();
let vestingInformation = null;
if (vestingType === 1) {
Expand Down
Loading