specshield 3.4.14 → 3.4.16
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/CHANGELOG.md +58 -1
- package/README.md +186 -1
- package/package.json +3 -2
- package/src/api/bdctClient.js +8 -0
- package/src/cli.js +4 -0
- package/src/commands/compare.js +7 -1
- package/src/commands/deprecations.js +236 -0
- package/src/commands/fix.js +173 -0
- package/src/core/classifyChanges.js +1 -1
- package/src/core/configLoader.js +6 -1
- package/src/core/fix/fixSpec.js +179 -0
- package/src/core/fix/locate.js +263 -0
- package/src/core/fix/ops.js +233 -0
- package/src/core/fix/shim/spring.js +532 -0
- package/src/core/fix/shim/transforms.js +231 -0
- package/src/core/fix/strategies.js +578 -0
- package/src/core/outputFormatter.js +23 -1
- package/src/core/usage/deprecations.js +438 -0
- package/src/core/usage/impact.js +42 -24
- package/src/core/usage/usageStore.js +32 -5
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const YAML = require('yaml');
|
|
4
|
+
const jsYaml = require('js-yaml');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Edits a fix makes to the target spec, as data:
|
|
8
|
+
*
|
|
9
|
+
* { op: 'set', path, value } set a key (parents created as needed)
|
|
10
|
+
* { op: 'delete', path } remove a key
|
|
11
|
+
* { op: 'push', path, value } append to an array unless already there
|
|
12
|
+
* { op: 'pull', path, value } remove a value from an array (drops the array if it empties)
|
|
13
|
+
* { op: 'insert', path, index, value } insert into an array at index
|
|
14
|
+
*
|
|
15
|
+
* The same ops apply to a plain object (to verify the fix with the diff
|
|
16
|
+
* engine) and to a `yaml` Document (to write the fix into the author's file
|
|
17
|
+
* with comments, ordering and quoting intact).
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
function clone(v) {
|
|
21
|
+
return v === undefined ? undefined : JSON.parse(JSON.stringify(v, (k, x) => (x instanceof Date ? x.toISOString().slice(0, 10) : x)));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// ─── plain objects ──────────────────────────────────────────────────────────
|
|
25
|
+
|
|
26
|
+
function applyOps(doc, ops) {
|
|
27
|
+
for (const o of ops) applyOne(doc, o);
|
|
28
|
+
return doc;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function applyOne(doc, o) {
|
|
32
|
+
const parentPath = o.path.slice(0, -1);
|
|
33
|
+
const key = o.path[o.path.length - 1];
|
|
34
|
+
if (o.op === 'set') {
|
|
35
|
+
const parent = ensure(doc, parentPath);
|
|
36
|
+
parent[key] = clone(o.value);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const parent = getAt(doc, parentPath);
|
|
40
|
+
if (!parent || typeof parent !== 'object') return;
|
|
41
|
+
if (o.op === 'delete') { delete parent[key]; return; }
|
|
42
|
+
if (o.op === 'push') {
|
|
43
|
+
if (!Array.isArray(parent[key])) parent[key] = [];
|
|
44
|
+
if (!parent[key].some((x) => same(x, o.value))) parent[key].push(clone(o.value));
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (o.op === 'pull') {
|
|
48
|
+
if (!Array.isArray(parent[key])) return;
|
|
49
|
+
parent[key] = parent[key].filter((x) => !same(x, o.value));
|
|
50
|
+
if (parent[key].length === 0) delete parent[key];
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (o.op === 'insert') {
|
|
54
|
+
if (!Array.isArray(parent[key])) parent[key] = [];
|
|
55
|
+
parent[key].splice(o.index, 0, clone(o.value));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function getAt(doc, path) {
|
|
60
|
+
let cur = doc;
|
|
61
|
+
for (const k of path) {
|
|
62
|
+
if (cur === null || typeof cur !== 'object') return undefined;
|
|
63
|
+
cur = cur[k];
|
|
64
|
+
}
|
|
65
|
+
return cur;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function ensure(doc, path) {
|
|
69
|
+
let cur = doc;
|
|
70
|
+
for (const k of path) {
|
|
71
|
+
if (cur[k] === null || typeof cur[k] !== 'object') cur[k] = {};
|
|
72
|
+
cur = cur[k];
|
|
73
|
+
}
|
|
74
|
+
return cur;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function same(a, b) {
|
|
78
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ─── yaml Documents ─────────────────────────────────────────────────────────
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Applies ops to the source text of a YAML or JSON spec and returns the new
|
|
85
|
+
* text. YAML keeps its comments and layout; JSON keeps its indentation.
|
|
86
|
+
*/
|
|
87
|
+
function applyOpsToText(text, ops, { json = false } = {}) {
|
|
88
|
+
if (json) {
|
|
89
|
+
const doc = JSON.parse(text);
|
|
90
|
+
applyOps(doc, ops);
|
|
91
|
+
const indent = (/\n([ \t]+)"/.exec(text) || [, ' '])[1];
|
|
92
|
+
return JSON.stringify(doc, null, indent) + (text.endsWith('\n') ? '\n' : '');
|
|
93
|
+
}
|
|
94
|
+
const doc = YAML.parseDocument(text, { keepSourceTokens: false });
|
|
95
|
+
for (const o of ops) applyYaml(doc, o);
|
|
96
|
+
const singles = (text.match(/'/g) || []).length;
|
|
97
|
+
const doubles = (text.match(/"/g) || []).length;
|
|
98
|
+
const out = doc.toString({ lineWidth: 0, flowCollectionPadding: false, singleQuote: singles > doubles });
|
|
99
|
+
return keepOriginalLines(text, out);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The yaml printer re-renders lines it didn't change: comment spacing,
|
|
104
|
+
* padding inside [ ] and { }, quote style. Put the author's line back
|
|
105
|
+
* wherever the new line is the same content, so the diff shows only the fix.
|
|
106
|
+
* Lines are matched in order, as far ahead as the next 200 original lines.
|
|
107
|
+
*/
|
|
108
|
+
function keepOriginalLines(original, rendered) {
|
|
109
|
+
const src = original.split('\n');
|
|
110
|
+
const out = rendered.split('\n');
|
|
111
|
+
// Indentation is structure in YAML: it must match exactly.
|
|
112
|
+
const norm = (l) => {
|
|
113
|
+
const [, indent, rest] = /^(\s*)(.*)$/.exec(l);
|
|
114
|
+
return indent + rest.replace(/\s+#/, ' #').replace(/\s+/g, ' ').replace(/([[{]) /g, '$1').replace(/ ([\]}])/g, '$1').replace(/"/g, "'").trimEnd();
|
|
115
|
+
};
|
|
116
|
+
const keys = src.map(norm);
|
|
117
|
+
let i = 0;
|
|
118
|
+
const result = out.map((line) => {
|
|
119
|
+
const n = norm(line);
|
|
120
|
+
for (let k = i; k < Math.min(src.length, i + 200); k++) {
|
|
121
|
+
if (keys[k] === n) { i = k + 1; return src[k]; }
|
|
122
|
+
}
|
|
123
|
+
return line;
|
|
124
|
+
});
|
|
125
|
+
return result.join('\n');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function applyYaml(doc, o) {
|
|
129
|
+
const parentPath = o.path.slice(0, -1);
|
|
130
|
+
const key = o.path[o.path.length - 1];
|
|
131
|
+
if (o.op === 'set') {
|
|
132
|
+
const parent = ensureNode(doc, parentPath);
|
|
133
|
+
const pair = findPair(parent, key);
|
|
134
|
+
const node = createNode(doc, o.value);
|
|
135
|
+
if (pair) pair.value = node;
|
|
136
|
+
else parent.add(doc.createPair(keyScalar(key), node));
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const parent = nodeAt(doc, parentPath);
|
|
140
|
+
if (!parent) return;
|
|
141
|
+
if (o.op === 'delete') {
|
|
142
|
+
if (YAML.isMap(parent)) {
|
|
143
|
+
const pair = findPair(parent, key);
|
|
144
|
+
if (pair) parent.items.splice(parent.items.indexOf(pair), 1);
|
|
145
|
+
}
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
let seq = childOf(parent, key);
|
|
149
|
+
if (o.op === 'pull') {
|
|
150
|
+
if (!YAML.isSeq(seq)) return;
|
|
151
|
+
seq.items = seq.items.filter((n) => !same(n.toJSON ? n.toJSON() : n, o.value));
|
|
152
|
+
if (seq.items.length === 0) applyYaml(doc, { op: 'delete', path: o.path });
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (!YAML.isSeq(seq)) {
|
|
156
|
+
applyYaml(doc, { op: 'set', path: o.path, value: [] });
|
|
157
|
+
seq = childOf(nodeAt(doc, parentPath), key);
|
|
158
|
+
}
|
|
159
|
+
if (o.op === 'push') {
|
|
160
|
+
if (!seq.items.some((n) => same(n.toJSON ? n.toJSON() : n, o.value))) seq.items.push(createNode(doc, o.value));
|
|
161
|
+
} else if (o.op === 'insert') {
|
|
162
|
+
seq.items.splice(o.index, 0, createNode(doc, o.value));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* A string that YAML would read back as something else — a date like
|
|
168
|
+
* 2027-03-31, a number, true/null — is quoted, so the file says exactly what
|
|
169
|
+
* was verified.
|
|
170
|
+
*/
|
|
171
|
+
function createNode(doc, value) {
|
|
172
|
+
const node = doc.createNode(clone(value));
|
|
173
|
+
YAML.visit(node, {
|
|
174
|
+
Scalar(_, s) {
|
|
175
|
+
// Judged by js-yaml, which reads the spec (YAML 1.1: dates are timestamps).
|
|
176
|
+
if (typeof s.value === 'string' && readsAsOther(s.value)) s.type = YAML.Scalar.QUOTE_SINGLE;
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
return node;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function readsAsOther(str) {
|
|
183
|
+
try {
|
|
184
|
+
return jsYaml.load(str) !== str;
|
|
185
|
+
} catch {
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Keys in a spec are strings, but YAML may hold `200:` as a number — match by string form. */
|
|
191
|
+
function findPair(map, key) {
|
|
192
|
+
if (!YAML.isMap(map)) return null;
|
|
193
|
+
return map.items.find((p) => String(YAML.isScalar(p.key) ? p.key.value : p.key) === String(key)) || null;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function childOf(node, key) {
|
|
197
|
+
if (YAML.isMap(node)) { const p = findPair(node, key); return p ? p.value : undefined; }
|
|
198
|
+
if (YAML.isSeq(node)) return node.items[Number(key)];
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function nodeAt(doc, path) {
|
|
203
|
+
let cur = doc.contents;
|
|
204
|
+
for (const k of path) {
|
|
205
|
+
cur = childOf(cur, k);
|
|
206
|
+
if (cur === undefined || cur === null) return undefined;
|
|
207
|
+
}
|
|
208
|
+
return cur;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function ensureNode(doc, path) {
|
|
212
|
+
let cur = doc.contents;
|
|
213
|
+
for (const k of path) {
|
|
214
|
+
let next = childOf(cur, k);
|
|
215
|
+
if (!YAML.isMap(next) && !YAML.isSeq(next)) {
|
|
216
|
+
next = doc.createNode({});
|
|
217
|
+
const pair = findPair(cur, k);
|
|
218
|
+
if (pair) pair.value = next;
|
|
219
|
+
else cur.add(doc.createPair(keyScalar(k), next));
|
|
220
|
+
}
|
|
221
|
+
cur = next;
|
|
222
|
+
}
|
|
223
|
+
return cur;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function keyScalar(key) {
|
|
227
|
+
// HTTP status codes stay quoted so they remain strings: '200'.
|
|
228
|
+
const s = new YAML.Scalar(String(key));
|
|
229
|
+
if (/^\d+$/.test(String(key))) s.type = YAML.Scalar.QUOTE_SINGLE;
|
|
230
|
+
return s;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
module.exports = { applyOps, applyOpsToText, clone };
|