Skip to content

Commit

Permalink
fix(eslint): add eslint-plugin-simple-import-sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Flavio Corpa committed Sep 18, 2020
1 parent 16779ed commit 5d46349
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"presets": ["@babel/preset-env"],
"plugins": ["@babel/plugin-proposal-class-properties"],
"plugins": [["@babel/plugin-proposal-class-properties", { "loose": true }]],
"env": {
"test": {
"plugins": ["@babel/plugin-transform-modules-commonjs"]
Expand Down
6 changes: 4 additions & 2 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ env:
es2021: true
node: true
jest: true
extends: 'eslint:recommended'
extends: eslint:recommended
parserOptions:
ecmaVersion: 12
sourceType: module
rules: {}
rules:
simple-import-sort/sort: warn
parser: '@babel/eslint-parser'
plugins:
- '@babel'
- simple-import-sort
9 changes: 6 additions & 3 deletions __tests__/Lens.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { lens, prop, ix } from '../src/Lens'
import { get, set as assoc, toUpper } from '../src/functions'
import { optic, preview, view, set, over } from '../src/operations'
import { ix, lens, prop } from '../src/Lens'
import { optic, over, preview, set, view } from '../src/operations'

const friends = ['Alejandro']
const user = { id: 1, name: 'Flavio' }
Expand All @@ -11,10 +11,11 @@ describe('Lens', () => {
const propName = get('name')
const assocName = assoc('name')
const lense = lens(propName, assocName)
const alex = { id: 1, name: 'Alejandro' }

expect(view(lense, user)).toBe('Flavio')
expect(preview(lense, user)).toBe('Flavio')
expect(set(lense, 'Alejandro', user)).toEqual({ id: 1, name: 'Alejandro' })
expect(set(lense, 'Alejandro', user)).toEqual(alex)
})

test('prop should build a lens', () => {
Expand Down Expand Up @@ -50,8 +51,10 @@ describe('Lens', () => {
})

test('Lens.asOptional -> should convert to an Optional correctly', () => {
const ageOptional = prop('age').asOptional
const nameOptional = prop('name').asOptional

expect(preview(ageOptional, user)).toBeUndefined()
expect(preview(nameOptional, user)).toEqual('Flavio')
})
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"conventional-github-releaser": "^3.1.5",
"coveralls": "^3.1.0",
"eslint": "^7.9.0",
"eslint-plugin-simple-import-sort": "^5.0.3",
"finepack": "^2.10.5",
"git-authors-cli": "^1.0.28",
"git-dirty": "^1.0.2",
Expand Down
4 changes: 2 additions & 2 deletions src/Lens.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { curry, get, set } from './functions'
import { getter } from './Getter'
import { setter } from './Setter'
import { optional } from './Optional'
import { curry, get, set } from './functions'
import { partialGetter } from './PartialGetter'
import { setter } from './Setter'

class Lens {
constructor(get, set) {
Expand Down
6 changes: 3 additions & 3 deletions src/Optional.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { setter } from './Setter'
import { partialGetter } from './PartialGetter'
import { curry } from './functions'
import { notFound, isNotFound } from './notFound'
import { isNotFound, notFound } from './notFound'
import { partialGetter } from './PartialGetter'
import { setter } from './Setter'

/**
* AKA: Affine Traversal
Expand Down
3 changes: 3 additions & 0 deletions src/PartialGetter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { curry } from './functions'

/**
* AKA: Affine Fold
*/
class PartialGetter {
constructor(preview) {
this.preview = preview
Expand Down
17 changes: 9 additions & 8 deletions src/Prism.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { curry } from './functions'
import { notFound, isNotFound } from './notFound'
import { setter } from './Setter'
import { isNotFound, notFound } from './notFound'
import { optional } from './Optional'
import { reviewer } from './Reviewer'
import { partialGetter } from './PartialGetter'
import { reviewer } from './Reviewer'
import { setter } from './Setter'

class Prism {
constructor(preview, set, review) {
Expand Down Expand Up @@ -55,8 +55,9 @@ const checkPresence = (mustBePresent, obj) =>
*
* @param {object} mustBePresent
*/
export const has = (mustBePresent) => prism(
(obj) => checkPresence(mustBePresent, obj) ? {...obj} : notFound,
(newObj, obj) => checkPresence(mustBePresent, obj) ? {...newObj} : {...obj},
(newObj) => { return { ...newObj, ...mustBePresent } }
)
export const has = (mustBePresent) =>
prism(
(obj) => (checkPresence(mustBePresent, obj) ? { ...obj } : notFound),
(newObj, obj) => (checkPresence(mustBePresent, obj) ? { ...newObj } : { ...obj }),
(newObj) => ({ ...newObj, ...mustBePresent }),
)
30 changes: 6 additions & 24 deletions src/operations.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { prism } from './Prism'
import { getter } from './Getter'
import { setter } from './Setter'
import { curry } from './functions'
import { lens, prop, ix } from './Lens'
import { reviewer } from './Reviewer'
import { optional, optionalProp, optionalIx } from './Optional'
import { getter } from './Getter'
import { ix, lens, prop } from './Lens'
import { optional, optionalIx, optionalProp } from './Optional'
import { partialGetter } from './PartialGetter'
import { prism } from './Prism'
import { reviewer } from './Reviewer'
import { setter } from './Setter'

// combine two previews
const combinePreviews = (p1, p2) => (x) => {
Expand Down Expand Up @@ -85,25 +85,7 @@ const toOptic = (optic) => {
* flatten the arguments to account for composeOptics(['this', 'that'])
*/
export const composeOptics = (...optics) => optics.flat().map(toOptic).reduce(compose2Optics)

/**
* Create a new optic by composition.
*
* You can use a string or integer to directly create a lens,
* or wrap it with 'maybe' to create an optional
*
* @param {...any} optics - Comma-separated or array of optics to be composed
*/
export const optic = composeOptics

/**
* Create a new optic by composition.
*
* You can use a string or integer to directly create a lens,
* or wrap it with 'maybe' to create an optional
*
* @param {...any} optics - Comma-separated or array of optics to be composed
*/
export const path = composeOptics

// preview : AffineFold s a → s → Maybe a
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4157,6 +4157,11 @@ escodegen@^1.14.1:
optionalDependencies:
source-map "~0.6.1"

eslint-plugin-simple-import-sort@^5.0.3:
version "5.0.3"
resolved "https://registry.yarnpkg.com/eslint-plugin-simple-import-sort/-/eslint-plugin-simple-import-sort-5.0.3.tgz#9ae258ddada6efffc55e47a134afbd279eb31fc6"
integrity sha512-1rf3AWiHeWNCQdAq0iXNnlccnH1UDnelGgrPbjBBHE8d2hXVtOudcmy0vTF4hri3iJ0MKz8jBhmH6lJ0ZWZLHQ==

eslint-rule-composer@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/eslint-rule-composer/-/eslint-rule-composer-0.3.0.tgz#79320c927b0c5c0d3d3d2b76c8b4a488f25bbaf9"
Expand Down

0 comments on commit 5d46349

Please sign in to comment.