Skip to content

Commit

Permalink
Remove 'require' statements from main code (#1050)
Browse files Browse the repository at this point in the history
Removing require() invocations, which was causing troubles
for some users due to their transpiled behavior. Lodash utilities
are all imported with `import` now.

Some libraries did not have typings available: namely asap and
disposables. asap has been replaced with SetImmediate, and
disposables has been replaced with a local implementation. The
repository is not being maintained, and the recommended
replacement is inactive and lacks typings.

Moving the remaining documentation code to Typescript, which
makes the need for .eslint kind of vestigial now. So we're removing
eslint and vestigial babel configs.

Added any dependencies from `@types/*`, or that provide their
own typings, to the nohoist configuration of the top level. This
allowed us to discover some issues where packages were
depending on types included with another package.
  • Loading branch information
darthtrevino committed Jun 1, 2018
1 parent 03a909d commit c4bab9a
Show file tree
Hide file tree
Showing 73 changed files with 713 additions and 1,094 deletions.
4 changes: 0 additions & 4 deletions .babelrc

This file was deleted.

3 changes: 0 additions & 3 deletions .codeclimate.yml
@@ -1,6 +1,3 @@
engines:
eslint:
enabled: true
ratings:
paths:
- "**.js"
Expand Down
11 changes: 0 additions & 11 deletions .eslintignore

This file was deleted.

27 changes: 0 additions & 27 deletions .eslintrc

This file was deleted.

1 change: 0 additions & 1 deletion .vscode/extensions.json
@@ -1,6 +1,5 @@
{
"recommendations": [
"dbaeumer.vscode-eslint",
"eg2.tslint",
"esbenp.prettier-vscode"
]
Expand Down
4 changes: 0 additions & 4 deletions .vscode/settings.json
@@ -1,12 +1,8 @@
{
"files.associations": {
".eslintrc": "yaml"
},
"search.exclude": {
"**/node_modules": true,
"**/bower_components": true
},
"eslint.packageManager": "yarn",
"editor.tabSize": 2,
"[typescript]": {
"editor.formatOnSave": true
Expand Down
31 changes: 11 additions & 20 deletions package.json
Expand Up @@ -38,11 +38,8 @@
"unit_test": "jest",
"jest:watch": "jest --watch",
"jest:cov": "jest --coverage",
"lint:es": "eslint .",
"lint:ts":
"lint":
"tslint \"packages/**/*.{ts,tsx}\" -e \"**/{lib,node_modules}/**\"",
"lint:tsc": "tsc",
"lint": "run-s lint:*",
"test_modules": "lerna run test --stream",
"publish_docs": "./scripts/publishDocumentation.sh",
"test": "run-s lint test_modules jest:cov",
Expand All @@ -54,18 +51,6 @@
},
"devDependencies": {
"@types/jest": "^22.2.3",
"babel-eslint": "^8.2.3",
"babel-jest": "^22.4.3",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-env": "^1.6.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"eslint": "^4.19.1",
"eslint-config-airbnb": "^16.1.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-import": "^2.11.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"husky": "^0.14.3",
"jest": "^23.0.0-beta.3r",
"jest-environment-jsdom": "^22.4.3",
Expand All @@ -88,8 +73,7 @@
"<rootDir>/packages/**/__tests__/**/?(*.)(spec|test).(t|j)s(x|)"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest",
"^.+\\.(js|jsx)$": "babel-jest"
"^.+\\.(ts|tsx)$": "ts-jest"
},
"moduleNameMapper": {
"^react-dnd$": "<rootDir>/packages/react-dnd/src",
Expand All @@ -101,7 +85,8 @@
}
},
"lint-staged": {
"*.js": ["eslint --fix", "prettier --write", "git add"]
"*.{js,jsx}": ["prettier --write", "git add"],
"*.{ts,tsx}": ["tslint --fix", "prettier --write", "git add"]
},
"prettier": {
"semi": false,
Expand All @@ -110,6 +95,12 @@
"useTabs": true
},
"workspaces": {
"packages": ["packages/*"]
"packages": ["packages/*"],
"nohoist": [
"**/@types/*",
"**/hoist-non-react-statics",
"**/autobind-decorator",
"**/redux"
]
}
}
5 changes: 0 additions & 5 deletions packages/dnd-core/.eslintrc

This file was deleted.

2 changes: 0 additions & 2 deletions packages/dnd-core/.npmignore
@@ -1,6 +1,4 @@
test
.babelrc
.eslintrc
.travis.yml
coverage
test-results.xml
7 changes: 3 additions & 4 deletions packages/dnd-core/package.json
Expand Up @@ -16,10 +16,9 @@
},
"dependencies": {
"@types/invariant": "^2.2.29",
"@types/lodash": "^4.14.107",
"asap": "^2.0.6",
"invariant": "^2.0.0",
"lodash": "^4.2.0",
"@types/lodash": "^4.14.109",
"invariant": "^2.2.4",
"lodash": "^4.17.10",
"redux": "^4.0.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/dnd-core/src/DragDropManagerImpl.ts
Expand Up @@ -72,7 +72,7 @@ export default class DragDropManagerImpl<Context>
const action: ActionCreator<any> = (actions as any)[
key
] as ActionCreator<any>
;(boundActions as any)[key] = bindActionCreator(action) // eslint-disable-line no-param-reassign
;(boundActions as any)[key] = bindActionCreator(action)
return boundActions
},
{} as DragDropActions,
Expand Down
58 changes: 30 additions & 28 deletions packages/dnd-core/src/HandlerRegistryImpl.ts
@@ -1,7 +1,6 @@
import { Store } from 'redux'
import invariant from 'invariant'
import isArray from 'lodash/isArray'
const asap = require('asap')
import {
addSource,
addTarget,
Expand Down Expand Up @@ -48,10 +47,23 @@ function parseRoleFromHandlerId(handlerId: string) {
}
}

