-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw Flow syntax errors instead of continuing to process the AST (#3…
…9035) Summary: Pull Request resolved: #39035 Changelog: [General][Fixed] Flow syntax errors in Codegen specs are no longer ignored. Instead of throwing errors like most parsers, the `flow-parser` package returns errors as part of the AST (along with a best-effort parse). It turns out that `react-native/codegen` ignores such errors and only detects a subset of them after the fact. Here we change the behaviour to immediately throwing a descriptive error message (containing the file name and a code frame). **This change is theoretically breaking** for any published packages that already contain broken Flow code (that somehow doesn't happen to affect the Codegen output today). Hopefully, anyone using Flow-flavoured RN Codegen is also typechecking with Flow and/or building with Metro (which would both flag the same errors), so the impact should be fairly contained. Reviewed By: huntie Differential Revision: D48385786 fbshipit-source-id: 240ac5b8dce6cfd496bdc42d2e959e540384451d
- Loading branch information
1 parent
970ba05
commit 7713614
Showing
10 changed files
with
126 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/react-native-codegen/src/parsers/flow/parseFlowAndThrowErrors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* 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 strict | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// $FlowFixMe[untyped-import] there's no flowtype flow-parser | ||
const flowParser = require('flow-parser'); | ||
|
||
// $FlowFixMe[untyped-import] | ||
const {codeFrameColumns} = require('@babel/code-frame'); | ||
|
||
class FlowParserSyntaxError extends Error { | ||
constructor( | ||
code: string, | ||
filename: ?string, | ||
errors: $ReadOnlyArray<{ | ||
loc: $ReadOnly<{ | ||
start: $ReadOnly<{line: number, column: number}>, | ||
end: $ReadOnly<{line: number, column: number}>, | ||
}>, | ||
message: string, | ||
}>, | ||
) { | ||
const firstError = errors[0]; | ||
const codeFrame = codeFrameColumns( | ||
code, | ||
{ | ||
start: { | ||
line: firstError.loc.start.line, | ||
// flow-parser returns 0-indexed columns but Babel expects 1-indexed | ||
column: firstError.loc.start.column + 1, | ||
}, | ||
end: { | ||
line: firstError.loc.end.line, | ||
// flow-parser returns 0-indexed columns but Babel expects 1-indexed | ||
column: firstError.loc.end.column + 1, | ||
}, | ||
}, | ||
{ | ||
forceColor: false, | ||
}, | ||
); | ||
const additionalErrorsMessage = errors.length | ||
? '\n\nand ' + | ||
errors.length + | ||
' other error' + | ||
(errors.length > 1 ? 's' : '') + | ||
' in the same file.' | ||
: ''; | ||
|
||
super( | ||
(filename != null ? `Syntax error in ${filename}: ` : 'Syntax error: ') + | ||
firstError.message + | ||
'\n' + | ||
codeFrame + | ||
additionalErrorsMessage, | ||
); | ||
} | ||
} | ||
|
||
function parseFlowAndThrowErrors( | ||
code: string, | ||
options: $ReadOnly<{filename?: ?string}> = {}, | ||
): $FlowFixMe { | ||
const ast = flowParser.parse(code, { | ||
enums: true, | ||
}); | ||
if (ast.errors && ast.errors.length) { | ||
throw new FlowParserSyntaxError(code, options.filename, ast.errors); | ||
} | ||
return ast; | ||
} | ||
|
||
module.exports = { | ||
parseFlowAndThrowErrors, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters