Skip to content

Commit

Permalink
Slightly improve line-break detection
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Apr 18, 2018
1 parent 876408d commit 6a12566
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 24 deletions.
21 changes: 11 additions & 10 deletions src/utils/renderHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,19 @@ export function findFirstOccurrenceOutsideComment(
}

function findFirstLineBreakOutsideComment(code: string, start: number = 0) {
let commentStart, lineBreakPos, nextChar;
let lineBreakPos, charCodeAfterSlash;
while (true) {
lineBreakPos = code.indexOf('\n', start);
do {
commentStart = code.indexOf('/', start);
if (commentStart === -1 || commentStart > lineBreakPos) return lineBreakPos;
nextChar = code[commentStart + 1];
if (nextChar === '/') return lineBreakPos;
if (nextChar === '*') break;
start = commentStart + 2;
} while (true);
start = code.indexOf('*/', commentStart) + 2;
while (true) {
// check if linebreak is inside a comment
start = code.indexOf('/', start);
if (start === -1 || start > lineBreakPos) return lineBreakPos;
charCodeAfterSlash = code.charCodeAt(++start);
++start;
if (charCodeAfterSlash === 47 /*"/"*/) return lineBreakPos;
if (charCodeAfterSlash === 42 /*"*"*/) break;
}
start = code.indexOf('*/', start) + 2;
if (start === -1) return -1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ define(function () { 'use strict';
var foo = () => 'foo';

// /*

console.log(foo(), bar());
console.log(foo());

});
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
var foo = () => 'foo';

// /*

console.log(foo(), bar());
console.log(foo());
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var foo = () => 'foo';

// /*

console.log(foo(), bar());
console.log(foo());
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
var foo = () => 'foo';

// /*

console.log(foo(), bar());
console.log(foo());

}());
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ System.register([], function (exports, module) {
var foo = () => 'foo';

// /*

console.log(foo(), bar());
console.log(foo());

}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
var foo = () => 'foo';

// /*

console.log(foo(), bar());
console.log(foo());

})));
6 changes: 4 additions & 2 deletions test/form/samples/comment-start-inside-comment/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// /*
import foo from './foo.js'; //
import foo from './foo.js';

console.log(foo(), bar());
const bar = 'unused'; /*/ this comment is not closed yet
*/
console.log(foo());

0 comments on commit 6a12566

Please sign in to comment.