Skip to content

Commit

Permalink
no-inferred-empty-object: check tagged templates (#213)
Browse files Browse the repository at this point in the history
Fixes: #190
  • Loading branch information
ajafff authored Apr 22, 2018
1 parent bab3a60 commit 9e3c4e9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,18 @@ new Promise<object>((resolve, reject) => {
}
});
});

declare function myTagFn<T>(parts: TemplateStringsArray, ...values: T[]): string;
declare function myOtherTag<T>(parts: TemplateStringsArray): T;

myTagFn``;
~~~~~~~~~ [error no-inferred-empty-object: TypeParameter 'T' is inferred as '{}'. Consider adding type arguments to the call.]
myTagFn`${''}`;
myTagFn`${1}`;
myTagFn`${1}${''}`;
myTagFn<string | number>`${1}${''}`;

myOtherTag``;
~~~~~~~~~~~~ [error no-inferred-empty-object: TypeParameter 'T' is inferred as '{}'. Consider adding type arguments to the call.]
myOtherTag`${''}`;
myOtherTag<string>``;
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,16 @@ new Promise<object>((resolve, reject) => {
}
});
});

declare function myTagFn<T>(parts: TemplateStringsArray, ...values: T[]): string;
declare function myOtherTag<T>(parts: TemplateStringsArray): T;

myTagFn``;
myTagFn`${''}`;
myTagFn`${1}`;
myTagFn`${1}${''}`;
myTagFn<string | number>`${1}${''}`;

myOtherTag``;
myOtherTag`${''}`;
myOtherTag<string>``;
10 changes: 7 additions & 3 deletions packages/mimir/src/rules/no-inferred-empty-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@ export class Rule extends TypedRule {
} else if (node.kind === ts.SyntaxKind.NewExpression) {
if ((<ts.NewExpression>node).typeArguments === undefined)
this.checkNewExpression(<ts.NewExpression>node);
} else if (!typescriptPre290 && // passing type arguments to JSX elements is only possible starting from [email protected]
(node.kind === ts.SyntaxKind.JsxOpeningElement || node.kind === ts.SyntaxKind.JsxSelfClosingElement) &&
} else if (!typescriptPre290 && // type arguments on JSX elements and tagged templates was introduced in [email protected]
(
node.kind === ts.SyntaxKind.JsxOpeningElement ||
node.kind === ts.SyntaxKind.JsxSelfClosingElement ||
node.kind === ts.SyntaxKind.TaggedTemplateExpression
) &&
// TODO fix assertion on upgrade to [email protected]
(<any>node).typeArguments === undefined) {
this.checkCallExpression(<ts.JsxOpeningLikeElement>node);
}
}
}

private checkCallExpression(node: ts.CallExpression | ts.JsxOpeningLikeElement) {
private checkCallExpression(node: ts.CallExpression | ts.JsxOpeningLikeElement | ts.TaggedTemplateExpression) {
const signature = this.checker.getResolvedSignature(node);
// wotan-disable-next-line no-useless-predicate
if (signature.declaration !== undefined) {
Expand Down
13 changes: 13 additions & 0 deletions packages/mimir/test/no-inferred-empty-object/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,16 @@ new Promise<object>((resolve, reject) => {
}
});
});

declare function myTagFn<T>(parts: TemplateStringsArray, ...values: T[]): string;
declare function myOtherTag<T>(parts: TemplateStringsArray): T;

myTagFn``;
myTagFn`${''}`;
myTagFn`${1}`;
myTagFn`${1}${''}`;
myTagFn<string | number>`${1}${''}`;

myOtherTag``;
myOtherTag`${''}`;
myOtherTag<string>``;

0 comments on commit 9e3c4e9

Please sign in to comment.