|
| 1 | +import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; |
| 2 | +import { Tree, readProjectConfiguration, joinPathFragments } from '@nx/devkit'; |
| 3 | +import { lintWorkspaceRulesProjectGenerator } from '@nx/eslint/src/generators/workspace-rules-project/workspace-rules-project'; |
| 4 | + |
| 5 | +import { eslintRuleGenerator } from './generator'; |
| 6 | +import { EslintRuleGeneratorSchema } from './schema'; |
| 7 | + |
| 8 | +describe('eslint-rule generator', () => { |
| 9 | + // eslint-disable-next-line @typescript-eslint/no-empty-function |
| 10 | + const noop = () => {}; |
| 11 | + let tree: Tree; |
| 12 | + const options: EslintRuleGeneratorSchema = { name: 'uppercase' }; |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + jest.spyOn(console, 'info').mockImplementation(noop); |
| 16 | + tree = createTreeWithEmptyWorkspace(); |
| 17 | + await lintWorkspaceRulesProjectGenerator(tree, {}); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should generate new eslint rule', async () => { |
| 21 | + const config = readProjectConfiguration(tree, 'eslint-rules'); |
| 22 | + const paths = { |
| 23 | + impl: joinPathFragments(config.root, 'rules/uppercase.ts'), |
| 24 | + spec: joinPathFragments(config.root, 'rules/uppercase.spec.ts'), |
| 25 | + }; |
| 26 | + |
| 27 | + await eslintRuleGenerator(tree, options); |
| 28 | + |
| 29 | + expect(tree.read(paths.impl, 'utf-8')).toMatchInlineSnapshot(` |
| 30 | + "/** |
| 31 | + * This file sets you up with structure needed for an ESLint rule. |
| 32 | + * |
| 33 | + * It leverages utilities from @typescript-eslint to allow TypeScript to |
| 34 | + * provide autocompletions etc for the configuration. |
| 35 | + * |
| 36 | + * Your rule's custom logic will live within the create() method below |
| 37 | + * and you can learn more about writing ESLint rules on the official guide: |
| 38 | + * |
| 39 | + * https://eslint.org/docs/developer-guide/working-with-rules |
| 40 | + * |
| 41 | + * You can also view many examples of existing rules here: |
| 42 | + * |
| 43 | + * https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin/src/rules |
| 44 | + */ |
| 45 | + import { ESLintUtils } from '@typescript-eslint/experimental-utils'; |
| 46 | + // NOTE: The rule will be available in ESLint configs as \\"@nx/workspace-uppercase\\" |
| 47 | + export const RULE_NAME = 'uppercase'; |
| 48 | + export const rule = ESLintUtils.RuleCreator(() => __filename)({ |
| 49 | + name: RULE_NAME, |
| 50 | + meta: { |
| 51 | + type: 'problem', |
| 52 | + docs: { |
| 53 | + category: 'Best Practices', |
| 54 | + description: \`\`, |
| 55 | + recommended: 'error', |
| 56 | + }, |
| 57 | + schema: [], |
| 58 | + messages: {}, |
| 59 | + }, |
| 60 | + defaultOptions: [], |
| 61 | + create(context) { |
| 62 | + return {}; |
| 63 | + }, |
| 64 | + }); |
| 65 | + " |
| 66 | + `); |
| 67 | + |
| 68 | + expect(tree.read(paths.spec, 'utf-8')).toMatchInlineSnapshot(` |
| 69 | + "import { TSESLint } from '@typescript-eslint/experimental-utils'; |
| 70 | + import { rule, RULE_NAME } from './uppercase'; |
| 71 | + const ruleTester = new TSESLint.RuleTester({ |
| 72 | + parser: require.resolve('@typescript-eslint/parser'), |
| 73 | + }); |
| 74 | + ruleTester.run(RULE_NAME, rule, { |
| 75 | + valid: [\`const example = true;\`], |
| 76 | + invalid: [], |
| 77 | + }); |
| 78 | + " |
| 79 | + `); |
| 80 | + }); |
| 81 | +}); |
0 commit comments