Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Booleans: Support both IRIs as well as native JS values #40

Merged
merged 5 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 67 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@
"devDependencies": {
"@types/argparse": "^1.0.36",
"@types/diff": "^3.5.2",
"@types/htmlparser2": "^3.10.0",
"@types/jasmine": "^3.3.12",
"@types/node": "^10.14.6",
"@types/htmlparser2": "^3.10.1",
"@types/jasmine": "^3.4.4",
"@types/node": "^10.17.0",
"child_process": "^1.0.2",
"clang-format": "^1.2.4",
"del": "^3.0.0",
"diff": "^3.5.0",
"gulp": "^4.0.2",
"gulp-file": "^0.4.0",
"gulp-typescript": "^5.0.1",
"jasmine": "^3.4.0",
"jasmine": "^3.5.0",
"through2": "^3.0.1",
"tslint": "^5.16.0",
"typescript": "^3.4.5"
"tslint": "^5.20.0",
"typescript": "^3.5.3"
},
"dependencies": {
"argparse": "^1.0.10",
"htmlparser2": "^3.10.1",
"rxjs": "^6.5.1"
"rxjs": "^6.5.3"
},
"peerDependencies": {
"typescript": "^3.1.6"
"typescript": "^3.4.0"
},
"keywords": [
"typescript",
Expand Down
56 changes: 43 additions & 13 deletions src/ts/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {createEnumDeclaration, createEnumMember, createIntersectionTypeNode, createModifiersFromModifierFlags, createParenthesizedType, createStringLiteral, createTypeAliasDeclaration, createTypeLiteralNode, createTypeReferenceNode, createUnionTypeNode, DeclarationStatement, EnumDeclaration, ModifierFlags, Statement, TypeAliasDeclaration, TypeNode} from 'typescript';
import {createAsExpression, createEnumDeclaration, createIntersectionTypeNode, createLiteralTypeNode, createModifiersFromModifierFlags, createObjectLiteral, createParenthesizedType, createPropertyAssignment, createStringLiteral, createTypeAliasDeclaration, createTypeLiteralNode, createTypeReferenceNode, createUnionTypeNode, createVariableDeclaration, createVariableDeclarationList, createVariableStatement, DeclarationStatement, EnumDeclaration, ModifierFlags, NodeFlags, Statement, TypeAliasDeclaration, TypeNode} from 'typescript';

import {Log} from '../logging';
import {TObject, TPredicate, TSubject} from '../triples/triple';
Expand Down Expand Up @@ -232,7 +232,8 @@ export class Class {
this._enums.map(e => e.toNode()));
}

toNode(context: Context, skipDeprecatedProperties: boolean) {
toNode(context: Context, skipDeprecatedProperties: boolean):
readonly Statement[] {
const typeValue: TypeNode = this.totalType(context);
const declaration = withComments(
this.comment,
Expand Down Expand Up @@ -261,7 +262,7 @@ export class Builtin extends Class {
super(UrlNode.Parse(url), false);
}

toNode(): DeclarationStatement[] {
toNode(): readonly Statement[] {
return [
withComments(
this.doc,
Expand All @@ -285,16 +286,45 @@ export class BooleanEnum extends Builtin {
super(url, '', doc);
}

toNode(): DeclarationStatement[] {
return [withComments(
this.doc,
createEnumDeclaration(
/*decotrators=*/[],
createModifiersFromModifierFlags(ModifierFlags.Export),
this.subject.name, [
createEnumMember('True', createStringLiteral(this.trueUrl)),
createEnumMember('False', createStringLiteral(this.falseUrl)),
]))];
toNode(): readonly Statement[] {
return [
withComments(
this.doc,
createTypeAliasDeclaration(
/*decotrators=*/[],
createModifiersFromModifierFlags(ModifierFlags.Export),
this.subject.name,
/*typeParameters=*/[],
createUnionTypeNode([
createTypeReferenceNode('true', /*typeArgs=*/[]),
createTypeReferenceNode('false', /*typeArgs=*/[]),
createLiteralTypeNode(createStringLiteral(this.trueUrl)),
createLiteralTypeNode(createStringLiteral(this.falseUrl)),
]),
)),
createVariableStatement(
createModifiersFromModifierFlags(ModifierFlags.Export),
createVariableDeclarationList(
[createVariableDeclaration(
this.subject.name,
/*type=*/undefined,
createObjectLiteral(
[
createPropertyAssignment(
'True',
createAsExpression(
createStringLiteral(this.trueUrl),
createTypeReferenceNode('const', undefined))),
createPropertyAssignment(
'False',
createAsExpression(
createStringLiteral(this.falseUrl),
createTypeReferenceNode('const', undefined))),
],
true),
)],
NodeFlags.Const))
];
}
}

Expand Down
9 changes: 5 additions & 4 deletions tests/baselines/comments.ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ export type WithContext<T extends Thing> = T & {
};

/** Boolean: True or False. */
export enum Boolean {
True = "https://schema.org/True",
False = "https://schema.org/False"
}
export type Boolean = true | false | "https://schema.org/True" | "https://schema.org/False";
export const Boolean = {
True: ("https://schema.org/True" as const),
False: ("https://schema.org/False" as const)
};

/** A date value in {@link http://en.wikipedia.org/wiki/ISO_8601 ISO 8601 date format}. */
export type Date = string;
Expand Down
9 changes: 5 additions & 4 deletions tests/baselines/inheritance_multiple.ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ export type WithContext<T extends Thing> = T & {
};

/** Boolean: True or False. */
export enum Boolean {
True = "https://schema.org/True",
False = "https://schema.org/False"
}
export type Boolean = true | false | "https://schema.org/True" | "https://schema.org/False";
export const Boolean = {
True: ("https://schema.org/True" as const),
False: ("https://schema.org/False" as const)
};

/** A date value in {@link http://en.wikipedia.org/wiki/ISO_8601 ISO 8601 date format}. */
export type Date = string;
Expand Down
9 changes: 5 additions & 4 deletions tests/baselines/inheritance_one.ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ export type WithContext<T extends Thing> = T & {
};

/** Boolean: True or False. */
export enum Boolean {
True = "https://schema.org/True",
False = "https://schema.org/False"
}
export type Boolean = true | false | "https://schema.org/True" | "https://schema.org/False";
export const Boolean = {
True: ("https://schema.org/True" as const),
False: ("https://schema.org/False" as const)
};

/** A date value in {@link http://en.wikipedia.org/wiki/ISO_8601 ISO 8601 date format}. */
export type Date = string;
Expand Down
9 changes: 5 additions & 4 deletions tests/baselines/quantities.ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ export type WithContext<T extends Thing> = T & {
};

/** Boolean: True or False. */
export enum Boolean {
True = "https://schema.org/True",
False = "https://schema.org/False"
}
export type Boolean = true | false | "https://schema.org/True" | "https://schema.org/False";
export const Boolean = {
True: ("https://schema.org/True" as const),
False: ("https://schema.org/False" as const)
};

/** A date value in {@link http://en.wikipedia.org/wiki/ISO_8601 ISO 8601 date format}. */
export type Date = string;
Expand Down
Loading