-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(no-node-access): use new testing library rule maker (#237)
* build: add npmrc file Adding .npmrc file to indicate we don't want to generate package-lock properly. * refactor: first approach for testing library detection * refactor: move testing library detection to high-order function * refactor: include create-testing-library-rule * refactor(no-node-access): use create-testing-library-rule * test: decrease coverage threshold for utils detection * test: decrease coverage threshold for utils detection branches * style: add missing return type on function * style: format with prettier properly Apparently the regexp for formatting the files within npm command must be passed with double quotes. More details here: https://dev.to/gruckion/comment/b665 * docs: copied types clarification
- Loading branch information
Showing
11 changed files
with
157 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ESLintUtils, TSESLint } from '@typescript-eslint/experimental-utils'; | ||
import { getDocsUrl } from './utils'; | ||
import { | ||
detectTestingLibraryUtils, | ||
DetectionHelpers, | ||
} from './detect-testing-library-utils'; | ||
|
||
// These 2 types are copied from @typescript-eslint/experimental-utils | ||
type CreateRuleMetaDocs = Omit<TSESLint.RuleMetaDataDocs, 'url'>; | ||
type CreateRuleMeta<TMessageIds extends string> = { | ||
docs: CreateRuleMetaDocs; | ||
} & Omit<TSESLint.RuleMetaData<TMessageIds>, 'docs'>; | ||
|
||
export function createTestingLibraryRule< | ||
TOptions extends readonly unknown[], | ||
TMessageIds extends string, | ||
TRuleListener extends TSESLint.RuleListener = TSESLint.RuleListener | ||
>( | ||
config: Readonly<{ | ||
name: string; | ||
meta: CreateRuleMeta<TMessageIds>; | ||
defaultOptions: Readonly<TOptions>; | ||
create: ( | ||
context: Readonly<TSESLint.RuleContext<TMessageIds, TOptions>>, | ||
optionsWithDefault: Readonly<TOptions>, | ||
detectionHelpers: Readonly<DetectionHelpers> | ||
) => TRuleListener; | ||
}> | ||
): TSESLint.RuleModule<TMessageIds, TOptions, TRuleListener> { | ||
const { create, ...remainingConfig } = config; | ||
|
||
return ESLintUtils.RuleCreator(getDocsUrl)({ | ||
...remainingConfig, | ||
create: detectTestingLibraryUtils<TOptions, TMessageIds, TRuleListener>( | ||
create | ||
), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils'; | ||
|
||
export type DetectionHelpers = { | ||
getIsImportingTestingLibrary: () => boolean; | ||
}; | ||
|
||
/** | ||
* Enhances a given rule `create` with helpers to detect Testing Library utils. | ||
*/ | ||
export function detectTestingLibraryUtils< | ||
TOptions extends readonly unknown[], | ||
TMessageIds extends string, | ||
TRuleListener extends TSESLint.RuleListener = TSESLint.RuleListener | ||
>( | ||
ruleCreate: ( | ||
context: Readonly<TSESLint.RuleContext<TMessageIds, TOptions>>, | ||
optionsWithDefault: Readonly<TOptions>, | ||
detectionHelpers: Readonly<DetectionHelpers> | ||
) => TRuleListener | ||
) { | ||
return ( | ||
context: Readonly<TSESLint.RuleContext<TMessageIds, TOptions>>, | ||
optionsWithDefault: Readonly<TOptions> | ||
): TRuleListener => { | ||
let isImportingTestingLibrary = false; | ||
|
||
// TODO: init here options based on shared ESLint config | ||
|
||
// helpers for Testing Library detection | ||
const helpers: DetectionHelpers = { | ||
getIsImportingTestingLibrary() { | ||
return isImportingTestingLibrary; | ||
}, | ||
}; | ||
|
||
// instructions for Testing Library detection | ||
const detectionInstructions: TSESLint.RuleListener = { | ||
ImportDeclaration(node: TSESTree.ImportDeclaration) { | ||
isImportingTestingLibrary = /testing-library/g.test( | ||
node.source.value as string | ||
); | ||
}, | ||
}; | ||
|
||
// update given rule to inject Testing Library detection | ||
const ruleInstructions = ruleCreate(context, optionsWithDefault, helpers); | ||
const enhancedRuleInstructions = Object.assign({}, ruleInstructions); | ||
|
||
Object.keys(detectionInstructions).forEach((instruction) => { | ||
(enhancedRuleInstructions as TSESLint.RuleListener)[instruction] = ( | ||
node | ||
) => { | ||
if (instruction in detectionInstructions) { | ||
detectionInstructions[instruction](node); | ||
} | ||
|
||
if (ruleInstructions[instruction]) { | ||
return ruleInstructions[instruction](node); | ||
} | ||
}; | ||
}); | ||
|
||
return enhancedRuleInstructions; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.