-
-
Notifications
You must be signed in to change notification settings - Fork 26.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve unmapper file heuristic, add limited warning support, and ignore internal errors #2128
Merged
Merged
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
afba3fc
Browser sort is not stable
Timer 47cd783
Fix ordering of final message
Timer cf897f0
Register the warning capture
Timer a150609
Display only createElement warnings
Timer 8dfb007
Use different method name
Timer 4bae90d
Fix regression
Timer ab81262
Ignore errors with only node_module files
Timer 79af0fc
Ignore null files, too
Timer ec12b8c
Revise count
Timer fb7411b
Revise warning
Timer cd9c43f
Update overlay.js
Timer 83e6324
Add support for https://github.com/facebook/react/pull/9679
Timer ede4f9b
Use absolute paths
Timer 8997f58
Trim path if it's absolute
Timer 19eadc2
Make sure it's an absolute path
Timer 43a97e3
Oops
Timer 52a39a4
Tweak for new behavior
gaearon 4d9f647
Make it safer
gaearon d7f81e7
More resilient warnings
gaearon 2d4fdad
Merge branch 'master' into unmapper-fix
gaearon 4cb6be3
Prettier output
gaearon 11fc0f0
Merge remote-tracking branch 'Timer/unmapper-fix' into Timer-unmapper…
gaearon 9da83b3
Fix flow
gaearon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,19 @@ import { getSourceMap } from './getSourceMap'; | |
import { getLinesAround } from './getLinesAround'; | ||
import path from 'path'; | ||
|
||
function count(search: string, string: string): number { | ||
// Count starts at -1 becuse a do-while loop always runs at least once | ||
let count = -1, index = -1; | ||
do { | ||
// First call or the while case evaluated true, meaning we have to make | ||
// count 0 or we found a character | ||
++count; | ||
// Find the index of our search string, starting after the previous index | ||
index = string.indexOf(search, index + 1); | ||
} while (index !== -1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's better thanks! |
||
return count; | ||
} | ||
|
||
/** | ||
* Turns a set of mapped <code>StackFrame</code>s back into their generated code position and enhances them with code. | ||
* @param {string} fileUri The URI of the <code>bundle.js</code> file. | ||
|
@@ -39,28 +52,23 @@ async function unmap( | |
return frame; | ||
} | ||
const fN: string = fileName; | ||
const splitCache1: any = {}, splitCache2: any = {}, splitCache3: any = {}; | ||
const source = map | ||
.getSources() | ||
.map(s => s.replace(/[\\]+/g, '/')) | ||
.filter(s => { | ||
s = path.normalize(s); | ||
return s.indexOf(fN) === s.length - fN.length; | ||
}) | ||
.sort((a, b) => { | ||
let a2 = splitCache1[a] || (splitCache1[a] = a.split(path.sep)), | ||
b2 = splitCache1[b] || (splitCache1[b] = b.split(path.sep)); | ||
return Math.sign(a2.length - b2.length); | ||
}) | ||
.sort((a, b) => { | ||
let a2 = splitCache2[a] || (splitCache2[a] = a.split('node_modules')), | ||
b2 = splitCache2[b] || (splitCache2[b] = b.split('node_modules')); | ||
return Math.sign(a2.length - b2.length); | ||
.filter(p => { | ||
p = path.normalize(p); | ||
const i = p.lastIndexOf(fN); | ||
return i !== -1 && i === p.length - fN.length; | ||
}) | ||
.map(p => ({ | ||
token: p, | ||
seps: count(path.sep, path.normalize(p)), | ||
penalties: count('node_modules', p) + count('~', p), | ||
})) | ||
.sort((a, b) => { | ||
let a2 = splitCache3[a] || (splitCache3[a] = a.split('~')), | ||
b2 = splitCache3[b] || (splitCache3[b] = b.split('~')); | ||
return Math.sign(a2.length - b2.length); | ||
const s = Math.sign(a.seps - b.seps); | ||
if (s !== 0) return s; | ||
return Math.sign(a.penalties - b.penalties); | ||
}); | ||
if (source.length < 1 || lineNumber == null) { | ||
return new StackFrame( | ||
|
@@ -76,13 +84,14 @@ async function unmap( | |
null | ||
); | ||
} | ||
const sourceT = source[0].token; | ||
const { line, column } = map.getGeneratedPosition( | ||
source[0], | ||
sourceT, | ||
lineNumber, | ||
// $FlowFixMe | ||
columnNumber | ||
); | ||
const originalSource = map.getSource(source[0]); | ||
const originalSource = map.getSource(sourceT); | ||
return new StackFrame( | ||
functionName, | ||
fileUri, | ||
|
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,43 @@ | ||
// @flow | ||
|
||
// This is a list of React warnings to display | ||
// There must be zero or one capture group; and the capture group is assumed to be a missing stack frame. | ||
const warnings = [ | ||
/^.*React.createElement: type is invalid.+Check your code at (.*?:.*)[.]$/, | ||
]; | ||
// This is a list of information to remove from React warnings, it's not particularly useful to show | ||
const removals = [/Check your code at (.*?:.*)[.]/]; | ||
|
||
function massage(warning: string): { message: string, stack: string } | null { | ||
const nIndex = warning.indexOf('\n'); | ||
let message = warning; | ||
if (nIndex !== -1) { | ||
message = message.substring(0, nIndex); | ||
} | ||
let stack = warning.substring(nIndex + 1); | ||
|
||
let found = false; | ||
for (const warning of warnings) { | ||
const m = message.match(warning); | ||
if (!m) { | ||
continue; | ||
} | ||
found = true; | ||
if (!m[1]) { | ||
break; | ||
} | ||
stack = `in (render function) (at ${m[1]})\n${stack}`; | ||
break; | ||
} | ||
if (!found) { | ||
return null; | ||
} | ||
|
||
for (const trim of removals) { | ||
message = message.replace(trim, ''); | ||
} | ||
|
||
return { message, stack }; | ||
} | ||
|
||
export { massage }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be careful not to leave messages that would be confusing to users. If one sees this message, is this a bug? Should they file an issue? Should the message say so?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would be a better message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. Can’t we just rethrow the error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error is always thrown, we don't swallow it. We're intentionally not showing it -- so we need some message. With no message they might think it's a bug on our end (that the overlay didn't show up).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh. I think in this case it's fine not to print anything extra. IMO let's silently leave the browser behavior in this case and see what people say.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay!