<commit_before>/**
 * Copyright 2018 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {createArrayTypeNode, createKeywordTypeNode, createPropertySignature, createStringLiteral, createToken, createTypeReferenceNode, createUnionTypeNode, PropertySignature, SyntaxKind} from 'typescript';

import {Log} from '../logging';
import {Format, ObjectPredicate, TObject, TSubject} from '../triples/triple';
import {GetComment, IsDomainIncludes, IsRangeIncludes, IsSupersededBy} from '../triples/wellKnown';

import {ClassMap} from './class';
import {Context} from './context';
import {withComments} from './util/comments';
import {toTypeName} from './util/names';

/**
 * A "class" of properties, not associated with any particuar object.
 */
export class PropertyType {
  private readonly types: TObject[] = [];
  private _comment?: string;
  private readonly _supersededBy: TObject[] = [];

  constructor(private readonly subject: TSubject) {}

  get comment() {
    if (!this.deprecated) return this._comment;
    const deprecated = `@deprecated Consider using ${
        this._supersededBy.map(o => o.toString()).join(' or ')} instead.`;

    return this._comment ? `${this._comment}\n${deprecated}` : deprecated;
  }

  get deprecated() {
    return this._supersededBy.length > 0;
  }

  add(value: ObjectPredicate, classes: ClassMap): boolean {
    const c = GetComment(value);
    if (c) {
      if (this._comment) {
        Log(`Duplicate comments provided on property ${
            this.subject.toString()}. It will be overwritten.`);
      }
      this._comment = c.comment;
      return true;
    }

    if (IsRangeIncludes(value.Predicate)) {
      this.types.push(value.Object);
      return true;
    }

    if (IsDomainIncludes(value.Predicate)) {
      const cls = classes.get(value.Object.toString());
      if (!cls) {
        throw new Error(
            `Could not find class for ${this.subject.name}, ${Format(value)}.`);
      }
      cls.addProp(new Property(this.subject, this));
      return true;
    }

    if (IsSupersededBy(value.Predicate)) {
      this._supersededBy.push(value.Object);
      return true;
    }

    return false;
  }

  scalarTypeNode() {
    const typeNodes =
        this.types.sort((a, b) => toTypeName(a).localeCompare(toTypeName(b)))
            .map(type => createTypeReferenceNode(toTypeName(type), []));
    switch (typeNodes.length) {
      case 0:
        return createKeywordTypeNode(SyntaxKind.NeverKeyword);
      case 1:
        return typeNodes[0];
      default:
        return createUnionTypeNode(typeNodes);
    }
  }
}

/**
 * A Property on a particular object.
 */
export class Property {
  constructor(readonly key: TSubject, private readonly type: PropertyType) {}

  get deprecated() {
    return this.type.deprecated;
  }

  private typeNode() {
    const node = this.type.scalarTypeNode();
    return createUnionTypeNode([node, createArrayTypeNode(node)]);<commit_msg>Declare Arrays in JSON-LD as readonly.<commit_after>/**
 * Copyright 2018 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {createArrayTypeNode, createKeywordTypeNode, createPropertySignature, createStringLiteral, createToken, createTypeOperatorNode, createTypeReferenceNode, createUnionTypeNode, PropertySignature, SyntaxKind} from 'typescript';

import {Log} from '../logging';
import {Format, ObjectPredicate, TObject, TSubject} from '../triples/triple';
import {GetComment, IsDomainIncludes, IsRangeIncludes, IsSupersededBy} from '../triples/wellKnown';

import {ClassMap} from './class';
import {Context} from './context';
import {withComments} from './util/comments';
import {toTypeName} from './util/names';

/**
 * A "class" of properties, not associated with any particuar object.
 */
export class PropertyType {
  private readonly types: TObject[] = [];
  private _comment?: string;
  private readonly _supersededBy: TObject[] = [];

  constructor(private readonly subject: TSubject) {}

  get comment() {
    if (!this.deprecated) return this._comment;
    const deprecated = `@deprecated Consider using ${
        this._supersededBy.map(o => o.toString()).join(' or ')} instead.`;

    return this._comment ? `${this._comment}\n${deprecated}` : deprecated;
  }

  get deprecated() {
    return this._supersededBy.length > 0;
  }

  add(value: ObjectPredicate, classes: ClassMap): boolean {
    const c = GetComment(value);
    if (c) {
      if (this._comment) {
        Log(`Duplicate comments provided on property ${
            this.subject.toString()}. It will be overwritten.`);
      }
      this._comment = c.comment;
      return true;
    }

    if (IsRangeIncludes(value.Predicate)) {
      this.types.push(value.Object);
      return true;
    }

    if (IsDomainIncludes(value.Predicate)) {
      const cls = classes.get(value.Object.toString());
      if (!cls) {
        throw new Error(
            `Could not find class for ${this.subject.name}, ${Format(value)}.`);
      }
      cls.addProp(new Property(this.subject, this));
      return true;
    }

    if (IsSupersededBy(value.Predicate)) {
      this._supersededBy.push(value.Object);
      return true;
    }

    return false;
  }

  scalarTypeNode() {
    const typeNodes =
        this.types.sort((a, b) => toTypeName(a).localeCompare(toTypeName(b)))
            .map(type => createTypeReferenceNode(toTypeName(type), []));
    switch (typeNodes.length) {
      case 0:
        return createKeywordTypeNode(SyntaxKind.NeverKeyword);
      case 1:
        return typeNodes[0];
      default:
        return createUnionTypeNode(typeNodes);
    }
  }
}

/**
 * A Property on a particular object.
 */
export class Property {
  constructor(readonly key: TSubject, private readonly type: PropertyType) {}

  get deprecated() {
    return this.type.deprecated;
  }

  private typeNode() {
    const node = this.type.scalarTypeNode();
    return createUnionTypeNode([
      node,
      createTypeOperatorNode(
          SyntaxKind.ReadonlyKeyword, createArrayTypeNode(node))
    ]);
{'fraction_non_alphanumeric': 0.11880487114131974, 'fraction_numerical': 0.0033984706881903144, 'mean_word_length': 3.3869565217391306, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '7014338', 'n_tokens_mistral': 2034, 'n_tokens_neox': 1983, 'n_words': 588}