Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into matt/v2
  • Loading branch information
mattpocock committed Jun 13, 2023
2 parents 6ec93f6 + 7116901 commit f3fe7e2
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
72 changes: 71 additions & 1 deletion packages/parser/src/tips.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,74 @@ export const allTips = [
spreadIntoTupleType,
keyofIndexedAccess,
templateLiteral,
createInlineTip(
'generic-component-declaration',
z.object({
id: z.object({
name: z.string().refine((name) => {
return name.charAt(0) === name.charAt(0).toUpperCase();
}),
}),
typeParameters: z.object({
type: z.literal('TSTypeParameterDeclaration'),
loc: SourceLocationSchema,
params: z
.array(
z.object({
name: z.string(),
}),
)
.min(1),
}),
}),
({ parse, push }) => {
return {
FunctionDeclaration(path) {
safeParse(() => {
const node = parse(path.node);
if (node.typeParameters) {
push(node.typeParameters.loc);
}
});
},
};
},
),
createInlineTip(
'generic-component-declaration',
z.object({
id: z.object({
name: z.string().refine((name) => {
return name.charAt(0) === name.charAt(0).toUpperCase();
}),
}),
init: z.object({
typeParameters: z.object({
type: z.literal('TSTypeParameterDeclaration'),
loc: SourceLocationSchema,
params: z
.array(
z.object({
name: z.string(),
}),
)
.min(1),
}),
}),
}),
({ parse, push }) => {
return {
VariableDeclarator(path) {
safeParse(() => {
const node = parse(path.node);
if (node.init.typeParameters) {
push(node.init.typeParameters.loc);
}
});
},
};
},
),
createInlineTip(
'as-assertion',
z.object({
Expand Down Expand Up @@ -703,7 +771,9 @@ export const allTips = [
...utilityTypeTips,
];

export const tipsAsStrings = allTips.map((tip) => tip.type);
export const tipsAsStrings = Array.from(
new Set(allTips.map((tip) => tip.type)),
);

export type TipType = typeof allTips[number]['type'];
export type Tip = { type: TipType; loc: t.SourceLocation };
20 changes: 20 additions & 0 deletions packages/parser/src/tips/inlineTips.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ it('t-prefix-in-generic-arguments', () => {
expect(tips).toContain('t-prefix-in-generic-arguments');
});

it('generic-component-declaration in arrow functions', () => {
const fileContents = `
const Component = <T>() => {};
`;

const tips = getTipsFromFile(fileContents).map((tip) => tip.type);

expect(tips).toContain('generic-component-declaration');
});

it('generic-component-declaration in normal functions', () => {
const fileContents = `
function Component<T>() {}
`;

const tips = getTipsFromFile(fileContents).map((tip) => tip.type);

expect(tips).toContain('generic-component-declaration');
});

it('Union type', () => {
const fileContents = `
type Yeah = string | number;
Expand Down

0 comments on commit f3fe7e2

Please sign in to comment.