Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b38f085
Add prettier
yevdyko Mar 22, 2021
11d4276
Add prettier config
yevdyko Mar 22, 2021
3b45c3f
Format component using prettier
yevdyko Mar 22, 2021
05e9530
Add lint-staged
yevdyko Mar 24, 2021
0fb5e3e
Add lint-staged config
yevdyko Mar 26, 2021
70ac63a
Add simple-git-hooks
yevdyko Mar 26, 2021
c3367ae
Add pre-commit git hook
yevdyko Mar 26, 2021
5bd812f
Add eslint packages for typescript
yevdyko Mar 26, 2021
3af1794
Add eslint config
yevdyko Mar 26, 2021
cb3eaad
Add react specific linting rules for eslint
yevdyko Mar 27, 2021
7c1ee09
Update eslint config with react plugin preset
yevdyko Mar 27, 2021
1b6612f
Add packages to integrate eslint with prettier rules
yevdyko Mar 27, 2021
63ec2bb
Update eslint config with recommended prettier setup
yevdyko Mar 27, 2021
9a58a4c
Avoid using trailing commas
yevdyko Mar 28, 2021
9770c31
Add ci github action workflow
yevdyko Mar 28, 2021
eb793a8
Add documentation about eslint configuration
yevdyko Mar 29, 2021
d44e2e1
Add documentation about prettier configuration
yevdyko Mar 31, 2021
ca06551
Format the whole project using prettier
yevdyko Mar 31, 2021
41e3178
Actualize ignored files
yevdyko Mar 31, 2021
0866ab0
Remove unused styles
yevdyko Apr 1, 2021
5d9fe39
Extend eslint config to use with js files
yevdyko Apr 1, 2021
e807316
Fix linting for example js files
yevdyko Apr 1, 2021
7626241
Update ci.yml
jamesrweb Apr 3, 2021
c0960a9
Update package.json
jamesrweb Apr 3, 2021
c554824
Update package-lock.json file after removing packages
yevdyko Apr 3, 2021
70d13e2
Move testing to the separate job in CI
yevdyko Apr 6, 2021
04048fd
Format JSON files as well
yevdyko Apr 8, 2021
dcc6067
Format README file using Prettier
yevdyko Apr 8, 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
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
}
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
37 changes: 37 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"root": true,
"env": {
"browser": true
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"settings": {
"react": {
"version": "detect"
}
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:prettier/recommended"
],
"overrides": [
{
"files": ["*.ts", "*.tsx"],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"rules": {
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/explicit-module-boundary-types": "off"
}
}
]
}
89 changes: 89 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: CI workflow

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
lint:
runs-on: ubuntu-latest

steps:
- name: Check out the repository
uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}

- name: Setup node
uses: actions/setup-node@v2
with:
node-version: 14.x

- name: Cache npm packages
uses: actions/cache@v2
id: npm-cache
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-

- name: Install dependencies
run: npm ci
env:
CI: true

- name: Format files
run: npm run format

- name: Commit formatting changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Apply formatting changes
branch: ${{ github.head_ref }}

- name: Lint files
run: npm run lint

- name: Linting failed (attempting fix...)
if: ${{ failure() }}
run: npm run lint:fix

- name: Commit fixed lint issues
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Apply fixed lint issues
branch: ${{ github.head_ref }}

test:
runs-on: ubuntu-latest

steps:
- name: Check out the repository
uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}

- name: Setup node
uses: actions/setup-node@v2
with:
node-version: 14.x

- name: Cache npm packages
uses: actions/cache@v2
id: npm-cache
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-

- name: Install dependencies
run: npm ci
env:
CI: true

- name: Run test suite
run: npm run test
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dist
node_modules
package.json
package-lock.json
*.html
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"trailingComma": "none"
}
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ import React from "react";
import P5Wrapper from "react-p5-wrapper";

