Files
Dawid Małecki 6b40f35032 Add diff-api-snapshot for public API breaking change detection (#51972)
Summary:
This diff adds snapshot `diff-api-snapshot` script for public JS API breaking change detection.

### Motivation
Detecting if there are any breaking changes introduced in the commit. It is achieved by comparing `ReactNativeApi.d.ts` rollup from the current and previous revision.

This is a naive implementation with a three possible outcomes:
- BREAKING
- POTENTIALLY_NOT_BREAKING,
- NOT_BREAKING

The algorithm analyses exported top-level statements (after inlining) in both rollups and tries to create a mapping between them by name.

The **BREAKING** outcome happens whenever the statement is:
- removed
- renamed
- changed
- not exported anymore (private)

The **POTENTIALLY_NOT_BREAKING** outcome  happens if it's not BREAKING and the new statement is added.

The **NOT_BREAKING** outcome happens if public API snapshot doesn't change.

Changelog:
[General][Added] - Add public JS API breaking change detection under `yarn diff-api-snapshot` script.

Pull Request resolved: https://github.com/facebook/react-native/pull/51972

Test Plan:
Signals, added tests.

In `react-native-github` run:
`yarn test scripts/diff-api-snapshot/__tests__/diffApiSnapshot-test.js`

Rollback Plan:

Reviewed By: j-piasecki

Differential Revision: D76430965

Pulled By: coado

fbshipit-source-id: 095a196aa4f643501db0af9262556ddefff5d30d
2025-06-17 02:27:53 -07:00

62 lines
1.5 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
* @oncall react_native
*/
require('../babel-register').registerForScript();
const {diffApiSnapshot} = require('./diffApiSnapshot');
const fs = require('fs');
const {parseArgs} = require('util');
const config = {
allowPositionals: true,
options: {
help: {type: 'boolean'},
},
};
async function main() {
const {
positionals: [prevSnapshot, newSnapshot],
values: {help},
/* $FlowFixMe[incompatible-call] Natural Inference rollout. See
* https://fburl.com/workplace/6291gfvu */
} = parseArgs(config);
if (help) {
console.log(`
Usage: node ./scripts/diff-api-snapshot <prev-snapshot> <new-snapshot>
Analyze changes between two versions of React Native's JavaScript API
snapshot (yarn build-types). Returns a JSON object with the following
fields:
- result: 'BREAKING', 'POTENTIALLY_NON_BREAKING' or 'NON_BREAKING'.
- changedApis: List of changed APIs.
`);
process.exitCode = 0;
return;
}
try {
const result = diffApiSnapshot(
fs.readFileSync(prevSnapshot, 'utf8'),
fs.readFileSync(newSnapshot, 'utf8'),
);
console.log(JSON.stringify(result, null, 2));
} catch (e) {
console.error('Error parsing API snapshot:', e);
process.exitCode = 1;
}
}
if (require.main === module) {
void main();
}