From f6de7b826d90f069b3a8ee103fcf1d90b542e11b Mon Sep 17 00:00:00 2001 From: Christoph Nakazawa Date: Wed, 1 Apr 2020 05:30:51 -0700 Subject: [PATCH] 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 --- .../__snapshots__/index-test.js.snap | 33 +++++++++++++++-- .../__tests__/index-test.js | 6 ++- .../babel-plugin-inline-view-configs/index.js | 37 ++++++++----------- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/packages/babel-plugin-inline-view-configs/__tests__/__snapshots__/index-test.js.snap b/packages/babel-plugin-inline-view-configs/__tests__/__snapshots__/index-test.js.snap index 67d908a6075..af56f2b191b 100644 --- a/packages/babel-plugin-inline-view-configs/__tests__/__snapshots__/index-test.js.snap +++ b/packages/babel-plugin-inline-view-configs/__tests__/__snapshots__/index-test.js.snap @@ -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(); + | ^ + 20 | + 21 | export default (codegenNativeComponent('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('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('Module'): NativeType); + 22 | " +`; diff --git a/packages/babel-plugin-inline-view-configs/__tests__/index-test.js b/packages/babel-plugin-inline-view-configs/__tests__/index-test.js index 26c0e09ad06..0a18d8c806c 100644 --- a/packages/babel-plugin-inline-view-configs/__tests__/index-test.js +++ b/packages/babel-plugin-inline-view-configs/__tests__/index-test.js @@ -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; } diff --git a/packages/babel-plugin-inline-view-configs/index.js b/packages/babel-plugin-inline-view-configs/index.js index ba58d0600fb..1a80f9c60b4 100644 --- a/packages/babel-plugin-inline-view-configs/index.js +++ b/packages/babel-plugin-inline-view-configs/index.js @@ -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: {