Skip to content

Commit

Permalink
[diff] Fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2020
1 parent b2e9071 commit 500d207
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 80 deletions.
7 changes: 6 additions & 1 deletion packages/@sanity/diff/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"extends": ["../../../.eslintrc.js"]
"extends": ["../../../.eslintrc.js"],
"rules": {
"complexity": "off",
"max-depth": "off",
"id-length": "off"
}
}
78 changes: 43 additions & 35 deletions packages/@sanity/diff/src/calculate/diffArray.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ArrayDiff, ArrayInput, ItemDiff, DiffOptions} from '../types'
import {replaceProperty} from '../helpers'
import {diffInput, removedInput, addedInput} from './diffInput'
import {getLongestCommonSubsequence} from './lcs'
import {replaceProperty} from '../helpers'

export function diffArray<A>(
fromInput: ArrayInput<A>,
Expand Down Expand Up @@ -80,13 +80,18 @@ function diffExactByPosition<A>(
toInput: ArrayInput<A>,
options: DiffOptions
): ItemDiff<A>[] | undefined {
if (fromInput.length !== toInput.length) return
if (fromInput.length !== toInput.length) {
return undefined
}

const items: ItemDiff<A>[] = []

for (let idx = 0; idx < fromInput.length; idx++) {
let diff = diffInput(fromInput.at(idx), toInput.at(idx), options)
if (diff.isChanged) return
const diff = diffInput(fromInput.at(idx), toInput.at(idx), options)
if (diff.isChanged) {
return undefined
}

items.push({
fromIndex: idx,
toIndex: idx,
Expand All @@ -106,7 +111,7 @@ function diffArrayByReinsert<A>(
const items: ItemDiff<A>[] = []

for (let idx = 0; idx < toInput.length; idx++) {
let input = toInput.at(idx)
const input = toInput.at(idx)

items.push({
fromIndex: undefined,
Expand All @@ -117,7 +122,7 @@ function diffArrayByReinsert<A>(
}

for (let idx = 0; idx < fromInput.length; idx++) {
let input = fromInput.at(idx)
const input = fromInput.at(idx)

items.push({
fromIndex: idx,
Expand Down Expand Up @@ -149,10 +154,10 @@ function diffArrayByKey<A>(
deletePositionInIndex(fromKeyIndex.index, key, fromIndex)
deletePositionInIndex(toKeyIndex.index, key, toIndex)

let fromInput = fromArray.at(fromIndex)
let toInput = toArray.at(toIndex)
const fromInput = fromArray.at(fromIndex)
const toInput = toArray.at(toIndex)

let diff = diffInput(fromInput, toInput)
const diff = diffInput(fromInput, toInput)
items.push({
fromIndex,
toIndex,
Expand All @@ -165,26 +170,26 @@ function diffArrayByKey<A>(
}
}

let lcs = getLongestCommonSubsequence(fromKeyIndex.keys, toKeyIndex.keys)
const lcs = getLongestCommonSubsequence(fromKeyIndex.keys, toKeyIndex.keys)

for (let fromIndex = 0; fromIndex < fromKeyIndex.keys.length; fromIndex++) {
let key = fromKeyIndex.keys[fromIndex]
const key = fromKeyIndex.keys[fromIndex]

let subsequenceIdx = lcs.prevIndices.indexOf(fromIndex)
const subsequenceIdx = lcs.prevIndices.indexOf(fromIndex)
if (subsequenceIdx !== -1) {
// Part of the common subsequence => hasMoved:false
diffCommon(key, fromIndex, lcs.nextIndices[subsequenceIdx], false)
continue
}

let toIndex = toKeyIndex.index.get(key)?.[0]
const toIndex = toKeyIndex.index.get(key)?.[0]
if (toIndex !== undefined) {
// Not a part of the subsequence, but is present in the to-version => hasMoved:true
diffCommon(key, fromIndex, toIndex, true)
continue
}

let input = fromArray.at(fromIndex)
const input = fromArray.at(fromIndex)

items.push({
fromIndex,
Expand All @@ -197,9 +202,9 @@ function diffArrayByKey<A>(
}

// The remaining data in toKeyIndex are the new elements which has been added
for (let positions of toKeyIndex.index.values()) {
for (let toIndex of positions) {
let input = toArray.at(toIndex)
for (const positions of toKeyIndex.index.values()) {
for (const toIndex of positions) {
const input = toArray.at(toIndex)
items.push({
fromIndex: undefined,
toIndex,
Expand Down Expand Up @@ -239,15 +244,15 @@ function compareItemDiff<A>(a: ItemDiff<A>, b: ItemDiff<A>): number {
}

function deletePositionInIndex(index: Map<Key, number[]>, key: Key, pos: number) {
let positions = index.get(key)!
const positions = index.get(key)!
deleteArrayValue(positions, pos)
if (positions.length === 0) {
index.delete(key)
}
}

function deleteArrayValue<E>(arr: E[], value: E) {
let idx = arr.indexOf(value)
const idx = arr.indexOf(value)
if (idx === -1) throw new Error('value not found')
arr.splice(idx, 1)
}
Expand All @@ -265,18 +270,18 @@ type KeyIndex = {
* - Numbers
*/
function indexByKey<A>(arr: ArrayInput<A>): KeyIndex | undefined {
let index = new Map<Key, number[]>()
let keys: Key[] = []
let length = arr.length
const index = new Map<Key, number[]>()
const keys: Key[] = []
const length = arr.length

for (let i = 0; i < length; i++) {
let item = arr.at(i)
const item = arr.at(i)

let key: Key | null = null

switch (item.type) {
case 'string':
key = 's' + item.value
key = `s${item.value}`
break
case 'number':
key = item.value
Expand All @@ -287,19 +292,22 @@ function indexByKey<A>(arr: ArrayInput<A>): KeyIndex | undefined {
case 'null':
key = 'n'
break
case 'object': {
const keyField = item.get('_key')
if (keyField && keyField.type === 'string') {
key = 'k' + keyField.value

// We do not handle duplicate _key
if (index.has(key)) return
case 'object':
{
const keyField = item.get('_key')
if (keyField && keyField.type === 'string') {
key = `k${keyField.value}`

// We do not handle duplicate _key
if (index.has(key)) return undefined
}
}
}
break
default:
}

// No key => abort
if (key === null) return
if (key === null) return undefined

keys.push(key)
let positions = index.get(key)
Expand Down Expand Up @@ -330,7 +338,7 @@ export function removedArray<A>(
get items(): ArrayDiff<A>['items'] {
const items: ArrayDiff<A>['items'] = []
for (let i = 0; i < input.length; i++) {
let item = input.at(i)
const item = input.at(i)
items.push({
fromIndex: i,
toIndex: undefined,
Expand Down Expand Up @@ -360,7 +368,7 @@ export function addedArray<A>(
get items(): ArrayDiff<A>['items'] {
const items: ArrayDiff<A>['items'] = []
for (let i = 0; i < input.length; i++) {
let item = input.at(i)
const item = input.at(i)
items.push({
fromIndex: undefined,
toIndex: i,
Expand Down
6 changes: 6 additions & 0 deletions packages/@sanity/diff/src/calculate/diffInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ function diffWithType<A>(
return diffArray(fromInput as ArrayInput<A>, toInput as ArrayInput<A>, options)
case 'object':
return diffObject(fromInput as ObjectInput<A>, toInput as ObjectInput<A>, options)
default:
throw new Error(`Unhandled diff type "${type}"`)
}
}

Expand Down Expand Up @@ -101,6 +103,8 @@ export function removedInput<A>(
return removedArray(input, toValue, options)
case 'object':
return removedObject(input, toValue, options)
default:
throw new Error('Unhandled diff type')
}
}

Expand Down Expand Up @@ -143,5 +147,7 @@ export function addedInput<A>(
return addedArray(input, fromValue, options)
case 'object':
return addedObject(input, fromValue, options)
default:
throw new Error('Unhandled diff type')
}
}
22 changes: 11 additions & 11 deletions packages/@sanity/diff/src/calculate/diffObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {replaceProperty} from '../helpers'
import {ObjectDiff, ObjectInput, DiffOptions} from '../types'
import {diffInput, removedInput, addedInput} from './diffInput'
import {replaceProperty} from '../helpers'

const ignoredFields = new Set(['_id', '_type', '_createdAt', '_updatedAt', '_rev', '_weak'])

Expand All @@ -12,14 +12,14 @@ export function diffObject<A>(
const fields: ObjectDiff<A>['fields'] = {}
let isChanged = false

for (let key of fromInput.keys) {
for (const key of fromInput.keys) {
if (ignoredFields.has(key)) continue

let fromField = fromInput.get(key)!
const fromField = fromInput.get(key)!

let toField = toInput.get(key)
const toField = toInput.get(key)
if (toField) {
let fieldDiff = diffInput(fromField, toField, options)
const fieldDiff = diffInput(fromField, toField, options)
fields[key] = fieldDiff
if (fieldDiff.isChanged) isChanged = true
} else {
Expand All @@ -28,13 +28,13 @@ export function diffObject<A>(
}
}

for (let key of toInput.keys) {
for (const key of toInput.keys) {
if (ignoredFields.has(key)) continue

// Already handled above
if (fromInput.get(key)) continue

let toField = toInput.get(key)!
const toField = toInput.get(key)!
fields[key] = addedInput(toField, undefined, options)
isChanged = true
}
Expand Down Expand Up @@ -79,8 +79,8 @@ export function removedObject<A>(

get fields(): ObjectDiff<A>['fields'] {
const fields: ObjectDiff<A>['fields'] = {}
for (let key of input.keys) {
let value = input.get(key)!
for (const key of input.keys) {
const value = input.get(key)!
fields[key] = removedInput(value, undefined, options)
}
return replaceProperty<typeof fields>(this, 'fields', fields)
Expand All @@ -103,8 +103,8 @@ export function addedObject<A>(

get fields(): ObjectDiff<A>['fields'] {
const fields: ObjectDiff<A>['fields'] = {}
for (let key of input.keys) {
let value = input.get(key)!
for (const key of input.keys) {
const value = input.get(key)!
fields[key] = addedInput(value, undefined, options)
}
return replaceProperty<typeof fields>(this, 'fields', fields)
Expand Down
10 changes: 6 additions & 4 deletions packages/@sanity/diff/src/calculate/diffString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ function buildSegments<A>(
fromInput: StringInput<A>,
toInput: StringInput<A>
): StringDiffSegment<A>[] {
let segments: StringDiffSegment<A>[] = []
const segments: StringDiffSegment<A>[] = []

const dmpDiffs = dmp.diff_main(fromInput.value, toInput.value)
dmp.diff_cleanupSemantic(dmpDiffs)

let fromIdx = 0
let toIdx = 0

for (let [op, text] of dmpDiffs) {
for (const [op, text] of dmpDiffs) {
switch (op) {
case DIFF_EQUAL:
segments.push({type: 'stringSegment', action: 'unchanged', text})
fromIdx += text.length
toIdx += text.length
break
case DIFF_DELETE:
for (let segment of fromInput.sliceAnnotation(fromIdx, fromIdx + text.length)) {
for (const segment of fromInput.sliceAnnotation(fromIdx, fromIdx + text.length)) {
segments.push({
type: 'stringSegment',
action: 'removed',
Expand All @@ -75,7 +75,7 @@ function buildSegments<A>(
fromIdx += text.length
break
case DIFF_INSERT:
for (let segment of toInput.sliceAnnotation(toIdx, toIdx + text.length)) {
for (const segment of toInput.sliceAnnotation(toIdx, toIdx + text.length)) {
segments.push({
type: 'stringSegment',
action: 'added',
Expand All @@ -85,6 +85,8 @@ function buildSegments<A>(
}
toIdx += text.length
break
default:
throw new Error(`Unhandled diff-match-patch operation "${op}"`)
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/@sanity/diff/src/calculate/lcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Subsequence<E> = {
nextIndices: number[]
}

export function getLongestCommonSubsequence<E>(previous: E[], next: E[]) {
export function getLongestCommonSubsequence<E>(previous: E[], next: E[]): Subsequence<E> {
const matrix = getLengthMatrix(previous, next)
const result = backtrack(matrix, previous, next)
return result
Expand All @@ -24,7 +24,7 @@ function getLengthMatrix<E>(previous: E[], next: E[]): LengthMatrix {
let y = 0

// initialize empty matrix of len1+1 x len2+1
let matrix: LengthMatrix = new Array(len1 + 1)
const matrix: LengthMatrix = new Array(len1 + 1)
for (x = 0; x < len1 + 1; x++) {
matrix[x] = [len2 + 1]
for (y = 0; y < len2 + 1; y++) {
Expand Down
10 changes: 5 additions & 5 deletions packages/@sanity/diff/src/inputWrappers/array.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ArrayInput, Input} from '../types'
import {wrap} from '.'
import {wrap} from './index'

export default class ArrayWrapper<A> implements ArrayInput<A> {
type: 'array' = 'array'
Expand All @@ -15,13 +15,13 @@ export default class ArrayWrapper<A> implements ArrayInput<A> {
this.length = value.length
}

at(idx: number) {
at(idx: number): Input<A> {
if (idx >= this.length) throw new Error('out of bounds')
let input = this.elements[idx]
const input = this.elements[idx]
if (input) {
return input
} else {
return (this.elements[idx] = wrap(this.value[idx], this.annotation))
}

return (this.elements[idx] = wrap(this.value[idx], this.annotation))
}
}

0 comments on commit 500d207

Please sign in to comment.