import assert from 'assert'; import { findCommentOnlyConditionals, isCommentOnlyConditional } from './commentOnlyConditional'; // Expected values match formiojs 4.19.4. const V4_CASES: Array<[string, boolean, string]> = [ ["// show = (data.mode === 'a') ? true : false;", true, 'a commented-out condition'], ['// a\n// b', true, 'several comment lines'], ['/* x */ // y', true, 'block comment then a line comment'], [' // indented', true, 'leading whitespace'], ['/* c */', false, 'block comment only (v4: shown)'], [';', false, 'semicolon only (v4: shown)'], ['// c\n', false, 'ends with a newline (v4: shown)'], ['', false, 'empty (no condition)'], [' ', false, 'whitespace only (v4: shown)'], ['show = true // note', false, 'code with a trailing comment is evaluated by Form.io'], ['show = "http://x" === data.a', false, 'a URL in a string is code'], ['// /* old\nshow = true;\n// */', false, 'code between comment lines'], ['/* line one\n// not code\n*/', false, 'unterminated-looking block that ends with */'], ['/* a */\n// b', true, 'block comment line then a line comment'], ['// a\n/* b */', false, 'ends in a block comment (v4: shown)'], ['/*\n*/ // z', true, 'multi-line block then a line comment'], [';; // x', true, 'semicolons then a line comment'], ['// a\r\n// b', true, 'Windows line endings'], ['// a\n ', false, 'trailing whitespace line (v4: shown)'], ['/* unclosed // x', false, 'unclosed block (v4: syntax error, not hidden by this rule)'], ]; describe('isCommentOnlyConditional (v4 parity)', () => { for (const [input, expected, why] of V4_CASES) { it(`${expected ? 'hides' : 'leaves to Form.io'}: ${why}`, () => { assert.strictEqual(isCommentOnlyConditional(input), expected, JSON.stringify(input)); }); } it('ignores non-string conditions', () => { assert.strictEqual(isCommentOnlyConditional(undefined), false); assert.strictEqual(isCommentOnlyConditional(null), false); assert.strictEqual(isCommentOnlyConditional(42), false); }); }); describe('findCommentOnlyConditionals', () => { it('lists commented-out conditions anywhere in a form, with their path', () => { const schema = { display: 'wizard', components: [ { key: 'page1', type: 'panel', components: [ { key: 'commentedOut', type: 'radio', customConditional: "// show = data.mode === 'a';" }, { key: 'ok', type: 'textfield', customConditional: 'show = true;' }, { key: 'cols', type: 'columns', columns: [{ components: [{ key: 'inCol', customConditional: '// old' }] }] }, { key: 'tbl', type: 'table', rows: [[{ components: [{ key: 'inTable', customConditional: '/* x */ // y' }] }]] }, ], }, { key: 'grid', type: 'datagrid', components: [{ key: 'group', type: 'container', customConditional: '// x', components: [] }] }, ], }; assert.deepStrictEqual( findCommentOnlyConditionals(schema).map((finding) => finding.path), ['page1.commentedOut', 'page1.cols.inCol', 'page1.tbl.inTable', 'grid.group'], ); }); it('returns nothing for a schema with no such conditions, or no schema', () => { assert.deepStrictEqual(findCommentOnlyConditionals({ components: [{ key: 'a', customConditional: '' }] }), []); assert.deepStrictEqual(findCommentOnlyConditionals(undefined), []); }); });