Skip to content

Commit

Permalink
Merge pull request #52 from Eyas/readonly-array
Browse files Browse the repository at this point in the history
Declare Arrays in JSON-LD as readonly.
  • Loading branch information
Eyas authored Jan 8, 2020
2 parents a3bc417 + 6df3e53 commit 2e5b233
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 27 deletions.
8 changes: 6 additions & 2 deletions src/ts/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {createArrayTypeNode, createKeywordTypeNode, createPropertySignature, createStringLiteral, createToken, createTypeReferenceNode, createUnionTypeNode, PropertySignature, SyntaxKind} from 'typescript';
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';
Expand Down Expand Up @@ -108,7 +108,11 @@ export class Property {

private typeNode() {
const node = this.type.scalarTypeNode();
return createUnionTypeNode([node, createArrayTypeNode(node)]);
return createUnionTypeNode([
node,
createTypeOperatorNode(
SyntaxKind.ReadonlyKeyword, createArrayTypeNode(node))
]);
}

toNode(context: Context): PropertySignature {
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/comments.ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ type ThingBase = {
/** IRI identifying the canonical address of this object. */
"@id"?: string;
/** Names are great! {@link X Y} */
"name"?: Text | Text[];
"name"?: Text | readonly Text[];
};
/**
* Things are amazing!
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/inheritance_multiple.ts.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type PersonLikeBase = ThingBase & {
"height"?: Number | Number[];
"height"?: Number | readonly Number[];
};
export type PersonLike = {
"@type": "PersonLike";
Expand All @@ -8,14 +8,14 @@ export type PersonLike = {
type ThingBase = {
/** IRI identifying the canonical address of this object. */
"@id"?: string;
"name"?: Text | Text[];
"name"?: Text | readonly Text[];
};
export type Thing = ({
"@type": "Thing";
} & ThingBase) | (PersonLike | Vehicle);

type VehicleBase = ThingBase & {
"doors"?: Number | Number[];
"doors"?: Number | readonly Number[];
};
export type Vehicle = {
"@type": "Vehicle";
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/inheritance_one.ts.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type PersonLikeBase = ThingBase & {
"height"?: Number | Number[];
"height"?: Number | readonly Number[];
};
export type PersonLike = {
"@type": "PersonLike";
Expand All @@ -8,7 +8,7 @@ export type PersonLike = {
type ThingBase = {
/** IRI identifying the canonical address of this object. */
"@id"?: string;
"name"?: Text | Text[];
"name"?: Text | readonly Text[];
};
export type Thing = ({
"@type": "Thing";
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/quantities.ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type Quantity = ({
type ThingBase = {
/** IRI identifying the canonical address of this object. */
"@id"?: string;
"name"?: Text | Text[];
"name"?: Text | readonly Text[];
};
export type Thing = ({
"@type": "Thing";
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/simple.ts.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type ThingBase = {
/** IRI identifying the canonical address of this object. */
"@id"?: string;
"name"?: Text | Text[];
"name"?: Text | readonly Text[];
};
export type Thing = {
"@type": "Thing";
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/sorted_props.ts.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
type ThingBase = {
/** IRI identifying the canonical address of this object. */
"@id"?: string;
"a"?: Text | Text[];
"b"?: Text | Text[];
"c"?: Text | Text[];
"d"?: Text | Text[];
"a"?: Text | readonly Text[];
"b"?: Text | readonly Text[];
"c"?: Text | readonly Text[];
"d"?: Text | readonly Text[];
};
export type Thing = {
"@type": "Thing";
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/sorted_proptypes.ts.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type ThingBase = {
/** IRI identifying the canonical address of this object. */
"@id"?: string;
"a"?: (Boolean | Date | DateTime | Number | Text | Time) | (Boolean | Date | DateTime | Number | Text | Time)[];
"a"?: (Boolean | Date | DateTime | Number | Text | Time) | readonly (Boolean | Date | DateTime | Number | Text | Time)[];
};
export type Thing = {
"@type": "Thing";
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/stringlike_http.ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ export type EntryPoint = ({
} & EntryPointBase) | string;

type OrganizationBase = ThingBase & {
"locatedIn"?: Place | Place[];
"owner"?: Person | Person[];
"urlTemplate"?: URL | URL[];
"locatedIn"?: Place | readonly Place[];
"owner"?: Person | readonly Person[];
"urlTemplate"?: URL | readonly URL[];
};
export type Organization = ({
"@type": "Organization";
} & OrganizationBase) | string;

type PersonBase = ThingBase & {
"height"?: Quantity | Quantity[];
"locatedIn"?: Place | Place[];
"height"?: Quantity | readonly Quantity[];
"locatedIn"?: Place | readonly Place[];
};
export type Person = ({
"@type": "Person";
Expand All @@ -33,7 +33,7 @@ export type Quantity = ({
type ThingBase = {
/** IRI identifying the canonical address of this object. */
"@id"?: string;
"name"?: Text | Text[];
"name"?: Text | readonly Text[];
};
export type Thing = ({
"@type": "Thing";
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/stringlike_https.ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ export type EntryPoint = ({
} & EntryPointBase) | string;

type OrganizationBase = ThingBase & {
"locatedIn"?: Place | Place[];
"owner"?: Person | Person[];
"urlTemplate"?: URL | URL[];
"locatedIn"?: Place | readonly Place[];
"owner"?: Person | readonly Person[];
"urlTemplate"?: URL | readonly URL[];
};
export type Organization = ({
"@type": "Organization";
} & OrganizationBase) | string;

type PersonBase = ThingBase & {
"height"?: Quantity | Quantity[];
"locatedIn"?: Place | Place[];
"height"?: Quantity | readonly Quantity[];
"locatedIn"?: Place | readonly Place[];
};
export type Person = ({
"@type": "Person";
Expand All @@ -33,7 +33,7 @@ export type Quantity = ({
type ThingBase = {
/** IRI identifying the canonical address of this object. */
"@id"?: string;
"name"?: Text | Text[];
"name"?: Text | readonly Text[];
};
export type Thing = ({
"@type": "Thing";
Expand Down

0 comments on commit 2e5b233

Please sign in to comment.