|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow |
| 8 | + * @format |
| 9 | + * @oncall react_native |
| 10 | + */ |
| 11 | + |
| 12 | +const babel = require('@babel/core'); |
| 13 | +const generate = require('@babel/generator').default; |
| 14 | + |
| 15 | +const Result = { |
| 16 | + BREAKING: 'BREAKING', |
| 17 | + POTENTIALLY_NON_BREAKING: 'POTENTIALLY_NON_BREAKING', |
| 18 | + NON_BREAKING: 'NON_BREAKING', |
| 19 | +} as const; |
| 20 | + |
| 21 | +type Output = { |
| 22 | + result: $Values<typeof Result>, |
| 23 | + changedApis: Array<string>, |
| 24 | +}; |
| 25 | + |
| 26 | +function diffApiSnapshot(prevSnapshot: string, newSnapshot: string): Output { |
| 27 | + const prevSnapshotAST = babel.parseSync(prevSnapshot, { |
| 28 | + plugins: ['@babel/plugin-syntax-typescript'], |
| 29 | + }); |
| 30 | + const newSnapshotAST = babel.parseSync(newSnapshot, { |
| 31 | + plugins: ['@babel/plugin-syntax-typescript'], |
| 32 | + }); |
| 33 | + const prevStatements = getExportedStatements(prevSnapshotAST); |
| 34 | + const newStatements = getExportedStatements(newSnapshotAST); |
| 35 | + |
| 36 | + return analyzeStatements(prevStatements, newStatements); |
| 37 | +} |
| 38 | + |
| 39 | +function getExportedStatements( |
| 40 | + ast: BabelNodeFile, |
| 41 | +): Array<BabelNodeExportNamedDeclaration> { |
| 42 | + return ast.program.body.filter( |
| 43 | + statement => statement.type === 'ExportNamedDeclaration', |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +function analyzeStatements( |
| 48 | + prevStatements: Array<BabelNodeExportNamedDeclaration>, |
| 49 | + newStatements: Array<BabelNodeExportNamedDeclaration>, |
| 50 | +): Output { |
| 51 | + const output = { |
| 52 | + result: Result.NON_BREAKING, |
| 53 | + changedApis: [], |
| 54 | + } as Output; |
| 55 | + |
| 56 | + // Create a mapping between prev and new statements |
| 57 | + type Pair = Map<'prev' | 'new', BabelNodeExportNamedDeclaration>; |
| 58 | + const mapping: Array<[string, Pair]> = []; |
| 59 | + const prevNodesMapping = getExportedNodesNames(prevStatements); |
| 60 | + const newNodesMapping = Object.fromEntries( |
| 61 | + getExportedNodesNames(newStatements), |
| 62 | + ); |
| 63 | + |
| 64 | + for (const [name, prevNode] of prevNodesMapping) { |
| 65 | + if (newNodesMapping[name]) { |
| 66 | + const pairMap: Pair = new Map(); |
| 67 | + pairMap.set('new', newNodesMapping[name]); |
| 68 | + pairMap.set('prev', prevNode); |
| 69 | + mapping.push([name, pairMap]); |
| 70 | + // remove the node to check if there are any new nodes later |
| 71 | + delete newNodesMapping[name]; |
| 72 | + } else { |
| 73 | + // There is no statement of that name in the new rollup which means that: |
| 74 | + // 1. This statement was entirely removed |
| 75 | + // 2. This statement was renamed |
| 76 | + // 3. It is not public anymore |
| 77 | + output.result = Result.BREAKING; |
| 78 | + output.changedApis.push(stripSuffix(name)); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + for (const [name, pair] of mapping) { |
| 83 | + const prevNode = pair.get('prev'); |
| 84 | + const newNode = pair.get('new'); |
| 85 | + if (!prevNode || !newNode) { |
| 86 | + throw new Error('Node in pair is undefined'); |
| 87 | + } |
| 88 | + if (didStatementChange(prevNode, newNode)) { |
| 89 | + output.result = Result.BREAKING; |
| 90 | + output.changedApis.push(stripSuffix(name)); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // if all prev nodes are matched and there are some new nodes left |
| 95 | + if ( |
| 96 | + output.result === Result.NON_BREAKING && |
| 97 | + Object.keys(newNodesMapping).length > 0 |
| 98 | + ) { |
| 99 | + // New statement added |
| 100 | + output.result = Result.POTENTIALLY_NON_BREAKING; |
| 101 | + for (const name of Object.keys(newNodesMapping)) { |
| 102 | + output.changedApis.push(stripSuffix(name)); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return output; |
| 107 | +} |
| 108 | + |
| 109 | +function getExportedNodesNames( |
| 110 | + nodes: Array<BabelNodeExportNamedDeclaration>, |
| 111 | +): Array<[string, BabelNodeExportNamedDeclaration]> { |
| 112 | + const nodeNames: Array<[string, BabelNodeExportNamedDeclaration]> = []; |
| 113 | + nodes.forEach(node => { |
| 114 | + if (node.declaration) { |
| 115 | + let name = getExportedNodeName(node); |
| 116 | + // for declare const/type case we get two statements with the same name |
| 117 | + // export declare const foo = string; |
| 118 | + // export declare type foo = typeof foo; |
| 119 | + // we add a _type and _var suffix to differentiate them |
| 120 | + if (node.declaration?.type === 'TSTypeAliasDeclaration') { |
| 121 | + name += '__type'; |
| 122 | + } else if (node.declaration?.type === 'VariableDeclaration') { |
| 123 | + name += '__var'; |
| 124 | + } |
| 125 | + nodeNames.push([name, node]); |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + return nodeNames; |
| 130 | +} |
| 131 | + |
| 132 | +function stripSuffix(name: string): string { |
| 133 | + const regex = /(__type|__var)$/; |
| 134 | + return name.replace(regex, ''); |
| 135 | +} |
| 136 | + |
| 137 | +function getExportedNodeName(node: BabelNodeExportNamedDeclaration): string { |
| 138 | + if (node.declaration?.type === 'TSTypeAliasDeclaration') { |
| 139 | + return node.declaration.id.name; |
| 140 | + } else if (node.declaration?.type === 'VariableDeclaration') { |
| 141 | + if (node.declaration.declarations.length !== 1) { |
| 142 | + throw new Error('Unsupported number of variable declarations'); |
| 143 | + } |
| 144 | + const variableDeclaration = node.declaration.declarations[0]; |
| 145 | + if (variableDeclaration.id.type !== 'Identifier') { |
| 146 | + throw new Error('Variable declaration id type is not Identifier'); |
| 147 | + } |
| 148 | + |
| 149 | + return variableDeclaration.id.name; |
| 150 | + } else if (node.declaration?.type === 'ClassDeclaration') { |
| 151 | + if (!node.declaration.id) { |
| 152 | + throw new Error('Class declaration id is undefined'); |
| 153 | + } |
| 154 | + |
| 155 | + return node.declaration.id.name; |
| 156 | + } else if (node.declaration?.type === 'TSModuleDeclaration') { |
| 157 | + if (node.declaration.id.type === 'StringLiteral') { |
| 158 | + return node.declaration.id.value; |
| 159 | + } else { |
| 160 | + return node.declaration.id.name; |
| 161 | + } |
| 162 | + } else if (node.declaration?.type === 'TSDeclareFunction') { |
| 163 | + if (!node.declaration.id) { |
| 164 | + throw new Error('Function declaration id is undefined'); |
| 165 | + } |
| 166 | + return node.declaration.id?.name; |
| 167 | + } else if (node.declaration?.type === 'TSInterfaceDeclaration') { |
| 168 | + return node.declaration.id.name; |
| 169 | + } |
| 170 | + |
| 171 | + throw new Error('Unsupported node declaration type'); |
| 172 | +} |
| 173 | + |
| 174 | +function didStatementChange( |
| 175 | + previousAST: BabelNodeStatement, |
| 176 | + newAST: BabelNodeStatement, |
| 177 | +) { |
| 178 | + const previousCode = getMinifiedCode(previousAST); |
| 179 | + const newCode = getMinifiedCode(newAST); |
| 180 | + return previousCode !== newCode; |
| 181 | +} |
| 182 | + |
| 183 | +function getMinifiedCode(ast: BabelNodeStatement) { |
| 184 | + return generate(ast, { |
| 185 | + minified: true, |
| 186 | + }).code; |
| 187 | +} |
| 188 | + |
| 189 | +module.exports = {diffApiSnapshot, Result}; |
0 commit comments