-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track lineLengths during loader process
Workaround for: nodejs/node#48460
- Loading branch information
Showing
9 changed files
with
117 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// TODO: Refactor once https://github.com/nodejs/node/issues/48460 fixed | ||
|
||
import { fileURLToPath } from 'url' | ||
|
||
const kLLC = Symbol.for('@tapjs/processinfo lineLength cache') | ||
const g = global as { | ||
[kLLC]?: Map<string, number[]> | ||
} | ||
const cache = g[kLLC] || new Map<string, number[]>() | ||
g[kLLC] = cache | ||
|
||
const sourceMapComment = '//# sourceMappingURL=' | ||
export const saveLineLengths = (filename: string, content: string) => { | ||
if (filename.startsWith('file://')) filename = fileURLToPath(filename) | ||
if (cache.has(filename)) return | ||
// no need if it's not sourcemapped | ||
const ll = !content.includes(sourceMapComment) | ||
? [] | ||
: content | ||
.replace(/[\n\u2028\u2029]$/, '') | ||
.split(/\n|\u2028|\u2029/) | ||
.map(l => l.length) | ||
cache.set(filename, ll) | ||
} | ||
|
||
export const getLineLengths = (filename: string) => { | ||
if (filename.startsWith('file://')) filename = fileURLToPath(filename) | ||
return cache.get(filename) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,27 @@ | ||
// just here to explicitly hit the bit that C8 doesn't capture otherwise | ||
import t from 'tap' | ||
import { | ||
getLineLengths, | ||
saveLineLengths, | ||
} from '../dist/cjs/line-lengths.js' | ||
|
||
const contentNoSM = `#!/usr/bin/env node | ||
console.log('hello, world') | ||
process.exit(99) | ||
` | ||
|
||
const contentWithSM = `${contentNoSM} | ||
//# sourceMappingURL=https://www.example.com/ | ||
` | ||
|
||
t.equal(getLineLengths('/content/no-sm'), undefined) | ||
t.equal(getLineLengths('/content/with-sm'), undefined) | ||
saveLineLengths('file:///content/no-sm', contentNoSM) | ||
saveLineLengths('/content/with-sm', contentWithSM) | ||
t.strictSame(getLineLengths('/content/no-sm'), []) | ||
t.strictSame( | ||
getLineLengths('file:///content/with-sm'), | ||
[19, 0, 27, 0, 16, 0, 45] | ||
) |
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