Skip to content
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

Remove nodejs deprecation warnings for custom 'inspect' functions #1605

Merged
merged 2 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"test": "npm run lint && npm run check && npm run testonly",
"test:ci": "npm run lint && npm run check && npm run testonly:coveralls",
"t": "mocha --require @babel/register --require @babel/polyfill",
"testonly": "mocha --require @babel/register --require @babel/polyfill --check-leaks --full-trace --timeout 15000 src/**/__tests__/**/*-test.js",
"testonly": "mocha --throw-deprecation --require @babel/register --require @babel/polyfill --check-leaks --full-trace --timeout 15000 src/**/__tests__/**/*-test.js",
"testonly:cover": "nyc --reporter html --reporter text-summary -- npm run testonly",
"testonly:coveralls": "nyc --silent -- npm run testonly && nyc report --reporter text-lcov | coveralls",
"lint": "eslint --report-unused-disable-directives src || (printf '\\033[33mTry: \\033[7m npm run lint -- --fix \\033[0m\\n' && exit 1)",
Expand Down
17 changes: 17 additions & 0 deletions src/jsutils/__tests__/inspect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';
import inspect from '../inspect';
import invariant from '../invariant';
import nodejsCustomInspectSymbol from '../nodejsCustomInspectSymbol';

describe('inspect', () => {
it('undefined', () => {
Expand Down Expand Up @@ -75,4 +77,19 @@ describe('inspect', () => {

expect(inspect(object)).to.equal('<custom inspect>');
});

it('custom inspect', () => {
invariant(nodejsCustomInspectSymbol);

const object = {
inspect() {
return '<custom inspect>';
},
[String(nodejsCustomInspectSymbol)]() {
return '<custom symbol inspect>';
},
};

expect(inspect(object)).to.equal('<custom symbol inspect>');
});
});
17 changes: 13 additions & 4 deletions src/jsutils/defineToJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@
* @flow strict
*/

import nodejsCustomInspectSymbol from './nodejsCustomInspectSymbol';

/**
* The `defineToJSON()` function defines toJSON() and inspect() prototype
* methods which are aliases for toString().
* methods, if no function provided they become aliases for toString().
*/
export default function defineToJSON(classObject: Class<any>): void {
classObject.prototype.toJSON = classObject.prototype.inspect =
classObject.prototype.toString;
export default function defineToJSON(
// eslint-disable-next-line flowtype/no-weak-types
classObject: Class<any> | Function,
fn?: () => any = classObject.prototype.toString,
): void {
classObject.prototype.toJSON = fn;
classObject.prototype.inspect = fn;
if (nodejsCustomInspectSymbol) {
classObject.prototype[nodejsCustomInspectSymbol] = fn;
}
}
7 changes: 6 additions & 1 deletion src/jsutils/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @flow strict
*/

import nodejsCustomInspectSymbol from './nodejsCustomInspectSymbol';

/**
* Used to print values in error messages.
*/
Expand All @@ -18,7 +20,10 @@ export default function inspect(value: mixed): string {
return value.name ? `[function ${value.name}]` : '[function]';
case 'object':
if (value) {
if (typeof value.inspect === 'function') {
const customInspectFn = value[String(nodejsCustomInspectSymbol)];
if (typeof customInspectFn === 'function') {
return customInspectFn();
} else if (typeof value.inspect === 'function') {
return value.inspect();
} else if (Array.isArray(value)) {
return '[' + value.map(inspect).join(', ') + ']';
Expand Down
15 changes: 15 additions & 0 deletions src/jsutils/nodejsCustomInspectSymbol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

const nodejsCustomInspectSymbol =
typeof Symbol === 'function'
? Symbol.for('nodejs.util.inspect.custom')
: undefined;

export default nodejsCustomInspectSymbol;
5 changes: 3 additions & 2 deletions src/language/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @flow strict
*/

import defineToJSON from '../jsutils/defineToJSON';
import type { Token } from './ast';
import type { Source } from './source';
import { syntaxError } from '../error';
Expand Down Expand Up @@ -162,14 +163,14 @@ function Tok(
}

// Print a simplified form when appearing in JSON/util.inspect.
Tok.prototype.toJSON = Tok.prototype.inspect = function toJSON() {
defineToJSON(Tok, function() {
return {
kind: this.kind,
value: this.value,
line: this.line,
column: this.column,
};
};
});

function printCharCode(code) {
return (
Expand Down
5 changes: 3 additions & 2 deletions src/language/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import inspect from '../jsutils/inspect';
import defineToJSON from '../jsutils/defineToJSON';
import { Source } from './source';
import { syntaxError } from '../error';
import type { GraphQLError } from '../error';
Expand Down Expand Up @@ -1444,9 +1445,9 @@ function Loc(startToken: Token, endToken: Token, source: Source) {
}

// Print a simplified form when appearing in JSON/util.inspect.
Loc.prototype.toJSON = Loc.prototype.inspect = function toJSON() {
defineToJSON(Loc, function() {
return { start: this.start, end: this.end };
};
});

/**
* Determines if the next token is of a given kind
Expand Down