Repository navigation
Expand file tree
/
Copy pathoxlint.js
More file actions
94 lines (77 loc) · 2.82 KB
/
Copy pathoxlint.js
File metadata and controls
94 lines (77 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// An oxlint plugin that shows what samey finds as you write, in an editor.
// It's silent when everything is linted: `samey` is a step of its own then,
// since it reads more than a linter does.
import {spawnSync} from 'node:child_process';
import {readFileSync, statSync} from 'node:fs';
import {relative, sep} from 'node:path';
import {fileURLToPath} from 'node:url';
const CLI = fileURLToPath(new URL('./src/cli.ts', import.meta.url));
/** Other files may have changed after this many milliseconds. */
const STALE = 10_000;
const SAID = {
exact: 'The same code is at',
renamed: 'The same code, but for the names of locals, is at',
nearly: 'Nearly the same code is at',
};
const states = new Map();
/** What there is in a folder, asked again when a file has changed or it's been a while. */
const findingsIn = (root, changed) => {
const known = states.get(root);
const now = Date.now();
if (known && changed <= known.at && now - known.at < STALE) {
return known.findings;
}
const findings = ask(root);
states.set(root, {findings, at: now});
return findings;
};
/** What samey finds that's new, by the file it's in. */
const ask = root => {
const {status, stdout} = spawnSync('bun', [CLI, '--json'], {
cwd: root,
encoding: 'utf8',
maxBuffer: 2 ** 28,
});
const found = status === 0 || status === 1 ? JSON.parse(stdout).new : [];
return Map.groupBy(
found.flatMap(finding => finding.copies.map(copy => ({finding, copy}))),
({copy}) => copy.path,
);
};
const messageFor = ({finding, copy}) => {
const others = finding.copies
.filter(each => each !== copy)
.map(({path, line}) => `${path}:${line}`);
const where = [...others.slice(0, 3), ...(others.length > 3 ? ['elsewhere'] : [])].join(', ');
return `${SAID[finding.kind]} ${where}. ${copy.import === null ? finding.what : `Instead: ${copy.import}`}`;
};
/** How long a line is. */
const endOf = (text, line) => text.split('\n')[line - 1]?.length ?? 0;
export const copies = {
meta: {type: 'suggestion', docs: {description: "Says where else what's just been written is"}},
create: context => ({
Program() {
const {cwd, filename, sourceCode} = context;
const stat = statSync(filename, {throwIfNoEntry: false});
// samey reads files as they're saved.
if (!stat?.isFile() || readFileSync(filename, 'utf8') !== sourceCode.text) {
return;
}
const path = relative(cwd, filename).split(sep).join('/');
(findingsIn(cwd, stat.mtimeMs).get(path) ?? []).forEach(found =>
context.report({
message: messageFor(found),
loc: {
start: {line: found.copy.line, column: 0},
end: {line: found.copy.endLine, column: endOf(sourceCode.text, found.copy.endLine)},
},
}),
);
},
}),
};
const silent = {...copies, create: () => ({})};
export default {
meta: {name: 'samey'},
rules: {copies: process.argv.includes('--lsp') ? copies : silent},
};