Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { runTests } from "@root/test-utils/eslint-rule-tester";
- import mustUsePluralInFunctionNamesIfTheFunctionReturnsAnArrayOfAnything from "@root/rules/must-use-plural-in-function-names-if-the-function-returns-an-array-of-anything";
- const validCases = {
- valid: [
- {
- code: `
- function getItems() {
- return [];
- }
- `,
- },
- {
- code: `
- function getItem() {
- return { id: 1 };
- }
- `,
- },
- {
- code: `
- function getItem() {
- return "hello";
- }
- `,
- },
- {
- code: `
- const getItems = () => [];
- `,
- },
- {
- code: `
- const getItems = function() {
- return [];
- };
- `,
- },
- {
- code: `
- class MyClass {
- getItems() {
- return [];
- }
- }
- `,
- },
- {
- code: `
- function getData() {
- return [];
- }
- `,
- options: [{ exceptions: ["getData"] }],
- },
- {
- code: `
- function getItems() {
- if (true) {
- return [];
- }
- return null;
- }
- `,
- },
- {
- code: `
- function getChildren() {
- return [];
- }
- `,
- },
- {
- code: `
- function processData() {
- return [];
- }
- `,
- options: [{ exceptions: ["processData"] }],
- },
- {
- code: `
- async function getItems() {
- return [];
- }
- `,
- },
- {
- code: `
- function getItems(): string[] {
- return [];
- }
- `,
- },
- {
- code: `
- function getItems(): Promise<string[]> {
- return Promise.resolve([]);
- }
- `,
- },
- {
- code: `
- function getItems(): Array<string> {
- return [];
- }
- `,
- },
- {
- code: `
- function getItems(): ReadonlyArray<string> {
- return [];
- }
- `,
- },
- {
- code: `
- function getItem(): string | undefined {
- return "hello";
- }
- `,
- },
- {
- code: `
- function getData(): string {
- return "data";
- }
- `,
- },
- {
- code: `
- function getAnalytics() {
- return [];
- }
- `,
- },
- {
- code: `
- async function getItem() {
- return [];
- }
- `,
- },
- {
- code: `
- function getItem(): Promise<string[]> {
- return Promise.resolve([]);
- }
- `,
- },
- {
- code: `
- function getMetadata() {
- return [];
- }
- `,
- },
- {
- code: `
- const obj = {
- ["getItem"]() {
- return [];
- }
- };
- `,
- },
- ],
- };
- const invalidCases = {
- invalid: [
- {
- code: `
- function getItem() {
- return [];
- }
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- {
- code: `
- const getItem = () => [];
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- {
- code: `
- const getItem = function() {
- return [];
- };
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- {
- code: `
- class MyClass {
- getItem() {
- return [];
- }
- }
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- {
- code: `
- function getItem() {
- if (true) {
- return [];
- }
- return null;
- }
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- {
- code: `
- function getItem() {
- if (false) {
- return null;
- } else {
- return [];
- }
- }
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- {
- code: `
- function getItem() {
- try {
- return [];
- } catch (error) {
- return null;
- }
- }
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- {
- code: `
- const obj = {
- getItem() {
- return [];
- }
- };
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- {
- code: `
- function getItem() {
- if (Math.random() > 0.5) {
- return [];
- }
- return "string";
- }
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- {
- code: `
- function getItem() {
- let someArray = [];
- return someArray;
- }
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- {
- code: `
- function getItem() {
- return items.filter(Boolean);
- }
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- {
- code: `
- function getItem() {
- return new Array(5);
- }
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- {
- code: `
- function getItem(): string[] {
- return [];
- }
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- {
- code: `
- function getItem(): Array<string> {
- return [];
- }
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- {
- code: `
- function getItem(): ReadonlyArray<string> {
- return [];
- }
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- {
- code: `
- function getAnalytic() {
- return [];
- }
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getAnalytic",
- suggestedName: "getAnalytics",
- },
- },
- ],
- },
- {
- code: `
- function getItem(): string[] | null {
- return [];
- }
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- {
- code: `
- function getItem(): (string | number)[] {
- return [];
- }
- `,
- errors: [
- {
- messageId: "usePluralInFunctionName",
- data: {
- functionName: "getItem",
- suggestedName: "getItems",
- },
- },
- ],
- },
- ],
- };
- const testCases = [validCases, invalidCases];
- runTests(
- "must-use-plural-in-function-names-if-the-function-returns-an-array-of-anything",
- mustUsePluralInFunctionNamesIfTheFunctionReturnsAnArrayOfAnything,
- testCases,
- );
Advertisement
Add Comment
Please, Sign In to add comment