Commit 101d536f authored by Lee ByeongJun's avatar Lee ByeongJun
Browse files

feat: add type guards

parent bbb69155
Loading
Loading
Loading
Loading
+9 −9
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ jobs:
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: denoland/setup-deno@v2
      with:
        deno-version: "2.4.1"  # FIXME: https://github.com/fedify-dev/fedify/issues/303
        deno-version: v2.x
    - run: deno task test --coverage=.cov --junit-path=.test-report.xml
      env:
        RUST_BACKTRACE: ${{ runner.debug }}
@@ -124,7 +124,7 @@ jobs:
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: denoland/setup-deno@v2
      with:
        deno-version: "2.4.1"  # FIXME: https://github.com/fedify-dev/fedify/issues/303
        deno-version: v2.x
    - uses: pnpm/action-setup@v4
      with:
        version: 10
@@ -181,7 +181,7 @@ jobs:
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: denoland/setup-deno@v2
      with:
        deno-version: "2.4.1"  # FIXME: https://github.com/fedify-dev/fedify/issues/303
        deno-version: v2.x
    - uses: pnpm/action-setup@v4
      with:
        version: 10
@@ -207,7 +207,7 @@ jobs:
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: denoland/setup-deno@v2
      with:
        deno-version: "2.4.1"  # FIXME: https://github.com/fedify-dev/fedify/issues/303
        deno-version: v2.x
    - uses: pnpm/action-setup@v4
      with:
        version: 10
@@ -230,7 +230,7 @@ jobs:
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: denoland/setup-deno@v2
      with:
        deno-version: "2.4.1"  # FIXME: https://github.com/fedify-dev/fedify/issues/303
        deno-version: v2.x
    - run: deno task hooks:pre-commit

  release-test:
@@ -248,7 +248,7 @@ jobs:
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: denoland/setup-deno@v2
      with:
        deno-version: "2.4.1"  # FIXME: https://github.com/fedify-dev/fedify/issues/303
        deno-version: v2.x
    - uses: pnpm/action-setup@v4
      with:
        version: 10
@@ -283,7 +283,7 @@ jobs:
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: denoland/setup-deno@v2
      with:
        deno-version: "2.4.1"  # FIXME: https://github.com/fedify-dev/fedify/issues/303
        deno-version: v2.x
    - uses: pnpm/action-setup@v4
      with:
        version: 10
@@ -449,7 +449,7 @@ jobs:
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: denoland/setup-deno@v2
      with:
        deno-version: "2.4.1"  # FIXME: https://github.com/fedify-dev/fedify/issues/303
        deno-version: v2.x
    - run: deno task codegen
      working-directory: ${{ github.workspace }}/fedify/
    - uses: denoland/deployctl@v1
@@ -480,7 +480,7 @@ jobs:
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: denoland/setup-deno@v2
      with:
        deno-version: "2.4.1"  # FIXME: https://github.com/fedify-dev/fedify/issues/303
        deno-version: v2.x
    - uses: pnpm/action-setup@v4
      with:
        version: 10
