Skip to content

Commit

Permalink
fix(collection-of): add Enum decorator support when it's a prop array
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Apr 3, 2024
1 parent 8755af6 commit 4a55387
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,21 @@ describe("explicitCollectionOfDecorator", () => {
@Property()
thisIsABooleanProp = false
}`,
}
},
{
code: `
import {Default, Enum} from "@tsed/schema";
enum GrantTypes {
AUTHORIZATION_CODE = "authorization_code"
}
class TestClass {
@Default(GrantTypes.AUTHORIZATION_CODE)
@Enum(GrantTypes)
grantTypes: GrantTypes[] = [GrantTypes.AUTHORIZATION_CODE]
}`
},
],
invalid: [
{
Expand Down Expand Up @@ -297,6 +311,61 @@ describe("explicitCollectionOfDecorator", () => {
thisIsAStringProp?: string[];
}`
},
{
code: `
import {Default} from "@tsed/schema";
class Nested {}
class TestClass {
@Default([])
thisIsAStringProp?: Nested[] = [];
}`,
errors: [
{
messageId: "missing-collection-of-decorator",
},
],
output: `
import {Default, ArrayOf} from "@tsed/schema";
class Nested {}
class TestClass {
@ArrayOf(Nested)
@Default([])
thisIsAStringProp?: Nested[] = [];
}`
},
{
code: `
import {Default, Enum, ArrayOf} from "@tsed/schema";
enum GrantTypes {
AUTHORIZATION_CODE = "authorization_code"
}
class TestClass {
@Default(GrantTypes.AUTHORIZATION_CODE)
@Enum(GrantTypes)
@ArrayOf(GrantTypes)
grantTypes: GrantTypes[] = [GrantTypes.AUTHORIZATION_CODE]
}`,
errors: [
{
messageId: "unnecessary-array-of-decorator",
},
],
output: `
import {Default, Enum, ArrayOf} from "@tsed/schema";
enum GrantTypes {
AUTHORIZATION_CODE = "authorization_code"
}
class TestClass {
@Default(GrantTypes.AUTHORIZATION_CODE)
@Enum(GrantTypes)
grantTypes: GrantTypes[] = [GrantTypes.AUTHORIZATION_CODE]
}`,
},
],
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type RULES =
| "unnecessary-collection-of-decorator"
| "unexpected-map-of-decorator";

type DECORATORS_TYPES = "Property" | "CollectionOf" | "MapOf" | "ArrayOf";
const DECORATORS: DECORATORS_TYPES[] = ["Property", "CollectionOf", "MapOf", "ArrayOf"];
type DECORATORS_TYPES = "Property" | "CollectionOf" | "MapOf" | "ArrayOf" | "Enum";
const DECORATORS: DECORATORS_TYPES[] = ["Property", "CollectionOf", "MapOf", "ArrayOf", "Enum"];

const TYPES_TO_DECORATORS: Record<string, DECORATORS_TYPES> = {
Array: "ArrayOf",
Expand Down Expand Up @@ -50,8 +50,8 @@ const RULES_CHECK: RuleOptions<RULES, CollectionOfDecoratorsStatus>[] = [
messageId: "missing-collection-of-decorator",
message: "Property returning Array, Set or Map must set CollectionOf decorator",
test: ({decorators, isCollection}) =>
isCollection && !(decorators.has("CollectionOf") || decorators.has("MapOf") || decorators.has("ArrayOf")),
* fix({fixer, node}) {
isCollection && !(decorators.has("CollectionOf") || decorators.has("MapOf") || decorators.has("ArrayOf") || decorators.has("Enum")),
* fix({fixer, node, decoratorsStatus}) {
const {itemType, collectionType} = getTypes(node);
const decoratorName = TYPES_TO_DECORATORS[collectionType as string];

Expand All @@ -75,8 +75,8 @@ const RULES_CHECK: RuleOptions<RULES, CollectionOfDecoratorsStatus>[] = [
messageId: "unnecessary-array-of-decorator",
message: "Unnecessary ArrayOf decorator over a Property not returning Array",
test: ({decorators, isCollection}) =>
!isCollection
&& decorators.has("ArrayOf"),
decorators.has("ArrayOf") &&
(!isCollection || (isCollection && decorators.has("Enum"))),
fix({fixer, node}) {
const collectionOfDecorator = findPropertyDecorator(node, "ArrayOf");

Expand Down
3 changes: 2 additions & 1 deletion src/utils/getTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export function getTypes(
) {

const collectionType = (node.typeAnnotation as any)?.typeAnnotation?.typeName?.name;
const itemType = extractType((node.typeAnnotation?.typeAnnotation as any )?.elementType?.type) ;
const elementType = (node.typeAnnotation?.typeAnnotation as any )?.elementType
const itemType = elementType?.typeName?.name || extractType(elementType?.type) ;

if (!itemType) {
if (["Map", "Set"].includes(collectionType)) {
Expand Down

0 comments on commit 4a55387

Please sign in to comment.