-
Notifications
You must be signed in to change notification settings - Fork 2k
Soften name validation warnings to avoid CI issues #717
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,139 @@ | ||
/** | ||
* Copyright (c) 2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
import { describe, it } from 'mocha'; | ||
import { expect } from 'chai'; | ||
import { formatWarning } from '../assertValidName'; | ||
|
||
/** | ||
* Helper for dedenting indented template literals. This helps us to | ||
* keep the tests pretty. | ||
*/ | ||
function dedent(string) { | ||
// Get lines, discarding empty leading and trailing lines. | ||
const lines = string.replace(/^[ \t]*\n|\n[ \t]*$/g, '').split('\n'); | ||
|
||
// Find smallest indent. | ||
const indent = lines.reduce((currentMinimum, line) => { | ||
const whitespace = line.match(/^ +/); | ||
return Math.min( | ||
whitespace ? whitespace[0].length : 0, | ||
currentMinimum | ||
); | ||
}, Infinity); | ||
|
||
// Remove indent from each line. | ||
return lines.map(line => line.slice(indent)).join('\n'); | ||
} | ||
|
||
/** | ||
* Convenience method for creating an Error object with a defined stack. | ||
*/ | ||
function createErrorObject(message, stack) { | ||
const error = new Error(message); | ||
error.stack = stack; | ||
return error; | ||
} | ||
|
||
describe('formatWarning()', () => { | ||
it('formats given a Chrome-style stack property', () => { | ||
const chromeStack = dedent(` | ||
Error: foo | ||
at z (<anonymous>:1:21) | ||
at y (<anonymous>:1:15) | ||
at x (<anonymous>:1:15) | ||
at <anonymous>:1:6 | ||
`); | ||
const error = createErrorObject('foo', chromeStack); | ||
expect(formatWarning(error)).to.equal(dedent(` | ||
foo | ||
at z (<anonymous>:1:21) | ||
at y (<anonymous>:1:15) | ||
at x (<anonymous>:1:15) | ||
at <anonymous>:1:6 | ||
`)); | ||
}); | ||
|
||
it('formats given a Node-style stack property', () => { | ||
const nodeStack = dedent(` | ||
Error: foo | ||
at z (repl:1:29) | ||
at y (repl:1:23) | ||
at x (repl:1:23) | ||
at repl:1:6 | ||
at ContextifyScript.Script.runInThisContext (vm.js:23:33) | ||
at REPLServer.defaultEval (repl.js:340:29) | ||
at bound (domain.js:280:14) | ||
at REPLServer.runBound [as eval] (domain.js:293:12) | ||
at REPLServer.onLine (repl.js:537:10) | ||
at emitOne (events.js:101:20) | ||
`); | ||
const error = createErrorObject('foo', nodeStack); | ||
expect(formatWarning(error)).to.equal(dedent(` | ||
foo | ||
at z (repl:1:29) | ||
at y (repl:1:23) | ||
at x (repl:1:23) | ||
at repl:1:6 | ||
at ContextifyScript.Script.runInThisContext (vm.js:23:33) | ||
at REPLServer.defaultEval (repl.js:340:29) | ||
at bound (domain.js:280:14) | ||
at REPLServer.runBound [as eval] (domain.js:293:12) | ||
at REPLServer.onLine (repl.js:537:10) | ||
at emitOne (events.js:101:20) | ||
`)); | ||
}); | ||
|
||
it('formats given a Firefox-style stack property', () => { | ||
const firefoxStack = dedent(` | ||
z@debugger eval code:1:20 | ||
y@debugger eval code:1:14 | ||
x@debugger eval code:1:14 | ||
@debugger eval code:1:5 | ||
`); | ||
const error = createErrorObject('foo', firefoxStack); | ||
expect(formatWarning(error)).to.equal(dedent(` | ||
foo | ||
z@debugger eval code:1:20 | ||
y@debugger eval code:1:14 | ||
x@debugger eval code:1:14 | ||
@debugger eval code:1:5 | ||
`)); | ||
}); | ||
|
||
it('formats given a Safari-style stack property', () => { | ||
const safariStack = dedent(` | ||
z | ||
y | ||
x | ||
global code | ||
evaluateWithScopeExtension@[native code] | ||
_evaluateOn | ||
_evaluateAndWrap | ||
evaluate | ||
`); | ||
const error = createErrorObject('foo', safariStack); | ||
expect(formatWarning(error)).to.equal(dedent(` | ||
foo | ||
z | ||
y | ||
x | ||
global code | ||
evaluateWithScopeExtension@[native code] | ||
_evaluateOn | ||
_evaluateAndWrap | ||
evaluate | ||
`)); | ||
}); | ||
|
||
it('formats in the absence of a stack property', () => { | ||
const error = createErrorObject('foo'); | ||
expect(formatWarning(error)).to.equal('foo'); | ||
}); | ||
}); |
This file contains hidden or 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
*/ | ||
|
||
const NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/; | ||
const ERROR_PREFIX_RX = /^Error: /; | ||
|
||
// Ensures console warnings are only issued once. | ||
let hasWarnedAboutDunder = false; | ||
|
@@ -28,12 +29,13 @@ export function assertValidName( | |
if (!isIntrospection && name.slice(0, 2) === '__' && !hasWarnedAboutDunder) { | ||
hasWarnedAboutDunder = true; | ||
/* eslint-disable no-console */ | ||
if (console && console.error) { | ||
if (console && console.warn) { | ||
const error = new Error( | ||
`Name "${name}" must not begin with "__", which is reserved by ` + | ||
'GraphQL introspection.' | ||
'GraphQL introspection. In a future release of graphql this will ' + | ||
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. nit: graphql -> graphql-js? 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. I went with |
||
'become a hard error.' | ||
); | ||
console.error(error.stack || String(error)); | ||
console.warn(formatWarning(error)); | ||
} | ||
/* eslint-enable no-console */ | ||
} | ||
|
@@ -43,3 +45,20 @@ export function assertValidName( | |
); | ||
} | ||
} | ||
|
||
/** | ||
* Returns a human-readable warning based an the supplied Error object, | ||
* including stack trace information if available. | ||
*/ | ||
export function formatWarning(error: Error): string { | ||
let formatted = ''; | ||
const errorString = String(error).replace(ERROR_PREFIX_RX, ''); | ||
const stack = error.stack; | ||
if (stack) { | ||
formatted = stack.replace(ERROR_PREFIX_RX, ''); | ||
} | ||
if (formatted.indexOf(errorString) === -1) { | ||
formatted = errorString + '\n' + formatted; | ||
} | ||
return formatted.trim(); | ||
} |
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.
Love this