Skip to content

Commit

Permalink
fixed fixSourceRange
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Aug 3, 2024
1 parent 5469636 commit c68a3db
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 82 deletions.
5 changes: 5 additions & 0 deletions lib/converter/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,11 @@ const handleGeneratedBytesCoverage = (state) => {
if (js) {
coverageList.forEach((block) => {
block.ranges.forEach((range) => {

const { fixedStart, fixedEnd } = Util.fixSourceRange(state.locator, range.startOffset, range.endOffset);
range.startOffset = fixedStart;
range.endOffset = fixedEnd;

addJsBytesCoverage(state, range);
});
});
Expand Down
82 changes: 5 additions & 77 deletions lib/converter/find-original-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -880,82 +880,7 @@ const getMappingInfo = (start, end, state) => {
};
};

const fixOriginalTextRange = (res) => {

const locator = res.originalState.locator;
const rangeText = locator.getSlice(res.start, res.end);

let text = rangeText;

// =======================================
// handle end first
const oldLen = text.length;
text = text.trimEnd();
const newLen = text.length;
if (newLen < oldLen) {
res.end -= oldLen - newLen;
}

// =======================================
// handle start

const comments = locator.lineParser.commentParser.comments;

let startOffset = 0;

// never start in a comment, skip to comment end
const inComment = comments.find((it) => res.start > it.start && res.start < it.end);
if (inComment) {
startOffset = inComment.end - res.start;
// next text
text = text.slice(startOffset);
}

while (startOffset < newLen) {

const beforeLen = text.length;
text = text.trimStart();
const afterLen = text.length;
if (afterLen === beforeLen) {
// no indent
break;
}

const indentLen = beforeLen - afterLen;

startOffset += indentLen;

// no comments
if (!comments.length) {
break;
}

const nextPos = res.start + startOffset;
const comment = comments.find((it) => it.start === nextPos);
if (!comment) {
break;
}

const commentLen = comment.end - comment.start;

startOffset += commentLen;

// next text
text = text.slice(commentLen);

}

// It should never be possible to start with }
if (rangeText[startOffset] === '}') {
startOffset += 1;
}

res.start += startOffset;

};


// skip \s and comments
const findOriginalRange = (start, end, state, originalMap, options = {}) => {

const { sourcePath, rangeCache } = state;
Expand Down Expand Up @@ -1002,8 +927,9 @@ const findOriginalRange = (start, end, state, originalMap, options = {}) => {
}

const { originalState } = cache;
const locator = originalState.locator;

const inComment = originalState.locator.lineParser.commentParser.isComment(originalStart, originalEnd);
const inComment = locator.lineParser.commentParser.isComment(originalStart, originalEnd);
if (inComment) {
return createMappingError(['the range in a original comment']);
}
Expand All @@ -1016,7 +942,9 @@ const findOriginalRange = (start, end, state, originalMap, options = {}) => {
};

if (options.fixOriginalRange) {
fixOriginalTextRange(res);
const { fixedStart, fixedEnd } = Util.fixSourceRange(locator, originalStart, originalEnd);
res.start = fixedStart;
res.end = fixedEnd;
}

// cache response
Expand Down
81 changes: 81 additions & 0 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,87 @@ const Util = {
return lines.join(',');
},

// skip \s and comments
fixSourceRange: (locator, start, end) => {

let fixedStart = start;
let fixedEnd = end;

const rangeText = locator.getSlice(start, end);

let text = rangeText;

// =======================================
// handle end first
const oldLen = text.length;
text = text.trimEnd();
const newLen = text.length;
if (newLen < oldLen) {
fixedEnd -= oldLen - newLen;
}

// =======================================
// handle start

const comments = locator.lineParser.commentParser.comments;

let startOffset = 0;

// never start in a comment, skip to comment end
const inComment = comments.find((it) => start > it.start && start < it.end);
if (inComment) {
startOffset = inComment.end - start;
// next text
text = text.slice(startOffset);
}

while (startOffset < newLen) {

const beforeLen = text.length;
text = text.trimStart();
const afterLen = text.length;
if (afterLen === beforeLen) {
// no indent
break;
}

const indentLen = beforeLen - afterLen;

startOffset += indentLen;

// no comments
if (!comments.length) {
break;
}

const nextPos = start + startOffset;
const comment = comments.find((it) => it.start === nextPos);
if (!comment) {
break;
}

const commentLen = comment.end - comment.start;

startOffset += commentLen;

// next text
text = text.slice(commentLen);

}

// It should never be possible to start with }
if (rangeText[startOffset] === '}') {
startOffset += 1;
}

fixedStart = start + startOffset;

return {
fixedStart,
fixedEnd
};
},

cmpVersion: (v1, v2) => {
const [strMajor1, strMinor1, strPatch1] = `${v1}`.split('.');
const [strMajor2, strMinor2, strPatch2] = `${v2}`.split('.');
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
"test-merge": "node ./test/test-merge.js",
"test-merge-istanbul": "node ./test/test-merge-istanbul.js",
"test-client": "node ./test/test-client.js",
"test-all": "node ./test/test.js && node ./scripts/docs.js",
"test-all": "node ./test/test.js",
"test-pr": "node --inspect=9230 ./test/test-pr.js",
"test": "npm run test-unit && npx mcr npm run test-all -c test/mcr.config.mcr.js",
"test": "npm run test-unit && npx mcr npm run test-all -c test/mcr.config.mcr.js && node ./scripts/docs.js",
"test:snap": "cross-env TEST_SNAPSHOT=true npm run test",
"dev": "npx sf d app",
"open": "node ./scripts/open.js",
Expand Down Expand Up @@ -113,4 +113,4 @@
"vine-ui": "^3.1.16",
"ws": "^8.18.0"
}
}
}
4 changes: 2 additions & 2 deletions test/snapshot/anonymous.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "v8",
"summary": {
"bytes": "28.41 %",
"bytes": "28.57 %",
"statements": "25.10 %",
"branches": "12.43 %",
"functions": "40.63 %",
Expand All @@ -13,7 +13,7 @@
"branches": "12.43 %",
"statements": "23.18 %",
"lines": "21.50 %",
"bytes": "26.51 %",
"bytes": "26.67 %",
"uncoveredLines": "5-19,21,34-62,65,68-83,85,87,89,91-98,106-107,111-205,218,229-235,240,246-248,250-255,258-299",
"extras": "1b,4b,23b,24b,25c,31b,32c,211b,212c,301b"
},
Expand Down

0 comments on commit c68a3db

Please sign in to comment.