Skip to content
Merged

diff #32

Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@types/lodash": "^4.14.202",
"cheerio": "^1.0.0",
"dedent": "^1.5.1",
"diff": "^8.0.2",
"eslint-plugin-markdown": "^4.0.1",
"expect": "^29.7.0",
"fp-ts": "^2.1.0",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 34 additions & 3 deletions src/rule.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import {createHash} from 'crypto'
import * as diff from 'diff'
import type * as eslint from 'eslint'
import expect from 'expect'
import {tryCatch} from 'fp-ts/lib/Either'
import {globSync} from 'glob'
import * as jsYaml from 'js-yaml'
import ms from 'ms'
import * as os from 'os'
import * as path from 'path'
import stripAnsi from 'strip-ansi'
import {dependencies} from './dependencies'
import * as presetsModule from './presets'
import {equivalentSimplified} from './simplify'

export const codegen: eslint.Rule.RuleModule = {
// @ts-expect-error types are wrong?
Expand Down Expand Up @@ -216,6 +214,27 @@ export const codegen: eslint.Rule.RuleModule = {
return
}

const patch = diff.createPatch(context.physicalFilename, existingContent.trimEnd(), result.right.trimEnd())
for (const parsedPatch of diff.parsePatch(patch)) {
for (const hunk of parsedPatch.hunks) {
let message = `Content doesn't match:\n`
message += hunk.lines.join('\n')
const startPosition = position(range[0])

const fix: Parameters<typeof context.report>[0]['fix'] = fixer =>
fixer.replaceTextRange(range, normalise(result.right) + os.EOL)
context.report({
message,
loc: {
start: {line: startPosition.line + hunk.oldStart, column: 0},
end: {line: startPosition.line + hunk.oldLines, column: 0},
},
fix,
// todo: fix or suggest based on an option
})
}
}
/*
try {
expect(normalise(existingContent)).toBe(normalise(result.right))
} catch (e: unknown) {
Expand All @@ -231,6 +250,18 @@ export const codegen: eslint.Rule.RuleModule = {
fix: fixer => fixer.replaceTextRange(range, normalise(result.right) + os.EOL),
})
}
*/
// try {
// expect(normalise(existingContent)).toBe(normalise(result.right))
// } catch (e: unknown) {
// let message = `content doesn't match: ${e as string}`
// if (process.env.NODE_ENV === 'test') message = stripAnsi(message)
// context.report({
// message,
// loc: {start: position(range[0]), end: position(range[1])},
// fix: fixer => fixer.replaceTextRange(range, normalise(result.right) + os.EOL),
// })
// }
})
}

Expand Down