<commit_before>'use strict';

var fs = require('fs');
var argv = require('optimist').argv;
var utils = require('./utils');
var crypto = require('crypto');

var schema = {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "",
  "properties": {}
};

var Compiler = function() {
  this.schema = this.getBaseSchema();
};

Compiler.prototype.disableAdditionalProperties = function() {
  this.disableAdditionalProperties = true;
};

/**
Sets the base properties on a new Draft-v4 
schema object
*/
Compiler.prototype.getBaseSchema = function() {
  return {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "",
  };
};

/**
Generates a JSON schema based on the provided AST tree.

@param {Object} tree AST
@param {Object} schema The schema object
@param {Object} parent Schema node parent object
@return void
*/
Compiler.prototype.generate = function(tree, schema, parent) {
	for (var i in tree.children) {
		var child = tree.children[i];
		if (child.type === 'object') {
			parent.required.push(i);
			schema[i] = {
				type: 'object'
				,properties: {}
				,required: []
			};
			this.generate(child, schema[i].properties, schema[i]);
		} else if (child.type === 'array') {
			parent.required.push(i);
			schema[i] = {
				type: 'array'
				,uniqueItems: child.uniqueItems
				,minItems: child.minItems
				,items: {
					required:[]
					,properties: {}
				}
			}
			this.generate(child, schema[i].items.properties, schema[i]);
		} else {
			schema[i] = {};
			if (child.type) {
				schema[i].type = child.type;
			}

			if (child.minLength) { 
				schema[i].minLength = child.minLength;
			}

			if (child.required) {
				if (parent.items && parent.items.required) {
					parent.items.required.push(i);
				} else {
					parent.required.push(i);
				}
			}
		}
	}
};

/**
Initates compiling the given AST into a
JSON schema.

@param {Object} tree AST object
@return void
*/
Compiler.prototype.compile = function(tree) {
	if (tree.type === 'object') {
	 this.schema = {
	 	'$schema': 'http://json-schema.org/draft-04/schema#'
	 	,description: ''
	 	,type: 'object'
	 	,properties: {}
	 	,required: []
	 };
	 this.generate(tree, this.schema.properties, this.schema);
	} else {
		this.schema = {
			type: 'array'
			,'$schema': 'http://json-schema.org/draft-04/schema#'
			,'description': ''
			,minItems: 1
			,uniqueItems: true
			,items: {
				type: 'object'
				,required: []
				,properties: {}
			}
		};

		this.generate(tree, this.schema.items.properties, this.schema.items);
	}
};

module.exports = Compiler;<commit_msg>Added source comments<commit_after>'use strict';

var fs = require('fs');
var argv = require('optimist').argv;
var utils = require('./utils');
var crypto = require('crypto');

var schema = {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "",
  "properties": {}
};

var Compiler = function() {
  this.schema = this.getBaseSchema();
};

Compiler.prototype.disableAdditionalProperties = function() {
  this.disableAdditionalProperties = true;
};

/**
Sets the base properties on a new Draft-v4 
schema object
*/
Compiler.prototype.getBaseSchema = function() {
  return {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "",
  };
};

/**
Generates a JSON schema based on the provided AST tree.

@param {Object} tree AST
@param {Object} schema The schema object
@param {Object} parent Schema node parent object
@return void
*/
Compiler.prototype.generate = function(tree, schema, parent) {
  for (var i in tree.children) {
    var child = tree.children[i];
    if (child.type === 'object') {
      parent.required.push(i);
      schema[i] = {
        type: 'object'
        ,properties: {}
        ,required: []
      };
      this.generate(child, schema[i].properties, schema[i]);
    } else if (child.type === 'array') {
      parent.required.push(i);
      schema[i] = {
        type: 'array'
        ,uniqueItems: child.uniqueItems
        ,minItems: child.minItems
        ,items: {
          required:[]
          ,properties: {}
        }
      }
      this.generate(child, schema[i].items.properties, schema[i]);
    } else {
      schema[i] = {};
      if (child.type) {
        schema[i].type = child.type;
      }

      if (child.minLength) { 
        schema[i].minLength = child.minLength;
      }

      if (child.required) {
        if (parent.items && parent.items.required) {
          parent.items.required.push(i);
        } else {
          parent.required.push(i);
        }
      }
    }
  }
};

/**
Initates compiling the given AST into a
JSON schema.

@param {Object} tree AST object
@return void
*/
Compiler.prototype.compile = function(tree) {
  if (tree.type === 'object') {
   this.schema = {
    '$schema': 'http://json-schema.org/draft-04/schema#'
    ,description: ''
    ,type: 'object'
    ,properties: {}
    ,required: []
   };
   this.generate(tree, this.schema.properties, this.schema);
  } else {
    this.schema = {
      type: 'array'
      ,'$schema': 'http://json-schema.org/draft-04/schema#'
      ,'description': ''
      ,minItems: 1
      ,uniqueItems: true
      ,items: {
        type: 'object'
        ,required: []
        ,properties: {}
      }
    };

    this.generate(tree, this.schema.items.properties, this.schema.items);
  }
};

module.exports = Compiler;
{'fraction_non_alphanumeric': 0.17014869188782233, 'fraction_numerical': 0.003764351590438547, 'mean_word_length': 2.834054834054834, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '58036', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1713, 'n_words': 375}