Skip to content

Commit

Permalink
Fix error message (#166)
Browse files Browse the repository at this point in the history
Always print own before injected
  • Loading branch information
guyca authored Jul 26, 2024
1 parent 7422265 commit cf518f3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {Context} from '../../dto/context';
export type Options = readonly [
{
injectedPropsPattern: string;
ownPropsPattern: string;
},
];

Expand Down Expand Up @@ -39,12 +40,22 @@ export const stronglyTypedInjectComponentGenerator = () => {
},
additionalProperties: false,
},
{
type: 'object',
properties: {
ownPropsPattern: {
type: 'string',
},
},
additionalProperties: false,
},
],
type: 'problem',
},
defaultOptions: [
{
injectedPropsPattern: '/\\b(Injected|InjectedProps)\\b/',
ownPropsPattern: '/\\b(Own|Props|OwnProps)\\b/',
},
],
}) satisfies Rule;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import type { Type } from '../../../dto/types/type';
import type { Result } from './result';

export class MissingTypeError implements Result {
readonly isError = true;

constructor(private readonly missingTypes: Type[]) { }
constructor(private readonly own: string[], private readonly injected: string[]) { }

getMessage() {
return `The call to injectComponent is missing prop types. It should be typed as: injectComponent<${this.missingTypes.map(t => t.toString()).join(', ')}> `;
console.log(this.getGenerics());
return `The call to injectComponent is missing prop types. It should be typed as: injectComponent<${this.getGenerics()}> `;
}

private getGenerics(): string {
const own = this.own[0];
const injected = this.injected[0];
return own && injected ? `${own}, ${injected}` : own;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class TypeValidator {
public validate(injectedComponent?: Variable, generics?: Generics): Result {
if (!injectedComponent) return new Success();
const componentProps = new FunctionalComponent(injectedComponent.arrowFunction).props.type;
const injectComponentGenerics = generics?.types || [];
const injectComponentGenerics = generics?.types ?? [];
return this.areTypesValid(componentProps, injectComponentGenerics);
}

Expand All @@ -35,8 +35,10 @@ export class TypeValidator {
return new Success();
}

// TODO: Report the actual missing type
return new MissingTypeError(injectComponentGenerics);
const injected = this.getInjectedTypes(componentProps);
const own = this.getOwnTypes(componentProps);
if (!own && !injected) return new Success();
return new MissingTypeError(own!, injected ?? []);
}

private hasInlineType(injectComponentGenerics: Type[]) {
Expand All @@ -48,8 +50,15 @@ export class TypeValidator {
}

private isInjected(componentProps: Type) {
return componentProps.size() === 1 &&
!!componentProps.toString()[0].match(stringToRegex(this.injectedPattern));
return componentProps.size() === 1 && !!this.getInjectedTypes(componentProps);
}

private getInjectedTypes(componentProps: Type) {
return componentProps.toString().join(',').match(stringToRegex(this.injectedPattern));
}

private getOwnTypes(componentProps: Type) {
return componentProps.toString().join(',').match(stringToRegex(this.ownPattern));
}

private typesAreInCorrectOrder(injectComponentGenerics: Type[], componentProps: Type) {
Expand All @@ -62,4 +71,8 @@ export class TypeValidator {
private get injectedPattern() {
return this.options[0].injectedPropsPattern;
}

private get ownPattern() {
return this.options[0].ownPropsPattern;
}
}

0 comments on commit cf518f3

Please sign in to comment.