-
Notifications
You must be signed in to change notification settings - Fork 47
Avoid infinite loop and fatal errors for invalid table data #296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,14 +88,19 @@ const { ColSpanCell, RowSpanCell } = Cell; | |
} | ||
|
||
function addRowSpanCells(table) { | ||
table.forEach(function (row, rowIndex) { | ||
table.forEach(function (row) { | ||
row.forEach(function (cell) { | ||
for (let i = 1; i < cell.rowSpan; i++) { | ||
let rowSpanCell = new RowSpanCell(cell); | ||
const line = cell.y + i; | ||
rowSpanCell.x = cell.x; | ||
rowSpanCell.y = cell.y + i; | ||
rowSpanCell.y = line; | ||
rowSpanCell.colSpan = cell.colSpan; | ||
insertCell(rowSpanCell, table[rowIndex + i]); | ||
if (!table[line]) { | ||
const { x, y } = rowSpanCell; | ||
warn(`${x}-${y}: Expected empty array for row ${line - 1} (line ${line}).`); | ||
} | ||
insertCell(rowSpanCell, table[rowSpanCell.y] || []); | ||
} | ||
}); | ||
}); | ||
|
@@ -110,7 +115,7 @@ const { ColSpanCell, RowSpanCell } = Cell; | |
let colSpanCell = new ColSpanCell(); | ||
colSpanCell.x = cell.x + k; | ||
colSpanCell.y = cell.y; | ||
cellColumns.splice(columnIndex + 1, 0, colSpanCell); | ||
cellColumns.splice(colSpanCell.x, 0, colSpanCell); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes the (known) infinite loop that can be caused by invalid-table-data. |
||
} | ||
} | ||
} | ||
|
@@ -146,7 +151,7 @@ const { ColSpanCell, RowSpanCell } = Cell; | |
cell.x = opts.x; | ||
cell.y = opts.y; | ||
warn(`Missing cell at ${cell.y}-${cell.x}.`); | ||
insertCell(cell, table[y]); | ||
insertCell(cell, table[y] || []); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Phantom row will never be output. But a fatal error is avoided. |
||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const Table = require('../..'); | ||
|
||
/** | ||
* This test doesn't actually fail it never completes. I could not find a method | ||
* to get the infinite loop to timeout within the test. Not sure how GHA will | ||
* handle this. | ||
*/ | ||
test('it should not loop infinitely with invalid table data', async () => { | ||
const table = new Table(); | ||
table.push( | ||
[ | ||
{ content: 'A', colSpan: 2 }, | ||
{ content: 'B', rowSpan: 3 }, | ||
], | ||
[], | ||
[{ content: 'C', colSpan: 3 }] | ||
); | ||
expect(() => table.toString()).not.toThrow(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This expectation induces an infinite loop before the "fix" in this pr. |
||
}); | ||
|
||
test('it should not error on invalid table data', () => { | ||
const table = new Table({ debug: true }); | ||
table.push( | ||
[ | ||
{ content: 'A', colSpan: 2 }, | ||
{ content: 'B', rowSpan: 3 }, | ||
], | ||
[{ content: 'C', colSpan: 3 }] | ||
); | ||
expect(() => table.toString()).not.toThrow(); | ||
// This expectation can be dropped if the expectation in the code changes | ||
expect(table.messages).toContain('2-2: Expected empty array for row 1 (line 2).'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Silent solution to the missing row. Future solutions can add the missing row to correct the output. |
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Phantom row will never be output. But a fatal error is avoided.