diff --git a/package.json b/package.json index 50647e6..14797b8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5752304..a9f484c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -53,6 +53,9 @@ importers: dedent: specifier: ^1.5.1 version: 1.5.1 + diff: + specifier: ^8.0.2 + version: 8.0.2 eslint-plugin-markdown: specifier: ^4.0.1 version: 4.0.1(eslint@8.57.1) @@ -2010,6 +2013,10 @@ packages: resolution: {integrity: sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==} engines: {node: '>=0.3.1'} + diff@8.0.2: + resolution: {integrity: sha512-sSuxWU5j5SR9QQji/o2qMvqRNYRDOcBTgsJ/DeCf4iSN4gW+gNMXM7wFIP+fdXZxoNiAnHUTGjCr+TSWXdRDKg==} + engines: {node: '>=0.3.1'} + dir-glob@3.0.1: resolution: {integrity: sha512-WkrWp9GR4KXfKGYzOLmTuGVi1UWFfws377n9cc55/tb6DuqyF6pcQ5AbiHEshaDpY9v6oaSr2XCDidGmMwdzIA==} engines: {node: '>=8'} @@ -6374,6 +6381,8 @@ snapshots: diff@4.0.2: {} + diff@8.0.2: {} + dir-glob@3.0.1: dependencies: path-type: 4.0.0 diff --git a/src/rule.ts b/src/rule.ts index cc1312d..fe2d531 100644 --- a/src/rule.ts +++ b/src/rule.ts @@ -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? @@ -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[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) { @@ -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), + // }) + // } }) }