Skip to content

Commit b8f5ac5

Browse files
authored
TSDiffer add RAC (#6736)
1 parent 617ad2e commit b8f5ac5

File tree

4 files changed

+66
-19
lines changed

4 files changed

+66
-19
lines changed

packages/dev/parcel-transformer-docs/DocsTransformer.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,15 @@ module.exports = new Transformer({
113113
}
114114
if (path.isVariableDeclarator()) {
115115
if (!path.node.init) {
116+
node.id = `${asset.filePath}:${path.node.id.name}`;
117+
node.name = path.node.id.name;
116118
return Object.assign(node, {type: 'any'});
117119
}
118120

119121
let docs = getJSDocs(path.parentPath);
120122
processExport(path.get('init'), node);
121123
addDocs(node, docs);
122-
if (node.type === 'interface') {
124+
if (node.type === 'interface' || node.type === 'component') {
123125
node.id = `${asset.filePath}:${path.node.id.name}`;
124126
node.name = path.node.id.name;
125127
}
@@ -342,6 +344,18 @@ module.exports = new Transformer({
342344
});
343345
}
344346

347+
if (path.isTSMappedType()) {
348+
return Object.assign(node, {
349+
type: 'mapped',
350+
readonly: path.node.readonly,
351+
typeParameter: {
352+
...processExport(path.get('typeParameter')),
353+
isMappedType: true
354+
},
355+
typeAnnotation: processExport(path.get('typeAnnotation'))
356+
});
357+
}
358+
345359
if (path.isTSInterfaceDeclaration()) {
346360
let properties = {};
347361
for (let propertyPath of path.get('body.body')) {
@@ -495,7 +509,11 @@ module.exports = new Transformer({
495509
}
496510

497511
if (path.isTSAnyKeyword()) {
498-
return Object.assign(node, {type: 'any'});
512+
return Object.assign(node, {
513+
id: path.node.id ? `${asset.filePath}:${path.node.id.name}` : null,
514+
name: path.node.id ? path.node.id.name : null,
515+
type: 'any'
516+
});
499517
}
500518

501519
if (path.isTSNullKeyword()) {

scripts/buildBranchAPI.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ async function build() {
152152

153153
// Build the website
154154
console.log('building api files');
155-
await run('yarn', ['parcel', 'build', 'packages/@react-{spectrum,aria,stately}/*/', 'packages/@internationalized/{message,string,date,number}', '--target', 'apiCheck'], {cwd: dir, stdio: 'inherit'});
155+
await run('yarn', ['parcel', 'build', 'packages/react-aria-components', 'packages/@react-{spectrum,aria,stately}/*/', 'packages/@internationalized/{message,string,date,number}', '--target', 'apiCheck'], {cwd: dir, stdio: 'inherit'});
156156

157157
// Copy the build back into dist, and delete the temp dir.
158158
fs.copySync(path.join(dir, 'packages'), distDir, {dereference: true});

scripts/buildPublishedAPI.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ async function build() {
204204

205205
// Build the website
206206
console.log('building api files');
207-
await run('yarn', ['parcel', 'build', 'packages/@react-{spectrum,aria,stately}/*/', 'packages/@internationalized/{message,string,date,number}'], {cwd: dir, stdio: 'inherit'});
207+
await run('yarn', ['parcel', 'build', 'packages/react-aria-components', 'packages/@react-{spectrum,aria,stately}/*/', 'packages/@internationalized/{message,string,date,number}', '--target', 'apiCheck'], {cwd: dir, stdio: 'inherit'});
208208

209209
// Copy the build back into dist, and delete the temp dir.
210210
// dev/docs/node_modules has some react spectrum components, we don't want those, and i couldn't figure out how to not build them

scripts/compareAPIs.js

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ async function compare() {
5555
// find all matching pairs based on what's been published
5656
for (let pubApi of publishedAPIs) {
5757
let pubApiPath = pubApi.split(path.sep);
58-
let sharedPath = path.join(...pubApiPath.slice(pubApiPath.length - 4));
58+
let pkgJson = fs.readJsonSync(path.join('/', ...pubApiPath.slice(0, pubApiPath.length - 2), 'package.json'));
59+
let name = pkgJson.name;
60+
let sharedPath = path.join(name, 'dist', 'api.json');
5961
let found = false;
6062
for (let branchApi of branchAPIs) {
6163
if (branchApi.includes(sharedPath)) {
@@ -72,7 +74,9 @@ async function compare() {
7274
// don't care about private APIs, but we do care if we're about to publish a new one
7375
for (let branchApi of branchAPIs) {
7476
let branchApiPath = branchApi.split(path.sep);
75-
let sharedPath = path.join(...branchApiPath.slice(branchApiPath.length - 4));
77+
let pkgJson = fs.readJsonSync(path.join('/', ...branchApiPath.slice(0, branchApiPath.length - 2), 'package.json'));
78+
let name = pkgJson.name;
79+
let sharedPath = path.join(name, 'dist', 'api.json');
7680
let found = false;
7781
for (let pubApi of publishedAPIs) {
7882
if (pubApi.includes(sharedPath)) {
@@ -114,10 +118,11 @@ async function compare() {
114118
if (diff.length > 0) {
115119
let affected = followInvertedDependencies(simplifiedName, invertedDeps);
116120
// combine export change messages
121+
// remove extra newline we added between the name of the interface and the properties to make the diffs easier to read
117122
changes.push(`
118123
#### ${simplifiedName}
119124
${changedByDeps.length > 0 ? `changed by:
120-
- ${changedByDeps.join('\n - ')}\n\n` : ''}${diff.length > 0 ? diff : ''}${affected.length > 0 ? `
125+
- ${changedByDeps.join('\n - ')}\n\n` : ''}${diff.split('\n').filter(line => line !== ' ').join('\n')}${affected.length > 0 ? `
121126
it changed:
122127
- ${affected.join('\n - ')}
123128
` : ''}
@@ -371,6 +376,9 @@ function processType(value) {
371376
}
372377
return name;
373378
}
379+
if (value.type === 'mapped') {
380+
return `${value.readonly === '-' ? '-readonly' : ''}${processType(value.typeParameter)}: ${processType(value.typeAnnotation)}`;
381+
}
374382
// interface still needed if we have it at top level?
375383
if (value.type === 'object') {
376384
if (value.properties) {
@@ -428,7 +436,7 @@ ${value.exact ? '\\}' : '}'}`;
428436
return `keyof ${processType(value.keyof)}`;
429437
}
430438

431-
console.log('unknown type', value);
439+
return `UNKNOWN: ${value.type}`;
432440
}
433441

434442
function rebuildInterfaces(json) {
@@ -445,10 +453,12 @@ function rebuildInterfaces(json) {
445453
let item = json.exports[key];
446454
if (item?.type == null) {
447455
// todo what to do here??
456+
exports[item.name] = 'UNTYPED';
448457
return;
449458
}
450459
if (item.props?.type === 'identifier') {
451460
// todo what to do here??
461+
exports[item.name] = 'UNTYPED';
452462
return;
453463
}
454464
if (item.type === 'component') {
@@ -479,8 +489,11 @@ function rebuildInterfaces(json) {
479489
compInterface[name] = {optional, defaultVal, value};
480490
}
481491
let name = item.name ?? key;
482-
if (item.typeParameters.length > 0) {
483-
name = name + `<${item.typeParameters.map(processType).sort().join(', ')}>`;
492+
if (item.typeParameters?.length > 0) {
493+
compInterface['typeParameters'] = `<${item.typeParameters.map(processType).sort().join(', ')}>`;
494+
}
495+
if (item.props?.extends?.length > 0) {
496+
compInterface['extend'] = `extends ${item.props.extends.map(processType).sort().join(', ')}`;
484497
}
485498
exports[name] = compInterface;
486499
} else if (item.type === 'function') {
@@ -497,10 +510,11 @@ function rebuildInterfaces(json) {
497510
});
498511
let returnVal = processType(item.return);
499512
let name = item.name ?? key;
500-
if (item.typeParameters.length > 0) {
501-
name = name + `<${item.typeParameters.map(processType).sort().join(', ')}>`;
513+
funcInterface['returnVal'] = returnVal;
514+
if (item.typeParameters?.length > 0) {
515+
funcInterface['typeParameters'] = `<${item.typeParameters.map(processType).sort().join(', ')}>`;
502516
}
503-
exports[name] = {...funcInterface, returnVal};
517+
exports[name] = funcInterface;
504518
} else if (item.type === 'interface') {
505519
let funcInterface = {};
506520
Object.entries(item.properties).sort((([keyA], [keyB]) => keyA > keyB ? 1 : -1)).forEach(([, property]) => {
@@ -515,8 +529,8 @@ function rebuildInterfaces(json) {
515529
funcInterface[name] = {optional, defaultVal, value};
516530
});
517531
let name = item.name ?? key;
518-
if (item.typeParameters.length > 0) {
519-
name = name + `<${item.typeParameters.map(processType).sort().join(', ')}>`;
532+
if (item.typeParameters?.length > 0) {
533+
funcInterface['typeParameters'] = `<${item.typeParameters.map(processType).sort().join(', ')}>`;
520534
}
521535
exports[name] = funcInterface;
522536
} else if (item.type === 'link') {
@@ -532,7 +546,8 @@ function rebuildInterfaces(json) {
532546
exports[name] = {isType, optional, defaultVal, value};
533547
}
534548
} else {
535-
console.log('unknown top level export', item);
549+
exports[key] = 'UNTYPED';
550+
// console.log('unknown top level export', key, item);
536551
}
537552
});
538553
return exports;
@@ -544,10 +559,24 @@ function formatProp([name, prop]) {
544559

545560
function formatInterfaces(interfaces, allInterfaces) {
546561
return allInterfaces.map(name => {
547-
if (interfaces[name]) {
548-
let output = `${name} {\n`;
549-
output += interfaces[name].isType ? formatProp(name, interfaces[name]) : Object.entries(interfaces[name]).map(formatProp).join('\n');
562+
let interfaceO = interfaces[name];
563+
if (interfaceO && interfaceO !== 'UNTYPED') {
564+
let output = `${name}`;
565+
if (interfaceO.typeParameters) {
566+
output += ` ${interfaceO.typeParameters}`;
567+
delete interfaceO.typeParameters;
568+
}
569+
if (interfaceO.extend) {
570+
output += ` ${interfaceO.extend}`;
571+
delete interfaceO.extend;
572+
}
573+
// include extra newline so that the names of the interface are always compared
574+
output += ' {\n\n';
575+
output += interfaces[name].isType ? formatProp(name, interfaceO) : Object.entries(interfaceO).map(formatProp).join('\n');
550576
return `${output}\n}\n`;
577+
} else if (interfaceO === 'UNTYPED') {
578+
// include extra newline so that the names of the interface are always compared
579+
return `${name} {\n\n UNTYPED\n}\n`;
551580
} else {
552581
return '\n';
553582
}

0 commit comments

Comments
 (0)