Merge "ES6ify /gr-diff-processor/*"

This commit is contained in:
Kasper Nilsson
2017-05-17 22:50:12 +00:00
committed by Gerrit Code Review
2 changed files with 213 additions and 215 deletions

View File

@@ -14,20 +14,20 @@
(function() { (function() {
'use strict'; 'use strict';
var WHOLE_FILE = -1; const WHOLE_FILE = -1;
var DiffSide = { const DiffSide = {
LEFT: 'left', LEFT: 'left',
RIGHT: 'right', RIGHT: 'right',
}; };
var DiffGroupType = { const DiffGroupType = {
ADDED: 'b', ADDED: 'b',
BOTH: 'ab', BOTH: 'ab',
REMOVED: 'a', REMOVED: 'a',
}; };
var DiffHighlights = { const DiffHighlights = {
ADDED: 'edit_b', ADDED: 'edit_b',
REMOVED: 'edit_a', REMOVED: 'edit_a',
}; };
@@ -40,7 +40,7 @@
* _asyncThreshold of 64, but feel free to tune this constant to your * _asyncThreshold of 64, but feel free to tune this constant to your
* performance needs. * performance needs.
*/ */
var MAX_GROUP_SIZE = 120; const MAX_GROUP_SIZE = 120;
Polymer({ Polymer({
is: 'gr-diff-processor', is: 'gr-diff-processor',
@@ -66,7 +66,7 @@
*/ */
keyLocations: { keyLocations: {
type: Object, type: Object,
value: function() { return {left: {}, right: {}}; }, value() { return {left: {}, right: {}}; },
}, },
/** /**
@@ -81,18 +81,18 @@
_isScrolling: Boolean, _isScrolling: Boolean,
}, },
attached: function() { attached() {
this.listen(window, 'scroll', '_handleWindowScroll'); this.listen(window, 'scroll', '_handleWindowScroll');
}, },
detached: function() { detached() {
this.cancel(); this.cancel();
this.unlisten(window, 'scroll', '_handleWindowScroll'); this.unlisten(window, 'scroll', '_handleWindowScroll');
}, },
_handleWindowScroll: function() { _handleWindowScroll() {
this._isScrolling = true; this._isScrolling = true;
this.debounce('resetIsScrolling', function() { this.debounce('resetIsScrolling', () => {
this._isScrolling = false; this._isScrolling = false;
}, 50); }, 50);
}, },
@@ -103,23 +103,23 @@
* @return {Promise} A promise that resolves when the diff is completely * @return {Promise} A promise that resolves when the diff is completely
* processed. * processed.
*/ */
process: function(content, isImageDiff) { process(content, isImageDiff) {
this.groups = []; this.groups = [];
this.push('groups', this._makeFileComments()); this.push('groups', this._makeFileComments());
// If image diff, only render the file lines. // If image diff, only render the file lines.
if (isImageDiff) { return Promise.resolve(); } if (isImageDiff) { return Promise.resolve(); }
return new Promise(function(resolve) { return new Promise(resolve => {
var state = { const state = {
lineNums: {left: 0, right: 0}, lineNums: {left: 0, right: 0},
sectionIndex: 0, sectionIndex: 0,
}; };
content = this._splitCommonGroupsWithComments(content); content = this._splitCommonGroupsWithComments(content);
var currentBatch = 0; let currentBatch = 0;
var nextStep = function() { const nextStep = () => {
if (this._isScrolling) { if (this._isScrolling) {
this.async(nextStep, 100); this.async(nextStep, 100);
return; return;
@@ -132,11 +132,11 @@
} }
// Process the next section and incorporate the result. // Process the next section and incorporate the result.
var result = this._processNext(state, content); const result = this._processNext(state, content);
result.groups.forEach(function(group) { for (const group of result.groups) {
this.push('groups', group); this.push('groups', group);
currentBatch += group.lines.length; currentBatch += group.lines.length;
}, this); }
state.lineNums.left += result.lineDelta.left; state.lineNums.left += result.lineDelta.left;
state.lineNums.right += result.lineDelta.right; state.lineNums.right += result.lineDelta.right;
@@ -151,13 +151,13 @@
}; };
nextStep.call(this); nextStep.call(this);
}.bind(this)); });
}, },
/** /**
* Cancel any jobs that are running. * Cancel any jobs that are running.
*/ */
cancel: function() { cancel() {
if (this._nextStepHandle !== undefined) { if (this._nextStepHandle !== undefined) {
this.cancelAsync(this._nextStepHandle); this.cancelAsync(this._nextStepHandle);
this._nextStepHandle = undefined; this._nextStepHandle = undefined;
@@ -167,29 +167,29 @@
/** /**
* Process the next section of the diff. * Process the next section of the diff.
*/ */
_processNext: function(state, content) { _processNext(state, content) {
var section = content[state.sectionIndex]; const section = content[state.sectionIndex];
var rows = { const rows = {
both: section[DiffGroupType.BOTH] || null, both: section[DiffGroupType.BOTH] || null,
added: section[DiffGroupType.ADDED] || null, added: section[DiffGroupType.ADDED] || null,
removed: section[DiffGroupType.REMOVED] || null, removed: section[DiffGroupType.REMOVED] || null,
}; };
var highlights = { const highlights = {
added: section[DiffHighlights.ADDED] || null, added: section[DiffHighlights.ADDED] || null,
removed: section[DiffHighlights.REMOVED] || null, removed: section[DiffHighlights.REMOVED] || null,
}; };
if (rows.both) { // If it's a shared section. if (rows.both) { // If it's a shared section.
var sectionEnd = null; let sectionEnd = null;
if (state.sectionIndex === 0) { if (state.sectionIndex === 0) {
sectionEnd = 'first'; sectionEnd = 'first';
} else if (state.sectionIndex === content.length - 1) { } else if (state.sectionIndex === content.length - 1) {
sectionEnd = 'last'; sectionEnd = 'last';
} }
var sharedGroups = this._sharedGroupsFromRows( const sharedGroups = this._sharedGroupsFromRows(
rows.both, rows.both,
content.length > 1 ? this.context : WHOLE_FILE, content.length > 1 ? this.context : WHOLE_FILE,
state.lineNums.left, state.lineNums.left,
@@ -204,8 +204,7 @@
groups: sharedGroups, groups: sharedGroups,
}; };
} else { // Otherwise it's a delta section. } else { // Otherwise it's a delta section.
const deltaGroup = this._deltaGroupFromRows(
var deltaGroup = this._deltaGroupFromRows(
rows.added, rows.added,
rows.removed, rows.removed,
state.lineNums.left, state.lineNums.left,
@@ -234,14 +233,14 @@
* 'last' and null respectively. * 'last' and null respectively.
* @return {Array<GrDiffGroup>} * @return {Array<GrDiffGroup>}
*/ */
_sharedGroupsFromRows: function(rows, context, startLineNumLeft, _sharedGroupsFromRows(rows, context, startLineNumLeft,
startLineNumRight, opt_sectionEnd) { startLineNumRight, opt_sectionEnd) {
var result = []; const result = [];
var lines = []; const lines = [];
var line; let line;
// Map each row to a GrDiffLine. // Map each row to a GrDiffLine.
for (var i = 0; i < rows.length; i++) { for (let i = 0; i < rows.length; i++) {
line = new GrDiffLine(GrDiffLine.Type.BOTH); line = new GrDiffLine(GrDiffLine.Type.BOTH);
line.text = rows[i]; line.text = rows[i];
line.beforeNumber = ++startLineNumLeft; line.beforeNumber = ++startLineNumLeft;
@@ -252,7 +251,7 @@
// Find the hidden range based on the user's context preference. If this // Find the hidden range based on the user's context preference. If this
// is the first or the last section of the diff, make sure the collapsed // is the first or the last section of the diff, make sure the collapsed
// part of the section extends to the edge of the file. // part of the section extends to the edge of the file.
var hiddenRange = [context, rows.length - context]; const hiddenRange = [context, rows.length - context];
if (opt_sectionEnd === 'first') { if (opt_sectionEnd === 'first') {
hiddenRange[0] = 0; hiddenRange[0] = 0;
} else if (opt_sectionEnd === 'last') { } else if (opt_sectionEnd === 'last') {
@@ -261,15 +260,15 @@
// If there is a range to hide. // If there is a range to hide.
if (context !== WHOLE_FILE && hiddenRange[1] - hiddenRange[0] > 1) { if (context !== WHOLE_FILE && hiddenRange[1] - hiddenRange[0] > 1) {
var linesBeforeCtx = lines.slice(0, hiddenRange[0]); const linesBeforeCtx = lines.slice(0, hiddenRange[0]);
var hiddenLines = lines.slice(hiddenRange[0], hiddenRange[1]); const hiddenLines = lines.slice(hiddenRange[0], hiddenRange[1]);
var linesAfterCtx = lines.slice(hiddenRange[1]); const linesAfterCtx = lines.slice(hiddenRange[1]);
if (linesBeforeCtx.length > 0) { if (linesBeforeCtx.length > 0) {
result.push(new GrDiffGroup(GrDiffGroup.Type.BOTH, linesBeforeCtx)); result.push(new GrDiffGroup(GrDiffGroup.Type.BOTH, linesBeforeCtx));
} }
var ctxLine = new GrDiffLine(GrDiffLine.Type.CONTEXT_CONTROL); const ctxLine = new GrDiffLine(GrDiffLine.Type.CONTEXT_CONTROL);
ctxLine.contextGroup = ctxLine.contextGroup =
new GrDiffGroup(GrDiffGroup.Type.BOTH, hiddenLines); new GrDiffGroup(GrDiffGroup.Type.BOTH, hiddenLines);
result.push(new GrDiffGroup(GrDiffGroup.Type.CONTEXT_CONTROL, result.push(new GrDiffGroup(GrDiffGroup.Type.CONTEXT_CONTROL,
@@ -294,9 +293,9 @@
* @param {Number} startLineNumRight * @param {Number} startLineNumRight
* @return {GrDiffGroup} * @return {GrDiffGroup}
*/ */
_deltaGroupFromRows: function(rowsAdded, rowsRemoved, startLineNumLeft, _deltaGroupFromRows(rowsAdded, rowsRemoved, startLineNumLeft,
startLineNumRight, highlights) { startLineNumRight, highlights) {
var lines = []; let lines = [];
if (rowsRemoved) { if (rowsRemoved) {
lines = lines.concat(this._deltaLinesFromRows(GrDiffLine.Type.REMOVE, lines = lines.concat(this._deltaLinesFromRows(GrDiffLine.Type.REMOVE,
rowsRemoved, startLineNumLeft, highlights.removed)); rowsRemoved, startLineNumLeft, highlights.removed));
@@ -311,7 +310,7 @@
/** /**
* @return {Array<GrDiffLine>} * @return {Array<GrDiffLine>}
*/ */
_deltaLinesFromRows: function(lineType, rows, startLineNum, _deltaLinesFromRows(lineType, rows, startLineNum,
opt_highlights) { opt_highlights) {
// Normalize highlights if they have been passed. // Normalize highlights if they have been passed.
if (opt_highlights) { if (opt_highlights) {
@@ -319,9 +318,9 @@
opt_highlights); opt_highlights);
} }
var lines = []; const lines = [];
var line; let line;
for (var i = 0; i < rows.length; i++) { for (let i = 0; i < rows.length; i++) {
line = new GrDiffLine(lineType); line = new GrDiffLine(lineType);
line.text = rows[i]; line.text = rows[i];
if (lineType === GrDiffLine.Type.ADD) { if (lineType === GrDiffLine.Type.ADD) {
@@ -330,16 +329,15 @@
line.beforeNumber = ++startLineNum; line.beforeNumber = ++startLineNum;
} }
if (opt_highlights) { if (opt_highlights) {
line.highlights = opt_highlights.filter( line.highlights = opt_highlights.filter(hl => hl.contentIndex === i);
function(hl) { return hl.contentIndex === i; });
} }
lines.push(line); lines.push(line);
} }
return lines; return lines;
}, },
_makeFileComments: function() { _makeFileComments() {
var line = new GrDiffLine(GrDiffLine.Type.BOTH); const line = new GrDiffLine(GrDiffLine.Type.BOTH);
line.beforeNumber = GrDiffLine.FILE; line.beforeNumber = GrDiffLine.FILE;
line.afterNumber = GrDiffLine.FILE; line.afterNumber = GrDiffLine.FILE;
return new GrDiffGroup(GrDiffGroup.Type.BOTH, [line]); return new GrDiffGroup(GrDiffGroup.Type.BOTH, [line]);
@@ -352,18 +350,18 @@
* @param {Object} content The diff content object. * @param {Object} content The diff content object.
* @return {Object} A new diff content object with regions split up. * @return {Object} A new diff content object with regions split up.
*/ */
_splitCommonGroupsWithComments: function(content) { _splitCommonGroupsWithComments(content) {
var result = []; const result = [];
var leftLineNum = 0; let leftLineNum = 0;
var rightLineNum = 0; let rightLineNum = 0;
// If the context is set to "whole file", then break down the shared // If the context is set to "whole file", then break down the shared
// chunks so they can be rendered incrementally. Note: this is not enabled // chunks so they can be rendered incrementally. Note: this is not enabled
// for any other context preference because manipulating the chunks in // for any other context preference because manipulating the chunks in
// this way violates assumptions by the context grouper logic. // this way violates assumptions by the context grouper logic.
if (this.context === -1) { if (this.context === -1) {
var newContent = []; const newContent = [];
content.forEach(function(group) { for (const group of content) {
if (group.ab && group.ab.length > MAX_GROUP_SIZE * 2) { if (group.ab && group.ab.length > MAX_GROUP_SIZE * 2) {
// Split large shared groups in two, where the first is the maximum // Split large shared groups in two, where the first is the maximum
// group size. // group size.
@@ -372,13 +370,12 @@
} else { } else {
newContent.push(group); newContent.push(group);
} }
}.bind(this)); }
content = newContent; content = newContent;
} }
// For each section in the diff. // For each section in the diff.
for (var i = 0; i < content.length; i++) { for (let i = 0; i < content.length; i++) {
// If it isn't a common group, append it as-is and update line numbers. // If it isn't a common group, append it as-is and update line numbers.
if (!content[i].ab) { if (!content[i].ab) {
if (content[i].a) { if (content[i].a) {
@@ -388,25 +385,24 @@
rightLineNum += content[i].b.length; rightLineNum += content[i].b.length;
} }
this._breakdownGroup(content[i]).forEach(function(group) { for (const group of this._breakdownGroup(content[i])) {
result.push(group); result.push(group);
}); }
continue; continue;
} }
var chunk = content[i].ab; const chunk = content[i].ab;
var currentChunk = {ab: []}; let currentChunk = {ab: []};
// For each line in the common group. // For each line in the common group.
for (var j = 0; j < chunk.length; j++) { for (const subChunk of chunk) {
leftLineNum++; leftLineNum++;
rightLineNum++; rightLineNum++;
// If this line should not be collapsed. // If this line should not be collapsed.
if (this.keyLocations[DiffSide.LEFT][leftLineNum] || if (this.keyLocations[DiffSide.LEFT][leftLineNum] ||
this.keyLocations[DiffSide.RIGHT][rightLineNum]) { this.keyLocations[DiffSide.RIGHT][rightLineNum]) {
// If any lines have been accumulated into the chunk leading up to // If any lines have been accumulated into the chunk leading up to
// this non-collapse line, then add them as a chunk and start a new // this non-collapse line, then add them as a chunk and start a new
// one. // one.
@@ -416,10 +412,10 @@
} }
// Add the non-collapse line as its own chunk. // Add the non-collapse line as its own chunk.
result.push({ab: [chunk[j]]}); result.push({ab: [subChunk]});
} else { } else {
// Append the current line to the current chunk. // Append the current line to the current chunk.
currentChunk.ab.push(chunk[j]); currentChunk.ab.push(subChunk);
} }
} }
@@ -449,14 +445,13 @@
* - endIndex: (optional) Where the highlight should end. If omitted, the * - endIndex: (optional) Where the highlight should end. If omitted, the
* highlight is meant to be a continuation onto the next line. * highlight is meant to be a continuation onto the next line.
*/ */
_normalizeIntralineHighlights: function(content, highlights) { _normalizeIntralineHighlights(content, highlights) {
var contentIndex = 0; let contentIndex = 0;
var idx = 0; let idx = 0;
var normalized = []; const normalized = [];
for (var i = 0; i < highlights.length; i++) { for (const hl of highlights) {
var line = content[contentIndex] + '\n'; let line = content[contentIndex] + '\n';
var hl = highlights[i]; let j = 0;
var j = 0;
while (j < hl[0]) { while (j < hl[0]) {
if (idx === line.length) { if (idx === line.length) {
idx = 0; idx = 0;
@@ -466,8 +461,8 @@
idx++; idx++;
j++; j++;
} }
var lineHighlight = { let lineHighlight = {
contentIndex: contentIndex, contentIndex,
startIndex: idx, startIndex: idx,
}; };
@@ -478,7 +473,7 @@
line = content[++contentIndex] + '\n'; line = content[++contentIndex] + '\n';
normalized.push(lineHighlight); normalized.push(lineHighlight);
lineHighlight = { lineHighlight = {
contentIndex: contentIndex, contentIndex,
startIndex: idx, startIndex: idx,
}; };
continue; continue;
@@ -499,8 +494,8 @@
* @param {!Object} A raw chunk from a diff response. * @param {!Object} A raw chunk from a diff response.
* @return {!Array<!Array<!Object>>} * @return {!Array<!Array<!Object>>}
*/ */
_breakdownGroup: function(group) { _breakdownGroup(group) {
var key = null; let key = null;
if (group.a && !group.b) { if (group.a && !group.b) {
key = 'a'; key = 'a';
} else if (group.b && !group.a) { } else if (group.b && !group.a) {
@@ -512,11 +507,11 @@
if (!key) { return [group]; } if (!key) { return [group]; }
return this._breakdown(group[key], MAX_GROUP_SIZE) return this._breakdown(group[key], MAX_GROUP_SIZE)
.map(function(subgroupLines) { .map(subgroupLines => {
var subGroup = {}; const subGroup = {};
subGroup[key] = subgroupLines; subGroup[key] = subgroupLines;
return subGroup; return subGroup;
}); });
}, },
/** /**
@@ -527,12 +522,12 @@
* @return {!Array<!Array<T>>} * @return {!Array<!Array<T>>}
* @template T * @template T
*/ */
_breakdown: function(array, size) { _breakdown(array, size) {
if (!array.length) { return []; } if (!array.length) { return []; }
if (array.length < size) { return [array]; } if (array.length < size) { return [array]; }
var head = array.slice(0, array.length - size); const head = array.slice(0, array.length - size);
var tail = array.slice(array.length - size); const tail = array.slice(array.length - size);
return this._breakdown(head, size).concat([tail]); return this._breakdown(head, size).concat([tail]);
}, },

View File

@@ -33,40 +33,40 @@ limitations under the License.
</test-fixture> </test-fixture>
<script> <script>
suite('gr-diff-processor tests', function() { suite('gr-diff-processor tests', () => {
var WHOLE_FILE = -1; const WHOLE_FILE = -1;
var loremIpsum = 'Lorem ipsum dolor sit amet, ei nonumes vituperata ius. ' + const loremIpsum =
'Lorem ipsum dolor sit amet, ei nonumes vituperata ius. ' +
'Duo animal omnesque fabellas et. Id has phaedrum dignissim ' + 'Duo animal omnesque fabellas et. Id has phaedrum dignissim ' +
'deterruisset, pro ei petentium comprehensam, ut vis solum dicta. ' + 'deterruisset, pro ei petentium comprehensam, ut vis solum dicta. ' +
'Eos cu aliquam labores qualisque, usu postea inermis te, et solum ' + 'Eos cu aliquam labores qualisque, usu postea inermis te, et solum ' +
'fugit assum per.'; 'fugit assum per.';
var element; let element;
var sandbox; let sandbox;
setup(function() { setup(() => {
sandbox = sinon.sandbox.create(); sandbox = sinon.sandbox.create();
}); });
teardown(function() { teardown(() => {
sandbox.restore(); sandbox.restore();
}); });
suite('not logged in', function() { suite('not logged in', () => {
setup(() => {
setup(function() {
element = fixture('basic'); element = fixture('basic');
element.context = 4; element.context = 4;
}); });
test('process loaded content', function(done) { test('process loaded content', done => {
var content = [ const content = [
{ {
ab: [ ab: [
'<!DOCTYPE html>', '<!DOCTYPE html>',
'<meta charset="utf-8">', '<meta charset="utf-8">',
] ],
}, },
{ {
a: [ a: [
@@ -82,16 +82,16 @@ limitations under the License.
'Leela: This is the only place the ship cant hear us, so ', 'Leela: This is the only place the ship cant hear us, so ',
'everyone pretend to shower.', 'everyone pretend to shower.',
'Fry: Same as every day. Got it.', 'Fry: Same as every day. Got it.',
] ],
}, },
]; ];
element.process(content).then(function() { element.process(content).then(() => {
var groups = element.groups; const groups = element.groups;
assert.equal(groups.length, 4); assert.equal(groups.length, 4);
var group = groups[0]; let group = groups[0];
assert.equal(group.type, GrDiffGroup.Type.BOTH); assert.equal(group.type, GrDiffGroup.Type.BOTH);
assert.equal(group.lines.length, 1); assert.equal(group.lines.length, 1);
assert.equal(group.lines[0].text, ''); assert.equal(group.lines[0].text, '');
@@ -144,27 +144,27 @@ limitations under the License.
}); });
}); });
test('insert context groups', function(done) { test('insert context groups', done => {
var content = [ const content = [
{ab: []}, {ab: []},
{a: ['all work and no play make andybons a dull boy']}, {a: ['all work and no play make andybons a dull boy']},
{ab: []}, {ab: []},
{b: ['elgoog elgoog elgoog']}, {b: ['elgoog elgoog elgoog']},
{ab: []}, {ab: []},
]; ];
for (var i = 0; i < 100; i++) { for (let i = 0; i < 100; i++) {
content[0].ab.push('all work and no play make jack a dull boy'); content[0].ab.push('all work and no play make jack a dull boy');
content[4].ab.push('all work and no play make jill a dull girl'); content[4].ab.push('all work and no play make jill a dull girl');
} }
for (var i = 0; i < 5; i++) { for (let i = 0; i < 5; i++) {
content[2].ab.push('no tv and no beer make homer go crazy'); content[2].ab.push('no tv and no beer make homer go crazy');
} }
var context = 10; const context = 10;
element.context = context; element.context = context;
element.process(content).then(function() { element.process(content).then(() => {
var groups = element.groups; const groups = element.groups;
assert.equal(groups[0].type, GrDiffGroup.Type.BOTH); assert.equal(groups[0].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[0].lines.length, 1); assert.equal(groups[0].lines.length, 1);
@@ -175,15 +175,15 @@ limitations under the License.
assert.equal(groups[1].type, GrDiffGroup.Type.CONTEXT_CONTROL); assert.equal(groups[1].type, GrDiffGroup.Type.CONTEXT_CONTROL);
assert.instanceOf(groups[1].lines[0].contextGroup, GrDiffGroup); assert.instanceOf(groups[1].lines[0].contextGroup, GrDiffGroup);
assert.equal(groups[1].lines[0].contextGroup.lines.length, 90); assert.equal(groups[1].lines[0].contextGroup.lines.length, 90);
groups[1].lines[0].contextGroup.lines.forEach(function(l) { for (const l of groups[1].lines[0].contextGroup.lines) {
assert.equal(l.text, content[0].ab[0]); assert.equal(l.text, content[0].ab[0]);
}); }
assert.equal(groups[2].type, GrDiffGroup.Type.BOTH); assert.equal(groups[2].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[2].lines.length, context); assert.equal(groups[2].lines.length, context);
groups[2].lines.forEach(function(l) { for (const l of groups[2].lines) {
assert.equal(l.text, content[0].ab[0]); assert.equal(l.text, content[0].ab[0]);
}); }
assert.equal(groups[3].type, GrDiffGroup.Type.DELTA); assert.equal(groups[3].type, GrDiffGroup.Type.DELTA);
assert.equal(groups[3].lines.length, 1); assert.equal(groups[3].lines.length, 1);
@@ -193,9 +193,9 @@ limitations under the License.
assert.equal(groups[4].type, GrDiffGroup.Type.BOTH); assert.equal(groups[4].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[4].lines.length, 5); assert.equal(groups[4].lines.length, 5);
groups[4].lines.forEach(function(l) { for (const l of groups[4].lines) {
assert.equal(l.text, content[2].ab[0]); assert.equal(l.text, content[2].ab[0]);
}); }
assert.equal(groups[5].type, GrDiffGroup.Type.DELTA); assert.equal(groups[5].type, GrDiffGroup.Type.DELTA);
assert.equal(groups[5].lines.length, 1); assert.equal(groups[5].lines.length, 1);
@@ -204,36 +204,36 @@ limitations under the License.
assert.equal(groups[6].type, GrDiffGroup.Type.BOTH); assert.equal(groups[6].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[6].lines.length, context); assert.equal(groups[6].lines.length, context);
groups[6].lines.forEach(function(l) { for (const l of groups[6].lines) {
assert.equal(l.text, content[4].ab[0]); assert.equal(l.text, content[4].ab[0]);
}); }
assert.equal(groups[7].type, GrDiffGroup.Type.CONTEXT_CONTROL); assert.equal(groups[7].type, GrDiffGroup.Type.CONTEXT_CONTROL);
assert.instanceOf(groups[7].lines[0].contextGroup, GrDiffGroup); assert.instanceOf(groups[7].lines[0].contextGroup, GrDiffGroup);
assert.equal(groups[7].lines[0].contextGroup.lines.length, 90); assert.equal(groups[7].lines[0].contextGroup.lines.length, 90);
groups[7].lines[0].contextGroup.lines.forEach(function(l) { for (const l of groups[7].lines[0].contextGroup.lines) {
assert.equal(l.text, content[4].ab[0]); assert.equal(l.text, content[4].ab[0]);
}); }
done(); done();
}); });
}); });
test('insert context groups', function(done) { test('insert context groups', done => {
var content = [ const content = [
{a: ['all work and no play make andybons a dull boy']}, {a: ['all work and no play make andybons a dull boy']},
{ab: []}, {ab: []},
{b: ['elgoog elgoog elgoog']}, {b: ['elgoog elgoog elgoog']},
]; ];
for (var i = 0; i < 50; i++) { for (let i = 0; i < 50; i++) {
content[1].ab.push('no tv and no beer make homer go crazy'); content[1].ab.push('no tv and no beer make homer go crazy');
} }
var context = 10; const context = 10;
element.context = context; element.context = context;
element.process(content).then(function() { element.process(content).then(() => {
var groups = element.groups; const groups = element.groups;
assert.equal(groups[0].type, GrDiffGroup.Type.BOTH); assert.equal(groups[0].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[0].lines.length, 1); assert.equal(groups[0].lines.length, 1);
@@ -249,22 +249,22 @@ limitations under the License.
assert.equal(groups[2].type, GrDiffGroup.Type.BOTH); assert.equal(groups[2].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[2].lines.length, context); assert.equal(groups[2].lines.length, context);
groups[2].lines.forEach(function(l) { for (const l of groups[2].lines) {
assert.equal(l.text, content[1].ab[0]); assert.equal(l.text, content[1].ab[0]);
}); }
assert.equal(groups[3].type, GrDiffGroup.Type.CONTEXT_CONTROL); assert.equal(groups[3].type, GrDiffGroup.Type.CONTEXT_CONTROL);
assert.instanceOf(groups[3].lines[0].contextGroup, GrDiffGroup); assert.instanceOf(groups[3].lines[0].contextGroup, GrDiffGroup);
assert.equal(groups[3].lines[0].contextGroup.lines.length, 30); assert.equal(groups[3].lines[0].contextGroup.lines.length, 30);
groups[3].lines[0].contextGroup.lines.forEach(function(l) { for (const l of groups[3].lines[0].contextGroup.lines) {
assert.equal(l.text, content[1].ab[0]); assert.equal(l.text, content[1].ab[0]);
}); }
assert.equal(groups[4].type, GrDiffGroup.Type.BOTH); assert.equal(groups[4].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[4].lines.length, context); assert.equal(groups[4].lines.length, context);
groups[4].lines.forEach(function(l) { for (const l of groups[4].lines) {
assert.equal(l.text, content[1].ab[0]); assert.equal(l.text, content[1].ab[0]);
}); }
assert.equal(groups[5].type, GrDiffGroup.Type.DELTA); assert.equal(groups[5].type, GrDiffGroup.Type.DELTA);
assert.equal(groups[5].lines.length, 1); assert.equal(groups[5].lines.length, 1);
@@ -275,14 +275,14 @@ limitations under the License.
}); });
}); });
test('break up common diff chunks', function() { test('break up common diff chunks', () => {
element.keyLocations = { element.keyLocations = {
left: {1: true}, left: {1: true},
right: {10: true}, right: {10: true},
}; };
var lineNums = {left: 0, right: 0}; const lineNums = {left: 0, right: 0};
var content = [ const content = [
{ {
ab: [ ab: [
'Copyright (C) 2015 The Android Open Source Project', 'Copyright (C) 2015 The Android Open Source Project',
@@ -300,10 +300,11 @@ limitations under the License.
'either express or implied. See the License for the specific ', 'either express or implied. See the License for the specific ',
'language governing permissions and limitations under the ' + 'language governing permissions and limitations under the ' +
'License.', 'License.',
] ],
} },
]; ];
var result = element._splitCommonGroupsWithComments(content, lineNums); const result =
element._splitCommonGroupsWithComments(content, lineNums);
assert.deepEqual(result, [ assert.deepEqual(result, [
{ {
ab: ['Copyright (C) 2015 The Android Open Source Project'], ab: ['Copyright (C) 2015 The Android Open Source Project'],
@@ -319,11 +320,11 @@ limitations under the License.
'http://www.apache.org/licenses/LICENSE-2.0', 'http://www.apache.org/licenses/LICENSE-2.0',
'', '',
'Unless required by applicable law or agreed to in writing, ', 'Unless required by applicable law or agreed to in writing, ',
] ],
}, },
{ {
ab: [ ab: [
'software distributed under the License is distributed on an '], 'software distributed under the License is distributed on an '],
}, },
{ {
ab: [ ab: [
@@ -331,48 +332,50 @@ limitations under the License.
'either express or implied. See the License for the specific ', 'either express or implied. See the License for the specific ',
'language governing permissions and limitations under the ' + 'language governing permissions and limitations under the ' +
'License.', 'License.',
] ],
} },
]); ]);
}); });
test('breaks-down shared chunks w/ whole-file', function() { test('breaks-down shared chunks w/ whole-file', () => {
var size = 120 * 2 + 5; const size = 120 * 2 + 5;
var lineNums = {left: 0, right: 0}; const lineNums = {left: 0, right: 0};
var content = [{ const content = [{
ab: _.times(size, function() { return '' + Math.random(); }), ab: _.times(size, () => { return `${Math.random()}`; }),
}]; }];
element.context = -1; element.context = -1;
var result = element._splitCommonGroupsWithComments(content, lineNums); const result =
element._splitCommonGroupsWithComments(content, lineNums);
assert.equal(result.length, 2); assert.equal(result.length, 2);
assert.deepEqual(result[0].ab, content[0].ab.slice(0, 120)); assert.deepEqual(result[0].ab, content[0].ab.slice(0, 120));
assert.deepEqual(result[1].ab, content[0].ab.slice(120)); assert.deepEqual(result[1].ab, content[0].ab.slice(120));
}); });
test('does not break-down shared chunks w/ context', function() { test('does not break-down shared chunks w/ context', () => {
var lineNums = {left: 0, right: 0}; const lineNums = {left: 0, right: 0};
var content = [{ const content = [{
ab: _.times(75, function() { return '' + Math.random(); }), ab: _.times(75, () => { return `${Math.random()}`; }),
}]; }];
element.context = 4; element.context = 4;
var result = element._splitCommonGroupsWithComments(content, lineNums); const result =
element._splitCommonGroupsWithComments(content, lineNums);
assert.deepEqual(result, content); assert.deepEqual(result, content);
}); });
test('intraline normalization', function() { test('intraline normalization', () => {
// The content and highlights are in the format returned by the Gerrit // The content and highlights are in the format returned by the Gerrit
// REST API. // REST API.
var content = [ let content = [
' <section class="summary">', ' <section class="summary">',
' <gr-linked-text content="' + ' <gr-linked-text content="' +
'[[_computeCurrentRevisionMessage(change)]]"></gr-linked-text>', '[[_computeCurrentRevisionMessage(change)]]"></gr-linked-text>',
' </section>', ' </section>',
]; ];
var highlights = [ let highlights = [
[31, 34], [42, 26] [31, 34], [42, 26],
]; ];
var results = element._normalizeIntralineHighlights(content, let results = element._normalizeIntralineHighlights(content,
highlights); highlights);
assert.deepEqual(results, [ assert.deepEqual(results, [
{ {
@@ -392,7 +395,7 @@ limitations under the License.
contentIndex: 2, contentIndex: 2,
startIndex: 0, startIndex: 0,
endIndex: 6, endIndex: 6,
} },
]); ]);
content = [ content = [
@@ -439,18 +442,18 @@ limitations under the License.
contentIndex: 5, contentIndex: 5,
startIndex: 12, startIndex: 12,
endIndex: 41, endIndex: 41,
} },
]); ]);
}); });
test('scrolling pauses rendering', function() { test('scrolling pauses rendering', () => {
var contentRow = { const contentRow = {
ab: [ ab: [
'<!DOCTYPE html>', '<!DOCTYPE html>',
'<meta charset="utf-8">', '<meta charset="utf-8">',
] ],
}; };
var content = _.times(200, _.constant(contentRow)); const content = _.times(200, _.constant(contentRow));
sandbox.stub(element, 'async'); sandbox.stub(element, 'async');
element._isScrolling = true; element._isScrolling = true;
element.process(content); element.process(content);
@@ -460,14 +463,14 @@ limitations under the License.
assert.equal(element.groups.length, 33); assert.equal(element.groups.length, 33);
}); });
test('image diffs', function() { test('image diffs', () => {
var contentRow = { const contentRow = {
ab: [ ab: [
'<!DOCTYPE html>', '<!DOCTYPE html>',
'<meta charset="utf-8">', '<meta charset="utf-8">',
] ],
}; };
var content = _.times(200, _.constant(contentRow)); const content = _.times(200, _.constant(contentRow));
sandbox.stub(element, 'async'); sandbox.stub(element, 'async');
element.process(content, true); element.process(content, true);
assert.equal(element.groups.length, 1); assert.equal(element.groups.length, 1);
@@ -477,17 +480,17 @@ limitations under the License.
}); });
suite('gr-diff-processor helpers', function() { suite('gr-diff-processor helpers', () => {
var rows; let rows;
setup(function() { setup(() => {
rows = loremIpsum.split(' '); rows = loremIpsum.split(' ');
}); });
test('_sharedGroupsFromRows WHOLE_FILE', function() { test('_sharedGroupsFromRows WHOLE_FILE', () => {
var context = WHOLE_FILE; const context = WHOLE_FILE;
var lineNumbers = {left: 10, right: 100}; const lineNumbers = {left: 10, right: 100};
var result = element._sharedGroupsFromRows( const result = element._sharedGroupsFromRows(
rows, context, lineNumbers.left, lineNumbers.right, null); rows, context, lineNumbers.left, lineNumbers.right, null);
// Results in one, uncollapsed group with all rows. // Results in one, uncollapsed group with all rows.
@@ -505,11 +508,11 @@ limitations under the License.
lineNumbers.right + rows.length); lineNumbers.right + rows.length);
}); });
test('_sharedGroupsFromRows context', function() { test('_sharedGroupsFromRows context', () => {
var context = 10; const context = 10;
var result = element._sharedGroupsFromRows( const result = element._sharedGroupsFromRows(
rows, context, 10, 100, null); rows, context, 10, 100, null);
var expectedCollapseSize = rows.length - 2 * context; const expectedCollapseSize = rows.length - 2 * context;
assert.equal(result.length, 3, 'Results in three groups'); assert.equal(result.length, 3, 'Results in three groups');
@@ -524,11 +527,11 @@ limitations under the License.
expectedCollapseSize); expectedCollapseSize);
}); });
test('_sharedGroupsFromRows first', function() { test('_sharedGroupsFromRows first', () => {
var context = 10; const context = 10;
var result = element._sharedGroupsFromRows( const result = element._sharedGroupsFromRows(
rows, context, 10, 100, 'first'); rows, context, 10, 100, 'first');
var expectedCollapseSize = rows.length - context; const expectedCollapseSize = rows.length - context;
assert.equal(result.length, 2, 'Results in two groups'); assert.equal(result.length, 2, 'Results in two groups');
@@ -541,11 +544,11 @@ limitations under the License.
expectedCollapseSize); expectedCollapseSize);
}); });
test('_sharedGroupsFromRows few-rows', function() { test('_sharedGroupsFromRows few-rows', () => {
// Only ten rows. // Only ten rows.
rows = rows.slice(0, 10); rows = rows.slice(0, 10);
var context = 10; const context = 10;
var result = element._sharedGroupsFromRows( const result = element._sharedGroupsFromRows(
rows, context, 10, 100, 'first'); rows, context, 10, 100, 'first');
// Results in one uncollapsed group with all rows. // Results in one uncollapsed group with all rows.
@@ -553,10 +556,10 @@ limitations under the License.
assert.equal(result[0].lines.length, rows.length); assert.equal(result[0].lines.length, rows.length);
}); });
test('_sharedGroupsFromRows no single line collapse', function() { test('_sharedGroupsFromRows no single line collapse', () => {
rows = rows.slice(0, 7); rows = rows.slice(0, 7);
var context = 3; const context = 3;
var result = element._sharedGroupsFromRows( const result = element._sharedGroupsFromRows(
rows, context, 10, 100); rows, context, 10, 100);
// Results in one uncollapsed group with all rows. // Results in one uncollapsed group with all rows.
@@ -564,9 +567,9 @@ limitations under the License.
assert.equal(result[0].lines.length, rows.length); assert.equal(result[0].lines.length, rows.length);
}); });
test('_deltaLinesFromRows', function() { test('_deltaLinesFromRows', () => {
var startLineNum = 10; const startLineNum = 10;
var result = element._deltaLinesFromRows(GrDiffLine.Type.ADD, rows, let result = element._deltaLinesFromRows(GrDiffLine.Type.ADD, rows,
startLineNum); startLineNum);
assert.equal(result.length, rows.length); assert.equal(result.length, rows.length);
@@ -590,54 +593,54 @@ limitations under the License.
}); });
}); });
suite('_breakdown*', function() { suite('_breakdown*', () => {
test('_breakdownGroup breaks down additions', function() { test('_breakdownGroup breaks down additions', () => {
sandbox.spy(element, '_breakdown'); sandbox.spy(element, '_breakdown');
var chunk = {b: ['blah', 'blah', 'blah']}; const chunk = {b: ['blah', 'blah', 'blah']};
var result = element._breakdownGroup(chunk); const result = element._breakdownGroup(chunk);
assert.deepEqual(result, [chunk]); assert.deepEqual(result, [chunk]);
assert.isTrue(element._breakdown.called); assert.isTrue(element._breakdown.called);
}); });
test('_breakdown common case', function() { test('_breakdown common case', () => {
var array = 'Lorem ipsum dolor sit amet, suspendisse inceptos' const array = 'Lorem ipsum dolor sit amet, suspendisse inceptos'
.split(' '); .split(' ');
var size = 3; const size = 3;
var result = element._breakdown(array, size); const result = element._breakdown(array, size);
result.forEach(function(subResult) { for (const subResult of result) {
assert.isAtMost(subResult.length, size); assert.isAtMost(subResult.length, size);
}); }
var flattened = result const flattened = result
.reduce(function(a, b) { return a.concat(b); }, []); .reduce((a, b) => { return a.concat(b); }, []);
assert.deepEqual(flattened, array); assert.deepEqual(flattened, array);
}); });
test('_breakdown smaller than size', function() { test('_breakdown smaller than size', () => {
var array = 'Lorem ipsum dolor sit amet, suspendisse inceptos' const array = 'Lorem ipsum dolor sit amet, suspendisse inceptos'
.split(' '); .split(' ');
var size = 10; const size = 10;
var expected = [array]; const expected = [array];
var result = element._breakdown(array, size); const result = element._breakdown(array, size);
assert.deepEqual(result, expected); assert.deepEqual(result, expected);
}); });
test('_breakdown empty', function() { test('_breakdown empty', () => {
var array = []; const array = [];
var size = 10; const size = 10;
var expected = []; const expected = [];
var result = element._breakdown(array, size); const result = element._breakdown(array, size);
assert.deepEqual(result, expected); assert.deepEqual(result, expected);
}); });
}); });
}); });
test('detaching cancels', function() { test('detaching cancels', () => {
element = fixture('basic'); element = fixture('basic');
sandbox.stub(element, 'cancel'); sandbox.stub(element, 'cancel');
element.detached(); element.detached();