From d9b82a3938e88d856f36af58697a2e2fd570ffbc Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 19 Jul 2019 18:01:33 +1200 Subject: [PATCH 01/17] chore: convert `collectReferences` util fn to TS --- src/rules/tsUtils.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/rules/tsUtils.ts b/src/rules/tsUtils.ts index 91aaf4779..f7979415b 100644 --- a/src/rules/tsUtils.ts +++ b/src/rules/tsUtils.ts @@ -3,6 +3,7 @@ import { basename } from 'path'; import { AST_NODE_TYPES, ESLintUtils, + TSESLint, TSESTree, } from '@typescript-eslint/experimental-utils'; import { version } from '../../package.json'; @@ -41,3 +42,31 @@ export const isDescribe = (node: TSESTree.CallExpression): boolean => { export const isLiteralNode = (node: { type: AST_NODE_TYPES; }): node is TSESTree.Literal => node.type === AST_NODE_TYPES.Literal; + +/* istanbul ignore next */ +const collectReferences = (scope: TSESLint.Scope.Scope) => { + const locals = new Set(); + const unresolved = new Set(); + + let currentScope: TSESLint.Scope.Scope | null = scope; + + while (currentScope !== null) { + for (const ref of currentScope.variables) { + const isReferenceDefined = ref.defs.some(def => { + return def.type !== 'ImplicitGlobalVariable'; + }); + + if (isReferenceDefined) { + locals.add(ref.name); + } + } + + for (const ref of currentScope.through) { + unresolved.add(ref.identifier.name); + } + + currentScope = currentScope.upper; + } + + return { locals, unresolved }; +}; From d9b53f1dc43c50ba24e46425ea13eb914595d110 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 19 Jul 2019 18:09:40 +1200 Subject: [PATCH 02/17] chore: convert `scopeHasLocalReference` util fn to TS --- src/rules/tsUtils.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/rules/tsUtils.ts b/src/rules/tsUtils.ts index f7979415b..5983d8da4 100644 --- a/src/rules/tsUtils.ts +++ b/src/rules/tsUtils.ts @@ -43,7 +43,6 @@ export const isLiteralNode = (node: { type: AST_NODE_TYPES; }): node is TSESTree.Literal => node.type === AST_NODE_TYPES.Literal; -/* istanbul ignore next */ const collectReferences = (scope: TSESLint.Scope.Scope) => { const locals = new Set(); const unresolved = new Set(); @@ -70,3 +69,18 @@ const collectReferences = (scope: TSESLint.Scope.Scope) => { return { locals, unresolved }; }; + +/* istanbul ignore next */ +export const scopeHasLocalReference = ( + scope: TSESLint.Scope.Scope, + referenceName: string, +) => { + const references = collectReferences(scope); + return ( + // referenceName was found as a local variable or function declaration. + references.locals.has(referenceName) || + // referenceName was not found as an unresolved reference, + // meaning it is likely not an implicit global reference. + !references.unresolved.has(referenceName) + ); +}; From 3c04635b9fd3e564b59872d493a443af1cdf876f Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 19 Jul 2019 17:36:37 +1200 Subject: [PATCH 03/17] chore: convert `getNodeName` util fn to TS --- src/rules/tsUtils.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/rules/tsUtils.ts b/src/rules/tsUtils.ts index 5983d8da4..4c54cc003 100644 --- a/src/rules/tsUtils.ts +++ b/src/rules/tsUtils.ts @@ -21,6 +21,31 @@ enum DescribeAlias { 'xdescribe', } +/* istanbul ignore next */ +export const getNodeName = (node?: TSESTree.Node): string | null => { + function joinNames(a?: string | null, b?: string | null): string | null { + return a && b ? `${a}.${b}` : null; + } + + if (!node) { + return null; + } + + switch (node.type) { + case 'Identifier': + return node.name; + case 'Literal': + return `${node.value}`; + case 'TemplateLiteral': + if (node.expressions.length === 0) return node.quasis[0].value.cooked; + break; + case 'MemberExpression': + return joinNames(getNodeName(node.object), getNodeName(node.property)); + } + + return null; +}; + export type FunctionExpression = | TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression; From 870f22b03484ea38c4e6a2fc6b4a0a5ac6b3cecb Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 19 Jul 2019 18:34:12 +1200 Subject: [PATCH 04/17] chore: convert `no-jasmine-globals` rule to TypeScript --- ...als.test.js => no-jasmine-globals.test.ts} | 4 +- ...smine-globals.js => no-jasmine-globals.ts} | 54 ++++++++++++------- 2 files changed, 37 insertions(+), 21 deletions(-) rename src/rules/__tests__/{no-jasmine-globals.test.js => no-jasmine-globals.test.ts} (97%) rename src/rules/{no-jasmine-globals.js => no-jasmine-globals.ts} (71%) diff --git a/src/rules/__tests__/no-jasmine-globals.test.js b/src/rules/__tests__/no-jasmine-globals.test.ts similarity index 97% rename from src/rules/__tests__/no-jasmine-globals.test.js rename to src/rules/__tests__/no-jasmine-globals.test.ts index 2d73156ca..2ad3c6bbc 100644 --- a/src/rules/__tests__/no-jasmine-globals.test.js +++ b/src/rules/__tests__/no-jasmine-globals.test.ts @@ -1,7 +1,7 @@ -import { RuleTester } from 'eslint'; +import { TSESLint } from '@typescript-eslint/experimental-utils'; import rule from '../no-jasmine-globals'; -const ruleTester = new RuleTester(); +const ruleTester = new TSESLint.RuleTester(); ruleTester.run('no-jasmine-globals', rule, { valid: [ diff --git a/src/rules/no-jasmine-globals.js b/src/rules/no-jasmine-globals.ts similarity index 71% rename from src/rules/no-jasmine-globals.js rename to src/rules/no-jasmine-globals.ts index e3a617dce..c3a7c2ff1 100644 --- a/src/rules/no-jasmine-globals.js +++ b/src/rules/no-jasmine-globals.ts @@ -1,11 +1,14 @@ -import { getDocsUrl, getNodeName, scopeHasLocalReference } from './util'; +import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils'; +import { createRule, getNodeName, scopeHasLocalReference } from './tsUtils'; -export default { +export default createRule({ + name: __filename, meta: { docs: { - url: getDocsUrl(__filename), + category: 'Best Practices', + description: 'Disallow Jasmine globals', + recommended: false, }, - fixable: 'code', messages: { illegalGlobal: 'Illegal usage of global `{{ global }}`, prefer `{{ replacement }}`', @@ -17,16 +20,21 @@ export default { 'Illegal usage of `pending`, prefer explicitly skipping a test using `test.skip`', illegalJasmine: 'Illegal usage of jasmine global', }, + fixable: 'code', schema: [], + type: 'suggestion', }, + defaultOptions: [], create(context) { return { CallExpression(node) { - const calleeName = getNodeName(node.callee); + const { callee } = node; + const calleeName = getNodeName(callee); - if (!calleeName) { + if (callee.type !== AST_NODE_TYPES.MemberExpression || !calleeName) { return; } + if ( calleeName === 'spyOn' || calleeName === 'spyOnProperty' || @@ -69,7 +77,7 @@ export default { ) { context.report({ fix(fixer) { - return [fixer.replaceText(node.callee.object, 'expect')]; + return [fixer.replaceText(callee.object, 'expect')]; }, node, messageId: 'illegalMethod', @@ -109,18 +117,26 @@ export default { } }, MemberExpression(node) { - if (node.object.name === 'jasmine') { - if (node.parent.type === 'AssignmentExpression') { - if (node.property.name === 'DEFAULT_TIMEOUT_INTERVAL') { + if ('name' in node.object && node.object.name === 'jasmine') { + const { parent, property } = node; + + if (parent && parent.type === AST_NODE_TYPES.AssignmentExpression) { + if ( + 'name' in property && + property.name === 'DEFAULT_TIMEOUT_INTERVAL' + ) { + const { right } = parent; + context.report({ - fix(fixer) { - return [ - fixer.replaceText( - node.parent, - `jest.setTimeout(${node.parent.right.value})`, - ), - ]; - }, + fix: + right.type !== AST_NODE_TYPES.Literal + ? null + : fixer => [ + fixer.replaceText( + parent, + `jest.setTimeout(${right.value})`, + ), + ], node, messageId: 'illegalJasmine', }); @@ -133,4 +149,4 @@ export default { }, }; }, -}; +}); From 15e159de78e2458f15566a35985f5414e7608d5f Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 19 Jul 2019 20:30:09 +1200 Subject: [PATCH 05/17] chore: remove `/* istanbul ignore next */` lines --- src/rules/tsUtils.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/rules/tsUtils.ts b/src/rules/tsUtils.ts index 4c54cc003..0ccdf3496 100644 --- a/src/rules/tsUtils.ts +++ b/src/rules/tsUtils.ts @@ -21,7 +21,6 @@ enum DescribeAlias { 'xdescribe', } -/* istanbul ignore next */ export const getNodeName = (node?: TSESTree.Node): string | null => { function joinNames(a?: string | null, b?: string | null): string | null { return a && b ? `${a}.${b}` : null; @@ -95,7 +94,6 @@ const collectReferences = (scope: TSESLint.Scope.Scope) => { return { locals, unresolved }; }; -/* istanbul ignore next */ export const scopeHasLocalReference = ( scope: TSESLint.Scope.Scope, referenceName: string, From 6619db207d6895f28ef9635daa2eb3621bc5ece9 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 19 Jul 2019 20:56:11 +1200 Subject: [PATCH 06/17] chore(no-jasmine-globals): mark as `recommended` --- src/rules/no-jasmine-globals.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rules/no-jasmine-globals.ts b/src/rules/no-jasmine-globals.ts index c3a7c2ff1..6d54814a7 100644 --- a/src/rules/no-jasmine-globals.ts +++ b/src/rules/no-jasmine-globals.ts @@ -7,7 +7,7 @@ export default createRule({ docs: { category: 'Best Practices', description: 'Disallow Jasmine globals', - recommended: false, + recommended: 'error', }, messages: { illegalGlobal: From 4dd0d1cabb5061fdc85e2ea72d72e0278b602aa6 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 19 Jul 2019 21:03:23 +1200 Subject: [PATCH 07/17] chore(no-jasmine-globals): flip if condition --- src/rules/no-jasmine-globals.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/rules/no-jasmine-globals.ts b/src/rules/no-jasmine-globals.ts index 6d54814a7..bb7614920 100644 --- a/src/rules/no-jasmine-globals.ts +++ b/src/rules/no-jasmine-globals.ts @@ -127,16 +127,14 @@ export default createRule({ ) { const { right } = parent; + if (right.type !== AST_NODE_TYPES.Literal) { + return; + } + context.report({ - fix: - right.type !== AST_NODE_TYPES.Literal - ? null - : fixer => [ - fixer.replaceText( - parent, - `jest.setTimeout(${right.value})`, - ), - ], + fix: fixer => [ + fixer.replaceText(parent, `jest.setTimeout(${right.value})`), + ], node, messageId: 'illegalJasmine', }); From 99c5d8fc926988f11fc743b261c3b164bce178a4 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jul 2019 10:22:51 +1200 Subject: [PATCH 08/17] chore(no-jasmine-globals): use single quotes instead of template string --- src/rules/no-jasmine-globals.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rules/no-jasmine-globals.ts b/src/rules/no-jasmine-globals.ts index bb7614920..c9d3ca14b 100644 --- a/src/rules/no-jasmine-globals.ts +++ b/src/rules/no-jasmine-globals.ts @@ -95,7 +95,7 @@ export default createRule({ messageId: 'illegalMethod', data: { method: calleeName, - replacement: `expect.extend`, + replacement: 'expect.extend', }, }); return; From 891aeaee5e763224146131d1fd30b23603cb5385 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jul 2019 10:24:59 +1200 Subject: [PATCH 09/17] chore(no-jasmine-globals): pass `fix` as a property --- src/rules/no-jasmine-globals.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/rules/no-jasmine-globals.ts b/src/rules/no-jasmine-globals.ts index c9d3ca14b..e197db1f7 100644 --- a/src/rules/no-jasmine-globals.ts +++ b/src/rules/no-jasmine-globals.ts @@ -76,9 +76,7 @@ export default createRule({ functionName === 'stringMatching' ) { context.report({ - fix(fixer) { - return [fixer.replaceText(callee.object, 'expect')]; - }, + fix: fixer => [fixer.replaceText(callee.object, 'expect')], node, messageId: 'illegalMethod', data: { From ca2d065424c232ff78c9ac19ff4a589fe4efe77d Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jul 2019 10:25:23 +1200 Subject: [PATCH 10/17] chore(no-jasmine-globals): move `callee.type` check to after `switch` --- src/rules/no-jasmine-globals.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/rules/no-jasmine-globals.ts b/src/rules/no-jasmine-globals.ts index e197db1f7..f13f71f86 100644 --- a/src/rules/no-jasmine-globals.ts +++ b/src/rules/no-jasmine-globals.ts @@ -31,7 +31,7 @@ export default createRule({ const { callee } = node; const calleeName = getNodeName(callee); - if (callee.type !== AST_NODE_TYPES.MemberExpression || !calleeName) { + if (!calleeName) { return; } @@ -68,6 +68,10 @@ export default createRule({ if (calleeName.startsWith('jasmine.')) { const functionName = calleeName.replace('jasmine.', ''); + if (callee.type !== AST_NODE_TYPES.MemberExpression) { + return; + } + if ( functionName === 'any' || functionName === 'anything' || From 86d92211262890356a388a03fe15bfe82171dc40 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 19 Jul 2019 18:11:56 +1200 Subject: [PATCH 11/17] chore: convert `no-disabled-tests` rule to TypeScript --- ...led-tests.test.js => no-disabled-tests.test.ts} | 4 ++-- .../{no-disabled-tests.js => no-disabled-tests.ts} | 14 ++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) rename src/rules/__tests__/{no-disabled-tests.test.js => no-disabled-tests.test.ts} (96%) rename src/rules/{no-disabled-tests.js => no-disabled-tests.ts} (87%) diff --git a/src/rules/__tests__/no-disabled-tests.test.js b/src/rules/__tests__/no-disabled-tests.test.ts similarity index 96% rename from src/rules/__tests__/no-disabled-tests.test.js rename to src/rules/__tests__/no-disabled-tests.test.ts index 0ed3929a6..7a76f078a 100644 --- a/src/rules/__tests__/no-disabled-tests.test.js +++ b/src/rules/__tests__/no-disabled-tests.test.ts @@ -1,7 +1,7 @@ -import { RuleTester } from 'eslint'; +import { TSESLint } from '@typescript-eslint/experimental-utils'; import rule from '../no-disabled-tests'; -const ruleTester = new RuleTester({ +const ruleTester = new TSESLint.RuleTester({ parserOptions: { sourceType: 'module', }, diff --git a/src/rules/no-disabled-tests.js b/src/rules/no-disabled-tests.ts similarity index 87% rename from src/rules/no-disabled-tests.js rename to src/rules/no-disabled-tests.ts index 38fa7ec80..1937d7fcc 100644 --- a/src/rules/no-disabled-tests.js +++ b/src/rules/no-disabled-tests.ts @@ -1,9 +1,13 @@ -import { getDocsUrl, getNodeName, scopeHasLocalReference } from './util'; +import { createRule } from './tsUtils'; +import { getNodeName, scopeHasLocalReference } from './tsUtils'; -export default { +export default createRule({ + name: __filename, meta: { docs: { - url: getDocsUrl(__filename), + category: 'Best Practices', + description: 'Disallow disabled tests', + recommended: false, }, messages: { missingFunction: 'Test is missing function argument', @@ -16,7 +20,9 @@ export default { disabledTest: 'Disabled test', }, schema: [], + type: 'suggestion', }, + defaultOptions: [], create(context) { let suiteDepth = 0; let testDepth = 0; @@ -72,4 +78,4 @@ export default { }, }; }, -}; +}); From 0b40a10d9788851ae8b31b99503c6e297a5eb068 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jul 2019 10:56:53 +1200 Subject: [PATCH 12/17] chore(util): remove now unused methods --- src/rules/util.js | 38 -------------------------------------- 1 file changed, 38 deletions(-) diff --git a/src/rules/util.js b/src/rules/util.js index 4d1eaecd4..19da4db42 100644 --- a/src/rules/util.js +++ b/src/rules/util.js @@ -172,44 +172,6 @@ export const getDocsUrl = filename => { return `${REPO_URL}/blob/v${version}/docs/rules/${ruleName}.md`; }; -const collectReferences = scope => { - const locals = new Set(); - const unresolved = new Set(); - - let currentScope = scope; - - while (currentScope !== null) { - for (const ref of currentScope.variables) { - const isReferenceDefined = ref.defs.some(def => { - return def.type !== 'ImplicitGlobalVariable'; - }); - - if (isReferenceDefined) { - locals.add(ref.name); - } - } - - for (const ref of currentScope.through) { - unresolved.add(ref.identifier.name); - } - - currentScope = currentScope.upper; - } - - return { locals, unresolved }; -}; - -export const scopeHasLocalReference = (scope, referenceName) => { - const references = collectReferences(scope); - return ( - // referenceName was found as a local variable or function declaration. - references.locals.has(referenceName) || - // referenceName was not found as an unresolved reference, - // meaning it is likely not an implicit global reference. - !references.unresolved.has(referenceName) - ); -}; - export function composeFixers(node) { return (...fixers) => { return fixerApi => { From 028cc480d9986e1b0ae568efdb89b6fac5bb606c Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jul 2019 10:57:44 +1200 Subject: [PATCH 13/17] chore(no-disabled-tests): add couple of tests to improve coverage --- src/rules/__tests__/no-disabled-tests.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/rules/__tests__/no-disabled-tests.test.ts b/src/rules/__tests__/no-disabled-tests.test.ts index 7a76f078a..49fea2bbc 100644 --- a/src/rules/__tests__/no-disabled-tests.test.ts +++ b/src/rules/__tests__/no-disabled-tests.test.ts @@ -15,6 +15,7 @@ ruleTester.run('no-disabled-tests', rule, { 'it.only("foo", function () {})', 'test("foo", function () {})', 'test.only("foo", function () {})', + 'describe[`${"skip"}`]("foo", function () {})', 'var appliedSkip = describe.skip; appliedSkip.apply(describe)', 'var calledSkip = it.skip; calledSkip.call(it)', '({ f: function () {} }).f()', @@ -57,6 +58,10 @@ ruleTester.run('no-disabled-tests', rule, { code: 'describe.skip("foo", function () {})', errors: [{ messageId: 'skippedTestSuite', column: 1, line: 1 }], }, + { + code: 'describe[`skip`]("foo", function () {})', + errors: [{ messageId: 'skippedTestSuite', column: 1, line: 1 }], + }, { code: 'describe["skip"]("foo", function () {})', errors: [{ messageId: 'skippedTestSuite', column: 1, line: 1 }], From b902813385c3ce0ab9ad0f4d0ac46a918af2482d Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jul 2019 11:02:49 +1200 Subject: [PATCH 14/17] chore(no-jasmine-globals): merge if conditions to improve coverage --- src/rules/no-jasmine-globals.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/rules/no-jasmine-globals.ts b/src/rules/no-jasmine-globals.ts index f13f71f86..a0f9c7535 100644 --- a/src/rules/no-jasmine-globals.ts +++ b/src/rules/no-jasmine-globals.ts @@ -65,13 +65,12 @@ export default createRule({ return; } - if (calleeName.startsWith('jasmine.')) { + if ( + callee.type === AST_NODE_TYPES.MemberExpression && + calleeName.startsWith('jasmine.') + ) { const functionName = calleeName.replace('jasmine.', ''); - if (callee.type !== AST_NODE_TYPES.MemberExpression) { - return; - } - if ( functionName === 'any' || functionName === 'anything' || From 2fa5e1b955979f4d99fb3e935e3b3c1b110f56b9 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jul 2019 11:08:33 +1200 Subject: [PATCH 15/17] chore(no-jasmine-globals): invert if condition & add new test --- .../__tests__/no-jasmine-globals.test.ts | 4 ++++ src/rules/no-jasmine-globals.ts | 21 ++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/rules/__tests__/no-jasmine-globals.test.ts b/src/rules/__tests__/no-jasmine-globals.test.ts index 2ad3c6bbc..254c62d30 100644 --- a/src/rules/__tests__/no-jasmine-globals.test.ts +++ b/src/rules/__tests__/no-jasmine-globals.test.ts @@ -53,6 +53,10 @@ ruleTester.run('no-jasmine-globals', rule, { errors: [{ messageId: 'illegalJasmine', column: 1, line: 1 }], output: 'jest.setTimeout(5000);', }, + { + code: 'jasmine.DEFAULT_TIMEOUT_INTERVAL = function() {}', + errors: [{ messageId: 'illegalJasmine', column: 1, line: 1 }], + }, { code: 'jasmine.addMatchers(matchers)', errors: [ diff --git a/src/rules/no-jasmine-globals.ts b/src/rules/no-jasmine-globals.ts index a0f9c7535..5cc6a191d 100644 --- a/src/rules/no-jasmine-globals.ts +++ b/src/rules/no-jasmine-globals.ts @@ -128,18 +128,19 @@ export default createRule({ ) { const { right } = parent; - if (right.type !== AST_NODE_TYPES.Literal) { + if (right.type === AST_NODE_TYPES.Literal) { + context.report({ + fix: fixer => [ + fixer.replaceText( + parent, + `jest.setTimeout(${right.value})`, + ), + ], + node, + messageId: 'illegalJasmine', + }); return; } - - context.report({ - fix: fixer => [ - fixer.replaceText(parent, `jest.setTimeout(${right.value})`), - ], - node, - messageId: 'illegalJasmine', - }); - return; } context.report({ node, messageId: 'illegalJasmine' }); From 9bb8e73fcf0163034d9379935514e72aa759da4d Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jul 2019 11:12:33 +1200 Subject: [PATCH 16/17] chore(ts-utils): make parameter for `getNodeName` fn non-optional --- src/rules/tsUtils.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/rules/tsUtils.ts b/src/rules/tsUtils.ts index 0ccdf3496..f2ca82a5f 100644 --- a/src/rules/tsUtils.ts +++ b/src/rules/tsUtils.ts @@ -21,15 +21,11 @@ enum DescribeAlias { 'xdescribe', } -export const getNodeName = (node?: TSESTree.Node): string | null => { +export const getNodeName = (node: TSESTree.Node): string | null => { function joinNames(a?: string | null, b?: string | null): string | null { return a && b ? `${a}.${b}` : null; } - if (!node) { - return null; - } - switch (node.type) { case 'Identifier': return node.name; From 4dbc7d4973c927e92f9983a9133fc04e5b3be1a2 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jul 2019 11:18:42 +1200 Subject: [PATCH 17/17] chore(ts-utils): use `AST_NODE_TYPES` enum for comparing against `type` --- src/rules/tsUtils.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/rules/tsUtils.ts b/src/rules/tsUtils.ts index f2ca82a5f..73d9af889 100644 --- a/src/rules/tsUtils.ts +++ b/src/rules/tsUtils.ts @@ -27,14 +27,14 @@ export const getNodeName = (node: TSESTree.Node): string | null => { } switch (node.type) { - case 'Identifier': + case AST_NODE_TYPES.Identifier: return node.name; - case 'Literal': + case AST_NODE_TYPES.Literal: return `${node.value}`; - case 'TemplateLiteral': + case AST_NODE_TYPES.TemplateLiteral: if (node.expressions.length === 0) return node.quasis[0].value.cooked; break; - case 'MemberExpression': + case AST_NODE_TYPES.MemberExpression: return joinNames(getNodeName(node.object), getNodeName(node.property)); }