Skip to content

Commit

Permalink
Merge pull request #198 from epaew/bugfix/named-export_in_TSModuleBlock
Browse files Browse the repository at this point in the history
Bugfix rules/named-export: False positive detection of ExportNamedDeclaration in TSModuleBlock
  • Loading branch information
epaew authored Nov 22, 2020
2 parents 3278dab + 2c36015 commit 08aabda
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Unreleased
## Features
## Bugfixes
* [#198](https://github.com/epaew/eslint-plugin-filenames-simple/pull/198)
False positive detection of ExportNamedDeclaration in TSModuleBlock. (Mixed in [#196](https://github.com/epaew/eslint-plugin-filenames-simple/pull/196))

## Others
* [#196](https://github.com/epaew/eslint-plugin-filenames-simple/pull/196)
Refactor `rules/named-export` to use `context.getDeclaredVariables()`.
Expand Down
14 changes: 14 additions & 0 deletions __tests__/rules/named-export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,20 @@ describe('rules/named-export', () => {
},
],
});

ruleTester.run('single named export in module declaration', namedExport, {
valid: [
{
code: `
declare module 'espree' {
export function parse(code: string, options?: any): Node;
}
`,
filename: 'espree.d.ts',
},
],
invalid: [],
});
});
});
});
8 changes: 8 additions & 0 deletions docs/rules/named-export.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,13 @@ Specify one of the following as the file naming convention to which this rule ap
export type ClassExpression = { id: Identifier }
```

#### NOTE: This rule skips the detection of named exports in TypeScript module blocks.
* @types/espree.d.ts
```typescript
declare module 'espree' {
export function parse(code: string, options?: any): Node;
}
```

## See also
* [settings/pluralize](../settings/pluralize.md)
18 changes: 11 additions & 7 deletions src/rules/named-export.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import 'core-js/features/array/flat-map';

import path from 'path';

import { TSESTree } from '@typescript-eslint/typescript-estree';
import { Rule } from 'eslint';
import { ExportNamedDeclaration, Identifier } from 'estree';

import { presetRules } from '../utils/preset-rules';
import { Pluralize } from '../utils/pluralize';
import { presetRules } from '../utils/preset-rules';

type PluralizeRule = 'always' | 'singular' | 'plural';

class AloneNamedExportDetector {
class AloneExportNamedIdentifierDetector {
#context: Pick<Rule.RuleContext, 'getDeclaredVariables'>;
#exportedIdentifiers = new Set<Identifier>();
#isExportAllDetected = false;
Expand All @@ -19,14 +21,16 @@ class AloneNamedExportDetector {
this.#context = context;
}

get aloneNamedExport(): Identifier | void {
get aloneExportNamedIdentifier(): Identifier | void {
if (this.#isExportAllDetected || this.#isExportDefaultDetected) return;
if (this.#exportedIdentifiers.size !== 1) return;

return [...this.#exportedIdentifiers][0];
}

detectNamedExportDeclaration(node: ExportNamedDeclaration): void {
detectExportNamedDeclaration(node: ExportNamedDeclaration): void {
if ((node as TSESTree.ExportNamedDeclaration).parent?.type === 'TSModuleBlock') return;

if (node.declaration) {
/*
* NOTE: https://eslint.org/docs/developer-guide/working-with-rules#the-context-object
Expand Down Expand Up @@ -74,16 +78,16 @@ export const namedExport: Rule.RuleModule = {
const rule: PluralizeRule = context.options[0] ?? 'always';

const filename = fetchFilename(context);
const detector = new AloneNamedExportDetector(context);
const detector = new AloneExportNamedIdentifierDetector(context);

return {
ExportAllDeclaration: () => detector.detectExportAllDeclaration(),
ExportDefaultDeclaration: () => detector.detectExportDefaultDeclaration(),
ExportNamedDeclaration: node => detector.detectNamedExportDeclaration(node),
ExportNamedDeclaration: node => detector.detectExportNamedDeclaration(node),
'Program:exit': () => {
if (!(rule === 'always' || pluralize.isValidName(filename, rule))) return;

const exportedIdentifier = detector.aloneNamedExport;
const exportedIdentifier = detector.aloneExportNamedIdentifier;
if (!exportedIdentifier) return;
if (isSameName(exportedIdentifier.name, filename)) return;

Expand Down

0 comments on commit 08aabda

Please sign in to comment.