Expectation refactoring 5/7: Refactor "buildMesage"

Mostly just extract few functions, increasing readability.
redux
David Majda 8 years ago
parent 319931876d
commit 999cc7be74

@ -1111,34 +1111,38 @@ function generateJS(ast, options) {
' return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation);', ' return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation);',
' }', ' }',
'', '',
' var expectedDescs = new Array(expected.length),', ' function describeExpected(expected) {',
' expectedDesc, foundDesc, i, j;', ' var descriptions = new Array(expected.length),',
' i, j;',
'', '',
' for (i = 0; i < expected.length; i++) {', ' for (i = 0; i < expected.length; i++) {',
' expectedDescs[i] = describeExpectation(expected[i]);', ' descriptions[i] = describeExpectation(expected[i]);',
' }', ' }',
'', '',
' expectedDescs.sort();', ' descriptions.sort();',
'', '',
' if (expectedDescs.length > 0) {', ' if (descriptions.length > 0) {',
' for (i = 1, j = 1; i < expectedDescs.length; i++) {', ' for (i = 1, j = 1; i < descriptions.length; i++) {',
' if (expectedDescs[i - 1] !== expectedDescs[i]) {', ' if (descriptions[i - 1] !== descriptions[i]) {',
' expectedDescs[j] = expectedDescs[i];', ' descriptions[j] = descriptions[i];',
' j++;', ' j++;',
' }',
' }', ' }',
' descriptions.length = j;',
' }', ' }',
' expectedDescs.length = j;',
' }',
'', '',
' expectedDesc = expectedDescs.length > 1', ' return descriptions.length > 1',
' ? expectedDescs.slice(0, -1).join(", ")', ' ? descriptions.slice(0, -1).join(", ")',
' + " or "', ' + " or "',
' + expectedDescs[expectedDescs.length - 1]', ' + descriptions[descriptions.length - 1]',
' : expectedDescs[0];', ' : descriptions[0];',
' }',
'', '',
' foundDesc = found ? "\\"" + literalEscape(found) + "\\"" : "end of input";', ' function describeFound(found) {',
' return found ? "\\"" + literalEscape(found) + "\\"" : "end of input";',
' }',
'', '',
' return "Expected " + expectedDesc + " but " + foundDesc + " found.";', ' return "Expected " + describeExpected(expected) + " but " + describeFound(found) + " found.";',
' }', ' }',
'', '',
' return new peg$SyntaxError(', ' return new peg$SyntaxError(',

Loading…
Cancel
Save