textoneitor 0.0.0-stage → 1.2.2
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/LICENSE +101 -0
- package/LICENSE-AGPL-3.0 +661 -0
- package/README.md +69 -2
- package/dist/api/index.d.ts +34 -0
- package/dist/api/index.js +98 -0
- package/dist/baseline/index.d.ts +20 -0
- package/dist/baseline/index.js +81 -0
- package/dist/bundle/dist/cli.mjs +29893 -0
- package/dist/bundle/package.json +6 -0
- package/dist/bundle/rulepack/rulepack.json +1496 -0
- package/dist/bundle/rules/situaciones.yml +27 -0
- package/dist/cli/benchmark.d.ts +89 -0
- package/dist/cli/benchmark.js +185 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +377 -0
- package/dist/config/core.d.ts +55 -0
- package/dist/config/core.js +244 -0
- package/dist/config/glob.d.ts +2 -0
- package/dist/config/glob.js +72 -0
- package/dist/config/index.d.ts +8 -0
- package/dist/config/index.js +38 -0
- package/dist/config/situaciones.d.ts +17 -0
- package/dist/config/situaciones.js +43 -0
- package/dist/contracts/index.d.ts +238 -0
- package/dist/contracts/index.js +4 -0
- package/dist/document/index.d.ts +10 -0
- package/dist/document/index.js +70 -0
- package/dist/document/text.d.ts +22 -0
- package/dist/document/text.js +91 -0
- package/dist/document/tokenize.d.ts +4 -0
- package/dist/document/tokenize.js +59 -0
- package/dist/markdown/index.d.ts +7 -0
- package/dist/markdown/index.js +178 -0
- package/dist/paths.d.ts +4 -0
- package/dist/paths.js +14 -0
- package/dist/reporters/index.d.ts +29 -0
- package/dist/reporters/index.js +182 -0
- package/dist/rules/compiler/build.d.ts +1 -0
- package/dist/rules/compiler/build.js +46 -0
- package/dist/rules/compiler/index.d.ts +40 -0
- package/dist/rules/compiler/index.js +343 -0
- package/dist/rules/compiler/safety.d.ts +16 -0
- package/dist/rules/compiler/safety.js +124 -0
- package/dist/rules/detectors/index.d.ts +4 -0
- package/dist/rules/detectors/index.js +651 -0
- package/dist/rules/detectors/shared.d.ts +30 -0
- package/dist/rules/detectors/shared.js +87 -0
- package/dist/rules/runtime/index.d.ts +17 -0
- package/dist/rules/runtime/index.js +74 -0
- package/dist/runner/index.d.ts +18 -0
- package/dist/runner/index.js +176 -0
- package/dist/runner/lint-text.d.ts +19 -0
- package/dist/runner/lint-text.js +20 -0
- package/dist/scoring/index.d.ts +16 -0
- package/dist/scoring/index.js +41 -0
- package/dist/suppressions/index.d.ts +23 -0
- package/dist/suppressions/index.js +86 -0
- package/dist/web/engine.meta.json +1 -0
- package/dist/web/engine.mjs +11029 -0
- package/dist/web/index.d.ts +39 -0
- package/dist/web/index.js +37 -0
- package/dist/web/node-crypto-shim.d.ts +9 -0
- package/dist/web/node-crypto-shim.js +24 -0
- package/dist/web/sha256.d.ts +11 -0
- package/dist/web/sha256.js +89 -0
- package/package.json +88 -4
- package/rulepack/rulepack.json +1496 -0
- package/rules/profiles/academico.yml +4 -0
- package/rules/profiles/chat.yml +4 -0
- package/rules/profiles/correo.yml +3 -0
- package/rules/profiles/general.yml +2 -0
- package/rules/profiles/marketing.yml +4 -0
- package/rules/profiles/readme.yml +5 -0
- package/rules/profiles/redes.yml +6 -0
- package/rules/profiles/tecnico.yml +4 -0
- package/rules/situaciones.yml +27 -0
- package/schemas/rule.schema.json +90 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/** Palabras funcionales frecuentes; se excluyen de tríadas y n-gramas. */
|
|
2
|
+
export const STOPWORDS = new Set(("a al algo alguna algunas alguno algunos ante antes aquel aquella aquellas aquellos aquí así aun aunque bajo bien cada casi " +
|
|
3
|
+
"como con contra cual cuales cuando de del desde donde dos e el ella ellas ello ellos en entre era eran es esa esas ese eso esos esta " +
|
|
4
|
+
"estaba estaban estamos están estar estas este esto estos fue fueron ha había habían han hasta hay la las le les lo los más me " +
|
|
5
|
+
"mi mis mientras muy nada ni no nos nosotros nuestra nuestras nuestro nuestros o os otra otras otro otros para pero poco por " +
|
|
6
|
+
"porque que quien quienes se sea sean según ser será serán si sido sin sino sobre son su sus también tan tanto te tener tiene " +
|
|
7
|
+
"tienen toda todas todo todos tras tu tus un una unas uno unos usted ustedes vosotros ya yo él ésta éste").split(" "));
|
|
8
|
+
/** Escapa texto para usarlo literal en una RegExp. */
|
|
9
|
+
export function escapeRe(s) {
|
|
10
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
11
|
+
}
|
|
12
|
+
const LB = "(?<![\\p{L}\\p{M}\\p{N}])";
|
|
13
|
+
const RB = "(?![\\p{L}\\p{M}\\p{N}])";
|
|
14
|
+
/**
|
|
15
|
+
* Construye una única RegExp a partir de una lista de términos o expresiones.
|
|
16
|
+
* Los términos literales admiten espacios (se convierten en `\s+`) y se delimitan por límites de palabra Unicode.
|
|
17
|
+
* Los términos que empiezan por `re:` se insertan como expresión regular (ya validada por el compilador).
|
|
18
|
+
*/
|
|
19
|
+
export function buildTermRegExp(terms, flags = "giu") {
|
|
20
|
+
const alts = [];
|
|
21
|
+
const raw = [];
|
|
22
|
+
for (const t of terms) {
|
|
23
|
+
if (t.startsWith("raw:"))
|
|
24
|
+
raw.push(`(?:${t.slice(4)})`);
|
|
25
|
+
else if (t.startsWith("re:"))
|
|
26
|
+
alts.push(`(?:${t.slice(3)})`);
|
|
27
|
+
else if (t.trim())
|
|
28
|
+
alts.push(escapeRe(t.trim()).replace(/\s+/g, "\\s+"));
|
|
29
|
+
}
|
|
30
|
+
if (alts.length === 0 && raw.length === 0)
|
|
31
|
+
return null;
|
|
32
|
+
// Términos más largos primero para evitar coincidencias parciales.
|
|
33
|
+
alts.sort((a, b) => b.length - a.length);
|
|
34
|
+
const parts = [];
|
|
35
|
+
if (alts.length)
|
|
36
|
+
parts.push(`${LB}(?:${alts.join("|")})${RB}`);
|
|
37
|
+
parts.push(...raw); // `raw:` = expresión sin límites de palabra (símbolos, puntuación).
|
|
38
|
+
return new RegExp(parts.length === 1 ? parts[0] : `(?:${parts.join("|")})`, flags);
|
|
39
|
+
}
|
|
40
|
+
export function wordsIn(block, doc, blockIndex) {
|
|
41
|
+
let n = 0;
|
|
42
|
+
for (const t of doc.tokens) {
|
|
43
|
+
if (t.blockIndex !== blockIndex)
|
|
44
|
+
continue;
|
|
45
|
+
if (t.isWord)
|
|
46
|
+
n++;
|
|
47
|
+
}
|
|
48
|
+
return n;
|
|
49
|
+
}
|
|
50
|
+
/** Devuelve los índices de tokens por bloque (cache barata por documento). */
|
|
51
|
+
export function tokensByBlock(doc) {
|
|
52
|
+
const out = doc.blocks.map(() => []);
|
|
53
|
+
doc.tokens.forEach((t, i) => out[t.blockIndex].push(i));
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
/** Comprueba si una coincidencia cae dentro de una excepción (patrón sobre una ventana de contexto). */
|
|
57
|
+
export function isExcepted(rule, text, start, end) {
|
|
58
|
+
const pats = rule.exceptions.patterns;
|
|
59
|
+
if (!pats || pats.length === 0)
|
|
60
|
+
return false;
|
|
61
|
+
const ctxStart = Math.max(0, start - 40);
|
|
62
|
+
const ctxEnd = Math.min(text.length, end + 40);
|
|
63
|
+
const ctx = text.slice(ctxStart, ctxEnd);
|
|
64
|
+
for (const p of pats) {
|
|
65
|
+
// La excepción debe solapar con la coincidencia, no solo aparecer cerca.
|
|
66
|
+
for (const m of execAll(new RegExp(p, "iu"), ctx)) {
|
|
67
|
+
const ms = ctxStart + m.index;
|
|
68
|
+
const me = ms + m.length;
|
|
69
|
+
if (ms < end && me > start)
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
export function execAll(re, text) {
|
|
76
|
+
const out = [];
|
|
77
|
+
const r = new RegExp(re.source, re.flags.includes("g") ? re.flags : re.flags + "g");
|
|
78
|
+
let m;
|
|
79
|
+
while ((m = r.exec(text)) !== null) {
|
|
80
|
+
if (m[0].length === 0) {
|
|
81
|
+
r.lastIndex++;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
out.push({ index: m.index, length: m[0].length, match: m });
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { CompiledRule, Document, Finding, Level } from "../../contracts/index.js";
|
|
2
|
+
export interface RuntimeOptions {
|
|
3
|
+
/** Nivel efectivo por regla. Las reglas ausentes usan su nivel por defecto; `off` las omite. */
|
|
4
|
+
levels?: Record<string, Level>;
|
|
5
|
+
/** Ruta relativa usada en la huella. */
|
|
6
|
+
relPath?: string;
|
|
7
|
+
/** Incluir snippets (privacidad). */
|
|
8
|
+
snippets?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/** Interpola {clave} en el mensaje con los datos de la coincidencia. */
|
|
11
|
+
export declare function formatMessage(template: string, data: Record<string, string | number> | undefined): string;
|
|
12
|
+
export declare function fingerprint(rule: string, relPath: string, snippet: string, ordinal: number): string;
|
|
13
|
+
/**
|
|
14
|
+
* Ejecuta las reglas compiladas sobre un documento. Puro: no lee archivos, no imprime.
|
|
15
|
+
* Los hallazgos se devuelven ordenados por posición y luego por id de regla.
|
|
16
|
+
*/
|
|
17
|
+
export declare function runRules(doc: Document, rules: CompiledRule[], opts?: RuntimeOptions): Finding[];
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { offsetToLineColumn, snippetOf } from "../../document/text.js";
|
|
3
|
+
import { DETECTORS } from "../detectors/index.js";
|
|
4
|
+
/** Interpola {clave} en el mensaje con los datos de la coincidencia. */
|
|
5
|
+
export function formatMessage(template, data) {
|
|
6
|
+
return template.replace(/\{(\w+)\}/g, (_m, k) => (data && data[k] !== undefined ? String(data[k]) : `{${k}}`));
|
|
7
|
+
}
|
|
8
|
+
function redactText(data) {
|
|
9
|
+
if (!data)
|
|
10
|
+
return data;
|
|
11
|
+
const out = {};
|
|
12
|
+
for (const [k, v] of Object.entries(data))
|
|
13
|
+
out[k] = typeof v === "string" ? "…" : v;
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
16
|
+
function normalizeSnippet(s) {
|
|
17
|
+
return s.toLowerCase().replace(/\s+/g, " ").trim();
|
|
18
|
+
}
|
|
19
|
+
export function fingerprint(rule, relPath, snippet, ordinal) {
|
|
20
|
+
return createHash("sha256")
|
|
21
|
+
.update(`${rule}\0${relPath}\0${normalizeSnippet(snippet)}\0${ordinal}`)
|
|
22
|
+
.digest("hex")
|
|
23
|
+
.slice(0, 24);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Ejecuta las reglas compiladas sobre un documento. Puro: no lee archivos, no imprime.
|
|
27
|
+
* Los hallazgos se devuelven ordenados por posición y luego por id de regla.
|
|
28
|
+
*/
|
|
29
|
+
export function runRules(doc, rules, opts = {}) {
|
|
30
|
+
const findings = [];
|
|
31
|
+
const relPath = opts.relPath ?? doc.path ?? "<texto>";
|
|
32
|
+
const withSnippets = opts.snippets !== false;
|
|
33
|
+
for (const rule of rules) {
|
|
34
|
+
const level = opts.levels?.[rule.id] ?? rule.default_level;
|
|
35
|
+
if (level === "off" || rule.status === "deprecated")
|
|
36
|
+
continue;
|
|
37
|
+
const detector = DETECTORS[rule.detector];
|
|
38
|
+
const matches = detector(rule, doc);
|
|
39
|
+
for (const m of matches) {
|
|
40
|
+
const block = doc.blocks[m.blockIndex];
|
|
41
|
+
const startOff = block.map[m.start];
|
|
42
|
+
const endOff = block.map[m.end];
|
|
43
|
+
const rawSnippet = snippetOf(doc.original, startOff, endOff);
|
|
44
|
+
findings.push({
|
|
45
|
+
rule: rule.id,
|
|
46
|
+
revision: rule.revision,
|
|
47
|
+
level,
|
|
48
|
+
category: rule.category,
|
|
49
|
+
// Sin snippets, el mensaje tampoco cita el texto: los datos de texto ({term}) se sustituyen; los números se quedan.
|
|
50
|
+
message: formatMessage(rule.message, withSnippets ? m.data : redactText(m.data)),
|
|
51
|
+
range: {
|
|
52
|
+
start: { offset: startOff, ...offsetToLineColumn(doc.original, doc.lineStarts, startOff) },
|
|
53
|
+
end: { offset: endOff, ...offsetToLineColumn(doc.original, doc.lineStarts, endOff) },
|
|
54
|
+
},
|
|
55
|
+
snippet: withSnippets ? rawSnippet : "",
|
|
56
|
+
fingerprint: "",
|
|
57
|
+
});
|
|
58
|
+
// La huella usa el snippet real aunque no se exponga.
|
|
59
|
+
findings[findings.length - 1]._raw = rawSnippet;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
findings.sort((a, b) => a.range.start.offset - b.range.start.offset || a.range.end.offset - b.range.end.offset || a.rule.localeCompare(b.rule, "en"));
|
|
63
|
+
// Ordinal por (regla, snippet) para distinguir repeticiones idénticas.
|
|
64
|
+
const counters = new Map();
|
|
65
|
+
for (const f of findings) {
|
|
66
|
+
const raw = f._raw ?? f.snippet;
|
|
67
|
+
const key = `${f.rule}\0${normalizeSnippet(raw)}`;
|
|
68
|
+
const n = counters.get(key) ?? 0;
|
|
69
|
+
counters.set(key, n + 1);
|
|
70
|
+
f.fingerprint = fingerprint(f.rule, relPath.replace(/\\/g, "/"), raw, n);
|
|
71
|
+
delete f._raw;
|
|
72
|
+
}
|
|
73
|
+
return findings;
|
|
74
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Config, FileResult, Finding, PolicyResult, ScanResult } from "../contracts/index.js";
|
|
2
|
+
import { type RunnerContext } from "./lint-text.js";
|
|
3
|
+
export type { RunnerContext } from "./lint-text.js";
|
|
4
|
+
export { lintDocumentText } from "./lint-text.js";
|
|
5
|
+
export declare function discoverFiles(ctx: RunnerContext, targets: string[]): string[];
|
|
6
|
+
export interface ScanOptions {
|
|
7
|
+
targets?: string[];
|
|
8
|
+
/** Ignorar la caché aunque esté activada. */
|
|
9
|
+
noCache?: boolean;
|
|
10
|
+
/** Ruta de baseline (anula la configuración). */
|
|
11
|
+
baseline?: string | null;
|
|
12
|
+
/** Se llama con cada archivo que no se analiza porque no es texto (binario o UTF-16). */
|
|
13
|
+
onUnreadable?: (relPath: string) => void;
|
|
14
|
+
}
|
|
15
|
+
export declare function scanProject(ctx: RunnerContext, opts?: ScanOptions): ScanResult;
|
|
16
|
+
/** La política decide si el proyecto falla. El motor solo detecta. */
|
|
17
|
+
export declare function evaluatePolicy(files: FileResult[], config: Config): PolicyResult;
|
|
18
|
+
export declare function activeFindings(files: FileResult[]): Finding[];
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import fg from "fast-glob";
|
|
5
|
+
import ignore from "ignore";
|
|
6
|
+
import { SCHEMA_VERSION } from "../contracts/index.js";
|
|
7
|
+
import { computeScore } from "../scoring/index.js";
|
|
8
|
+
import { applyBaseline, readBaseline } from "../baseline/index.js";
|
|
9
|
+
import { lintDocumentText } from "./lint-text.js";
|
|
10
|
+
import { matchesAny } from "../config/glob.js";
|
|
11
|
+
export { lintDocumentText } from "./lint-text.js";
|
|
12
|
+
const LEVEL_ORDER = { off: 0, info: 1, warning: 2, error: 3 };
|
|
13
|
+
/* ---------------- Descubrimiento de archivos ---------------- */
|
|
14
|
+
export function discoverFiles(ctx, targets) {
|
|
15
|
+
const root = ctx.root;
|
|
16
|
+
const ig = ignore();
|
|
17
|
+
if (ctx.config.respect_gitignore) {
|
|
18
|
+
const gi = path.join(root, ".gitignore");
|
|
19
|
+
if (fs.existsSync(gi))
|
|
20
|
+
ig.add(fs.readFileSync(gi, "utf8"));
|
|
21
|
+
}
|
|
22
|
+
const files = new Set();
|
|
23
|
+
const explicit = [];
|
|
24
|
+
const patterns = [];
|
|
25
|
+
for (const t of targets.length ? targets : ["."]) {
|
|
26
|
+
const abs = path.resolve(root, t);
|
|
27
|
+
if (fs.existsSync(abs) && fs.statSync(abs).isFile())
|
|
28
|
+
explicit.push(abs);
|
|
29
|
+
else if (fs.existsSync(abs) && fs.statSync(abs).isDirectory()) {
|
|
30
|
+
const rel = path.relative(root, abs).replace(/\\/g, "/");
|
|
31
|
+
// El nombre del directorio va escapado: «docs (v1)» o «notas [2021]» son rutas, no patrones.
|
|
32
|
+
for (const inc of ctx.config.include)
|
|
33
|
+
patterns.push(rel ? `${fg.convertPathToPattern(rel)}/${inc}` : inc);
|
|
34
|
+
}
|
|
35
|
+
else
|
|
36
|
+
patterns.push(t.replace(/\\/g, "/"));
|
|
37
|
+
}
|
|
38
|
+
for (const f of explicit)
|
|
39
|
+
files.add(f);
|
|
40
|
+
if (patterns.length) {
|
|
41
|
+
const found = fg.sync(patterns, { cwd: root, dot: false, onlyFiles: true, ignore: ctx.config.exclude, followSymbolicLinks: false });
|
|
42
|
+
for (const f of found)
|
|
43
|
+
files.add(path.resolve(root, f));
|
|
44
|
+
}
|
|
45
|
+
const out = [];
|
|
46
|
+
for (const abs of files) {
|
|
47
|
+
const rel = path.relative(root, abs).replace(/\\/g, "/");
|
|
48
|
+
if (rel.startsWith("..")) {
|
|
49
|
+
out.push(abs);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
// `exclude` vale también para los archivos nombrados uno a uno (así los pasa pre-commit), igual que .gitignore.
|
|
53
|
+
if (matchesAny(rel, ctx.config.exclude))
|
|
54
|
+
continue;
|
|
55
|
+
if (ctx.config.respect_gitignore && ig.ignores(rel))
|
|
56
|
+
continue;
|
|
57
|
+
out.push(abs);
|
|
58
|
+
}
|
|
59
|
+
return out.sort((a, b) => a.localeCompare(b, "en"));
|
|
60
|
+
}
|
|
61
|
+
function cacheKey(ctx, relPath, content) {
|
|
62
|
+
const h = createHash("sha256");
|
|
63
|
+
h.update(ctx.toolVersion).update("\0").update(ctx.pack.version).update("\0");
|
|
64
|
+
h.update(JSON.stringify(ctx.config)).update("\0").update(relPath).update("\0").update(content);
|
|
65
|
+
// El pack completo también forma parte de la clave (cambios en reglas invalidan).
|
|
66
|
+
h.update(packHash(ctx.pack));
|
|
67
|
+
return h.digest("hex");
|
|
68
|
+
}
|
|
69
|
+
const packHashes = new WeakMap();
|
|
70
|
+
function packHash(pack) {
|
|
71
|
+
let h = packHashes.get(pack);
|
|
72
|
+
if (!h) {
|
|
73
|
+
h = createHash("sha256").update(JSON.stringify(pack.rules)).digest("hex");
|
|
74
|
+
packHashes.set(pack, h);
|
|
75
|
+
}
|
|
76
|
+
return h;
|
|
77
|
+
}
|
|
78
|
+
function cachePath(ctx, key) {
|
|
79
|
+
return path.join(ctx.root, ctx.config.cache.dir, key.slice(0, 2), `${key}.json`);
|
|
80
|
+
}
|
|
81
|
+
export function scanProject(ctx, opts = {}) {
|
|
82
|
+
const t0 = performance.now();
|
|
83
|
+
const files = discoverFiles(ctx, opts.targets ?? []);
|
|
84
|
+
const results = [];
|
|
85
|
+
const useCache = ctx.config.cache.enabled && !opts.noCache;
|
|
86
|
+
for (const abs of files) {
|
|
87
|
+
const rel = path.relative(ctx.root, abs).replace(/\\/g, "/");
|
|
88
|
+
const content = fs.readFileSync(abs, "utf8");
|
|
89
|
+
// Un carácter nulo delata un binario o un UTF-16 leído como UTF-8: analizarlo daría «0 hallazgos» sin haber leído nada.
|
|
90
|
+
if (content.includes("\0")) {
|
|
91
|
+
opts.onUnreadable?.(rel);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
let result = null;
|
|
95
|
+
let key = "";
|
|
96
|
+
if (useCache) {
|
|
97
|
+
key = cacheKey(ctx, rel, content);
|
|
98
|
+
const cp = cachePath(ctx, key);
|
|
99
|
+
if (fs.existsSync(cp)) {
|
|
100
|
+
try {
|
|
101
|
+
const e = JSON.parse(fs.readFileSync(cp, "utf8"));
|
|
102
|
+
if (e.key === key) {
|
|
103
|
+
result = { ...e.result, fromCache: true };
|
|
104
|
+
// Las supresiones por baseline se recalculan más abajo; limpiamos las antiguas.
|
|
105
|
+
for (const f of result.findings)
|
|
106
|
+
if (f.suppressed?.by === "baseline")
|
|
107
|
+
delete f.suppressed;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
result = null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (!result) {
|
|
116
|
+
result = lintDocumentText(content, ctx, { relPath: rel });
|
|
117
|
+
if (useCache) {
|
|
118
|
+
const cp = cachePath(ctx, key);
|
|
119
|
+
fs.mkdirSync(path.dirname(cp), { recursive: true });
|
|
120
|
+
fs.writeFileSync(cp, JSON.stringify({ key, result }), "utf8");
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
results.push(result);
|
|
124
|
+
}
|
|
125
|
+
results.sort((a, b) => a.path.localeCompare(b.path, "en"));
|
|
126
|
+
let baselineInfo;
|
|
127
|
+
const baselinePath = opts.baseline === undefined ? ctx.config.baseline.path : opts.baseline;
|
|
128
|
+
if (baselinePath) {
|
|
129
|
+
const abs = path.resolve(ctx.root, baselinePath);
|
|
130
|
+
if (fs.existsSync(abs)) {
|
|
131
|
+
const b = readBaseline(abs);
|
|
132
|
+
const r = applyBaseline(results, b);
|
|
133
|
+
baselineInfo = { path: baselinePath, matched: r.matched, stale: r.stale.length };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// Recalcular el índice tras la baseline.
|
|
137
|
+
const ruleMap = new Map(ctx.pack.rules.map((r) => [r.id, r]));
|
|
138
|
+
for (const r of results) {
|
|
139
|
+
r.score = computeScore(r.findings, ruleMap, r.score.eligibleWords, ctx.config.min_words_for_index);
|
|
140
|
+
}
|
|
141
|
+
const policy = evaluatePolicy(results, ctx.config);
|
|
142
|
+
return {
|
|
143
|
+
schema_version: SCHEMA_VERSION,
|
|
144
|
+
tool: { name: "textoneitor", version: ctx.toolVersion, rulepack: ctx.pack.version },
|
|
145
|
+
files: results,
|
|
146
|
+
policy,
|
|
147
|
+
baseline: baselineInfo,
|
|
148
|
+
durationMs: Math.round(performance.now() - t0),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/** La política decide si el proyecto falla. El motor solo detecta. */
|
|
152
|
+
export function evaluatePolicy(files, config) {
|
|
153
|
+
const counts = { error: 0, warning: 0, info: 0, suppressed: 0 };
|
|
154
|
+
const reasons = [];
|
|
155
|
+
for (const f of files) {
|
|
156
|
+
for (const x of f.findings) {
|
|
157
|
+
if (x.suppressed)
|
|
158
|
+
counts.suppressed++;
|
|
159
|
+
else
|
|
160
|
+
counts[x.level]++;
|
|
161
|
+
}
|
|
162
|
+
if (config.max_index !== null && f.score.index !== null && f.score.index > config.max_index) {
|
|
163
|
+
reasons.push(`${f.path}: índice ${f.score.index} > máximo ${config.max_index}`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (config.fail_on !== "never") {
|
|
167
|
+
const threshold = LEVEL_ORDER[config.fail_on];
|
|
168
|
+
const n = ["error", "warning", "info"].filter((l) => LEVEL_ORDER[l] >= threshold).reduce((a, l) => a + counts[l], 0);
|
|
169
|
+
if (n > 0)
|
|
170
|
+
reasons.push(`${n} hallazgo(s) de nivel ${config.fail_on} o superior`);
|
|
171
|
+
}
|
|
172
|
+
return { ok: reasons.length === 0, failOn: config.fail_on, maxIndex: config.max_index, reasons, counts };
|
|
173
|
+
}
|
|
174
|
+
export function activeFindings(files) {
|
|
175
|
+
return files.flatMap((f) => f.findings.filter((x) => !x.suppressed));
|
|
176
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Análisis de un texto en memoria. Puro: no lee archivos ni escribe nada.
|
|
3
|
+
*
|
|
4
|
+
* Vive fuera de `runner/index.ts` porque ese módulo trae `node:fs` y `fast-glob` para
|
|
5
|
+
* descubrir archivos, y esto tiene que poder ejecutarse también en el navegador
|
|
6
|
+
* (`src/web/index.ts`). Es la misma función para los dos caminos: lo que analiza la CLI
|
|
7
|
+
* y lo que analiza la web salen de aquí.
|
|
8
|
+
*/
|
|
9
|
+
import type { Config, FileResult, RulePack } from "../contracts/index.js";
|
|
10
|
+
export interface RunnerContext {
|
|
11
|
+
config: Config;
|
|
12
|
+
root: string;
|
|
13
|
+
pack: RulePack;
|
|
14
|
+
toolVersion: string;
|
|
15
|
+
}
|
|
16
|
+
export declare function lintDocumentText(text: string, ctx: RunnerContext, opts?: {
|
|
17
|
+
relPath?: string;
|
|
18
|
+
format?: "text" | "markdown";
|
|
19
|
+
}): FileResult;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { buildDocument, detectFormat } from "../document/index.js";
|
|
2
|
+
import { runRules } from "../rules/runtime/index.js";
|
|
3
|
+
import { computeScore } from "../scoring/index.js";
|
|
4
|
+
import { applySuppressions, parseSuppressions } from "../suppressions/index.js";
|
|
5
|
+
import { resolveEffectiveRules } from "../config/core.js";
|
|
6
|
+
export function lintDocumentText(text, ctx, opts = {}) {
|
|
7
|
+
const t0 = performance.now();
|
|
8
|
+
const relPath = (opts.relPath ?? "<texto>").replace(/\\/g, "/");
|
|
9
|
+
const format = opts.format ?? detectFormat(relPath === "<texto>" ? null : relPath);
|
|
10
|
+
const doc = buildDocument(text, { path: relPath, format });
|
|
11
|
+
const effective = resolveEffectiveRules(ctx.config, ctx.pack.rules, relPath === "<texto>" ? null : relPath);
|
|
12
|
+
const levels = {};
|
|
13
|
+
for (const e of effective)
|
|
14
|
+
levels[e.rule] = e.level;
|
|
15
|
+
const findings = runRules(doc, ctx.pack.rules, { levels, relPath, snippets: ctx.config.privacy.snippets });
|
|
16
|
+
applySuppressions(findings, parseSuppressions(doc.original, doc.lineStarts, doc.codeRanges));
|
|
17
|
+
const ruleMap = new Map(ctx.pack.rules.map((r) => [r.id, r]));
|
|
18
|
+
const score = computeScore(findings, ruleMap, doc.eligibleWords, ctx.config.min_words_for_index);
|
|
19
|
+
return { path: relPath, format, findings, score, durationMs: Math.round((performance.now() - t0) * 100) / 100 };
|
|
20
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CompiledRule, Finding, ScoreResult } from "../contracts/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Índice de patrones editoriales (0-100). Única fórmula del producto.
|
|
4
|
+
*
|
|
5
|
+
* Para cada regla con hallazgos no suprimidos:
|
|
6
|
+
* contribución = weight * min(cap, 1 + ln(n)) (rendimientos decrecientes por repetición)
|
|
7
|
+
* Suma de contribuciones, normalizada por palabras elegibles (por cada 1000) y comprimida
|
|
8
|
+
* con una curva saturante para acotar en 0-100:
|
|
9
|
+
* raw = 1000 * sum / eligibleWords
|
|
10
|
+
* index = round(100 * raw / (raw + K))
|
|
11
|
+
* K se fija en 40: un texto con ~40 puntos de contribución por mil palabras marca 50.
|
|
12
|
+
*
|
|
13
|
+
* Mide densidad de señales editoriales. No es una probabilidad de autoría.
|
|
14
|
+
*/
|
|
15
|
+
export declare const SCORE_K = 40;
|
|
16
|
+
export declare function computeScore(findings: Finding[], rules: Map<string, CompiledRule>, eligibleWords: number, minWords: number): ScoreResult;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Índice de patrones editoriales (0-100). Única fórmula del producto.
|
|
3
|
+
*
|
|
4
|
+
* Para cada regla con hallazgos no suprimidos:
|
|
5
|
+
* contribución = weight * min(cap, 1 + ln(n)) (rendimientos decrecientes por repetición)
|
|
6
|
+
* Suma de contribuciones, normalizada por palabras elegibles (por cada 1000) y comprimida
|
|
7
|
+
* con una curva saturante para acotar en 0-100:
|
|
8
|
+
* raw = 1000 * sum / eligibleWords
|
|
9
|
+
* index = round(100 * raw / (raw + K))
|
|
10
|
+
* K se fija en 40: un texto con ~40 puntos de contribución por mil palabras marca 50.
|
|
11
|
+
*
|
|
12
|
+
* Mide densidad de señales editoriales. No es una probabilidad de autoría.
|
|
13
|
+
*/
|
|
14
|
+
export const SCORE_K = 40;
|
|
15
|
+
export function computeScore(findings, rules, eligibleWords, minWords) {
|
|
16
|
+
const perRule = new Map();
|
|
17
|
+
for (const f of findings) {
|
|
18
|
+
if (f.suppressed)
|
|
19
|
+
continue;
|
|
20
|
+
perRule.set(f.rule, (perRule.get(f.rule) ?? 0) + 1);
|
|
21
|
+
}
|
|
22
|
+
const contributors = [...perRule.entries()]
|
|
23
|
+
.map(([rule, n]) => {
|
|
24
|
+
const r = rules.get(rule);
|
|
25
|
+
const weight = r?.score.weight ?? 1;
|
|
26
|
+
const cap = r?.score.cap ?? 3;
|
|
27
|
+
const contribution = weight * Math.min(cap, 1 + Math.log(n));
|
|
28
|
+
return { rule, findings: n, contribution: round2(contribution) };
|
|
29
|
+
})
|
|
30
|
+
.sort((a, b) => b.contribution - a.contribution || a.rule.localeCompare(b.rule, "en"));
|
|
31
|
+
if (eligibleWords < minWords) {
|
|
32
|
+
return { index: null, eligibleWords, minWords, contributors };
|
|
33
|
+
}
|
|
34
|
+
const sum = contributors.reduce((a, c) => a + c.contribution, 0);
|
|
35
|
+
const raw = eligibleWords > 0 ? (1000 * sum) / eligibleWords : 0;
|
|
36
|
+
const index = Math.round((100 * raw) / (raw + SCORE_K));
|
|
37
|
+
return { index, eligibleWords, minWords, contributors };
|
|
38
|
+
}
|
|
39
|
+
function round2(n) {
|
|
40
|
+
return Math.round(n * 100) / 100;
|
|
41
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Finding } from "../contracts/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Supresiones inline en Markdown mediante comentarios HTML (también válidas en texto plano):
|
|
4
|
+
* <!-- textoneitor-disable --> desactiva todo hasta el enable
|
|
5
|
+
* <!-- textoneitor-enable -->
|
|
6
|
+
* <!-- textoneitor-disable regla1, regla2 --> desactiva reglas concretas
|
|
7
|
+
* <!-- textoneitor-disable-next-line [reglas] --> siguiente línea
|
|
8
|
+
* <!-- textoneitor-disable-line [reglas] --> misma línea
|
|
9
|
+
* <!-- textoneitor-disable-file [reglas] --> todo el archivo
|
|
10
|
+
* Se admite `motivo: ...` tras `--` en el mismo comentario: <!-- textoneitor-disable regla -- motivo: cita literal -->
|
|
11
|
+
*
|
|
12
|
+
* `ia-linter-` sigue valiendo, que es como se llamaba antes y hay textos escritos con él.
|
|
13
|
+
*/
|
|
14
|
+
export interface SuppressionDirective {
|
|
15
|
+
kind: "disable" | "enable" | "next-line" | "line" | "file";
|
|
16
|
+
rules: string[] | null;
|
|
17
|
+
line: number;
|
|
18
|
+
offset: number;
|
|
19
|
+
reason?: string;
|
|
20
|
+
}
|
|
21
|
+
export declare function parseSuppressions(text: string, lineStarts: number[], ignore?: [number, number][]): SuppressionDirective[];
|
|
22
|
+
/** Marca como suprimidos los hallazgos afectados por directivas. Devuelve el mismo array (mutado). */
|
|
23
|
+
export declare function applySuppressions(findings: Finding[], directives: SuppressionDirective[]): Finding[];
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const DIRECTIVE_RE = /<!--\s*(?:textoneitor|ia-linter)-(disable-next-line|disable-line|disable-file|disable|enable)\b([^>]*?)-->/g;
|
|
2
|
+
export function parseSuppressions(text, lineStarts, ignore = []) {
|
|
3
|
+
const out = [];
|
|
4
|
+
const re = new RegExp(DIRECTIVE_RE.source, "g");
|
|
5
|
+
let m;
|
|
6
|
+
while ((m = re.exec(text)) !== null) {
|
|
7
|
+
// Una directiva dentro de código (un ejemplo en la documentación) no es una directiva.
|
|
8
|
+
const at = m.index;
|
|
9
|
+
if (ignore.some(([s, e]) => at >= s && at < e))
|
|
10
|
+
continue;
|
|
11
|
+
const kindRaw = m[1];
|
|
12
|
+
let body = (m[2] ?? "").trim();
|
|
13
|
+
let reason;
|
|
14
|
+
const sep = body.indexOf("--");
|
|
15
|
+
if (sep !== -1) {
|
|
16
|
+
reason = body.slice(sep + 2).replace(/^\s*motivo\s*:\s*/i, "").trim() || undefined;
|
|
17
|
+
body = body.slice(0, sep).trim();
|
|
18
|
+
}
|
|
19
|
+
const rules = body ? body.split(/[,\s]+/).filter(Boolean) : null;
|
|
20
|
+
const kind = kindRaw === "disable-next-line" ? "next-line" : kindRaw === "disable-line" ? "line" : kindRaw === "disable-file" ? "file" : kindRaw;
|
|
21
|
+
out.push({ kind, rules, line: lineOf(lineStarts, m.index), offset: m.index, reason });
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
function lineOf(lineStarts, offset) {
|
|
26
|
+
let lo = 0;
|
|
27
|
+
let hi = lineStarts.length - 1;
|
|
28
|
+
while (lo < hi) {
|
|
29
|
+
const mid = (lo + hi + 1) >> 1;
|
|
30
|
+
if (lineStarts[mid] <= offset)
|
|
31
|
+
lo = mid;
|
|
32
|
+
else
|
|
33
|
+
hi = mid - 1;
|
|
34
|
+
}
|
|
35
|
+
return lo + 1;
|
|
36
|
+
}
|
|
37
|
+
function ruleMatches(rules, id) {
|
|
38
|
+
if (rules === null)
|
|
39
|
+
return true;
|
|
40
|
+
return rules.some((r) => r === id || (r.endsWith("/*") && id.startsWith(r.slice(0, -1))));
|
|
41
|
+
}
|
|
42
|
+
/** Marca como suprimidos los hallazgos afectados por directivas. Devuelve el mismo array (mutado). */
|
|
43
|
+
export function applySuppressions(findings, directives) {
|
|
44
|
+
if (directives.length === 0)
|
|
45
|
+
return findings;
|
|
46
|
+
const fileRules = directives.filter((d) => d.kind === "file");
|
|
47
|
+
// Rangos disable/enable ordenados por offset.
|
|
48
|
+
const ranges = [];
|
|
49
|
+
const open = [];
|
|
50
|
+
for (const d of [...directives].sort((a, b) => a.offset - b.offset)) {
|
|
51
|
+
if (d.kind === "disable")
|
|
52
|
+
open.push({ from: d.offset, rules: d.rules, reason: d.reason });
|
|
53
|
+
else if (d.kind === "enable") {
|
|
54
|
+
for (let i = open.length - 1; i >= 0; i--) {
|
|
55
|
+
const o = open[i];
|
|
56
|
+
const closes = d.rules === null || o.rules === null || o.rules.every((r) => d.rules.includes(r));
|
|
57
|
+
if (closes) {
|
|
58
|
+
ranges.push({ from: o.from, to: d.offset, rules: o.rules, reason: o.reason });
|
|
59
|
+
open.splice(i, 1);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
for (const o of open)
|
|
65
|
+
ranges.push({ from: o.from, to: Number.MAX_SAFE_INTEGER, rules: o.rules, reason: o.reason });
|
|
66
|
+
const lineDirectives = directives.filter((d) => d.kind === "line" || d.kind === "next-line");
|
|
67
|
+
for (const f of findings) {
|
|
68
|
+
if (f.suppressed)
|
|
69
|
+
continue;
|
|
70
|
+
const fd = fileRules.find((d) => ruleMatches(d.rules, f.rule));
|
|
71
|
+
if (fd) {
|
|
72
|
+
f.suppressed = { by: "inline", reason: fd.reason };
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const startLine = f.range.start.line;
|
|
76
|
+
const ld = lineDirectives.find((d) => ruleMatches(d.rules, f.rule) && (d.kind === "line" ? d.line === startLine : d.line + 1 === startLine));
|
|
77
|
+
if (ld) {
|
|
78
|
+
f.suppressed = { by: "inline", reason: ld.reason };
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const rg = ranges.find((r) => f.range.start.offset >= r.from && f.range.start.offset < r.to && ruleMatches(r.rules, f.rule));
|
|
82
|
+
if (rg)
|
|
83
|
+
f.suppressed = { by: "inline", reason: rg.reason };
|
|
84
|
+
}
|
|
85
|
+
return findings;
|
|
86
|
+
}
|