diff --git a/lib/compile/util.js b/lib/compile/util.js index 713532b61..ec56b42fc 100644 --- a/lib/compile/util.js +++ b/lib/compile/util.js @@ -155,12 +155,13 @@ var ERRORS_REGEXP = /[^v\.]errors/g function finalCleanUpCode(out, async) { var matches = out.match(ERRORS_REGEXP); - if (!matches || matches.length !== 2) return out; - out = async - ? out.replace(REMOVE_ERRORS_ASYNC, '') - .replace(RETURN_ASYNC, RETURN_DATA_ASYNC) - : out.replace(REMOVE_ERRORS, '') - .replace(RETURN_VALID, RETURN_TRUE); + if (matches && matches.length == 2) { + out = async + ? out.replace(REMOVE_ERRORS_ASYNC, '') + .replace(RETURN_ASYNC, RETURN_DATA_ASYNC) + : out.replace(REMOVE_ERRORS, '') + .replace(RETURN_VALID, RETURN_TRUE); + } matches = out.match(ROOTDATA_REGEXP); if (!matches || matches.length !== 3) return out; diff --git a/spec/issues.spec.js b/spec/issues.spec.js index d520c8512..d4b39ed15 100644 --- a/spec/issues.spec.js +++ b/spec/issues.spec.js @@ -524,3 +524,27 @@ describe('issue #342, support uniqueItems with some non-JSON objects', function( validate([{foo: undefined}, {foo: undefined}]) .should.equal(false); }); }); + + +describe('issue #388, code clean-up not working', function() { + it('should remove assignement to rootData if it is not used', function() { + var ajv = new Ajv; + var validate = ajv.compile({ + type: 'object', + properties: { + foo: { type: 'string' } + } + }); + var code = validate.toString(); + code.match(/rootData/g).length .should.equal(1); + }); + + it('should remove assignement to errors if they are not used', function() { + var ajv = new Ajv; + var validate = ajv.compile({ + type: 'object' + }); + var code = validate.toString(); + should.equal(code.match(/[^\.]errors|vErrors/g), null); + }); +});