mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-02 18:11:04 +08:00
There is a name-clash on the Closure type-defs generated from CDP, which also use the Protocol namespace. Therefore, for es-imports we should use `import * as ProtocolModule` instead. Bug: 1006759 Change-Id: I1e7e92825fdafe56c4f5274e520650001789014c Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2016755 Auto-Submit: Tim van der Lippe <tvanderlippe@chromium.org> Reviewed-by: Paul Lewis <aerotwist@chromium.org> Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
69 lines
2.8 KiB
TypeScript
69 lines
2.8 KiB
TypeScript
// Copyright 2019 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import { parse, print, types, visit } from 'recast';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { promisify } from 'util';
|
|
|
|
const readDir = promisify(fs.readdir);
|
|
const readFile = promisify(fs.readFile);
|
|
const stat = promisify(fs.stat);
|
|
const b = types.builders;
|
|
|
|
const FRONT_END_FOLDER = path.join(__dirname, '..', '..', 'front_end')
|
|
|
|
export async function getMappings(namespace: string, mappings: Map<string, any>, useExternalRefs = false) {
|
|
const src = namespace.toLocaleLowerCase();
|
|
const externalModule = path.join(FRONT_END_FOLDER, src, `${src}.js`);
|
|
const legacy = path.join(FRONT_END_FOLDER, src, `${src}-legacy.js`);
|
|
|
|
if (!(await stat(legacy))) {
|
|
console.error(`Unable to find legacy file: ${legacy}`);
|
|
process.exit(1);
|
|
return mappings;
|
|
}
|
|
|
|
const legacyFileContents = await readFile(legacy, { encoding: 'utf-8' });
|
|
const ast = parse(legacyFileContents);
|
|
|
|
for (const statement of ast.program.body) {
|
|
if (statement.type !== 'ExpressionStatement') {
|
|
continue;
|
|
}
|
|
|
|
// We need to check that we have an assignment expression, of which the left and right are both member expressions.
|
|
// This allows us to extract things like Foo.Bar = FooModule.Bar.Bar from the legacy file, while ignoring self.Foo
|
|
// and Foo = Foo || {} statements.
|
|
const isMemberExpressionOnLeftAndRight = statement.expression && statement.expression.left && statement.expression.right &&
|
|
statement.expression.type === 'AssignmentExpression' && statement.expression.left.type === 'MemberExpression' &&
|
|
statement.expression.right.type === 'MemberExpression'
|
|
|
|
if (isMemberExpressionOnLeftAndRight) {
|
|
// Rename FooModule back to Foo because we know we will want to use the latter when doing replacements.
|
|
if (statement.expression.right.object && statement.expression.right.object.object &&
|
|
statement.expression.right.object.object.type === 'Identifier') {
|
|
statement.expression.right.object.object.name = statement.expression.right.object.object.name.replace(/Module$/, '');
|
|
}
|
|
|
|
const leftSide = print(statement.expression.left).code;
|
|
const rightSide = print(statement.expression.right).code;
|
|
const rightSideParts = rightSide.split('.');
|
|
const file = useExternalRefs ? externalModule : path.join(FRONT_END_FOLDER, src, rightSideParts[1] + '.js');
|
|
|
|
if (rightSideParts[0] === 'Protocol') {
|
|
rightSideParts[0] = 'ProtocolModule';
|
|
}
|
|
|
|
mappings.set(leftSide, {
|
|
file,
|
|
replacement: rightSideParts.join('.'),
|
|
sameFolderReplacement: rightSideParts[rightSideParts.length - 1]
|
|
});
|
|
}
|
|
}
|
|
|
|
return mappings;
|
|
}
|