From eb794f3f85daf22dc284729e490e1e4d4c4ca3bf Mon Sep 17 00:00:00 2001 From: "Xin Du (Clark)" Date: Sat, 2 Oct 2021 19:40:10 +0100 Subject: [PATCH] chore(eslint): forbid modules importing from core (#556) Co-authored-by: Pooya Parsa --- .eslintrc | 24 +++++++++++++++++++++-- packages/kit/src/types/hooks.ts | 3 +-- packages/nitro/src/context.ts | 1 + packages/nuxi/src/commands/info.ts | 2 +- packages/nuxt3/src/app/entry.ts | 6 +++--- packages/nuxt3/src/app/nuxt.ts | 2 +- packages/nuxt3/src/meta/runtime/plugin.ts | 1 + 7 files changed, 30 insertions(+), 9 deletions(-) diff --git a/.eslintrc b/.eslintrc index 36f4a83e78..d1e1d84994 100644 --- a/.eslintrc +++ b/.eslintrc @@ -6,7 +6,8 @@ "plugins": ["jsdoc"], "extends": [ "plugin:jsdoc/recommended", - "@nuxtjs/eslint-config-typescript" + "@nuxtjs/eslint-config-typescript", + "plugin:import/typescript" ], "rules": { "no-console": "off", @@ -15,7 +16,26 @@ "jsdoc/require-jsdoc": "off", "jsdoc/require-param": "off", "jsdoc/require-returns": "off", - "jsdoc/require-param-type": "off" + "jsdoc/require-param-type": "off", + "import/no-restricted-paths": ["error", { + "zones": [ + { + "target": "packages/nuxt3/src/!(core)/**/*", + "from": "packages/nuxt3/src/core", + "message": "core should not directly import from modules." + }, + { + "target": "packages/nuxt3/src/!(app)/**/*", + "from": "packages/nuxt3/src/app", + "message": "app should not directly import from modules." + }, + { + "target": "packages/nitro", + "from": "packages/!(nitro)/**/*", + "message": "nitro should not directly import other packages." + } + ] + }] }, "settings": { "jsdoc": { diff --git a/packages/kit/src/types/hooks.ts b/packages/kit/src/types/hooks.ts index a1fb625d61..98621cf226 100644 --- a/packages/kit/src/types/hooks.ts +++ b/packages/kit/src/types/hooks.ts @@ -3,8 +3,7 @@ import type { HookCallback } from 'hookable' import type { Compiler, Configuration, Stats } from 'webpack' import type { NuxtConfig, NuxtOptions } from '..' import type { ModuleContainer } from '../module/container' -import { NuxtTemplate } from '../types/nuxt' -import { Nuxt, NuxtApp } from './nuxt' +import type { NuxtTemplate, Nuxt, NuxtApp } from '../types/nuxt' type HookResult = Promise | void diff --git a/packages/nitro/src/context.ts b/packages/nitro/src/context.ts index 047b128153..f9e51c89e8 100644 --- a/packages/nitro/src/context.ts +++ b/packages/nitro/src/context.ts @@ -142,6 +142,7 @@ export function getNitroContext (nuxtOptions: NuxtOptions, input: NitroInput): N } defaults.preset = input.preset || process.env.NITRO_PRESET || detectTarget() || 'server' + // eslint-disable-next-line import/namespace let presetDefaults = PRESETS[defaults.preset] || tryImport(nuxtOptions.rootDir, defaults.preset) if (!presetDefaults) { throw new Error('Cannot resolve preset: ' + defaults.preset) diff --git a/packages/nuxi/src/commands/info.ts b/packages/nuxi/src/commands/info.ts index 7cb2bb686b..a2c2e1098a 100644 --- a/packages/nuxi/src/commands/info.ts +++ b/packages/nuxi/src/commands/info.ts @@ -5,7 +5,7 @@ import { resolve, dirname } from 'pathe' import jiti from 'jiti' import destr from 'destr' import { splitByCase } from 'scule' -import clipboardy from 'clipboardy' +import * as clipboardy from 'clipboardy' import { defineNuxtCommand } from './index' export default defineNuxtCommand({ diff --git a/packages/nuxt3/src/app/entry.ts b/packages/nuxt3/src/app/entry.ts index 2b1e5106a0..4154705def 100644 --- a/packages/nuxt3/src/app/entry.ts +++ b/packages/nuxt3/src/app/entry.ts @@ -1,5 +1,5 @@ import { createSSRApp, createApp, nextTick } from 'vue' -import { createNuxt, applyPlugins, normalizePlugins, CreateOptions } from '#app' +import { createNuxtApp, applyPlugins, normalizePlugins, CreateOptions } from '#app' import '#build/css' // @ts-ignore import _plugins from '#build/plugins' @@ -14,7 +14,7 @@ if (process.server) { entry = async function createNuxtAppServer (ssrContext: CreateOptions['ssrContext'] = {}) { const app = createApp(App) - const nuxt = createNuxt({ app, ssrContext }) + const nuxt = createNuxtApp({ app, ssrContext }) await applyPlugins(nuxt, plugins) @@ -37,7 +37,7 @@ if (process.client) { const isSSR = Boolean(window.__NUXT__?.serverRendered) const app = isSSR ? createSSRApp(App) : createApp(App) - const nuxt = createNuxt({ app }) + const nuxt = createNuxtApp({ app }) await applyPlugins(nuxt, plugins) diff --git a/packages/nuxt3/src/app/nuxt.ts b/packages/nuxt3/src/app/nuxt.ts index 00e62c5db0..06e9e1830e 100644 --- a/packages/nuxt3/src/app/nuxt.ts +++ b/packages/nuxt3/src/app/nuxt.ts @@ -64,7 +64,7 @@ export interface CreateOptions { globalName?: NuxtApp['globalName'] } -export function createNuxt (options: CreateOptions) { +export function createNuxtApp (options: CreateOptions) { const nuxt: NuxtApp = { provide: undefined, globalName: 'nuxt', diff --git a/packages/nuxt3/src/meta/runtime/plugin.ts b/packages/nuxt3/src/meta/runtime/plugin.ts index 057c875c15..bebdc01403 100644 --- a/packages/nuxt3/src/meta/runtime/plugin.ts +++ b/packages/nuxt3/src/meta/runtime/plugin.ts @@ -18,6 +18,7 @@ export default defineNuxtPlugin((nuxt) => { }) for (const name in Components) { + // eslint-disable-next-line import/namespace nuxt.app.component(name, Components[name]) } })