Use buildCodeFrameError in babel-plugin-inline-view-configs

Summary:
The next version of Babel changes how it prints file names in errors. This diff fixes the test by using `/` as the `cwd` and switches the plagin to use `path.buildCodeFrameError` so errors will be more helpful for users.

I renamed the `nodePath` variable to `path` because that's what babel plugins usually do.

Changelog: [Internal]

Reviewed By: motiz88

Differential Revision: D20781805

fbshipit-source-id: cc149dce6389aa9402ce70ea30035c74a6150ea3
This commit is contained in:
Christoph Nakazawa
2020-04-01 05:33:11 -07:00
committed by Facebook GitHub Bot
parent 2b062eadc8
commit f6de7b826d
3 changed files with 50 additions and 26 deletions
@@ -136,8 +136,35 @@ exports[`Babel plugin inline view configs can inline config for NotANativeCompon
export default 'Not a view config';"
`;
exports[`Babel plugin inline view configs fails on inline config for CommandsExportedWithDifferentNameNativeComponent.js 1`] = `"Native commands must be exported with the name 'Commands'"`;
exports[`Babel plugin inline view configs fails on inline config for CommandsExportedWithDifferentNameNativeComponent.js 1`] = `
"/CommandsExportedWithDifferentNameNativeComponent.js: Native commands must be exported with the name 'Commands'
17 | }
18 |
> 19 | export const Foo = codegenNativeCommands<NativeCommands>();
| ^
20 |
21 | export default (codegenNativeComponent<ModuleProps>('Module'): NativeType);
22 | "
`;
exports[`Babel plugin inline view configs fails on inline config for CommandsExportedWithShorthandNativeComponent.js 1`] = `"'Commands' is a reserved export and may only be used to export the result of codegenNativeCommands."`;
exports[`Babel plugin inline view configs fails on inline config for CommandsExportedWithShorthandNativeComponent.js 1`] = `
"/CommandsExportedWithShorthandNativeComponent.js: 'Commands' is a reserved export and may only be used to export the result of codegenNativeCommands.
19 | const Commands = 4;
20 |
> 21 | export {Commands};
| ^
22 |
23 | export default (codegenNativeComponent<ModuleProps>('Module'): NativeType);
24 | "
`;
exports[`Babel plugin inline view configs fails on inline config for OtherCommandsExportNativeComponent.js 1`] = `"'Commands' is a reserved export and may only be used to export the result of codegenNativeCommands."`;
exports[`Babel plugin inline view configs fails on inline config for OtherCommandsExportNativeComponent.js 1`] = `
"/OtherCommandsExportNativeComponent.js: 'Commands' is a reserved export and may only be used to export the result of codegenNativeCommands.
17 | }
18 |
> 19 | export const Commands = 4;
| ^
20 |
21 | export default (codegenNativeComponent<ModuleProps>('Module'): NativeType);
22 | "
`;
@@ -16,9 +16,11 @@ const failures = require('../__test_fixtures__/failures.js');
function transform(fixture, filename) {
return babelTransform(fixture, {
plugins: [require('@babel/plugin-syntax-flow'), require('../index')],
babelrc: false,
filename,
cwd: '/',
filename: filename,
highlightCode: false,
plugins: [require('@babel/plugin-syntax-flow'), require('../index')],
}).code;
}
@@ -11,14 +11,12 @@
const {parseString} = require('react-native-codegen/src/parsers/flow');
const RNCodegen = require('react-native-codegen/src/generators/RNCodegen');
const path = require('path');
const {basename} = require('path');
function generateViewConfig(filename, code) {
const schema = parseString(code);
const libraryName = path
.basename(filename)
.replace(/NativeComponent\.js$/, '');
const libraryName = basename(filename).replace(/NativeComponent\.js$/, '');
return RNCodegen.generateViewConfig({
schema,
libraryName,
@@ -65,17 +63,17 @@ module.exports = function(context) {
this.codeInserted = false;
},
visitor: {
ExportNamedDeclaration(nodePath) {
ExportNamedDeclaration(path) {
if (this.codeInserted) {
return;
}
if (
nodePath.node.declaration &&
nodePath.node.declaration.declarations &&
nodePath.node.declaration.declarations[0]
path.node.declaration &&
path.node.declaration.declarations &&
path.node.declaration.declarations[0]
) {
const firstDeclaration = nodePath.node.declaration.declarations[0];
const firstDeclaration = path.node.declaration.declarations[0];
if (firstDeclaration.type === 'VariableDeclarator') {
if (
@@ -87,40 +85,37 @@ module.exports = function(context) {
firstDeclaration.id.type === 'Identifier' &&
firstDeclaration.id.name !== 'Commands'
) {
throw new Error(
throw path.buildCodeFrameError(
"Native commands must be exported with the name 'Commands'",
);
}
this.commandsExport = nodePath;
this.commandsExport = path;
return;
} else {
if (firstDeclaration.id.name === 'Commands') {
throw new Error(
throw path.buildCodeFrameError(
"'Commands' is a reserved export and may only be used to export the result of codegenNativeCommands.",
);
}
}
}
} else if (
nodePath.node.specifiers &&
nodePath.node.specifiers.length > 0
) {
nodePath.node.specifiers.forEach(specifier => {
} else if (path.node.specifiers && path.node.specifiers.length > 0) {
path.node.specifiers.forEach(specifier => {
if (
specifier.type === 'ExportSpecifier' &&
specifier.local.type === 'Identifier' &&
specifier.local.name === 'Commands'
) {
throw new Error(
throw path.buildCodeFrameError(
"'Commands' is a reserved export and may only be used to export the result of codegenNativeCommands.",
);
}
});
}
},
ExportDefaultDeclaration(nodePath, state) {
if (isCodegenDeclaration(nodePath.node.declaration)) {
this.defaultExport = nodePath;
ExportDefaultDeclaration(path, state) {
if (isCodegenDeclaration(path.node.declaration)) {
this.defaultExport = path;
}
},
Program: {