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

chore(root): Prevent multi-level imports for @novu/* packages #6596

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Logger } from '@nestjs/common';
import sinon from 'sinon';
import { expect } from 'chai';
import { ApiServiceLevelEnum } from '@novu/shared';
// eslint-disable-next-line no-restricted-imports
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to raise an EE PR for these, so they are ignored for now.

import { StripeBillingIntervalEnum, StripeUsageTypeEnum } from '@novu/ee-billing/src/stripe/types';

const mockMonthlyBusinessSubscription = {
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/app/testing/billing/get-prices.e2e-ee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sinon from 'sinon';
import { expect } from 'chai';
import { ApiServiceLevelEnum } from '@novu/shared';
// eslint-disable-next-line no-restricted-imports
import { StripeBillingIntervalEnum } from '@novu/ee-billing/src/stripe/types';

describe('GetPrices', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sinon from 'sinon';
import { expect } from 'chai';
import { ApiServiceLevelEnum } from '@novu/shared';
// eslint-disable-next-line no-restricted-imports
import { StripeBillingIntervalEnum } from '@novu/ee-billing/src/stripe/types';

describe('Upsert setup intent', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import sinon from 'sinon';
import { CommunityOrganizationRepository, IntegrationRepository } from '@novu/dal';
import { expect } from 'chai';
import { ApiServiceLevelEnum } from '@novu/shared';
// eslint-disable-next-line no-restricted-imports
import { StripeBillingIntervalEnum, StripeUsageTypeEnum } from '@novu/ee-billing/src/stripe/types';

describe('UpsertSubscription', () => {
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/app/testing/billing/webhook.e2e-ee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sinon from 'sinon';
import { expect } from 'chai';
import { ApiServiceLevelEnum } from '@novu/shared';
// eslint-disable-next-line no-restricted-imports
import { StripeBillingIntervalEnum } from '@novu/ee-billing/src/stripe/types';
import { GetFeatureFlag } from '@novu/application-generic';

Expand Down
3 changes: 1 addition & 2 deletions apps/web/src/components/layout/components/ListPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import styled from '@emotion/styled';
import { IPaginationProps, PageContainer, Pagination, colors } from '@novu/design-system';
import { ITableProps } from '@novu/design-system/dist/types/table/Table';
import { IPaginationProps, PageContainer, Pagination, colors, ITableProps } from '@novu/design-system';
import { ComponentProps, PropsWithChildren } from 'react';
import PageHeader from './PageHeader';

Expand Down
28 changes: 22 additions & 6 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@

const compat = new FlatCompat({ baseDirectory: import.meta.dirname });

/**
* REUSED RULE CONFIGURATIONS
*
* This is necessary because Eslint doesn't merge rule configurations
* when they are targeting different paths.
*/

/**
* This rule ensures that "multi-level" imports are not used for `@novu/*` packages.
Copy link
Contributor

@djabarovgeorge djabarovgeorge Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should approach this from a different direction, specifically by not allowing the @novu domain libraries to have the /src suffix. @novu/**/src For example: @novu/framework/src, @novu/dal/src, @novu/ee/3rd-party/something/src, @novu/novui/src. I believe that, for now, this approach will be more beneficial and won't limit us in potential future implementations.
let me know what you think :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For packages under our control, I'm of the opinion that we should only allow imports from known exports values in the package.json. This benefits us by:

  • keeping module imports clean
  • creating a clear module boundary
  • ensuring that public package consumers can import the same things that we do, allowing us to dogfood

This PR introduces a pattern for exceptions of the rule, namely for multiple entry-point packages. That same pattern can be extended to handle other exceptions we might like to add in the future. I think the benefits of a "by exception" approach outweigh the cons of allowing any multi-level imports right now.

*/
const noRestrictedImportsMultiLevelNovuPattern = {
group: [
'@novu/*/**/*',
// These packages have legitimate exports 1 path part below the root level
// This flatmap logic ignores the path 1 below the root level and prevents deeper imports.

Check warning on line 46 in eslint.config.mjs

View workflow job for this annotation

GitHub Actions / Spell check

Unknown word (flatmap)
rifont marked this conversation as resolved.
Show resolved Hide resolved
...['framework', 'js', 'novui'].flatMap((pkg) => [`!@novu/${pkg}/**/*`, `@novu/${pkg}/*/**/*`]),
],
message: "Please import only from the root package entry point. For example, use 'import { Client } from '@novu/node';' instead of 'import { Client } from '@novu/node/src';'",
};

export default tsEslint.config(
/* ******************** RECOMMENDED CONFIG ******************** */
jsEslint.configs.recommended,
Expand Down Expand Up @@ -155,12 +175,7 @@
'error',
{
patterns: [
'@novu/shared/*',
'!@novu/shared/utils',
'@novu/dal/*',
'*../libs/dal/*',
'*../packages/shared/*',
'*../libs/stateless/*',
noRestrictedImportsMultiLevelNovuPattern,
],
},
],
Expand Down Expand Up @@ -275,6 +290,7 @@
'error',
{
patterns: [
noRestrictedImportsMultiLevelNovuPattern,
{
/**
* This rule ensures that the overridden Swagger decorators are used,
Expand Down
2 changes: 1 addition & 1 deletion libs/design-system/src/table/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { Table } from './Table';
export type { IExtendedColumn, IExtendedCellProps, IRow } from './Table';
export type { IExtendedColumn, IExtendedCellProps, IRow, ITableProps } from './Table';
export { withCellLoading } from './withCellLoading';
2 changes: 1 addition & 1 deletion packages/node/src/lib/novu.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios, { AxiosInstance } from 'axios';
import { getEnvVariable } from '@novu/shared/utils';
import { getEnvVariable } from '@novu/shared';
import { EventEmitter } from 'events';
import { Subscribers } from './subscribers/subscribers';
import { Changes } from './changes/changes';
Expand Down
1 change: 1 addition & 0 deletions packages/notification-center-vue/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { App } from 'vue';
import FloatingVue from '@novu/floating-vue';
// eslint-disable-next-line no-restricted-imports
import '@novu/floating-vue/dist/style.css';
import { NotificationCenterContentWebComponent } from '@novu/notification-center';

Expand Down
Loading