From b52e17c6a23459ac31e2f969d97b3148937f61bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Mino?= Date: Sun, 15 Sep 2024 10:11:50 +0200 Subject: [PATCH] feat(vue): patch eslint config file from typescript module rather than overwriting it --- .../vue/core/domain/VueModulesFactory.java | 33 +++++++++++++++- .../client/vue/eslint.config.js.mustache | 39 ------------------- .../core/domain/VueModulesFactoryTest.java | 24 +++++++++++- .../secondary/JHipsterModulesAssertions.java | 4 ++ 4 files changed, 59 insertions(+), 41 deletions(-) delete mode 100644 src/main/resources/generator/client/vue/eslint.config.js.mustache diff --git a/src/main/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactory.java b/src/main/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactory.java index da8c26dabe5..39055c1524c 100644 --- a/src/main/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactory.java @@ -69,7 +69,6 @@ public JHipsterModule buildVueModule(JHipsterModuleProperties properties) { .addScript(scriptKey("watch:tsc"), scriptCommand("npm run build:tsc -- --watch")) .and() .files() - .add(SOURCE.template("eslint.config.js.mustache"), to("eslint.config.js")) .add(SOURCE.file("tsconfig.build.json"), to("tsconfig.build.json")) .batch(SOURCE, to(".")) .addTemplate("vite.config.ts") @@ -97,12 +96,44 @@ public JHipsterModule buildVueModule(JHipsterModuleProperties properties) { .add(APP_SOURCE.template("test/webapp/unit/shared/http/infrastructure/secondary/AxiosStub.ts.mustache"), TEST_DESTINATION.append("unit/shared/http/infrastructure/secondary/AxiosStub.ts")) .add(APP_SOURCE.template("test/webapp/unit/router/infrastructure/primary/HomeRouter.spec.ts.mustache"), TEST_DESTINATION.append("unit/router/infrastructure/primary/HomeRouter.spec.ts")) .and() + .apply(patchEslintConfig(properties)) .apply(patchTsConfig(properties)) .apply(patchVitestConfig(properties)) .build(); //@formatter:on } + private Consumer patchEslintConfig(JHipsterModuleProperties properties) { + String vuePluginConfig = + """ + \t...vue.configs['flat/recommended'], + \t{ + \t\tfiles: ['**/*.vue'], + \t\tlanguageOptions: { + \t\t\tparserOptions: { parser: '@typescript-eslint/parser' }, + \t\t\tglobals: { ...globals.browser }, + \t\t}, + \t},\ + """.replace("\t", properties.indentation().spaces()); + //@formatter:off + return moduleBuilder -> moduleBuilder + .mandatoryReplacements() + .in(path("eslint.config.js")) + .add(lineAfterRegex("from 'typescript-eslint'"), "import vue from 'eslint-plugin-vue';") + .add(lineAfterRegex("...typescript.configs.recommended"), vuePluginConfig) + .add(text("files: ['src/*/webapp/**/*.ts']"), "files: ['src/*/webapp/**/*.vue', 'src/*/webapp/**/*.ts']") + .add(eslintTypescriptVueRule("'vue/html-self-closing': 'off',", properties.indentation())) + .add(eslintTypescriptVueRule("'@typescript-eslint/no-explicit-any': 'off',", properties.indentation())) + .add(eslintTypescriptVueRule("'@typescript-eslint/no-empty-object-type': 'off',", properties.indentation())) + .and() + .and(); + //@formatter:on + } + + private static MandatoryReplacer eslintTypescriptVueRule(String rule, Indentation indentation) { + return new MandatoryReplacer(lineAfterRegex("quotes: \\['error', 'single'"), indentation.times(3) + rule); + } + private Consumer patchTsConfig(JHipsterModuleProperties properties) { //@formatter:off return moduleBuilder -> moduleBuilder diff --git a/src/main/resources/generator/client/vue/eslint.config.js.mustache b/src/main/resources/generator/client/vue/eslint.config.js.mustache deleted file mode 100644 index bba52802447..00000000000 --- a/src/main/resources/generator/client/vue/eslint.config.js.mustache +++ /dev/null @@ -1,39 +0,0 @@ -import globals from 'globals'; -import typescript from 'typescript-eslint'; -import js from '@eslint/js'; -import vue from 'eslint-plugin-vue'; - -export default typescript.config( - { - languageOptions: { - globals: { - ...globals.node, - }, - }, - }, - { - ignores: ['{{projectBuildDirectory}}/'], - }, - js.configs.recommended, - ...typescript.configs.recommended.map(config => (config.name === 'typescript-eslint/base' ? config : { ...config, files: ['**/*.ts'] })), - ...vue.configs['flat/recommended'], - { - files: ['**/*.vue'], - languageOptions: { - parserOptions: { parser: '@typescript-eslint/parser' }, - globals: { ...globals.browser }, - }, - }, - { - files: ['src/*/webapp/**/*.vue', 'src/*/webapp/**/*.ts'], - languageOptions: { - globals: { ...globals.browser }, - }, - rules: { - quotes: ['error', 'single', { avoidEscape: true }], - '@typescript-eslint/no-explicit-any': 'off', - 'vue/html-self-closing': 'off', - '@typescript-eslint/no-empty-object-type': 'off', - }, - }, -); diff --git a/src/test/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactoryTest.java index 1652e354b26..4aa78c54eff 100644 --- a/src/test/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactoryTest.java +++ b/src/test/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactoryTest.java @@ -24,7 +24,7 @@ void shouldCreateVueModule() { JHipsterModule module = factory.buildVueModule(properties); //@formatter:off - assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFile(), tsConfigFile(), vitestConfigFile()) + assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFile(), tsConfigFile(), vitestConfigFile(), eslintConfigFile()) .hasFiles("documentation/vue.md") .hasFile("package.json") .notContaining(nodeDependency("@tsconfig/recommended")) @@ -81,6 +81,28 @@ void shouldCreateVueModule() { """ ) .and() + .hasFile("eslint.config.js") + .containing("import vue from 'eslint-plugin-vue';") + .containing(""" + ...vue.configs['flat/recommended'], + { + files: ['**/*.vue'], + languageOptions: { + parserOptions: { parser: '@typescript-eslint/parser' }, + globals: { ...globals.browser }, + }, + }, + """ + ) + .containing(""" + rules: { + quotes: ['error', 'single', { avoidEscape: true }], + '@typescript-eslint/no-empty-object-type': 'off', + '@typescript-eslint/no-explicit-any': 'off', + 'vue/html-self-closing': 'off', + """ + ) + .and() .hasFiles("src/main/webapp/app/shared/http/infrastructure/secondary/AxiosHttp.ts") .hasFiles("src/main/webapp/index.html") .hasPrefixedFiles("src/main/webapp/app", "env.d.ts", "AppVue.vue", "injections.ts", "router.ts", "main.ts") diff --git a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/JHipsterModulesAssertions.java b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/JHipsterModulesAssertions.java index 7775a882e63..cb33baf5c79 100644 --- a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/JHipsterModulesAssertions.java +++ b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/JHipsterModulesAssertions.java @@ -68,6 +68,10 @@ public static ModuleFile vitestConfigFile() { return file("src/main/resources/generator/typescript/vitest.config.ts.mustache", "vitest.config.ts"); } + public static ModuleFile eslintConfigFile() { + return file("src/main/resources/generator/typescript/eslint.config.js.mustache", "eslint.config.js"); + } + public static ModuleFile emptyLintStagedConfigFile() { return file("src/test/resources/projects/init/.lintstagedrc.empty.cjs", ".lintstagedrc.cjs"); }