Skip to content

Commit

Permalink
Merge a390845 into 8a1d7f0
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolay-borzov committed Feb 18, 2020
2 parents 8a1d7f0 + a390845 commit bc4d0dc
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 67 deletions.
4 changes: 3 additions & 1 deletion src/app-error.ts
Expand Up @@ -105,7 +105,9 @@ Check that you are using the correct source map.`;
case 'InvalidMappingColumn': {
const { generatedLine, generatedColumn, maxColumn } = context;

return `Your source map refers to generated column ${generatedColumn} on line ${generatedLine}, but the source only contains ${maxColumn} column(s) on that line.
// Add 1 since generatedColumn os 0-based
return `Your source map refers to generated column ${generatedColumn +
1} on line ${generatedLine}, but the source only contains ${maxColumn} column(s) on that line.
Check that you are using the correct source map.`;
}

Expand Down
24 changes: 5 additions & 19 deletions src/explore.ts
Expand Up @@ -176,24 +176,12 @@ function computeFileSizes(
});
}

// Ignore mapping referencing EOL character (e.g. https://github.com/microsoft/TypeScript/issues/34695)
if (`${line}${eol}`.lastIndexOf(eol) === generatedColumn) {
return;
}

checkInvalidMappingColumn({
// TODO: uncomment when https://github.com/danvk/source-map-explorer/issues/136 is fixed
/* checkInvalidMappingColumn({
generatedLine,
generatedColumn,
generatedColumn: lastGeneratedColumn || generatedColumn,
line,
});

if (lastGeneratedColumn !== null) {
checkInvalidMappingColumn({
generatedLine,
generatedColumn: lastGeneratedColumn,
line,
});
}
}); */

const start = generatedColumn;
const end = lastGeneratedColumn === null ? line.length - 1 : lastGeneratedColumn;
Expand All @@ -212,9 +200,7 @@ function computeFileSizes(
let files: FileDataMap = {};
let mappedBytes = 0;

const getSize = options.gzip
? (string: string) => gzipSize.sync(string)
: (string: string) => Buffer.byteLength(string);
const getSize = options.gzip ? gzipSize.sync : Buffer.byteLength;

mappingRanges.forEach((lineRanges, lineIndex) => {
const line = lines[lineIndex];
Expand Down
19 changes: 13 additions & 6 deletions tests/api.test.ts
Expand Up @@ -200,11 +200,12 @@ describe('api', () => {
bundlesAndFileTokens: { code: 'data/no-map.js' },
expectedErrorCode: 'NoSourceMap',
},
{
// TODO: Uncomment when #136 is fixed
/* {
name: 'should throw if source map reference column beyond generated last column in line',
bundlesAndFileTokens: 'data/invalid-map-column.js',
expectedErrorCode: 'InvalidMappingColumn',
},
}, */
{
name: 'should throw if source map reference more lines than available in source',
bundlesAndFileTokens: 'data/invalid-map-line.js',
Expand All @@ -231,7 +232,7 @@ describe('api', () => {
{
name: 'should throw when cannot save html to file',
bundlesAndFileTokens: 'data/inline-map.js',
// `/` supposed to be invalid filename on both Linux and Windows
// `/` is supposed to be invalid filename on both Linux and Windows
options: { output: { format: 'html', filename: '/' } },
expectedErrorCode: 'CannotSaveFile',
},
Expand All @@ -246,10 +247,16 @@ describe('api', () => {
});

it('should not throw if at least one result is successful', async () => {
const result = await explore(['data/foo.min.js', 'data/no-map.js']);
await expect(explore(['data/foo.min.js', 'data/no-map.js'])).to.not.be.rejected.then(
result => {
expect(result.bundles.length).to.eq(1);
expect(result.errors.length).to.eq(2);
}
);
});

expect(result.bundles.length).to.eq(1);
expect(result.errors.length).to.eq(2);
it('should not throw when analyzing source map referencing eol', async () => {
await expect(explore('data/map-reference-eol.js')).to.not.be.rejected;
});

it('should add "one source" source map warning when exploring single bundle', async () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/app-error.test.ts
Expand Up @@ -13,7 +13,7 @@ describe('app-error', () => {
{
code: 'InvalidMappingColumn',
generatedLine: 60,
generatedColumn: 81,
generatedColumn: 80,
maxColumn: 80,
},
{ code: 'CannotSaveFile' },
Expand Down
4 changes: 2 additions & 2 deletions tests/cli.test.ts
Expand Up @@ -68,14 +68,14 @@ describe('CLI', () => {
it('should output multiple results as tsv', async () => {
const result = await execute(SCRIPT_PATH, [
'data/inline-map.js',
'data/map-reference-eol.js',
'data/one-source.js',
'--tsv',
]);

expect(result)
.to.have.string('Source\tSize')
.and.have.string('src/bar.js\t104')
.and.have.string('../generate-data/src/typescript.ts\t52');
.and.have.string('src/bar.js\t55');
});

it('should output result as html', async () => {
Expand Down
5 changes: 2 additions & 3 deletions tests/data/invalid-map-column.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/data/invalid-map-line.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 0 additions & 34 deletions tests/generate-data/README.md

This file was deleted.

47 changes: 47 additions & 0 deletions tests/generate-data/build.js
@@ -0,0 +1,47 @@
const generate = require('generate-source-map');
const convert = require('convert-source-map');
const fs = require('fs');
const path = require('path');

const EOL = `\n`;

const destinationDirectory = path.resolve(__dirname, '../data');

function getSourceMapComment(source, sourceFile) {
const map = generate({ source, sourceFile });

return convert.fromJSON(map.toString()).toComment();
}

function getFileContent(file) {
return fs.readFileSync(file).toString();
}

function generateInvalidSourceMapLine() {
const filename = 'invalid-map-line.js';

const source = getFileContent('./src/invalid-map-line.1.js');
const invalidSource = getFileContent('./src/invalid-map-line.2.js');

const sourceMapComment = getSourceMapComment(source, filename);

const content = `${invalidSource}${EOL}${sourceMapComment}`;

fs.writeFileSync(path.join(destinationDirectory, filename), content);
}

function generateInvalidMapColumn() {
const filename = 'invalid-map-column.js';

const source = getFileContent('./src/invalid-map-column.1.js');
const invalidSource = getFileContent('./src/invalid-map-column.2.js');

const sourceMapComment = getSourceMapComment(source, filename);

const content = `${invalidSource}${EOL}${sourceMapComment}`;

fs.writeFileSync(path.join(destinationDirectory, filename), content);
}

generateInvalidSourceMapLine();
generateInvalidMapColumn();
33 changes: 33 additions & 0 deletions tests/generate-data/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/generate-data/package.json
Expand Up @@ -5,6 +5,8 @@
"@babel/preset-env": "7.7.1",
"babelify": "10.0.0",
"browserify": "16.5.0",
"convert-source-map": "^1.7.0",
"generate-source-map": "0.0.5",
"npm-run-all": "^4.1.5",
"terser": "4.4.0",
"typescript": "3.7.2"
Expand All @@ -18,6 +20,7 @@
"minify:no-comment": "terser dist/foo.js -c --source-map \"content=inline\" -o ../data/no-map.js",
"minify:one-source": "terser src/bar.js -b \"indent_level=2\" --source-map \"url=inline\" -o ../data/one-source.js",
"minify:typescript": "tsc --inlineSourceMap --removeComments src/typescript.ts --outFile ../data/map-reference-eol.js",
"minify:other": "node build.js",
"build-all": "npm run build && npm-run-all minify:*"
}
}
1 change: 1 addition & 0 deletions tests/generate-data/src/invalid-map-column.1.js
@@ -0,0 +1 @@
console.log('goodbye cruel world');
1 change: 1 addition & 0 deletions tests/generate-data/src/invalid-map-column.2.js
@@ -0,0 +1 @@
console.log('hello joyful world');
2 changes: 2 additions & 0 deletions tests/generate-data/src/invalid-map-line.1.js
@@ -0,0 +1,2 @@
console.log('hello happy world');
console.log('hello man');
1 change: 1 addition & 0 deletions tests/generate-data/src/invalid-map-line.2.js
@@ -0,0 +1 @@
console.log('hello happy world');

0 comments on commit bc4d0dc

Please sign in to comment.