@preventive/triage 1.0.0-alpha.64 → 1.0.0-alpha.65

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preventive/triage",
3
- "version": "1.0.0-alpha.64",
3
+ "version": "1.0.0-alpha.65",
4
4
  "description": "Client & relay server for triaging of automated reports",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "author": {
@@ -25,6 +25,12 @@
25
25
  // triple-slash comment that leads one side, where a directive steers, and
26
26
  // stands below a statement on the other, where it is prose, its line the same
27
27
  // on both (`moved`). Its diff is drawn whole, as one not proven is.
28
+ //
29
+ // Each sets aside a file's own source map comment, as tsc writes one with a
30
+ // declaration map: its last line exactly `//# sourceMappingURL=` its own name
31
+ // `.map`, a newline after it or none (withoutOwnMap). It names the file's own
32
+ // map, for a debugger alone, and is taken for one of its types: the file is
33
+ // proven, and its lines that run told, without it.
28
34
  import { stripTypeScriptTypes } from 'node:module'
29
35
  import { compareTrees, proveTypeOnly } from '@preventive/compare'
30
36
  import { parseSync } from 'oxc-parser'
@@ -39,9 +45,33 @@ const BLANK = /^\s*$/u
39
45
 
40
46
  const tree = file => (file ? [{ name: file.path, data: file.text }] : [])
41
47
 
48
+ // A file without its own source map comment, where its last line is that and
49
+ // nothing else: `//# sourceMappingURL=` and its name, `user.d.ts.map` for
50
+ // `types/user.d.ts`, ending the file with a newline or without. Any other
51
+ // name or spelling, or anything after it, and the file is as it is; so too
52
+ // where its own name is not plain (PLAIN_NAME): one holding a line terminator
53
+ // would end the comment within it, and what follows would run, so no other
54
+ // character than Latin letters, digits, `-`, `_` and `.` is taken in one.
55
+ const PLAIN_NAME = /^[A-Za-z0-9._-]+$/u
56
+ function withoutOwnMap(file) {
57
+ if (file === null) return null
58
+ const name = file.path.slice(file.path.lastIndexOf('/') + 1)
59
+ if (!PLAIN_NAME.test(name)) return file
60
+ const comment = `//# sourceMappingURL=${name}.map`
61
+ for (const last of [comment, `${comment}\n`]) {
62
+ const before = file.text.endsWith(last) ? file.text.slice(0, -last.length) : null
63
+ if (before !== null && (before === '' || before === '\uFEFF' || before.endsWith('\n'))) return { ...file, text: before }
64
+ }
65
+ return file
66
+ }
67
+
68
+ // Two the same but for their own source map comments changed nothing else:
69
+ // in types alone, where each is a declaration file proven to hold types alone.
42
70
  export function typeOnlyStamped(from, to) {
43
- const changes = compareTrees(tree(from), tree(to), { paths: 'keys', lines: false, renames: Number.MIN_VALUE })
44
- return changes.length > 0 && changes.every(change => change.typeOnly === true)
71
+ const [base, other] = [from, to].map(withoutOwnMap)
72
+ const changes = compareTrees(tree(base), tree(other), { paths: 'keys', lines: false, renames: Number.MIN_VALUE })
73
+ if (changes.length > 0) return changes.every(change => change.typeOnly === true)
74
+ return [base, other].every(file => file !== null && DECLARATION.test(file.path) && proveTypeOnly(file.text).proven)
45
75
  }
46
76
 
47
77
  // The lines of a text proven to hold types alone but for what runs that hold
@@ -52,7 +82,7 @@ export function typeOnlyStamped(from, to) {
52
82
  // it leaves is kept; what it reads starts past a byte order mark, and so do
53
83
  // the offsets in its tree.
54
84
  function runLines(text) {
55
- const shift = text.startsWith('') ? 1 : 0
85
+ const shift = text.startsWith('\uFEFF') ? 1 : 0
56
86
  const body = text.slice(shift)
57
87
  const code = stripTypeScriptTypes(body, { mode: 'strip' })
58
88
  const { program, comments } = parseSync('file.ts', body, { lang: 'ts', sourceType: 'module' })
@@ -94,7 +124,8 @@ const moved = (one, other) => [...one.leading].some(comment => !other.leading.ha
94
124
  // hold types alone but for what runs, and no directive steers on one side
95
125
  // alone unseen (`moved`); else false, and where the proof throws, as no
96
126
  // check should. Asked of a pair not stamped, which always runs some.
97
- export function typeOnlyRuns(from, to) {
127
+ export function typeOnlyRuns(...pair) {
128
+ const [from, to] = pair.map(withoutOwnMap)
98
129
  const sides = [from, to].filter(file => file !== null)
99
130
  if (sides.length === 0 || !sides.every(file => DECLARATION.test(file.path))) return false
100
131
  try {