From f9a0b7893a76f929aebf68893a097aea30b823ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E6=8C=AF=E5=AE=87?= <> Date: Fri, 7 Feb 2025 17:31:27 +0800 Subject: [PATCH] feat(lint): enhance ESLint integration by adding version and plugin support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 孙振宇 <> --- .../devops/builtins/lint/eslint/eslintrc.js | 3 ++- .../freeleaps/devops/CodeLintExecutor.groovy | 9 +++++++-- .../com/freeleaps/devops/lint/ESLint.groovy | 20 ++++++++----------- first-class-pipeline/tests/Jenkinsfile | 4 ++++ .../vars/executeFreeleapsPipeline.groovy | 2 +- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/first-class-pipeline/resources/com/freeleaps/devops/builtins/lint/eslint/eslintrc.js b/first-class-pipeline/resources/com/freeleaps/devops/builtins/lint/eslint/eslintrc.js index efcb2d2b..e305e3c8 100644 --- a/first-class-pipeline/resources/com/freeleaps/devops/builtins/lint/eslint/eslintrc.js +++ b/first-class-pipeline/resources/com/freeleaps/devops/builtins/lint/eslint/eslintrc.js @@ -1,4 +1,5 @@ module.exports = { + root: true, parser: "@typescript-eslint/parser", parserOptions: { ecmaVersion: 6, @@ -14,4 +15,4 @@ module.exports = { semi: "off", }, ignorePatterns: ["out", "dist", "**/*.d.ts"], -}; +}; \ No newline at end of file diff --git a/first-class-pipeline/src/com/freeleaps/devops/CodeLintExecutor.groovy b/first-class-pipeline/src/com/freeleaps/devops/CodeLintExecutor.groovy index 1ec40c1f..a7bea1f9 100644 --- a/first-class-pipeline/src/com/freeleaps/devops/CodeLintExecutor.groovy +++ b/first-class-pipeline/src/com/freeleaps/devops/CodeLintExecutor.groovy @@ -10,8 +10,9 @@ class CodeLintExecutor { def workspace def configs def linterType + def component - CodeLintExecutor(steps, workspace, configs, linterType) { + CodeLintExecutor(steps, workspace, configs, linterType, component) { this.steps = steps this.workspace = workspace this.configs = configs @@ -43,7 +44,11 @@ class CodeLintExecutor { linter = new PyLint(steps, workspace, configs) break case CodeLinterTypes.ESLINT: - linter = new ESLint(steps, workspace, configs) + if (component.eslintVersion == null || component.eslintVersion.isEmpty()) { + steps.log.error("CodeLintExecutor", "ESLint version is required") + return + } + linter = new ESLint(steps, workspace, configs, component.eslintVersion, component.eslintPlugins) break default: steps.log.error("CodeLintExecutor", "Unknown linter type") diff --git a/first-class-pipeline/src/com/freeleaps/devops/lint/ESLint.groovy b/first-class-pipeline/src/com/freeleaps/devops/lint/ESLint.groovy index ff4be472..46a6860c 100644 --- a/first-class-pipeline/src/com/freeleaps/devops/lint/ESLint.groovy +++ b/first-class-pipeline/src/com/freeleaps/devops/lint/ESLint.groovy @@ -4,25 +4,21 @@ import com.freeleaps.devops.enums.CodeLinterTypes import com.freeleaps.devops.lint.LinterBase class ESLint extends LinterBase { + def eslintVersion + def plugins - def deps = [ - 'eslint-define-config', - 'eslint-config-prettier', - 'eslint-plugin-prettier', - 'eslint-plugin-vue', - '@typescript-eslint/eslint-plugin', - '@typescript-eslint/parser', - 'typescript' - ] - - ESLint(steps, workspace, configs) { + ESLint(steps, workspace, configs, eslintVersion, plugins) { super(steps, workspace, configs, CodeLinterTypes.ESLINT) + this.eslintVersion = eslintVersion + this.plugins = plugins } def doLint() { steps.dir(workspace) { + steps.log.info("${linterType.linter}", "Install eslint with version: ${eslintVersion}") + steps.sh("npm install -g eslint@${eslintVersion}") steps.log.info("${linterType.linter}", "Install eslint dependencies...") - steps.sh("npm install -g ${deps.join(' ')}") + steps.sh("npm install -g ${plugins.join(' ')}") // check if given config file is not end with .json if (!configs.endsWith('.js')) { steps.log.warn("${linterType.linter}", "Invalid eslint configuration file, must be a JS file, convert file as valid JS file...") diff --git a/first-class-pipeline/tests/Jenkinsfile b/first-class-pipeline/tests/Jenkinsfile index 83ae3f13..631f22ae 100644 --- a/first-class-pipeline/tests/Jenkinsfile +++ b/first-class-pipeline/tests/Jenkinsfile @@ -47,6 +47,10 @@ executeFreeleapsPipeline { lintEnabled: true, // linter used to specify the code linter linter: 'eslint', + // eslintVersion used to specify the eslint version + eslintVersion: '7.32.0', + // eslintPlugins used to specify the eslint plugins + eslintPlugins: ['eslint-plugin-vue@8.0.3'], // linterConfig used to specify the code linter configuration file path, if not set, will use the default configuration // linterConfig: '.eslintrc.js', // sastEnabled used to specify whether to enable SAST scan diff --git a/first-class-pipeline/vars/executeFreeleapsPipeline.groovy b/first-class-pipeline/vars/executeFreeleapsPipeline.groovy index 068cb73d..b320a09b 100644 --- a/first-class-pipeline/vars/executeFreeleapsPipeline.groovy +++ b/first-class-pipeline/vars/executeFreeleapsPipeline.groovy @@ -142,7 +142,7 @@ def generateComponentStages(component, configurations) { def linterType = CodeLinterTypes.parse(component.linter) - def codeLintExecutor = new CodeLintExecutor(this, env.workroot + "/" + component.root + "/", component.linterConfig, linterType) + def codeLintExecutor = new CodeLintExecutor(this, env.workroot + "/" + component.root + "/", component.linterConfig, linterType, component) codeLintExecutor.execute() } }