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

Make line counts reflect all line endings #691

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
7 changes: 4 additions & 3 deletions packages/metro-source-map/src/source-map.js
Expand Up @@ -277,9 +277,10 @@ function addMapping(generator, mapping, carryOver) {
}
}

function countLines(string) {
return string.split('\n').length;
}
const newline = /\r\n?|\n|\u2028|\u2029/g;

const countLines = (string: string): number =>
(string.match(newline) || []).length + 1;

module.exports = {
BundleBuilder,
Expand Down
15 changes: 15 additions & 0 deletions packages/metro-transform-worker/src/__tests__/index-test.js
Expand Up @@ -600,3 +600,18 @@ it('skips minification in Hermes canary transform profile', async () => {
});"
`);
});

it('counts all line endings correctly', async () => {
const result = await Transformer.transform(
baseConfig,
'/root',
'local/file.js',
'one\rtwo\r\nthree\nfour\u2028five\u2029six',
{
dev: false,
minify: false,
type: 'module',
},
);
expect(result.output[0].data.lineCount).toEqual(9);
danielsmc marked this conversation as resolved.
Show resolved Hide resolved
});
9 changes: 3 additions & 6 deletions packages/metro/src/lib/countLines.js
Expand Up @@ -10,12 +10,9 @@

'use strict';

const nullthrows = require('nullthrows');
const newline = /\r\n?|\n|\u2028|\u2029/g;

const reLine = /^/gm;

function countLines(s: string): number {
return nullthrows(s.match(reLine)).length;
}
const countLines = (string: string): number =>
(string.match(newline) || []).length + 1;

module.exports = countLines;
5 changes: 1 addition & 4 deletions packages/metro/src/shared/output/RamBundle/util.js
Expand Up @@ -20,10 +20,7 @@ import type {
BasicSourceMap,
} from 'metro-source-map';

const newline = /\r\n?|\n|\u2028|\u2029/g;
// fastest implementation
const countLines = (string: string): number =>
(string.match(newline) || []).length + 1;
import countLines from '../../../lib/countLines';

function lineToLineSourceMap(
source: string,
Expand Down