File size: 3,322 Bytes
37a34fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// File-level encoding/structure rules — not tied to any registry, since
// these operate on raw file bytes/text rather than parsed records.
// Expects records shaped `{ path, text }` (see bin/validate.mjs /
// src/validate.mjs's file-scan helper for how these are built).

import { countBraces } from '../src/edit.mjs';

/**
 * "no-bom" — a UTF-8 BOM on a history/common file is unexpected: vanilla's
 * own history/common files have none (only vanilla's separate
 * localisation/*.yml files do, which are out of scope for this rule —
 * loc files are checked separately via the `loc` rule category, not this
 * one). A stray BOM on a data file the game itself doesn't expect there is
 * a real risk of the file failing to parse or parsing with a corrupted
 * first token.
 */
export const noBomRule = {
  id: 'no-bom',
  level: 'error',
  when: () => true,
  check: (record) => !record.bom,
  message: (record) => `${record.path}: file has a UTF-8 BOM (unexpected for history/common data files)`,
  note: 'Vanilla history/common data files never carry a BOM; one appearing here most likely means '
    + 'the file was saved by an editor that adds one by default, and the game may not parse it correctly.',
};

/**
 * "no-astral" — any character above U+FFFF (an astral/surrogate-pair code
 * point) in a history/common file is a strong signal of a mis-encoding
 * (e.g. two independent CP1252-mapped bytes accidentally recombining into
 * one astral code point on decode — see codec.mjs's header note on exactly
 * this failure mode) rather than an intentional character.
 */
export const noAstralRule = {
  id: 'no-astral',
  level: 'error',
  when: () => true,
  check: (record) => ![...record.text].some((ch) => ch.codePointAt(0) > 0xffff),
  message: (record) => `${record.path}: contains a character above U+FFFF (likely a mis-encoding, not an intentional character)`,
  note: 'EU4 data/localisation text is not expected to legitimately contain astral characters; when '
    + 'one appears it has, in practice, been two independent CP1252-mapped bytes recombining into a '
    + 'surrogate pair on decode — a mis-encoding, not real content.',
};

/**
 * "brace-balance" — comment-aware brace balance (reusing edit.mjs's own
 * countBraces, not a second hand-written brace scanner) must be even and
 * open===close. A naive `{`/`}` text scan is fooled by comments that
 * themselves contain a brace (real case: `# The Eden Agreement }`), which
 * is exactly why this reuses the lexer-based comment-aware counter instead
 * of re-implementing a second, potentially-diverging brace scan.
 */
export const braceBalanceRule = {
  id: 'brace-balance',
  level: 'error',
  when: () => true,
  check: (record) => {
    const { open, close } = countBraces(record.text);
    return open === close;
  },
  message: (record) => {
    const { open, close } = countBraces(record.text);
    return `${record.path}: unbalanced braces (open=${open}, close=${close})`;
  },
  note: 'An unbalanced file will either fail to load or silently absorb everything after the '
    + 'imbalance into the wrong scope. Comment-aware counting (via edit.mjs\'s countBraces) avoids '
    + 'false positives from braces inside comments, e.g. "# The Eden Agreement }".',
};

export default [noBomRule, noAstralRule, braceBalanceRule];