Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[no-namespace] Allow namespaces in module and the global namespace augmentation #2237

Closed
cherryblossom000 opened this issue Jun 22, 2020 · 3 comments · Fixed by #2238
Closed
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@cherryblossom000
Copy link
Contributor

Repro

{
  "rules": {
    "@typescript-eslint/no-namespace": [2, {"allowDeclarations": true}]
  }
}
declare module 'fs' {
  namespace SomeNamespace {}
}

declare global {
  namespace Intl {}
}
export {}

Expected Result
No error because the namespaces are being declared in a module augmentation or in the global namespace. declare namespace can't be used because it's already in an ambient context.

Actual Result

/path/to/file.ts
  2:3  error  ES2015 module syntax is preferred over custom TypeScript modules and namespaces  @typescript-eslint/no-namespace
  6:3  error  ES2015 module syntax is preferred over custom TypeScript modules and namespaces  @typescript-eslint/no-namespace

✖ 2 problems (2 errors, 0 warnings)

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 3.3.0
@typescript-eslint/parser 3.3.0
TypeScript 3.9.5
ESLint 7.3.0
node 12.18.0
npm 6.14.5
@cherryblossom000 cherryblossom000 added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Jun 22, 2020
@bradzacher
Copy link
Member

happy to accept a pr!

@bradzacher bradzacher added enhancement: plugin rule option New rule option for an existing eslint-plugin rule and removed triage Waiting for maintainers to take a look labels Jun 22, 2020
@rrrix
Copy link

rrrix commented Jun 25, 2020

Would this also be applicable to how Jest recommends extending expect with custom matchers?

https://jestjs.io/docs/en/expect#expectextendmatchers

declare global {
  namespace jest {
    interface Matchers<R> {
      toBeWithinRange(a: number, b: number): R;
    }
  }
}
expect.extend({
  toBeWithinRange(received, floor, ceiling) {
    const pass = received >= floor && received <= ceiling;
    if (pass) {
      return {
        message: () =>
          `expected ${received} not to be within range ${floor} - ${ceiling}`,
        pass: true,
      };
    } else {
      return {
        message: () =>
          `expected ${received} to be within range ${floor} - ${ceiling}`,
        pass: false,
      };
    }
  },
});

test('numeric ranges', () => {
  expect(100).toBeWithinRange(90, 110);
  expect(101).not.toBeWithinRange(0, 100);
  expect({apples: 6, bananas: 3}).toEqual({
    apples: expect.toBeWithinRange(1, 10),
    bananas: expect.not.toBeWithinRange(11, 20),
  });
});

Today this gives me an error with ESLint: ES2015 module syntax is preferred over custom TypeScript modules and namespaces.(@typescript-eslint/no-namespace).

I'm using all the defaults:

  plugins: ['@typescript-eslint'],
  extends: [
    'plugin:@typescript-eslint/recommended',
  ],

Is there a right / better/ correct way to do this with Jest?

@cherryblossom000
Copy link
Contributor Author

@rrrix That was actually one of the reasons why I opened this issue. I don't think there is another way of adding types for custom Jest matchers, so just use // eslint-disable-next-line @typescript-eslint/no-namespace for now until #2238 is merged.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants