rtl-analyzer 0.2.0 → 0.3.0
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/README.md +76 -52
- package/dist/cli.js +18 -1
- package/dist/codemod.js +50 -12
- package/dist/companion-security.js +2 -0
- package/dist/contextual-preview-bridge.js +113 -0
- package/dist/contextual-preview.js +74 -0
- package/dist/controlled-apply.js +222 -0
- package/dist/css-directional.js +60 -0
- package/dist/css-icons.js +51 -0
- package/dist/css-spacing.js +64 -0
- package/dist/dashboard-client.js +57 -40
- package/dist/dashboard-contextual-preview.js +119 -0
- package/dist/dashboard-data.js +1 -1
- package/dist/dashboard-embedded-preview.js +200 -0
- package/dist/dashboard-html.js +11 -10
- package/dist/dashboard-review-details.js +175 -0
- package/dist/dashboard-review-workflow.js +151 -0
- package/dist/directional-properties.js +53 -0
- package/dist/discovery.js +1 -2
- package/dist/eligibility.js +8 -8
- package/dist/evidence-store.js +110 -0
- package/dist/example-preview-mappings.js +98 -0
- package/dist/manual-change-store.js +27 -0
- package/dist/migration-lifecycle.js +19 -0
- package/dist/original-preview-runtime.js +68 -0
- package/dist/output-path.js +65 -0
- package/dist/policy-validation.js +1 -1
- package/dist/preview-cli.js +22 -0
- package/dist/preview-companion.js +621 -0
- package/dist/preview-configuration.js +144 -0
- package/dist/preview-runtime.js +98 -0
- package/dist/project-configuration.js +48 -0
- package/dist/project-root.js +12 -0
- package/dist/react-inline.js +65 -0
- package/dist/report-publisher.js +4 -7
- package/dist/review-approval.js +9 -0
- package/dist/review-session.js +199 -54
- package/dist/review-store.js +175 -0
- package/dist/review-workflow.js +554 -0
- package/dist/rules.js +14 -13
- package/dist/runtime-process.js +35 -0
- package/dist/scan-history.js +35 -0
- package/dist/scan-manifest-store.js +26 -0
- package/dist/scan.js +55 -4
- package/dist/serialization.js +1 -0
- package/dist/source-context.js +93 -0
- package/dist/source-snippets.js +3 -3
- package/dist/standalone-cli.js +14 -1
- package/dist/tailwind.js +3 -1
- package/dist/transformations.js +20 -0
- package/dist/validation.js +71 -0
- package/dist/workflow-validation.js +46 -0
- package/package.json +17 -8
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { access, lstat, mkdir, open, readFile, rename, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { constants } from 'node:fs';
|
|
4
|
+
import { join, dirname } from 'node:path';
|
|
5
|
+
import { homedir, tmpdir } from 'node:os';
|
|
6
|
+
import { validateEvidenceSnapshot } from './evidence-store.js';
|
|
7
|
+
import { validateReviewIdentity, validateReviewPath, validatePatch, validateProjectSourcePath, REVIEW_SOURCE } from './review-session.js';
|
|
8
|
+
export const APPLY_CONFIRMATION = 'APPLY_CONFIRMED';
|
|
9
|
+
const active = new Set();
|
|
10
|
+
const sha = (text) => createHash('sha256').update(text).digest('hex');
|
|
11
|
+
const readNoFollow = async (path) => { const handle = await open(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0)); try {
|
|
12
|
+
return await handle.readFile('utf8');
|
|
13
|
+
}
|
|
14
|
+
finally {
|
|
15
|
+
await handle.close();
|
|
16
|
+
} };
|
|
17
|
+
const atomicWrite = async (path, content) => { const temporary = `${path}.rtl-apply.${process.pid}.${randomUUID()}.tmp`; try {
|
|
18
|
+
await writeFile(temporary, content, { encoding: 'utf8', flag: 'wx', mode: 0o600 });
|
|
19
|
+
await rename(temporary, path);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
try {
|
|
23
|
+
await access(temporary, constants.F_OK);
|
|
24
|
+
await rename(temporary, `${temporary}.failed`);
|
|
25
|
+
}
|
|
26
|
+
catch { /* preserve original error */ }
|
|
27
|
+
throw error;
|
|
28
|
+
} };
|
|
29
|
+
export class ApplyAuditStore {
|
|
30
|
+
path;
|
|
31
|
+
constructor(path) {
|
|
32
|
+
this.path = path;
|
|
33
|
+
}
|
|
34
|
+
async list() { try {
|
|
35
|
+
return JSON.parse(await readFile(this.path, 'utf8'));
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
if (error.code === 'ENOENT')
|
|
39
|
+
return [];
|
|
40
|
+
throw new Error('APPLY_AUDIT_UNREADABLE');
|
|
41
|
+
} }
|
|
42
|
+
async append(event) { const parent = dirname(this.path); await mkdir(parent, { recursive: true, mode: 0o700 }); const events = await this.list(); events.push(event); await atomicWrite(this.path, `${JSON.stringify(events, null, 2)}\n`); }
|
|
43
|
+
}
|
|
44
|
+
export class ControlledApplyService {
|
|
45
|
+
repositoryRoot;
|
|
46
|
+
audit;
|
|
47
|
+
backupRoot;
|
|
48
|
+
constructor(repositoryRoot, audit = new ApplyAuditStore(process.env.RTL_AUDIT_STORE_PATH || join(homedir(), '.rtl-migration-analyzer', 'apply-audit-v1.json')), backupRoot = process.env.RTL_APPLY_BACKUP_ROOT || join(tmpdir(), 'rtl-migration-analyzer-apply-backups')) {
|
|
49
|
+
this.repositoryRoot = repositoryRoot;
|
|
50
|
+
this.audit = audit;
|
|
51
|
+
this.backupRoot = backupRoot;
|
|
52
|
+
}
|
|
53
|
+
async recoverAcceptedFileReceipts(operationId) {
|
|
54
|
+
const events = await this.audit.list();
|
|
55
|
+
const receipts = [];
|
|
56
|
+
for (const e of events.filter(e => e.type === 'APPLY_PREPARED' && e.auditId === `${operationId}:${e.path}`)) {
|
|
57
|
+
if (!e.backupPath || !e.resultingSha256 || sha(await readFile(e.backupPath, 'utf8')) !== e.sourceSha256)
|
|
58
|
+
throw new Error('APPLY_BACKUP_MISMATCH');
|
|
59
|
+
const current = sha(await readNoFollow(await validateProjectSourcePath(this.repositoryRoot, e.path)));
|
|
60
|
+
// Prepared but unwritten files are not tool-owned rollback changes.
|
|
61
|
+
if (current === e.resultingSha256 || events.some(done => done.auditId === e.auditId && done.type === 'APPLIED'))
|
|
62
|
+
receipts.push({ auditId: e.auditId, path: e.path, originalSha256: e.sourceSha256, appliedSha256: e.resultingSha256, backupPath: e.backupPath, appliedAt: e.timestamp });
|
|
63
|
+
}
|
|
64
|
+
return receipts;
|
|
65
|
+
}
|
|
66
|
+
/** Internal workflow boundary: callers must supply the accepted, validated file set. Journal precedes writes. */
|
|
67
|
+
async applyAcceptedFiles(operationId, files, confirmation) {
|
|
68
|
+
if (confirmation !== APPLY_CONFIRMATION)
|
|
69
|
+
throw new Error('APPLY_CONFIRMATION_REQUIRED');
|
|
70
|
+
const events = await this.audit.list();
|
|
71
|
+
const prepared = [];
|
|
72
|
+
for (const file of files) {
|
|
73
|
+
const path = await validateProjectSourcePath(this.repositoryRoot, file.path);
|
|
74
|
+
if (!(await lstat(path)).isFile())
|
|
75
|
+
throw new Error('APPLY_SOURCE_NOT_REGULAR_FILE');
|
|
76
|
+
const auditId = `${operationId}:${file.path}`;
|
|
77
|
+
const old = events.find(e => e.auditId === auditId && e.type === 'APPLY_PREPARED');
|
|
78
|
+
const current = sha(await readNoFollow(path));
|
|
79
|
+
if (current !== sha(file.before) && !(old && current === sha(file.after)))
|
|
80
|
+
throw new Error('STALE_APPLY_SOURCE');
|
|
81
|
+
if (old && (old.sourceSha256 !== sha(file.before) || old.resultingSha256 !== sha(file.after)))
|
|
82
|
+
throw new Error('APPLY_OPERATION_MISMATCH');
|
|
83
|
+
if (!old) {
|
|
84
|
+
await mkdir(this.backupRoot, { recursive: true, mode: 0o700 });
|
|
85
|
+
const backupPath = join(this.backupRoot, `${randomUUID()}.bak`);
|
|
86
|
+
await writeFile(backupPath, file.before, { encoding: 'utf8', flag: 'wx', mode: 0o600 });
|
|
87
|
+
const timestamp = new Date().toISOString();
|
|
88
|
+
await this.audit.append({ auditId, type: 'APPLY_PREPARED', path: file.path, sourceSha256: sha(file.before), resultingSha256: sha(file.after), backupPath, timestamp });
|
|
89
|
+
prepared.push({ auditId, path: file.path, originalSha256: sha(file.before), appliedSha256: sha(file.after), backupPath, appliedAt: timestamp });
|
|
90
|
+
}
|
|
91
|
+
else
|
|
92
|
+
prepared.push({ auditId, path: file.path, originalSha256: old.sourceSha256, appliedSha256: old.resultingSha256, backupPath: old.backupPath, appliedAt: old.timestamp });
|
|
93
|
+
}
|
|
94
|
+
for (const [index, receipt] of prepared.entries()) {
|
|
95
|
+
const path = await validateProjectSourcePath(this.repositoryRoot, receipt.path);
|
|
96
|
+
const current = sha(await readNoFollow(path));
|
|
97
|
+
if (sha(await readFile(receipt.backupPath, 'utf8')) !== receipt.originalSha256)
|
|
98
|
+
throw new Error('APPLY_BACKUP_MISMATCH');
|
|
99
|
+
if (current === receipt.appliedSha256)
|
|
100
|
+
continue;
|
|
101
|
+
if (current !== receipt.originalSha256)
|
|
102
|
+
throw new Error('STALE_APPLY_SOURCE');
|
|
103
|
+
await atomicWrite(path, files[index].after);
|
|
104
|
+
await this.audit.append({ auditId: receipt.auditId, type: 'APPLIED', path: receipt.path, sourceSha256: receipt.originalSha256, resultingSha256: receipt.appliedSha256, backupPath: receipt.backupPath, timestamp: new Date().toISOString() });
|
|
105
|
+
}
|
|
106
|
+
return prepared;
|
|
107
|
+
}
|
|
108
|
+
async rollbackAcceptedFiles(receipts) {
|
|
109
|
+
const events = await this.audit.list();
|
|
110
|
+
// Preflight the entire scope: a later user edit blocks every write in this rollback.
|
|
111
|
+
for (const r of receipts) {
|
|
112
|
+
const event = events.find(e => e.auditId === r.auditId && e.type === 'APPLY_PREPARED' && e.path === r.path && e.backupPath === r.backupPath && e.sourceSha256 === r.originalSha256 && e.resultingSha256 === r.appliedSha256);
|
|
113
|
+
if (!event)
|
|
114
|
+
throw new Error('ROLLBACK_RECEIPT_UNTRUSTED');
|
|
115
|
+
const current = sha(await readNoFollow(await validateProjectSourcePath(this.repositoryRoot, r.path)));
|
|
116
|
+
if (current !== r.appliedSha256 && current !== r.originalSha256)
|
|
117
|
+
throw new Error('ROLLBACK_CONFLICT');
|
|
118
|
+
if (sha(await readFile(r.backupPath, 'utf8')) !== r.originalSha256)
|
|
119
|
+
throw new Error('ROLLBACK_BACKUP_MISMATCH');
|
|
120
|
+
}
|
|
121
|
+
for (const r of receipts) {
|
|
122
|
+
const path = await validateProjectSourcePath(this.repositoryRoot, r.path);
|
|
123
|
+
const current = sha(await readNoFollow(path));
|
|
124
|
+
if (current === r.originalSha256)
|
|
125
|
+
continue;
|
|
126
|
+
if (current !== r.appliedSha256)
|
|
127
|
+
throw new Error('ROLLBACK_CONFLICT');
|
|
128
|
+
await atomicWrite(path, await readFile(r.backupPath, 'utf8'));
|
|
129
|
+
await this.audit.append({ auditId: r.auditId, type: 'ROLLED_BACK', path: r.path, sourceSha256: r.appliedSha256, resultingSha256: r.originalSha256, backupPath: r.backupPath, timestamp: new Date().toISOString() });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
async recoverRollbackReceipt(pathValue, originalSha256, appliedSha256) {
|
|
133
|
+
const path = validateReviewPath(this.repositoryRoot, pathValue);
|
|
134
|
+
const events = await this.audit.list();
|
|
135
|
+
const applied = [...events].reverse().find(event => event.type === 'APPLIED' && event.path === pathValue && event.sourceSha256 === originalSha256 && event.resultingSha256 === appliedSha256 && event.backupPath);
|
|
136
|
+
if (!applied?.backupPath || events.some(event => event.auditId === applied.auditId && event.type === 'ROLLED_BACK'))
|
|
137
|
+
return null;
|
|
138
|
+
const current = await readNoFollow(path);
|
|
139
|
+
if (sha(current) !== appliedSha256)
|
|
140
|
+
return null;
|
|
141
|
+
const backup = await readFile(applied.backupPath, 'utf8');
|
|
142
|
+
if (sha(backup) !== originalSha256)
|
|
143
|
+
return null;
|
|
144
|
+
return { auditId: applied.auditId, path: pathValue, originalSha256, appliedSha256, backupPath: applied.backupPath, appliedAt: applied.timestamp };
|
|
145
|
+
}
|
|
146
|
+
async apply(auth) {
|
|
147
|
+
validateReviewIdentity(auth.identity);
|
|
148
|
+
if (auth.confirmation !== APPLY_CONFIRMATION)
|
|
149
|
+
throw new Error('APPLY_CONFIRMATION_REQUIRED');
|
|
150
|
+
if (auth.reviewRecord.status !== 'Approved' || auth.reviewRecord.findingId !== auth.identity.findingId || auth.reviewRecord.inputDigest !== auth.identity.inputDigest || auth.reviewRecord.policyDigest !== auth.identity.policyDigest)
|
|
151
|
+
throw new Error('APPROVED_REVIEW_REQUIRED');
|
|
152
|
+
if (auth.patch.path !== REVIEW_SOURCE || auth.patch.findingId !== auth.identity.findingId)
|
|
153
|
+
throw new Error('APPLY_PATCH_IDENTITY_MISMATCH');
|
|
154
|
+
const evidence = auth.evidence.find(x => { try {
|
|
155
|
+
validateEvidenceSnapshot(x);
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
return false;
|
|
159
|
+
} return x.findingId === auth.identity.findingId && x.sourcePath === auth.identity.sourcePath && x.inputDigest === auth.identity.inputDigest && x.policyDigest === auth.identity.policyDigest && (x.decision === 'Approved' || x.decision === 'Ready for Review') && x.candidatePatchIdentity.expectedSha256 === auth.patch.expectedSha256 && x.candidatePatchIdentity.replacementSha256 === sha(auth.patch.replacement) && !x.scannerAfter.findingPresent && x.browserDomValidation.length > 0; });
|
|
160
|
+
if (!evidence)
|
|
161
|
+
throw new Error('VALIDATION_EVIDENCE_REQUIRED');
|
|
162
|
+
const path = validateReviewPath(this.repositoryRoot, auth.patch.path);
|
|
163
|
+
const key = path;
|
|
164
|
+
if (active.has(key))
|
|
165
|
+
throw new Error('APPLY_IN_PROGRESS');
|
|
166
|
+
active.add(key);
|
|
167
|
+
try {
|
|
168
|
+
const stat = await lstat(path);
|
|
169
|
+
if (!stat.isFile())
|
|
170
|
+
throw new Error('APPLY_SOURCE_NOT_REGULAR_FILE');
|
|
171
|
+
const original = await readNoFollow(path);
|
|
172
|
+
const originalSha256 = sha(original);
|
|
173
|
+
if (originalSha256 !== evidence.originalSourceSha256)
|
|
174
|
+
throw new Error('STALE_APPLY_SOURCE');
|
|
175
|
+
validatePatch(original, auth.patch);
|
|
176
|
+
const next = original.slice(0, auth.patch.startOffset) + auth.patch.replacement + original.slice(auth.patch.endOffset);
|
|
177
|
+
const appliedSha256 = sha(next);
|
|
178
|
+
if (appliedSha256 !== evidence.candidateSourceSha256)
|
|
179
|
+
throw new Error('APPLY_RESULT_MISMATCH');
|
|
180
|
+
await mkdir(this.backupRoot, { recursive: true, mode: 0o700 });
|
|
181
|
+
const backupPath = join(this.backupRoot, `${randomUUID()}.bak`);
|
|
182
|
+
await writeFile(backupPath, original, { encoding: 'utf8', mode: 0o600, flag: 'wx' });
|
|
183
|
+
if (sha(await readFile(backupPath, 'utf8')) !== originalSha256)
|
|
184
|
+
throw new Error('APPLY_BACKUP_MISMATCH');
|
|
185
|
+
const auditId = randomUUID();
|
|
186
|
+
try {
|
|
187
|
+
await atomicWrite(path, next);
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
await this.audit.append({ auditId, type: 'APPLY_FAILED', path: auth.patch.path, sourceSha256: originalSha256, timestamp: new Date().toISOString(), reason: 'ATOMIC_WRITE_FAILED' });
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
try {
|
|
194
|
+
await this.audit.append({ auditId, type: 'APPLIED', path: auth.patch.path, sourceSha256: originalSha256, resultingSha256: appliedSha256, backupPath, timestamp: new Date().toISOString() });
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
await atomicWrite(path, original);
|
|
198
|
+
throw new Error('APPLY_AUDIT_FAILED');
|
|
199
|
+
}
|
|
200
|
+
return { auditId, path: auth.patch.path, originalSha256, appliedSha256, backupPath, appliedAt: new Date().toISOString() };
|
|
201
|
+
}
|
|
202
|
+
finally {
|
|
203
|
+
active.delete(key);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
async rollback(receipt) { const path = validateReviewPath(this.repositoryRoot, receipt.path); const key = path; if (active.has(key))
|
|
207
|
+
throw new Error('APPLY_IN_PROGRESS'); active.add(key); try {
|
|
208
|
+
const current = await readNoFollow(path);
|
|
209
|
+
if (sha(current) !== receipt.appliedSha256) {
|
|
210
|
+
await this.audit.append({ auditId: receipt.auditId, type: 'ROLLBACK_FAILED', path: receipt.path, sourceSha256: receipt.originalSha256, resultingSha256: sha(current), timestamp: new Date().toISOString(), reason: 'CURRENT_SOURCE_CHANGED' });
|
|
211
|
+
throw new Error('ROLLBACK_CONFLICT');
|
|
212
|
+
}
|
|
213
|
+
const backup = await readFile(receipt.backupPath, 'utf8');
|
|
214
|
+
if (sha(backup) !== receipt.originalSha256)
|
|
215
|
+
throw new Error('ROLLBACK_BACKUP_MISMATCH');
|
|
216
|
+
await atomicWrite(path, backup);
|
|
217
|
+
await this.audit.append({ auditId: receipt.auditId, type: 'ROLLED_BACK', path: receipt.path, sourceSha256: receipt.appliedSha256, resultingSha256: receipt.originalSha256, backupPath: receipt.backupPath, timestamp: new Date().toISOString() });
|
|
218
|
+
}
|
|
219
|
+
finally {
|
|
220
|
+
active.delete(key);
|
|
221
|
+
} }
|
|
222
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import valueParser from 'postcss-value-parser';
|
|
2
|
+
import postcss from 'postcss';
|
|
3
|
+
import { hash } from './model.js';
|
|
4
|
+
import { directionalProperty, propertyConflict, readingEdgeReason } from './directional-properties.js';
|
|
5
|
+
export const CSS_DIRECTIONAL_TRANSFORMATION_ID = 'css-directional-logical';
|
|
6
|
+
export function proposeCssDirectional(finding, source) {
|
|
7
|
+
const reject = (s) => { throw Error('CSS_DIRECTIONAL_' + s); };
|
|
8
|
+
if (!['RTL.CSS.PHYSICAL', 'RTL.GEOMETRY.EDGE'].includes(finding.ruleId) || finding.sourceMechanism !== 'css' || !finding.path.endsWith('.css'))
|
|
9
|
+
reject('STYLESHEET_ONLY');
|
|
10
|
+
if (!new RegExp('^' + finding.ruleId.replaceAll('.', '\\.') + ':[a-f0-9]{24,64}$').test(finding.id))
|
|
11
|
+
reject('FINDING_IDENTITY');
|
|
12
|
+
if (finding.exceptionReason || ['INTENTIONAL_LTR', 'INFORMATIONAL'].includes(finding.category))
|
|
13
|
+
reject('INTENTIONAL_PHYSICAL');
|
|
14
|
+
if (finding.ownership !== 'custom-css')
|
|
15
|
+
reject('OWNER_REQUIRES_REVIEW');
|
|
16
|
+
const root = postcss.parse(source, { from: undefined });
|
|
17
|
+
let target;
|
|
18
|
+
root.walkDecls(d => { if (d.source?.start?.offset === finding.range.start.offset)
|
|
19
|
+
target = d; });
|
|
20
|
+
if (!target || target.source?.end?.offset !== finding.range.end.offset || source.slice(finding.range.start.offset, finding.range.end.offset) !== finding.original)
|
|
21
|
+
reject('STALE_RANGE');
|
|
22
|
+
const d = target;
|
|
23
|
+
if (d.parent?.type !== 'rule' || d.parent.parent?.type !== 'root')
|
|
24
|
+
reject('NESTED_OR_CONDITIONAL_RULE');
|
|
25
|
+
const rule = d.parent;
|
|
26
|
+
if (!/^\.[a-zA-Z_][\w-]*$/.test(rule.selector))
|
|
27
|
+
reject('SIMPLE_CLASS_SELECTOR_REQUIRED');
|
|
28
|
+
if (rule.nodes.some(n => n.type !== 'decl' && n.type !== 'comment'))
|
|
29
|
+
reject('UNSUPPORTED_RULE_CONTENT');
|
|
30
|
+
if (rule.nodes.some(n => n.type === 'comment' && /rtl-physical\s*:|rtl-icon\s*:/i.test(n.text)))
|
|
31
|
+
reject('INTENTIONAL_OR_ICON_REVIEW');
|
|
32
|
+
let direction = false, collision = false;
|
|
33
|
+
root.walkDecls(n => { if (n.prop.toLowerCase() === 'direction' || n.prop.toLowerCase() === 'writing-mode' && n.value !== 'horizontal-tb')
|
|
34
|
+
direction = true; });
|
|
35
|
+
if (direction)
|
|
36
|
+
reject('DIRECTION_OR_WRITING_MODE_REVIEW');
|
|
37
|
+
root.walkRules(r => { if (r !== rule && (r.selector.includes(rule.selector) || r.selector.includes('*')))
|
|
38
|
+
collision = true; });
|
|
39
|
+
if (collision)
|
|
40
|
+
reject('CASCADE_SELECTOR_CONFLICT');
|
|
41
|
+
const spec = directionalProperty(d.prop, d.value);
|
|
42
|
+
if (!spec)
|
|
43
|
+
reject('UNSUPPORTED_PROPERTY_OR_VALUE');
|
|
44
|
+
const peers = rule.nodes.filter((n) => n.type === 'decl' && n !== d);
|
|
45
|
+
if (peers.some(n => propertyConflict(d.prop, n.prop.toLowerCase(), spec.family)))
|
|
46
|
+
reject('CASCADE_DECLARATION_CONFLICT');
|
|
47
|
+
if (spec.family === 'positioning' && !peers.some(n => n.prop === 'position' && /^(absolute|fixed)$/.test(n.value)))
|
|
48
|
+
reject('EXPLICIT_POSITION_REQUIRED');
|
|
49
|
+
const offset = d.prop.length + (d.raws.between ?? ':').length;
|
|
50
|
+
if (source.slice(finding.range.start.offset, finding.range.start.offset + d.prop.length) !== d.prop)
|
|
51
|
+
reject('PROPERTY_SPELLING');
|
|
52
|
+
let replacement = spec.logical + finding.original.slice(d.prop.length);
|
|
53
|
+
if (spec.family === 'alignment') {
|
|
54
|
+
if (finding.original.slice(offset, offset + d.value.length) !== d.value)
|
|
55
|
+
reject('VALUE_RANGE');
|
|
56
|
+
const token = valueParser(d.value).nodes.find(n => n.type === 'word');
|
|
57
|
+
replacement = finding.original.slice(0, offset + token.sourceIndex) + spec.value + finding.original.slice(offset + token.sourceEndIndex);
|
|
58
|
+
}
|
|
59
|
+
return { transformationId: CSS_DIRECTIONAL_TRANSFORMATION_ID, reason: readingEdgeReason, patch: { findingId: finding.id, path: finding.path, startOffset: finding.range.start.offset, endOffset: finding.range.end.offset, expectedSha256: hash(finding.original), replacement } };
|
|
60
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import postcss from 'postcss';
|
|
2
|
+
import { hash, range } from './model.js';
|
|
3
|
+
export const CSS_ICON_TRANSFORMATION_ID = 'css-icon-progression-mirror';
|
|
4
|
+
const marked = (r) => r.nodes.some(n => n.type === 'comment' && /^rtl-icon:\s*progression-right\s*$/.test(n.text.trim()));
|
|
5
|
+
const mirrored = (r, selector) => r.selector === selector + ':dir(rtl)' && r.nodes.length === 1 && r.nodes[0].type === 'decl' && r.nodes[0].prop === 'scale' && r.nodes[0].value === '-1 1' && !r.nodes[0].important;
|
|
6
|
+
/** Explicit semantic opt-in only: property names and generic icon names do not prove intent. */
|
|
7
|
+
export function cssIconAnchors(path, source) {
|
|
8
|
+
const result = [];
|
|
9
|
+
let root;
|
|
10
|
+
try {
|
|
11
|
+
root = postcss.parse(source, { from: undefined });
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
root.walkRules(r => { if (marked(r) && !root.nodes.some(n => n.type === 'rule' && mirrored(n, r.selector))) {
|
|
17
|
+
const start = r.source.start.offset, end = r.source.end.offset;
|
|
18
|
+
const original = source.slice(start, end), context = 'stylesheet/' + r.selector;
|
|
19
|
+
result.push({ path, range: range(source, start, end), original, context, id: hash(path + '|' + context + '|' + original), mechanism: 'css' });
|
|
20
|
+
} });
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
export function proposeCssIcon(finding, source) {
|
|
24
|
+
const reject = (s) => { throw Error('CSS_ICON_' + s); };
|
|
25
|
+
if (finding.ruleId !== 'RTL.ICON.PROGRESSION' || finding.sourceMechanism !== 'css' || !finding.path.endsWith('.css') || !/^RTL\.ICON\.PROGRESSION:[a-f0-9]{24,64}$/.test(finding.id))
|
|
26
|
+
reject('EXPLICIT_SEMANTIC_MARKER_REQUIRED');
|
|
27
|
+
if (finding.exceptionReason || ['INTENTIONAL_LTR', 'INFORMATIONAL'].includes(finding.category) || finding.ownership !== 'custom-css')
|
|
28
|
+
reject('OWNER_OR_INTENT_REVIEW');
|
|
29
|
+
const root = postcss.parse(source, { from: undefined });
|
|
30
|
+
let rule;
|
|
31
|
+
root.walkRules(r => { if (r.source?.start?.offset === finding.range.start.offset && r.source?.end?.offset === finding.range.end.offset)
|
|
32
|
+
rule = r; });
|
|
33
|
+
if (!rule || source.slice(finding.range.start.offset, finding.range.end.offset) !== finding.original)
|
|
34
|
+
reject('STALE_RANGE');
|
|
35
|
+
const r = rule;
|
|
36
|
+
if (!marked(r) || r.parent?.type !== 'root' || !/^\.[a-zA-Z_][\w-]*$/.test(r.selector))
|
|
37
|
+
reject('FLAT_MARKED_CLASS_REQUIRED');
|
|
38
|
+
if (r.nodes.some(n => n.type !== 'decl' && n.type !== 'comment') || r.nodes.some(n => n.type === 'comment' && /rtl-physical\s*:/i.test(n.text)))
|
|
39
|
+
reject('INTENT_OR_RULE_CONTENT_REVIEW');
|
|
40
|
+
if (!r.nodes.some(n => n.type === 'decl' && n.prop === 'display' && /^(inline-block|block)$/.test(n.value)))
|
|
41
|
+
reject('DISPLAY_BOX_REQUIRED');
|
|
42
|
+
let conflict = false;
|
|
43
|
+
root.walkDecls(d => { if (/^(?:transform(?:-|$)|scale$|rotate$|translate$|animation(?:-|$)|transition(?:-|$)|direction$|writing-mode$|all$)/.test(d.prop.toLowerCase()))
|
|
44
|
+
conflict = true; });
|
|
45
|
+
root.walkRules(n => { if (n !== r && (n.selector.includes(r.selector) || n.selector.includes('*')))
|
|
46
|
+
conflict = true; });
|
|
47
|
+
if (conflict)
|
|
48
|
+
reject('CASCADE_OR_TRANSFORM_REVIEW');
|
|
49
|
+
const newline = source.includes('\r\n') ? '\r\n' : '\n';
|
|
50
|
+
return { transformationId: CSS_ICON_TRANSFORMATION_ID, reason: 'Requires semantic review: author marked an LTR right-facing progression icon. Confirm this class targets only the graphic, with no text, brand, trend or framework-owned mirroring; horizontal-tb and inherited CSS direction are required. Preserve LTR; mirror only :dir(rtl). Inspect nested transforms and external styles.', patch: { findingId: finding.id, path: finding.path, startOffset: finding.range.start.offset, endOffset: finding.range.end.offset, expectedSha256: hash(finding.original), replacement: finding.original + newline + r.selector + ':dir(rtl) { scale: -1 1; }' } };
|
|
51
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import postcss from 'postcss';
|
|
2
|
+
import valueParser from 'postcss-value-parser';
|
|
3
|
+
import { hash } from './model.js';
|
|
4
|
+
export const CSS_SPACING_TRANSFORMATION_ID = 'css-spacing-logical';
|
|
5
|
+
/** Syntactic proposal only. Accepting it declares horizontal-tb, LTR-authored reading-edge intent. */
|
|
6
|
+
export function proposeCssSpacing(finding, source) {
|
|
7
|
+
const reject = (code) => { throw Error(code); };
|
|
8
|
+
if (finding.ruleId !== 'RTL.CSS.PHYSICAL' || finding.sourceMechanism !== 'css' || !finding.path.endsWith('.css'))
|
|
9
|
+
reject('CSS_SPACING_STYLESHEET_ONLY');
|
|
10
|
+
if (!/^RTL\.CSS\.PHYSICAL:[a-f0-9]{24,64}$/.test(finding.id))
|
|
11
|
+
reject('CSS_SPACING_FINDING_IDENTITY');
|
|
12
|
+
if (finding.exceptionReason || finding.category === 'INTENTIONAL_LTR' || finding.category === 'INFORMATIONAL')
|
|
13
|
+
reject('CSS_SPACING_INTENTIONAL_PHYSICAL');
|
|
14
|
+
if (finding.ownership !== 'custom-css')
|
|
15
|
+
reject('CSS_SPACING_OWNER_REQUIRES_REVIEW');
|
|
16
|
+
let root;
|
|
17
|
+
try {
|
|
18
|
+
root = postcss.parse(source, { from: undefined });
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
reject('CSS_SPACING_PARSE_FAILED');
|
|
22
|
+
}
|
|
23
|
+
let target;
|
|
24
|
+
root.walkDecls(d => { if (d.source?.start?.offset === finding.range.start.offset)
|
|
25
|
+
target = d; });
|
|
26
|
+
if (!target || target.source?.end?.offset !== finding.range.end.offset || source.slice(finding.range.start.offset, finding.range.end.offset) !== finding.original)
|
|
27
|
+
reject('CSS_SPACING_STALE_RANGE');
|
|
28
|
+
const d = target, match = /^(margin|padding)-(left|right)$/.exec(d.prop);
|
|
29
|
+
if (!match)
|
|
30
|
+
reject('CSS_SPACING_UNSUPPORTED_PROPERTY');
|
|
31
|
+
if (d.parent?.type !== 'rule' || d.parent.parent?.type !== 'root')
|
|
32
|
+
reject('CSS_SPACING_NESTED_OR_CONDITIONAL_RULE');
|
|
33
|
+
const rule = d.parent;
|
|
34
|
+
if (!/^\.[a-zA-Z_][\w-]*$/.test(rule.selector))
|
|
35
|
+
reject('CSS_SPACING_SIMPLE_CLASS_SELECTOR_REQUIRED');
|
|
36
|
+
if (rule.nodes.some(n => n.type !== 'decl' && n.type !== 'comment'))
|
|
37
|
+
reject('CSS_SPACING_UNSUPPORTED_RULE_CONTENT');
|
|
38
|
+
if (rule.nodes.some(n => n.type === 'comment' && /rtl-physical\s*:/i.test(n.text)))
|
|
39
|
+
reject('CSS_SPACING_INTENTIONAL_PHYSICAL');
|
|
40
|
+
let writingMode = false, collision = false;
|
|
41
|
+
root.walkDecls(n => { if (n.prop.toLowerCase() === 'writing-mode' && n.value !== 'horizontal-tb')
|
|
42
|
+
writingMode = true; if (n.prop.toLowerCase() === 'direction')
|
|
43
|
+
writingMode = true; });
|
|
44
|
+
if (writingMode)
|
|
45
|
+
reject('CSS_SPACING_DIRECTION_OR_WRITING_MODE_REVIEW');
|
|
46
|
+
root.walkRules(r => { if (r !== rule && (r.selector.includes(rule.selector) || r.selector.includes('*')))
|
|
47
|
+
collision = true; });
|
|
48
|
+
if (collision)
|
|
49
|
+
reject('CSS_SPACING_CASCADE_SELECTOR_CONFLICT');
|
|
50
|
+
const family = match[1];
|
|
51
|
+
if (rule.nodes.some(n => { if (n === d || n.type !== 'decl')
|
|
52
|
+
return false; const prop = n.prop.toLowerCase(); return prop === 'all' || prop === 'position' || prop === 'writing-mode' || prop === 'direction' || prop === family || prop.startsWith(family + '-inline') || prop.startsWith(family + '-block') || prop === d.prop; }))
|
|
53
|
+
reject('CSS_SPACING_CASCADE_DECLARATION_CONFLICT');
|
|
54
|
+
const tokens = valueParser(d.value).nodes.filter(n => n.type !== 'space' && n.type !== 'comment');
|
|
55
|
+
if (tokens.length !== 1 || tokens[0].type !== 'word' || !/^(-?(?:\d*\.)?\d+(?:px|em|rem|%|vw|vh|ch|ex)?|auto)$/.test(tokens[0].value) || family === 'padding' && tokens[0].type === 'word' && (/^-/.test(tokens[0].value) || tokens[0].value === 'auto'))
|
|
56
|
+
reject('CSS_SPACING_UNSUPPORTED_VALUE');
|
|
57
|
+
if (tokens[0].type === 'word' && /^-?(?:\d*\.)?\d+$/.test(tokens[0].value) && Number(tokens[0].value) !== 0)
|
|
58
|
+
reject('CSS_SPACING_UNITLESS_LENGTH');
|
|
59
|
+
const logical = family + '-inline-' + (match[2] === 'left' ? 'start' : 'end');
|
|
60
|
+
const start = finding.range.start.offset, end = finding.range.end.offset;
|
|
61
|
+
if (source.slice(start, start + d.prop.length) !== d.prop)
|
|
62
|
+
reject('CSS_SPACING_UNSUPPORTED_PROPERTY_SPELLING');
|
|
63
|
+
return { transformationId: CSS_SPACING_TRANSFORMATION_ID, reason: 'Requires semantic review: horizontal-tb, LTR-authored reading-start/end spacing. RTL intentionally moves the side; physical coordinates are not preserved. Cross-stylesheet cascade and inherited writing mode must be reviewed. Value, formatting and !important are preserved.', patch: { findingId: finding.id, path: finding.path, startOffset: start, endOffset: end, expectedSha256: hash(finding.original), replacement: logical + finding.original.slice(d.prop.length) } };
|
|
64
|
+
}
|