+7 −3
Original line number Diff line number Diff line
import metadata from "../deno.json" with { type: "json" };
import { generateField, getFieldName } from "./field.ts";
import { isNonFunctionalProperty } from "./schema.ts";
import type { TypeSchema } from "./schema.ts";
import {
  areAllScalarTypes,
@@ -94,7 +95,10 @@ export async function* generateEncoder(
      }
      if (compactItems.length > 0) {
      `;
      if (property.functional || property.container !== "list") {
      if (
        property.functional ||
        (isNonFunctionalProperty(property) && property.container !== "list")
      ) {
        yield `
        result[${JSON.stringify(property.compactName)}]
          = compactItems.length > 1
@@ -162,7 +166,7 @@ export async function* generateEncoder(
    yield `
      );
    `;
    if (!property.functional && property.container === "graph") {
    if (isNonFunctionalProperty(property) && property.container === "graph") {
      yield `array.push({ "@graph": element });`;
    } else {
      yield `array.push(element);`;
@@ -172,7 +176,7 @@ export async function* generateEncoder(
    if (array.length > 0) {
      const propValue = (
    `;
    if (!property.functional && property.container === "list") {
    if (isNonFunctionalProperty(property) && property.container === "list") {
      yield `{ "@list": array }`;
    } else {
      yield `array`;
+8 −7
Original line number Diff line number Diff line
import { getFieldName } from "./field.ts";
import { hasSingularAccessor, isNonFunctionalProperty } from "./schema.ts";
import type { PropertySchema, TypeSchema } from "./schema.ts";
import {
  areAllScalarTypes,
@@ -14,7 +15,7 @@ function generateParameterType(
  const range = property.range;
  const scalar = areAllScalarTypes(range, types);
  const code: string[] = [];
  if (property.functional || property.singularAccessor) {
  if (hasSingularAccessor(property)) {
    if (scalar) {
      code.push(
        `${property.singularName}?: ${getTypeNames(range, types)} | null;`,
@@ -27,7 +28,7 @@ function generateParameterType(
      );
    }
  }
  if (!property.functional) {
  if (isNonFunctionalProperty(property)) {
    if (scalar) {
      code.push(
        `${property.pluralName}?: (${getTypeNames(range, types, true)})[];`,
@@ -114,7 +115,7 @@ export async function* generateConstructor(
  }
  for (const property of type.properties) {
    const fieldName = await getFieldName(property.uri);
    if (property.functional || property.singularAccessor) {
    if (hasSingularAccessor(property)) {
      let typeGuards = getTypeGuards(
        property.range,
        types,
@@ -142,7 +143,7 @@ export async function* generateConstructor(
        }
      `;
    }
    if (!property.functional) {
    if (isNonFunctionalProperty(property)) {
      let typeGuards = getTypeGuards(property.range, types, `v`);
      let typeNames = getTypeNames(property.range, types);
      const scalar = areAllScalarTypes(property.range, types);
@@ -154,7 +155,7 @@ export async function* generateConstructor(
        if ("${property.pluralName}" in values && \
            values.${property.pluralName} != null) {
      `;
      if (property.singularAccessor) {
      if (isNonFunctionalProperty(property) && property.singularAccessor) {
        yield `
          if ("${property.singularName}" in values &&
              values.${property.singularName} != null) {
@@ -229,7 +230,7 @@ export async function* generateCloner(
  for (const property of type.properties) {
    const fieldName = await getFieldName(property.uri);
    yield `clone.${fieldName} = this.${fieldName};`;
    if (property.functional || property.singularAccessor) {
    if (hasSingularAccessor(property)) {
      let typeGuards = getTypeGuards(
        property.range,
        types,
@@ -257,7 +258,7 @@ export async function* generateCloner(
        }
      `;
    }
    if (!property.functional) {
    if (isNonFunctionalProperty(property)) {
      let typeGuards = getTypeGuards(property.range, types, `v`);
      let typeNames = getTypeNames(property.range, types);
      const scalar = areAllScalarTypes(property.range, types);
+3 −2
Original line number Diff line number Diff line
import { getFieldName } from "./field.ts";
import { hasSingularAccessor, isNonFunctionalProperty } from "./schema.ts";
import type { TypeSchema } from "./schema.ts";
import { emitOverride } from "./type.ts";

@@ -52,14 +53,14 @@ export async function* generateInspector(
            }
          : v);
    `;
    if (property.functional || property.singularAccessor) {
    if (hasSingularAccessor(property)) {
      yield `
      if (${localName}.length == 1) {
        proxy.${property.singularName} = ${localName}[0];
      }
      `;
    }
    if (!property.functional) {
    if (isNonFunctionalProperty(property)) {
      yield `
      if (${localName}.length > 1
          || !(${JSON.stringify(property.singularName)} in proxy)
+5 −4
Original line number Diff line number Diff line
import { pascalCase } from "es-toolkit";
import metadata from "../deno.json" with { type: "json" };
import { getFieldName } from "./field.ts";
import { hasSingularAccessor, isNonFunctionalProperty } from "./schema.ts";
import type { PropertySchema, TypeSchema } from "./schema.ts";
import { areAllScalarTypes, getTypeNames } from "./type.ts";

@@ -29,7 +30,7 @@ async function* generateProperty(
  const override = emitOverride(type.uri, types, property);
  const doc = `\n/** ${property.description.replaceAll("\n", "\n * ")}\n */\n`;
  if (areAllScalarTypes(property.range, types)) {
    if (property.functional || property.singularAccessor) {
    if (hasSingularAccessor(property)) {
      yield doc;
      yield `${override} get ${property.singularName}(): (${
        getTypeNames(property.range, types)
@@ -45,7 +46,7 @@ async function* generateProperty(
      }
      `;
    }
    if (!property.functional) {
    if (isNonFunctionalProperty(property)) {
      yield doc;
      yield `get ${property.pluralName}(): (${
        getTypeNames(property.range, types, true)
@@ -161,7 +162,7 @@ async function* generateProperty(
    }

    `;
    if (property.functional || property.singularAccessor) {
    if (hasSingularAccessor(property)) {
      yield `
      /**
       * Similar to
@@ -230,7 +231,7 @@ async function* generateProperty(
      }
      `;
    }
    if (!property.functional) {
    if (isNonFunctionalProperty(property)) {
      yield `
      /**
       * Similar to
Loading