@@ -21,6 +21,88 @@ func quoteWhenIsNotJson(value string) string {
2121 }
2222}
2323
24+ func removePlugins (rule string ) (string , bool ) {
25+ // Handle plugin rules (those containing _)
26+ if strings .Contains (rule , "_" ) {
27+ parts := strings .Split (rule , "_" )
28+ if len (parts ) >= 2 {
29+ // First part is the plugin name, last part is the rule name
30+ plugin := parts [0 ]
31+ ruleName := parts [len (parts )- 1 ]
32+
33+ // Handle scoped packages
34+ if strings .HasPrefix (plugin , "@" ) {
35+ rule = fmt .Sprintf ("%s/%s" , plugin , ruleName )
36+ } else {
37+ // For non-scoped packages, add eslint-plugin- prefix
38+ rule = fmt .Sprintf ("%s/%s" , "eslint-plugin-" + plugin , ruleName )
39+ }
40+ }
41+ }
42+
43+ // Skip any rule that contains a plugin (contains "/")
44+ if strings .Contains (rule , "/" ) {
45+ return rule , true
46+ }
47+
48+ return rule , false
49+ }
50+
51+ func buildNamedParametersString (parameters []domain.ParameterConfiguration , patternDefinition domain.PatternDefinition ) string {
52+ namedParametersString := ""
53+ for _ , parameter := range parameters {
54+ if parameter .Name != "unnamedParam" {
55+ paramValue := parameter .Value
56+
57+ // If value is empty, look for default in pattern definition
58+ if paramValue == "" {
59+ for _ , paramDef := range patternDefinition .Parameters {
60+ if paramDef .Name == parameter .Name && paramDef .Default != "" {
61+ paramValue = paramDef .Default
62+ break
63+ }
64+ }
65+ }
66+
67+ // Skip only if both value and default are empty
68+ if paramValue == "" {
69+ continue
70+ }
71+
72+ if len (namedParametersString ) == 0 {
73+ namedParametersString += "{"
74+ } else {
75+ namedParametersString += ", "
76+ }
77+
78+ if paramValue == "true" || paramValue == "false" {
79+ namedParametersString += fmt .Sprintf ("\" %s\" : %s" , parameter .Name , paramValue )
80+ } else {
81+ namedParametersString += fmt .Sprintf ("\" %s\" : %s" , parameter .Name , quoteWhenIsNotJson (paramValue ))
82+ }
83+ }
84+ }
85+ if len (namedParametersString ) > 0 {
86+ namedParametersString += "}"
87+ }
88+ return namedParametersString
89+ }
90+
91+ func writeFile (filePath string , content string ) error {
92+ file , err := os .Create (filePath )
93+ if err != nil {
94+ return fmt .Errorf ("failed to create file: %v" , err )
95+ }
96+ defer file .Close ()
97+
98+ _ , err = file .WriteString (content )
99+ if err != nil {
100+ return fmt .Errorf ("failed to write content: %v" , err )
101+ }
102+
103+ return nil
104+ }
105+
24106func CreateEslintConfig (toolsConfigDir string , configuration []domain.PatternConfiguration ) error {
25107 result := `export default [
26108 {
@@ -30,26 +112,8 @@ func CreateEslintConfig(toolsConfigDir string, configuration []domain.PatternCon
30112 for _ , patternConfiguration := range configuration {
31113 rule := strings .TrimPrefix (patternConfiguration .PatternDefinition .Id , "ESLint8_" )
32114
33- // Handle plugin rules (those containing _)
34- if strings .Contains (rule , "_" ) {
35- parts := strings .Split (rule , "_" )
36- if len (parts ) >= 2 {
37- // First part is the plugin name, last part is the rule name
38- plugin := parts [0 ]
39- ruleName := parts [len (parts )- 1 ]
40-
41- // Handle scoped packages
42- if strings .HasPrefix (plugin , "@" ) {
43- rule = fmt .Sprintf ("%s/%s" , plugin , ruleName )
44- } else {
45- // For non-scoped packages, add eslint-plugin- prefix
46- rule = fmt .Sprintf ("%s/%s" , "eslint-plugin-" + plugin , ruleName )
47- }
48- }
49- }
50-
51- // Skip any rule that contains a plugin (contains "/")
52- if strings .Contains (rule , "/" ) {
115+ rule , skipRule := removePlugins (rule )
116+ if skipRule {
53117 continue
54118 }
55119
@@ -83,43 +147,8 @@ func CreateEslintConfig(toolsConfigDir string, configuration []domain.PatternCon
83147 parametersString += quoteWhenIsNotJson (defaultUnnamedParamValue )
84148 }
85149
86- // build named parameters json object
87- namedParametersString := ""
88- for _ , parameter := range patternConfiguration .Parameters {
89- if parameter .Name != "unnamedParam" {
90- paramValue := parameter .Value
91-
92- // If value is empty, look for default in pattern definition
93- if paramValue == "" {
94- for _ , paramDef := range patternConfiguration .PatternDefinition .Parameters {
95- if paramDef .Name == parameter .Name && paramDef .Default != "" {
96- paramValue = paramDef .Default
97- break
98- }
99- }
100- }
101-
102- // Skip only if both value and default are empty
103- if paramValue == "" {
104- continue
105- }
106-
107- if len (namedParametersString ) == 0 {
108- namedParametersString += "{"
109- } else {
110- namedParametersString += ", "
111- }
112-
113- if paramValue == "true" || paramValue == "false" {
114- namedParametersString += fmt .Sprintf ("\" %s\" : %s" , parameter .Name , paramValue )
115- } else {
116- namedParametersString += fmt .Sprintf ("\" %s\" : %s" , parameter .Name , quoteWhenIsNotJson (paramValue ))
117- }
118- }
119- }
120- if len (namedParametersString ) > 0 {
121- namedParametersString += "}"
122- }
150+ // Use the new helper method to build named parameters JSON object
151+ namedParametersString := buildNamedParametersString (patternConfiguration .Parameters , patternConfiguration .PatternDefinition )
123152
124153 if parametersString != "" && namedParametersString != "" {
125154 parametersString = fmt .Sprintf ("%s, %s" , parametersString , namedParametersString )
@@ -141,16 +170,7 @@ func CreateEslintConfig(toolsConfigDir string, configuration []domain.PatternCon
141170 result += ` }
142171 }
143172];`
144- eslintConfigFile , err := os .Create (filepath .Join (toolsConfigDir , "eslint.config.mjs" ))
145- if err != nil {
146- return fmt .Errorf ("failed to create eslint config file: %v" , err )
147- }
148- defer eslintConfigFile .Close ()
149-
150- _ , err = eslintConfigFile .WriteString (result )
151- if err != nil {
152- return fmt .Errorf ("failed to write eslint config: %v" , err )
153- }
154173
155- return nil
174+ eslintConfigFilePath := filepath .Join (toolsConfigDir , "eslint.config.mjs" )
175+ return writeFile (eslintConfigFilePath , result )
156176}
0 commit comments