function mapContainsValue<T>(map: Map<string, T>, searchValue: T) {
const entries = map.entries()
let isDone = false
do {
const { done, value: [key, value] } = entries.next()
if (value === searchValue) {
return true
}
isDone = done
} while (!isDone)
return false
}

export default class HandlerRegistryImpl implements HandlerRegistry {
private types: { [id: string]: SourceType | TargetType } = {}
private dragSources: { [id: string]: DragSource } = {}
private dropTargets: { [id: string]: DropTarget } = {}
private types: Map<string, SourceType | TargetType> = new Map()
private dragSources: Map<string, DragSource> = new Map()
private dropTargets: Map<string, DropTarget> = new Map()
private pinnedSourceId: string | null = null
private pinnedSource: any = null

Expand All @@ -77,37 +89,31 @@ export default class HandlerRegistryImpl implements HandlerRegistry {

public containsHandler(handler: DragSource | DropTarget) {
return (
Object.keys(this.dragSources).some(
key => this.dragSources[key] === handler,
) ||
Object.keys(this.dropTargets).some(
key => this.dropTargets[key] === handler,
)
mapContainsValue(this.dragSources, handler) ||
mapContainsValue(this.dropTargets, handler)
)
}

public getSource(sourceId: string, includePinned = false): DragSource {
invariant(this.isSourceId(sourceId), 'Expected a valid source ID.')

const isPinned = includePinned && sourceId === this.pinnedSourceId

const source = isPinned ? this.pinnedSource : this.dragSources[sourceId]
const source = isPinned ? this.pinnedSource : this.dragSources.get(sourceId)
return source
}

public getTarget(targetId: string): DropTarget {
invariant(this.isTargetId(targetId), 'Expected a valid target ID.')
return this.dropTargets[targetId] as DropTarget
return this.dropTargets.get(targetId) as DropTarget
}

public getSourceType(sourceId: string) {
invariant(this.isSourceId(sourceId), 'Expected a valid source ID.')
return this.types[sourceId] as Identifier
return this.types.get(sourceId) as Identifier
}

public getTargetType(targetId: string): Identifier | Identifier[] {
invariant(this.isTargetId(targetId), 'Expected a valid target ID.')
return this.types[targetId] as Identifier | Identifier[]
return this.types.get(targetId) as Identifier | Identifier[]
}

public isSourceId(handlerId: string) {
Expand All @@ -123,21 +129,17 @@ export default class HandlerRegistryImpl implements HandlerRegistry {
public removeSource(sourceId: string) {
invariant(this.getSource(sourceId), 'Expected an existing source.')
this.store.dispatch(removeSource(sourceId))

asap(() => {
delete this.dragSources[sourceId]
delete this.types[sourceId]
setImmediate(() => {
this.dragSources.delete(sourceId)
this.types.delete(sourceId)
})
}

public removeTarget(targetId: string) {
invariant(this.getTarget(targetId), 'Expected an existing target.')
this.store.dispatch(removeTarget(targetId))

asap(() => {
delete this.dropTargets[targetId]
delete this.types[targetId]
})
this.dropTargets.delete(targetId)
this.types.delete(targetId)
}

public pinSource(sourceId: string) {
Expand All @@ -161,11 +163,11 @@ export default class HandlerRegistryImpl implements HandlerRegistry {
handler: DragSource | DropTarget,
): string {
const id = getNextHandlerId(role)
this.types[id] = type
this.types.set(id, type)
if (role === HandlerRole.SOURCE) {
this.dragSources[id] = handler as DragSource
this.dragSources.set(id, handler as DragSource)
} else if (role === HandlerRole.TARGET) {
this.dropTargets[id] = handler as DropTarget
this.dropTargets.set(id, handler as DropTarget)
}
return id
}
Expand Down
2 changes: 0 additions & 2 deletions packages/dnd-core/src/__tests__/.eslintrc

This file was deleted.

44 changes: 38 additions & 6 deletions packages/dnd-core/src/__tests__/DragDropMonitor.spec.ts
Expand Up @@ -28,7 +28,11 @@ describe.only('DragDropMonitor', () => {

describe('state change subscription', () => {
it('throws on bad listener', () => {
expect(() => monitor.subscribeToStateChange(() => { /* empty */ })).not.toThrow()
expect(() =>
monitor.subscribeToStateChange(() => {
/* empty */
}),
).not.toThrow()

expect(() => (monitor as any).subscribeToStateChange()).toThrow()
expect(() => (monitor as any).subscribeToStateChange(42)).toThrow()
Expand All @@ -38,16 +42,40 @@ describe.only('DragDropMonitor', () => {

it('throws on bad handlerIds', () => {
expect(() =>
monitor.subscribeToStateChange(() => {/* empty */}, { handlerIds: [] }),
monitor.subscribeToStateChange(
() => {
/* empty */
},
{ handlerIds: [] },
),
).not.toThrow()
expect(() =>
monitor.subscribeToStateChange(() => {/* empty */}, { handlerIds: ['hi'] }),
monitor.subscribeToStateChange(
() => {
/* empty */
},
{ handlerIds: ['hi'] },
),
).not.toThrow()
expect(() =>
monitor.subscribeToStateChange(() => {/* empty */}, { handlerIds: {} as any}),
monitor.subscribeToStateChange(
() => {
/* empty */
},
{ handlerIds: {} as any },
),
).toThrow()
expect(() =>
monitor.subscribeToStateChange(() => {/* empty */}, { handlerIds: (() => {/* empty */}) as any }),
monitor.subscribeToStateChange(
() => {
/* empty */
},
{
handlerIds: (() => {
/* empty */
}) as any,
},
),
).toThrow()
})

Expand Down Expand Up @@ -545,7 +573,11 @@ describe.only('DragDropMonitor', () => {

describe('offset change subscription', () => {
it('throws on bad listener', () => {
expect(() => monitor.subscribeToOffsetChange(() => {/* empty */})).not.toThrow()
expect(() =>
monitor.subscribeToOffsetChange(() => {
/* empty */
}),
).not.toThrow()
expect(() => (monitor as any).subscribeToOffsetChange()).toThrow()
expect(() => (monitor as any).subscribeToOffsetChange(42)).toThrow()
expect(() => (monitor as any).subscribeToOffsetChange('hi')).toThrow()
Expand Down
15 changes: 0 additions & 15 deletions packages/documentation/examples/.eslintrc

This file was deleted.

Expand Up @@ -44,7 +44,7 @@ const boxSource = {
dropResult.name
}`
}
alert(alertMessage) // eslint-disable-line no-alert
alert(alertMessage)
}
},
}
Expand Down

0 comments on commit c4bab9a

Please sign in to comment.