-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Remove usage of
retainLines
(#5594)
* Remove usage of `retainLines` * update changelog * Move callsite to jest-util, add sourcemap support * This is a type * Remove unused function moved to jest-util * Fix lint * Minor clean `up * Feedback updates * fix flow violations and minimize diff * add todo * use sourcemaps in buffered console as well * fix stupid typo * Add unit tests for get_callsite * Fix tests * move changelog entry
- Loading branch information
Showing
17 changed files
with
189 additions
and
26 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
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
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import fs from 'fs'; | ||
import SourceMap from 'source-map'; | ||
import getCallsite from '../get_callsite'; | ||
|
||
jest.mock('fs'); | ||
|
||
describe('getCallsite', () => { | ||
test('without source map', () => { | ||
const site = getCallsite(0); | ||
|
||
expect(site.getFileName()).toEqual(__filename); | ||
expect(site.getColumnNumber()).toEqual(expect.any(Number)); | ||
expect(site.getLineNumber()).toEqual(expect.any(Number)); | ||
expect(fs.readFileSync).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('ignores errors when fs throws', () => { | ||
fs.readFileSync.mockImplementation(() => { | ||
throw new Error('Mock error'); | ||
}); | ||
|
||
const site = getCallsite(0, {[__filename]: 'mockedSourceMapFile'}); | ||
|
||
expect(site.getFileName()).toEqual(__filename); | ||
expect(site.getColumnNumber()).toEqual(expect.any(Number)); | ||
expect(site.getLineNumber()).toEqual(expect.any(Number)); | ||
expect(fs.readFileSync).toHaveBeenCalledWith('mockedSourceMapFile', 'utf8'); | ||
}); | ||
|
||
test('reads source map file to determine line and column', () => { | ||
fs.readFileSync.mockImplementation(() => 'file data'); | ||
|
||
const sourceMapColumn = 1; | ||
const sourceMapLine = 2; | ||
SourceMap.SourceMapConsumer = class { | ||
originalPositionFor(params) { | ||
expect(params).toMatchObject({ | ||
column: expect.any(Number), | ||
line: expect.any(Number), | ||
}); | ||
|
||
return { | ||
column: sourceMapColumn, | ||
line: sourceMapLine, | ||
}; | ||
} | ||
}; | ||
|
||
const site = getCallsite(0, {[__filename]: 'mockedSourceMapFile'}); | ||
|
||
expect(site.getFileName()).toEqual(__filename); | ||
expect(site.getColumnNumber()).toEqual(sourceMapColumn); | ||
expect(site.getLineNumber()).toEqual(sourceMapLine); | ||
expect(fs.readFileSync).toHaveBeenCalledWith('mockedSourceMapFile', 'utf8'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {SourceMapRegistry} from 'types/SourceMaps'; | ||
|
||
import fs from 'graceful-fs'; | ||
import callsites from 'callsites'; | ||
import {SourceMapConsumer} from 'source-map'; | ||
|
||
// Copied from https://github.com/rexxars/sourcemap-decorate-callsites/blob/5b9735a156964973a75dc62fd2c7f0c1975458e8/lib/index.js#L113-L158 | ||
const addSourceMapConsumer = (callsite, consumer) => { | ||
const getLineNumber = callsite.getLineNumber; | ||
const getColumnNumber = callsite.getColumnNumber; | ||
let position = null; | ||
|
||
function getPosition() { | ||
if (!position) { | ||
position = consumer.originalPositionFor({ | ||
column: getColumnNumber.call(callsite), | ||
line: getLineNumber.call(callsite), | ||
}); | ||
} | ||
|
||
return position; | ||
} | ||
|
||
Object.defineProperties(callsite, { | ||
getColumnNumber: { | ||
value() { | ||
return getPosition().column || getColumnNumber.call(callsite); | ||
}, | ||
writable: false, | ||
}, | ||
getLineNumber: { | ||
value() { | ||
return getPosition().line || getLineNumber.call(callsite); | ||
}, | ||
writable: false, | ||
}, | ||
}); | ||
}; | ||
|
||
export default (level: number, sourceMaps: ?SourceMapRegistry) => { | ||
const levelAfterThisCall = level + 1; | ||
const stack = callsites()[levelAfterThisCall]; | ||
const sourceMapFileName = sourceMaps && sourceMaps[stack.getFileName()]; | ||
|
||
if (sourceMapFileName) { | ||
try { | ||
const sourceMap = fs.readFileSync(sourceMapFileName, 'utf8'); | ||
addSourceMapConsumer(stack, new SourceMapConsumer(sourceMap)); | ||
} catch (e) { | ||
// ignore | ||
} | ||
} | ||
|
||
return stack; | ||
}; |
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
Oops, something went wrong.