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

pretty-format: Omit unnecessary symbol filter for object keys #7457

Merged
merged 4 commits into from
Dec 24, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
- `[jest-haste-map]` Fix `require` detection with trailing commas and ignore `import typeof` modules ([#7385](https://github.com/facebook/jest/pull/7385))
- `[jest-cli]` Fix to set prettierPath via config file ([#7412](https://github.com/facebook/jest/pull/7412))
- `[jest-cli]` Support dashed args ([#7497](https://github.com/facebook/jest/pull/7497))
- `[pretty-format]` Omit unnecessary symbol filter for object keys ([#7457](https://github.com/facebook/jest/pull/7457))

### Chore & Maintenance

Expand Down
27 changes: 14 additions & 13 deletions packages/pretty-format/src/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@

import type {Config, Printer, Refs} from 'types/PrettyFormat';

const getSymbols = Object.getOwnPropertySymbols || (obj => []);
const getKeysOfEnumerableProperties = (object: Object) => {
const keys = Object.keys(object).sort();

if (Object.getOwnPropertySymbols) {
Object.getOwnPropertySymbols(object).forEach(symbol => {
//$FlowFixMe because property enumerable is missing in undefined
if (Object.getOwnPropertyDescriptor(object, symbol).enumerable) {
keys.push(symbol);
}
});
}

const isSymbol = key =>
// $FlowFixMe string literal `symbol`. This value is not a valid `typeof` return value
typeof key === 'symbol' || toString.call(key) === '[object Symbol]';
return keys;
};

// Return entries (for example, of a map)
// with spacing, indentation, and comma
Expand Down Expand Up @@ -161,15 +170,7 @@ export function printObjectProperties(
printer: Printer,
): string {
let result = '';
let keys = Object.keys(val).sort();
const symbols = getSymbols(val).filter(
//$FlowFixMe because property enumerable is missing in undefined
symbol => Object.getOwnPropertyDescriptor(val, symbol).enumerable,
);

if (symbols.length) {
keys = keys.filter(key => !isSymbol(key)).concat(symbols);
}
const keys = getKeysOfEnumerableProperties(val);

if (keys.length) {
result += config.spacingOuter;
Expand Down