Skip to content

Commit

Permalink
fix: code clean-up, closes #388
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Feb 4, 2017
1 parent ca69163 commit f5bcd57
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/compile/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions spec/issues.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit f5bcd57

Please sign in to comment.