diff --git a/.eslintrc b/.eslintrc index d59b45b..2578864 100644 --- a/.eslintrc +++ b/.eslintrc @@ -14,5 +14,6 @@ "no-unused-vars": [2, {"args": "none"}], // Allow unused argument definition in order to keep functions signatures self documented. "no-use-before-define": [2, "nofunc"], "quote-props": [2, "as-needed"], // Disallow to quote properties (of object) when it's not requiered. + "max-len": 1 // Seems to be redundant to me. }, } diff --git a/src/rules/break/delete-array-value.js b/src/rules/break/delete-array-value.js new file mode 100644 index 0000000..e576f48 --- /dev/null +++ b/src/rules/break/delete-array-value.js @@ -0,0 +1,45 @@ + +export default function deleteArrayValue({ kind, path, item }) { + const match = kind === 'A' + && path.length > 1 + && item + && item.kind === 'D'; + if (match) { + const pathId = path[1]; + const arrayType = path.slice(-1); + const arrayName = path.slice(-2)[0]; + + if (path[0] === 'paths' && path[3] === 'parameters') { + const paramName = path[4]; + const method = path[2]; + + return { + path: pathId, + message: `${pathId} (${method}) - param ${paramName} - ${arrayName}.${item.lhs} ${arrayType} value deleted`, + method, + param: paramName, + arrayName, + arrayValue: item.lhs, + }; + } else if (path[0] === 'definitions' && path[2] === 'properties') { + const objectPath = path.slice(0, -2).join('/'); + const propertyName = path[3]; + + return { + message: `${objectPath} - Object property - ${arrayName}.${item.lhs} ${arrayType} value deleted`, + path: objectPath, + property: propertyName, + arrayName, + arrayValue: item.lhs, + }; + } + + return { + message: `${arrayName}.${item.lhs} - ${arrayType} value deleted`, + path: pathId, + arrayName, + arrayValue: item.lhs, + }; + } + return false; +} diff --git a/src/rules/index.js b/src/rules/index.js index e00d6ef..b754c73 100644 --- a/src/rules/index.js +++ b/src/rules/index.js @@ -7,6 +7,7 @@ export default { break: { 'add-required-object-property': require('./break/add-required-object-property.js'), 'add-required-param': require('./break/add-required-param.js'), + 'delete-array-value': require('./break/delete-array-value.js'), 'delete-definition': require('./break/delete-definition.js'), 'delete-method': require('./break/delete-method.js'), 'delete-operation-id': require('./break/delete-operation-id.js'), @@ -26,6 +27,7 @@ export default { 'edit-response': require('./break/edit-response.js'), }, smooth: { + 'add-array-value': require('./smooth/add-array-value.js'), 'add-definition': require('./smooth/add-definition.js'), 'add-description': require('./smooth/add-description.js'), 'add-method': require('./smooth/add-method.js'), diff --git a/src/rules/smooth/add-array-value.js b/src/rules/smooth/add-array-value.js new file mode 100644 index 0000000..7b814bc --- /dev/null +++ b/src/rules/smooth/add-array-value.js @@ -0,0 +1,45 @@ + +export default function addArrayValue({ kind, path, item }) { + const match = kind === 'A' + && path.length > 1 + && item + && item.kind === 'N'; + if (match) { + const pathId = path[1]; + const arrayType = path.slice(-1); + const arrayName = path.slice(-2)[0]; + + if (path[0] === 'paths' && path[3] === 'parameters') { + const paramName = path[4]; + const method = path[2]; + + return { + path: pathId, + message: `${pathId} (${method}) - param ${paramName} - ${arrayName}.${item.rhs} ${arrayType} value added`, + method, + param: paramName, + arrayName, + arrayValue: item.rhs, + }; + } else if (path[0] === 'definitions' && path[2] === 'properties') { + const objectPath = path.slice(0, -2).join('/'); + const propertyName = path[3]; + + return { + message: `${objectPath} - Object property - ${arrayName}.${item.rhs} ${arrayType} value added`, + path: objectPath, + property: propertyName, + arrayName, + arrayValue: item.rhs, + }; + } + + return { + message: `${arrayName}.${item.rhs} - ${arrayType} value added`, + path: pathId, + arrayName, + arrayValue: item.rhs, + }; + } + return false; +} diff --git a/test/rules/break/delete-array-value/0-new.json b/test/rules/break/delete-array-value/0-new.json new file mode 100644 index 0000000..814a1e3 --- /dev/null +++ b/test/rules/break/delete-array-value/0-new.json @@ -0,0 +1,22 @@ +{ + "swagger": "2.0", + "info": { + "version": "1.0.0", + "title": "Test API" + }, + "paths": {}, + "definitions": { + "Definition.Path.Name": { + "type": "object", + "properties": { + "EnumProperty": { + "enum": [ + "Value1", + "Value2" + ], + "type": "string" + } + } + } + } +} diff --git a/test/rules/break/delete-array-value/0-old.json b/test/rules/break/delete-array-value/0-old.json new file mode 100644 index 0000000..c2eb6ac --- /dev/null +++ b/test/rules/break/delete-array-value/0-old.json @@ -0,0 +1,23 @@ +{ + "swagger": "2.0", + "info": { + "version": "1.0.0", + "title": "Test API" + }, + "paths": {}, + "definitions": { + "Definition.Path.Name": { + "type": "object", + "properties": { + "EnumProperty": { + "enum": [ + "Value1", + "Value2", + "Value3" + ], + "type": "string" + } + } + } + } +} diff --git a/test/rules/break/delete-array-value/0-output.json b/test/rules/break/delete-array-value/0-output.json new file mode 100644 index 0000000..a19546c --- /dev/null +++ b/test/rules/break/delete-array-value/0-output.json @@ -0,0 +1,14 @@ +{ + "breaks": [ + { + "ruleId": "delete-array-value", + "message": "definitions/Definition.Path.Name/properties - Object property - EnumProperty.Value3 enum value deleted", + "path": "definitions/Definition.Path.Name/properties", + "property": "EnumProperty", + "arrayName": "EnumProperty", + "arrayValue": "Value3" + } + ], + "smooths": [], + "unmatchDiffs": [] +} diff --git a/test/rules/break/delete-array-value/1-new.json b/test/rules/break/delete-array-value/1-new.json new file mode 100644 index 0000000..a7c36b6 --- /dev/null +++ b/test/rules/break/delete-array-value/1-new.json @@ -0,0 +1,25 @@ +{ + "swagger": "2.0", + "info": { + "version": "1.0.0", + "title": "Test API" + }, + "paths": { + "/test/": { + "get": { + "parameters": [ + { + "name": "enumParam", + "in": "query", + "required": false, + "type": "string", + "enum": [ + "Value1", + "Value2" + ] + } + ] + } + } + } +} diff --git a/test/rules/break/delete-array-value/1-old.json b/test/rules/break/delete-array-value/1-old.json new file mode 100644 index 0000000..e33b813 --- /dev/null +++ b/test/rules/break/delete-array-value/1-old.json @@ -0,0 +1,26 @@ +{ + "swagger": "2.0", + "info": { + "version": "1.0.0", + "title": "Test API" + }, + "paths": { + "/test/": { + "get": { + "parameters": [ + { + "name": "enumParam", + "in": "query", + "required": false, + "type": "string", + "enum": [ + "Value1", + "Value2", + "Value3" + ] + } + ] + } + } + } +} diff --git a/test/rules/break/delete-array-value/1-output.json b/test/rules/break/delete-array-value/1-output.json new file mode 100644 index 0000000..6b00a57 --- /dev/null +++ b/test/rules/break/delete-array-value/1-output.json @@ -0,0 +1,15 @@ +{ + "breaks": [ + { + "ruleId": "delete-array-value", + "message": "/test/ (get) - param enumParam - enumParam.Value3 enum value deleted", + "path": "/test/", + "method": "get", + "param": "enumParam", + "arrayName": "enumParam", + "arrayValue": "Value3" + } + ], + "smooths": [], + "unmatchDiffs": [] +} diff --git a/test/rules/smooth/add-array-value/0-new.json b/test/rules/smooth/add-array-value/0-new.json new file mode 100644 index 0000000..cbce540 --- /dev/null +++ b/test/rules/smooth/add-array-value/0-new.json @@ -0,0 +1,24 @@ +{ + "swagger": "2.0", + "info": { + "version": "1.0.0", + "title": "Test API" + }, + "paths": {}, + "definitions": { + "Definition.Path.Name": { + "type": "object", + "properties": { + "EnumProperty": { + "enum": [ + "Value1", + "Value2", + "Value3", + "Value4" + ], + "type": "string" + } + } + } + } +} diff --git a/test/rules/smooth/add-array-value/0-old.json b/test/rules/smooth/add-array-value/0-old.json new file mode 100644 index 0000000..c2eb6ac --- /dev/null +++ b/test/rules/smooth/add-array-value/0-old.json @@ -0,0 +1,23 @@ +{ + "swagger": "2.0", + "info": { + "version": "1.0.0", + "title": "Test API" + }, + "paths": {}, + "definitions": { + "Definition.Path.Name": { + "type": "object", + "properties": { + "EnumProperty": { + "enum": [ + "Value1", + "Value2", + "Value3" + ], + "type": "string" + } + } + } + } +} diff --git a/test/rules/smooth/add-array-value/0-output.json b/test/rules/smooth/add-array-value/0-output.json new file mode 100644 index 0000000..86125b0 --- /dev/null +++ b/test/rules/smooth/add-array-value/0-output.json @@ -0,0 +1,14 @@ +{ + "breaks": [], + "smooths": [ + { + "ruleId": "add-array-value", + "message": "definitions/Definition.Path.Name/properties - Object property - EnumProperty.Value4 enum value added", + "path": "definitions/Definition.Path.Name/properties", + "property": "EnumProperty", + "arrayName": "EnumProperty", + "arrayValue": "Value4" + } + ], + "unmatchDiffs": [] +} diff --git a/test/rules/smooth/add-array-value/1-new.json b/test/rules/smooth/add-array-value/1-new.json new file mode 100644 index 0000000..4311768 --- /dev/null +++ b/test/rules/smooth/add-array-value/1-new.json @@ -0,0 +1,27 @@ +{ + "swagger": "2.0", + "info": { + "version": "1.0.0", + "title": "Test API" + }, + "paths": { + "/test/": { + "get": { + "parameters": [ + { + "name": "enumParam", + "in": "query", + "required": false, + "type": "string", + "enum": [ + "Value1", + "Value2", + "Value3", + "Value4" + ] + } + ] + } + } + } +} diff --git a/test/rules/smooth/add-array-value/1-old.json b/test/rules/smooth/add-array-value/1-old.json new file mode 100644 index 0000000..e33b813 --- /dev/null +++ b/test/rules/smooth/add-array-value/1-old.json @@ -0,0 +1,26 @@ +{ + "swagger": "2.0", + "info": { + "version": "1.0.0", + "title": "Test API" + }, + "paths": { + "/test/": { + "get": { + "parameters": [ + { + "name": "enumParam", + "in": "query", + "required": false, + "type": "string", + "enum": [ + "Value1", + "Value2", + "Value3" + ] + } + ] + } + } + } +} diff --git a/test/rules/smooth/add-array-value/1-output.json b/test/rules/smooth/add-array-value/1-output.json new file mode 100644 index 0000000..e2bdcb6 --- /dev/null +++ b/test/rules/smooth/add-array-value/1-output.json @@ -0,0 +1,15 @@ +{ + "breaks": [], + "smooths": [ + { + "ruleId": "add-array-value", + "message": "/test/ (get) - param enumParam - enumParam.Value4 enum value added", + "path": "/test/", + "method": "get", + "param": "enumParam", + "arrayName": "enumParam", + "arrayValue": "Value4" + } + ], + "unmatchDiffs": [] +}