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

This reverts commit fb62c17681.

Reason for revert: Incorrectly added file

Change-Id: I4970d5b796a8b3ae59368445223323b529516ca0
This commit is contained in:
Kasper Nilsson
2017-05-17 20:37:56 +00:00
parent fb62c17681
commit 6a41c80d50
3 changed files with 217 additions and 216 deletions

View File

@@ -58,7 +58,6 @@
"prefer-arrow-callback": "error", "prefer-arrow-callback": "error",
"prefer-const": "error", "prefer-const": "error",
"prefer-spread": "error", "prefer-spread": "error",
"prefer-template": "error",
"quote-props": ["error", "consistent-as-needed"], "quote-props": ["error", "consistent-as-needed"],
"require-jsdoc": "off", "require-jsdoc": "off",
"semi": [2, "always"], "semi": [2, "always"],

View File

@@ -14,20 +14,20 @@
(function() { (function() {
'use strict'; 'use strict';
const WHOLE_FILE = -1; var WHOLE_FILE = -1;
const DiffSide = { var DiffSide = {
LEFT: 'left', LEFT: 'left',
RIGHT: 'right', RIGHT: 'right',
}; };
const DiffGroupType = { var DiffGroupType = {
ADDED: 'b', ADDED: 'b',
BOTH: 'ab', BOTH: 'ab',
REMOVED: 'a', REMOVED: 'a',
}; };
const DiffHighlights = { var 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.
*/ */
const MAX_GROUP_SIZE = 120; var 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() { return {left: {}, right: {}}; }, value: function() { return {left: {}, right: {}}; },
}, },
/** /**
@@ -81,18 +81,18 @@
_isScrolling: Boolean, _isScrolling: Boolean,
}, },
attached() { attached: function() {
this.listen(window, 'scroll', '_handleWindowScroll'); this.listen(window, 'scroll', '_handleWindowScroll');
}, },
detached() { detached: function() {
this.cancel(); this.cancel();
this.unlisten(window, 'scroll', '_handleWindowScroll'); this.unlisten(window, 'scroll', '_handleWindowScroll');
}, },
_handleWindowScroll() { _handleWindowScroll: function() {
this._isScrolling = true; this._isScrolling = true;
this.debounce('resetIsScrolling', () => { this.debounce('resetIsScrolling', function() {
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(content, isImageDiff) { process: function(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(resolve => { return new Promise(function(resolve) {
const state = { var state = {
lineNums: {left: 0, right: 0}, lineNums: {left: 0, right: 0},
sectionIndex: 0, sectionIndex: 0,
}; };
content = this._splitCommonGroupsWithComments(content); content = this._splitCommonGroupsWithComments(content);
let currentBatch = 0; var currentBatch = 0;
const nextStep = () => { var nextStep = function() {
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.
const result = this._processNext(state, content); var result = this._processNext(state, content);
for (const group of result.groups) { result.groups.forEach(function(group) {
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() { cancel: function() {
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(state, content) { _processNext: function(state, content) {
const section = content[state.sectionIndex]; var section = content[state.sectionIndex];
const rows = { var 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,
}; };
const highlights = { var 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.
let sectionEnd = null; var 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';
} }
const sharedGroups = this._sharedGroupsFromRows( var 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,7 +204,8 @@
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,
@@ -233,14 +234,14 @@
* 'last' and null respectively. * 'last' and null respectively.
* @return {Array<GrDiffGroup>} * @return {Array<GrDiffGroup>}
*/ */
_sharedGroupsFromRows(rows, context, startLineNumLeft, _sharedGroupsFromRows: function(rows, context, startLineNumLeft,
startLineNumRight, opt_sectionEnd) { startLineNumRight, opt_sectionEnd) {
const result = []; var result = [];
const lines = []; var lines = [];
let line; var line;
// Map each row to a GrDiffLine. // Map each row to a GrDiffLine.
for (let i = 0; i < rows.length; i++) { for (var 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;
@@ -251,7 +252,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.
const hiddenRange = [context, rows.length - context]; var 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') {
@@ -260,15 +261,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) {
const linesBeforeCtx = lines.slice(0, hiddenRange[0]); var linesBeforeCtx = lines.slice(0, hiddenRange[0]);
const hiddenLines = lines.slice(hiddenRange[0], hiddenRange[1]); var hiddenLines = lines.slice(hiddenRange[0], hiddenRange[1]);
const linesAfterCtx = lines.slice(hiddenRange[1]); var 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));
} }
const ctxLine = new GrDiffLine(GrDiffLine.Type.CONTEXT_CONTROL); var 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,
@@ -293,9 +294,9 @@
* @param {Number} startLineNumRight * @param {Number} startLineNumRight
* @return {GrDiffGroup} * @return {GrDiffGroup}
*/ */
_deltaGroupFromRows(rowsAdded, rowsRemoved, startLineNumLeft, _deltaGroupFromRows: function(rowsAdded, rowsRemoved, startLineNumLeft,
startLineNumRight, highlights) { startLineNumRight, highlights) {
let lines = []; var 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));
@@ -310,7 +311,7 @@
/** /**
* @return {Array<GrDiffLine>} * @return {Array<GrDiffLine>}
*/ */
_deltaLinesFromRows(lineType, rows, startLineNum, _deltaLinesFromRows: function(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) {
@@ -318,9 +319,9 @@
opt_highlights); opt_highlights);
} }
const lines = []; var lines = [];
let line; var line;
for (let i = 0; i < rows.length; i++) { for (var 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) {
@@ -329,15 +330,16 @@
line.beforeNumber = ++startLineNum; line.beforeNumber = ++startLineNum;
} }
if (opt_highlights) { if (opt_highlights) {
line.highlights = opt_highlights.filter(hl => hl.contentIndex === i); line.highlights = opt_highlights.filter(
function(hl) { return hl.contentIndex === i; });
} }
lines.push(line); lines.push(line);
} }
return lines; return lines;
}, },
_makeFileComments() { _makeFileComments: function() {
const line = new GrDiffLine(GrDiffLine.Type.BOTH); var 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]);
@@ -350,18 +352,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(content) { _splitCommonGroupsWithComments: function(content) {
const result = []; var result = [];
let leftLineNum = 0; var leftLineNum = 0;
let rightLineNum = 0; var 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) {
const newContent = []; var newContent = [];
for (const group of content) { content.forEach(function(group) {
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.
@@ -370,12 +372,13 @@
} 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 (let i = 0; i < content.length; i++) { for (var 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) {
@@ -385,24 +388,25 @@
rightLineNum += content[i].b.length; rightLineNum += content[i].b.length;
} }
for (const group of this._breakdownGroup(content[i])) { this._breakdownGroup(content[i]).forEach(function(group) {
result.push(group); result.push(group);
} });
continue; continue;
} }
const chunk = content[i].ab; var chunk = content[i].ab;
let currentChunk = {ab: []}; var currentChunk = {ab: []};
// For each line in the common group. // For each line in the common group.
for (const subChunk of chunk) { for (var j = 0; j < chunk.length; j++) {
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.
@@ -412,10 +416,10 @@
} }
// Add the non-collapse line as its own chunk. // Add the non-collapse line as its own chunk.
result.push({ab: [subChunk]}); result.push({ab: [chunk[j]]});
} else { } else {
// Append the current line to the current chunk. // Append the current line to the current chunk.
currentChunk.ab.push(subChunk); currentChunk.ab.push(chunk[j]);
} }
} }
@@ -445,24 +449,25 @@
* - 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(content, highlights) { _normalizeIntralineHighlights: function(content, highlights) {
let contentIndex = 0; var contentIndex = 0;
let idx = 0; var idx = 0;
const normalized = []; var normalized = [];
for (const hl of highlights) { for (var i = 0; i < highlights.length; i++) {
let line = `${content[contentIndex]}\n`; var line = content[contentIndex] + '\n';
let j = 0; var hl = highlights[i];
var j = 0;
while (j < hl[0]) { while (j < hl[0]) {
if (idx === line.length) { if (idx === line.length) {
idx = 0; idx = 0;
line = `${content[++contentIndex]}\n`; line = content[++contentIndex] + '\n';
continue; continue;
} }
idx++; idx++;
j++; j++;
} }
let lineHighlight = { var lineHighlight = {
contentIndex, contentIndex: contentIndex,
startIndex: idx, startIndex: idx,
}; };
@@ -470,10 +475,10 @@
while (line && j < hl[1]) { while (line && j < hl[1]) {
if (idx === line.length) { if (idx === line.length) {
idx = 0; idx = 0;
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;
@@ -494,8 +499,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(group) { _breakdownGroup: function(group) {
let key = null; var 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) {
@@ -507,11 +512,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(subgroupLines => { .map(function(subgroupLines) {
const subGroup = {}; var subGroup = {};
subGroup[key] = subgroupLines; subGroup[key] = subgroupLines;
return subGroup; return subGroup;
}); });
}, },
/** /**
@@ -522,12 +527,12 @@
* @return {!Array<!Array<T>>} * @return {!Array<!Array<T>>}
* @template T * @template T
*/ */
_breakdown(array, size) { _breakdown: function(array, size) {
if (!array.length) { return []; } if (!array.length) { return []; }
if (array.length < size) { return [array]; } if (array.length < size) { return [array]; }
const head = array.slice(0, array.length - size); var head = array.slice(0, array.length - size);
const tail = array.slice(array.length - size); var 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', () => { suite('gr-diff-processor tests', function() {
const WHOLE_FILE = -1; var WHOLE_FILE = -1;
const loremIpsum = var loremIpsum = 'Lorem ipsum dolor sit amet, ei nonumes vituperata ius. ' +
'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.';
let element; var element;
let sandbox; var sandbox;
setup(() => { setup(function() {
sandbox = sinon.sandbox.create(); sandbox = sinon.sandbox.create();
}); });
teardown(() => { teardown(function() {
sandbox.restore(); sandbox.restore();
}); });
suite('not logged in', () => { suite('not logged in', function() {
setup(() => {
setup(function() {
element = fixture('basic'); element = fixture('basic');
element.context = 4; element.context = 4;
}); });
test('process loaded content', done => { test('process loaded content', function(done) {
const content = [ var 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(() => { element.process(content).then(function() {
const groups = element.groups; var groups = element.groups;
assert.equal(groups.length, 4); assert.equal(groups.length, 4);
let group = groups[0]; var 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', done => { test('insert context groups', function(done) {
const content = [ var 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 (let i = 0; i < 100; i++) { for (var 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 (let i = 0; i < 5; i++) { for (var 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');
} }
const context = 10; var context = 10;
element.context = context; element.context = context;
element.process(content).then(() => { element.process(content).then(function() {
const groups = element.groups; var 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);
for (const l of groups[1].lines[0].contextGroup.lines) { groups[1].lines[0].contextGroup.lines.forEach(function(l) {
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);
for (const l of groups[2].lines) { groups[2].lines.forEach(function(l) {
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);
for (const l of groups[4].lines) { groups[4].lines.forEach(function(l) {
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);
for (const l of groups[6].lines) { groups[6].lines.forEach(function(l) {
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);
for (const l of groups[7].lines[0].contextGroup.lines) { groups[7].lines[0].contextGroup.lines.forEach(function(l) {
assert.equal(l.text, content[4].ab[0]); assert.equal(l.text, content[4].ab[0]);
} });
done(); done();
}); });
}); });
test('insert context groups', done => { test('insert context groups', function(done) {
const content = [ var 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 (let i = 0; i < 50; i++) { for (var 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');
} }
const context = 10; var context = 10;
element.context = context; element.context = context;
element.process(content).then(() => { element.process(content).then(function() {
const groups = element.groups; var 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);
for (const l of groups[2].lines) { groups[2].lines.forEach(function(l) {
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);
for (const l of groups[3].lines[0].contextGroup.lines) { groups[3].lines[0].contextGroup.lines.forEach(function(l) {
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);
for (const l of groups[4].lines) { groups[4].lines.forEach(function(l) {
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', () => { test('break up common diff chunks', function() {
element.keyLocations = { element.keyLocations = {
left: {1: true}, left: {1: true},
right: {10: true}, right: {10: true},
}; };
const lineNums = {left: 0, right: 0}; var lineNums = {left: 0, right: 0};
const content = [ var content = [
{ {
ab: [ ab: [
'Copyright (C) 2015 The Android Open Source Project', 'Copyright (C) 2015 The Android Open Source Project',
@@ -300,11 +300,10 @@ 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.',
], ]
}, }
]; ];
const result = var result = element._splitCommonGroupsWithComments(content, lineNums);
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'],
@@ -320,11 +319,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: [
@@ -332,50 +331,48 @@ 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', () => { test('breaks-down shared chunks w/ whole-file', function() {
const size = 120 * 2 + 5; var size = 120 * 2 + 5;
const lineNums = {left: 0, right: 0}; var lineNums = {left: 0, right: 0};
const content = [{ var content = [{
ab: _.times(size, () => { return `${Math.random()}`; }), ab: _.times(size, function() { return '' + Math.random(); }),
}]; }];
element.context = -1; element.context = -1;
const result = var result = element._splitCommonGroupsWithComments(content, lineNums);
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', () => { test('does not break-down shared chunks w/ context', function() {
const lineNums = {left: 0, right: 0}; var lineNums = {left: 0, right: 0};
const content = [{ var content = [{
ab: _.times(75, () => { return `${Math.random()}`; }), ab: _.times(75, function() { return '' + Math.random(); }),
}]; }];
element.context = 4; element.context = 4;
const result = var result = element._splitCommonGroupsWithComments(content, lineNums);
element._splitCommonGroupsWithComments(content, lineNums);
assert.deepEqual(result, content); assert.deepEqual(result, content);
}); });
test('intraline normalization', () => { test('intraline normalization', function() {
// 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.
let content = [ var 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>',
]; ];
let highlights = [ var highlights = [
[31, 34], [42, 26], [31, 34], [42, 26]
]; ];
let results = element._normalizeIntralineHighlights(content, var results = element._normalizeIntralineHighlights(content,
highlights); highlights);
assert.deepEqual(results, [ assert.deepEqual(results, [
{ {
@@ -395,7 +392,7 @@ limitations under the License.
contentIndex: 2, contentIndex: 2,
startIndex: 0, startIndex: 0,
endIndex: 6, endIndex: 6,
}, }
]); ]);
content = [ content = [
@@ -442,18 +439,18 @@ limitations under the License.
contentIndex: 5, contentIndex: 5,
startIndex: 12, startIndex: 12,
endIndex: 41, endIndex: 41,
}, }
]); ]);
}); });
test('scrolling pauses rendering', () => { test('scrolling pauses rendering', function() {
const contentRow = { var contentRow = {
ab: [ ab: [
'<!DOCTYPE html>', '<!DOCTYPE html>',
'<meta charset="utf-8">', '<meta charset="utf-8">',
], ]
}; };
const content = _.times(200, _.constant(contentRow)); var 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);
@@ -463,14 +460,14 @@ limitations under the License.
assert.equal(element.groups.length, 33); assert.equal(element.groups.length, 33);
}); });
test('image diffs', () => { test('image diffs', function() {
const contentRow = { var contentRow = {
ab: [ ab: [
'<!DOCTYPE html>', '<!DOCTYPE html>',
'<meta charset="utf-8">', '<meta charset="utf-8">',
], ]
}; };
const content = _.times(200, _.constant(contentRow)); var 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);
@@ -480,17 +477,17 @@ limitations under the License.
}); });
suite('gr-diff-processor helpers', () => { suite('gr-diff-processor helpers', function() {
let rows; var rows;
setup(() => { setup(function() {
rows = loremIpsum.split(' '); rows = loremIpsum.split(' ');
}); });
test('_sharedGroupsFromRows WHOLE_FILE', () => { test('_sharedGroupsFromRows WHOLE_FILE', function() {
const context = WHOLE_FILE; var context = WHOLE_FILE;
const lineNumbers = {left: 10, right: 100}; var lineNumbers = {left: 10, right: 100};
const result = element._sharedGroupsFromRows( var 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.
@@ -508,11 +505,11 @@ limitations under the License.
lineNumbers.right + rows.length); lineNumbers.right + rows.length);
}); });
test('_sharedGroupsFromRows context', () => { test('_sharedGroupsFromRows context', function() {
const context = 10; var context = 10;
const result = element._sharedGroupsFromRows( var result = element._sharedGroupsFromRows(
rows, context, 10, 100, null); rows, context, 10, 100, null);
const expectedCollapseSize = rows.length - 2 * context; var expectedCollapseSize = rows.length - 2 * context;
assert.equal(result.length, 3, 'Results in three groups'); assert.equal(result.length, 3, 'Results in three groups');
@@ -527,11 +524,11 @@ limitations under the License.
expectedCollapseSize); expectedCollapseSize);
}); });
test('_sharedGroupsFromRows first', () => { test('_sharedGroupsFromRows first', function() {
const context = 10; var context = 10;
const result = element._sharedGroupsFromRows( var result = element._sharedGroupsFromRows(
rows, context, 10, 100, 'first'); rows, context, 10, 100, 'first');
const expectedCollapseSize = rows.length - context; var expectedCollapseSize = rows.length - context;
assert.equal(result.length, 2, 'Results in two groups'); assert.equal(result.length, 2, 'Results in two groups');
@@ -544,11 +541,11 @@ limitations under the License.
expectedCollapseSize); expectedCollapseSize);
}); });
test('_sharedGroupsFromRows few-rows', () => { test('_sharedGroupsFromRows few-rows', function() {
// Only ten rows. // Only ten rows.
rows = rows.slice(0, 10); rows = rows.slice(0, 10);
const context = 10; var context = 10;
const result = element._sharedGroupsFromRows( var 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.
@@ -556,10 +553,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', () => { test('_sharedGroupsFromRows no single line collapse', function() {
rows = rows.slice(0, 7); rows = rows.slice(0, 7);
const context = 3; var context = 3;
const result = element._sharedGroupsFromRows( var 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.
@@ -567,9 +564,9 @@ limitations under the License.
assert.equal(result[0].lines.length, rows.length); assert.equal(result[0].lines.length, rows.length);
}); });
test('_deltaLinesFromRows', () => { test('_deltaLinesFromRows', function() {
const startLineNum = 10; var startLineNum = 10;
let result = element._deltaLinesFromRows(GrDiffLine.Type.ADD, rows, var result = element._deltaLinesFromRows(GrDiffLine.Type.ADD, rows,
startLineNum); startLineNum);
assert.equal(result.length, rows.length); assert.equal(result.length, rows.length);
@@ -593,54 +590,54 @@ limitations under the License.
}); });
}); });
suite('_breakdown*', () => { suite('_breakdown*', function() {
test('_breakdownGroup breaks down additions', () => { test('_breakdownGroup breaks down additions', function() {
sandbox.spy(element, '_breakdown'); sandbox.spy(element, '_breakdown');
const chunk = {b: ['blah', 'blah', 'blah']}; var chunk = {b: ['blah', 'blah', 'blah']};
const result = element._breakdownGroup(chunk); var 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', () => { test('_breakdown common case', function() {
const array = 'Lorem ipsum dolor sit amet, suspendisse inceptos' var array = 'Lorem ipsum dolor sit amet, suspendisse inceptos'
.split(' '); .split(' ');
const size = 3; var size = 3;
const result = element._breakdown(array, size); var result = element._breakdown(array, size);
for (const subResult of result) { result.forEach(function(subResult) {
assert.isAtMost(subResult.length, size); assert.isAtMost(subResult.length, size);
} });
const flattened = result var flattened = result
.reduce((a, b) => { return a.concat(b); }, []); .reduce(function(a, b) { return a.concat(b); }, []);
assert.deepEqual(flattened, array); assert.deepEqual(flattened, array);
}); });
test('_breakdown smaller than size', () => { test('_breakdown smaller than size', function() {
const array = 'Lorem ipsum dolor sit amet, suspendisse inceptos' var array = 'Lorem ipsum dolor sit amet, suspendisse inceptos'
.split(' '); .split(' ');
const size = 10; var size = 10;
const expected = [array]; var expected = [array];
const result = element._breakdown(array, size); var result = element._breakdown(array, size);
assert.deepEqual(result, expected); assert.deepEqual(result, expected);
}); });
test('_breakdown empty', () => { test('_breakdown empty', function() {
const array = []; var array = [];
const size = 10; var size = 10;
const expected = []; var expected = [];
const result = element._breakdown(array, size); var result = element._breakdown(array, size);
assert.deepEqual(result, expected); assert.deepEqual(result, expected);
}); });
}); });
}); });
test('detaching cancels', () => { test('detaching cancels', function() {
element = fixture('basic'); element = fixture('basic');
sandbox.stub(element, 'cancel'); sandbox.stub(element, 'cancel');
element.detached(); element.detached();