function App() {
const sketch = p5 => {
const sketch = (p5) => {
p5.setup = () => {};
p5.draw = () => {};
};

return <P5Wrapper sketch={sketch} />;
}

Expand All @@ -61,12 +61,12 @@ import React from "react";
import P5Wrapper from "react-p5-wrapper";

function App() {
const sketch = p5 => {
const sketch = (p5) => {
let rotation = 0;

p5.setup = () => p5.createCanvas(600, 400, p5.WEBGL);

p5.myCustomRedrawAccordingToNewPropsHandler = props => {
p5.myCustomRedrawAccordingToNewPropsHandler = (props) => {
if (props.rotation) rotation = (props.rotation * Math.PI) / 180;
};

Expand All @@ -79,9 +79,9 @@ function App() {
p5.box(100);
p5.pop();
};
}
};

return <P5Wrapper sketch={sketch} rotation={rotation}/>;
return <P5Wrapper sketch={sketch} rotation={rotation} />;
}

export default App;
Expand Down
89 changes: 89 additions & 0 deletions docs/eslint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# ESLint

ESLint is a static code analysis tool for identifying problematic patterns found in JavaScript code, including Typescript.

## Packages

We use the following packages:

- `eslint`: This is the core ESLint library.
- `eslint-plugin-react`: This contains some standard linting rules for React code.
- `@typescript-eslint/parser`: This allows TypeScript code to be linted.
- `@typescript-eslint/eslint-plugin`: This contains some standard linting rules for TypeScript code.
- `eslint-config-prettier`: This turns off all rules that are unnecessary or might conflict with Prettier.
- `eslint-plugin-prettier`: This runs Prettier as an ESLint rule and reports differences as individual ESLint issues.

## Configuration

ESLint is configured in a `.eslintrc` file in the project root.

The configuration file contains the basic settings for JS files used in the examples:

1. Specify an environment provide browser global variables.

```json
{
"env": {
"browser": true
}
}
```

2. Parser options are set to use ECMAScript 2018 syntax and ECMAScript modules.

```json
{
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
}
}
```

3. Automatically select the React version you have installed.

```json
{
"settings": {
"react": {
"version": "detect"
}
}
}
```

4. Presets that extend the given ESLint configuration:

- `eslint:recommended`: This is the core eslint recommended rules.
- `plugin:react/recommended`: This plugin exports a recommended configuration that enforces React good practices.
- `plugin:prettier/recommended`: This plugin disables all formatting-related ESLint rules, and only enables rules that detect potential bugs. Must be added as the last extension.

We override the following properties for the Typescript related files:

1. A parser that allows ESLint to understand TypeScript syntax.

```json
{
"parser": "@typescript-eslint/parser"
}
```

2. Register the installed plugin package for linting TypeScript.

```json
{
"plugins": ["@typescript-eslint"]
}
```

3. Presets that extend the given ESLint configuration:

- `plugin:@typescript-eslint/recommended`: This plugin exports all the recommended rules for TypeScript.

## Usage

Run the following script to validate the code:

```
npm run lint
```
21 changes: 21 additions & 0 deletions docs/prettier.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Prettier

Prettier is a code formatter that enforces a consistent style by parsing the code and re-printing it with its own rules.

## Configuration

There are just a few options that can be configured in Prettier. They can found in a `.prettierrc` file in the project root.

The configuration file contains the following:

- `"trailingComma": "none"`: Print without trailing commas.

See more details in the [documentation](https://prettier.io/docs/en/options.html).

## Usage

Run the following script to format the files with `js`, `jsx`, `ts`, `tsx` and `md` extensions:

```
npm run format
```
50 changes: 28 additions & 22 deletions example/src/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,36 @@ import { render } from "react-dom";
import P5Wrapper from "../../src";
import sketch from "./sketches/sketch";
import sketch2 from "./sketches/sketch2";
import css from "./example.css";
import "./example.css";

function App(props) {
const [state, setState] = useState({ rotation: 160, sketch });
function App() {
const [state, setState] = useState({ rotation: 160, sketch });

return (
<Fragment>
<P5Wrapper sketch={state.sketch} rotation={state.rotation} />
<input
type="range"
defaultValue={state.rotation}
min="0"
max="360"
step="1"
onChange={event => setState({ ...state, rotation: event.target.value })}
/>
<button onClick={
event => setState({
...state,
sketch: state.sketch === sketch ? sketch2 : sketch
})
}>Change Sketch</button>
</Fragment>
);
return (
<Fragment>
<P5Wrapper sketch={state.sketch} rotation={state.rotation} />
<input
type="range"
defaultValue={state.rotation}
min="0"
max="360"
step="1"
onChange={(event) =>
setState({ ...state, rotation: event.target.value })
}
/>
<button
onClick={() =>
setState({
...state,
sketch: state.sketch === sketch ? sketch2 : sketch
})
}
>
Change Sketch
</button>
</Fragment>
);
}

render(<App />, document.getElementById("app"));
Loading