Skip to content

Commit

Permalink
Cherry pick from next 1 (#446)
Browse files Browse the repository at this point in the history
* chore(valid-title): merge if conditions

* chore(valid-title): order imports

* chore(valid-title): add type annotation to `node` parameter

* chore(valid-title): inline `getNodeTitle` function

* chore(valid-title): adjust documentation

* chore(utils): remove unneeded exporting of `isTemplateLiteral`
  • Loading branch information
G-Rath committed Oct 27, 2019
1 parent f07a10f commit e9f0503
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ installations requiring long-term consistency.
| [valid-describe][] | Enforce valid `describe()` callback | ![recommended][] | |
| [valid-expect-in-promise][] | Enforce having return statement when testing with promises | ![recommended][] | |
| [valid-expect][] | Enforce valid `expect()` usage | ![recommended][] | |
| [valid-title][] | Disallow invalid `describe`/`test` titles | | |
| [valid-title][] | Enforce valid titles for jest blocks | | |

## Credit

Expand Down
5 changes: 4 additions & 1 deletion docs/rules/valid-title.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# describe/test titles should be valid (valid-title)

A describe/ test block should not contain accidentalSpace or duplicatePrefix.
Checks that the title of Jest blocks are valid by ensuring that titles are:

- not prefixed with their block name,
- have no leading or trailing spaces

## Rule Details

Expand Down
2 changes: 1 addition & 1 deletion src/rules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ interface TemplateLiteral<Value extends string = string>
*
* @template V
*/
export const isTemplateLiteral = <V extends string>(
const isTemplateLiteral = <V extends string>(
node: TSESTree.Node,
value?: V,
): node is TemplateLiteral<V> =>
Expand Down
28 changes: 15 additions & 13 deletions src/rules/valid-title.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TSESTree } from '@typescript-eslint/experimental-utils';
import {
createRule,
getNodeName,
Expand All @@ -7,17 +8,9 @@ import {
isTestCase,
} from './utils';

import { TSESTree } from '@typescript-eslint/experimental-utils';

const trimFXprefix = (word: string) =>
['f', 'x'].includes(word.charAt(0)) ? word.substr(1) : word;

const getNodeTitle = (node: TSESTree.CallExpression): string | null => {
const [argument] = node.arguments;

return isStringNode(argument) ? getStringValue(argument) : null;
};

export default createRule({
name: __filename,
meta: {
Expand All @@ -36,13 +29,22 @@ export default createRule({
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (!isDescribe(node) && !isTestCase(node)) return;
CallExpression(node: TSESTree.CallExpression) {
if (!(isDescribe(node) || isTestCase(node)) || !node.arguments.length) {
return;
}

const [argument] = node.arguments;

if (!node.arguments.length) return;
if (!isStringNode(argument)) {
return;
}

const title = getNodeTitle(node);
if (!title) return;
const title = getStringValue(argument);

if (!title) {
return;
}

if (title.trimLeft().length !== title.length) {
context.report({
Expand Down

0 comments on commit e9f0503

Please sign in to comment.