@preventive/compare 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/names.d.ts CHANGED
@@ -33,7 +33,7 @@ export type RenameReason = 'unparsed' | 'dynamic' | 'jsx' | 'block-function' | '
33
33
  // function's or class's, the one name it reads (`function f`, `f = () => {}`);
34
34
  // `marks` the plain diff's blocks that differ in renamed names alone; and
35
35
  // `warnings` what a check that should never fail found.
36
- export function renamedDiff(before: string, after: string, options?: RenameOptions & { whitespace?: 'none' | 'all' | 'change' | 'trailing' }): Promise<{
36
+ export function renamedDiff(before: string, after: string, options?: RenameOptions & { whitespace?: Whitespace }): Promise<{
37
37
  before: string
38
38
  blocks: Pick<Block, 'a0' | 'a1' | 'b0' | 'b1'>[]
39
39
  reason: RenameReason | null
@@ -42,3 +42,17 @@ export function renamedDiff(before: string, after: string, options?: RenameOptio
42
42
  warnings: string[]
43
43
  }>
44
44
 
45
+ export type Whitespace = 'none' | 'all' | 'change' | 'trailing'
46
+
47
+ // renamedDiff for each of `whitespace`'s settings (`['none']` unless given),
48
+ // `diffs` in their order: the names found and the old side given them once
49
+ // for all, as are `before`, `reason`, `reads` and `warnings`; each setting's
50
+ // `blocks` checked as renamedDiff's are, and its `marks` read as they are.
51
+ export function renamedDiffs(before: string, after: string, options?: RenameOptions & { whitespace?: Whitespace[] }): Promise<{
52
+ before: string
53
+ reason: RenameReason | null
54
+ reads: string[]
55
+ diffs: { whitespace: Whitespace; blocks: Pick<Block, 'a0' | 'a1' | 'b0' | 'b1'>[]; marks: Pick<Block, 'a0' | 'a1' | 'b0' | 'b1'>[] }[]
56
+ warnings: string[]
57
+ }>
58
+
package/names.js CHANGED
@@ -5,5 +5,6 @@
5
5
  // peers: on Node loaded on first use, elsewhere oxc with this module.
6
6
  // compare.js loads none. Typed and described in names.d.ts.
7
7
 
8
- // The diff of two such files with renamed names set aside, checked to hide no change.
9
- export { renamedDiff } from './src/names/diff.js'
8
+ // The diff of two such files with renamed names set aside, checked to hide no
9
+ // change; and the same for several whitespace settings at once.
10
+ export { renamedDiff, renamedDiffs } from './src/names/diff.js'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preventive/compare",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Git-compatible file tree diffs and minified JavaScript comparison",
5
5
  "type": "module",
6
6
  "main": "./compare.js",
package/src/names/diff.js CHANGED
@@ -6,12 +6,17 @@ import { changeSet } from '@preventive/diff'
6
6
  import { blocksHold } from '../blocks.js'
7
7
  import { markedBlocks } from '../marked.js'
8
8
  import { format } from '../pretty.js'
9
+ import { checkLineOptions } from '../text.js'
9
10
  import { bindsAlike } from './binds.js'
10
11
  import { renameMap } from './rename.js'
11
12
 
12
13
  const ranges = ({ a0, a1, b0, b1 }) => ({ a0, a1, b0, b1 })
13
14
 
14
- export async function renamedDiff(before, after, { extension = 'js', otherExtension = extension, whitespace = 'none' } = {}) {
15
+ // The same, diffed once for each whitespace setting given, in order: the names
16
+ // found and the old side given them once for all.
17
+ export async function renamedDiffs(before, after, { extension = 'js', otherExtension = extension, whitespace = ['none'] } = {}) {
18
+ if (!Array.isArray(whitespace)) throw new TypeError(`whitespace must be an array, not ${typeof whitespace}`)
19
+ const settings = whitespace.map((setting) => checkLineOptions({ horizon: 0, whitespace: setting }))
15
20
  const warnings = []
16
21
  const mapped = renameMap(before, after, { extension, otherExtension })
17
22
  if (mapped.invariant) warnings.push(`a rename map check failed: ${mapped.invariant}`)
@@ -23,11 +28,20 @@ export async function renamedDiff(before, after, { extension = 'js', otherExtens
23
28
  if ('code' in formatted && !read) warnings.push("the renamed old side, formatted, read otherwise")
24
29
  renamed = read ? formatted.code : mapped.text
25
30
  }
26
- const options = { horizon: 0, whitespace }
27
- const blocks = changeSet(renamed, after, options).map(ranges)
28
- if (!blocksHold(renamed, after, blocks, options)) throw new Error('change blocks that do not take the old side to the new')
29
- // Read off the renamed text before formatting, line for line the old side's.
30
- const marks = mapped.reason ? [] : markedBlocks(before, mapped.text, after, options)
31
- if (marks === null) warnings.push('a renamed text with lines the old one does not have; none marked')
32
- return { before: renamed, blocks, reason: mapped.reason ?? null, reads: mapped.reads ?? [], marks: marks ?? [], warnings }
31
+ let unmarked = false
32
+ const diffs = settings.map((options) => {
33
+ const blocks = changeSet(renamed, after, options).map(ranges)
34
+ if (!blocksHold(renamed, after, blocks, options)) throw new Error('change blocks that do not take the old side to the new')
35
+ // Read off the renamed text before formatting, line for line the old side's.
36
+ const marks = mapped.reason ? [] : markedBlocks(before, mapped.text, after, options)
37
+ unmarked ||= marks === null
38
+ return { whitespace: options.whitespace, blocks, marks: marks ?? [] }
39
+ })
40
+ if (unmarked) warnings.push('a renamed text with lines the old one does not have; none marked')
41
+ return { before: renamed, reason: mapped.reason ?? null, reads: mapped.reads ?? [], diffs, warnings }
42
+ }
43
+
44
+ export async function renamedDiff(before, after, { whitespace = 'none', ...options } = {}) {
45
+ const { before: renamed, reason, reads, diffs: [{ blocks, marks }], warnings } = await renamedDiffs(before, after, { ...options, whitespace: [whitespace] })
46
+ return { before: renamed, blocks, reason, reads, marks, warnings }
33
47
  }