mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	check-openapi: Print diffs for Prettier formatting errors.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
		
				
					committed by
					
						
						Tim Abbott
					
				
			
			
				
	
			
			
			
						parent
						
							b45548e85a
						
					
				
				
					commit
					8854cd7945
				
			@@ -99,6 +99,7 @@
 | 
				
			|||||||
    "@typescript-eslint/parser": "^4.0.1",
 | 
					    "@typescript-eslint/parser": "^4.0.1",
 | 
				
			||||||
    "babel-plugin-rewire-ts": "^1.4.0",
 | 
					    "babel-plugin-rewire-ts": "^1.4.0",
 | 
				
			||||||
    "callsites": "^3.1.0",
 | 
					    "callsites": "^3.1.0",
 | 
				
			||||||
 | 
					    "diff": "^4.0.1",
 | 
				
			||||||
    "difflib": "^0.2.4",
 | 
					    "difflib": "^0.2.4",
 | 
				
			||||||
    "es-check": "^6.0.0",
 | 
					    "es-check": "^6.0.0",
 | 
				
			||||||
    "eslint": "^7.2.0",
 | 
					    "eslint": "^7.2.0",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,6 +4,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const fs = require("fs");
 | 
					const fs = require("fs");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Diff = require("diff");
 | 
				
			||||||
const ExampleValidator = require("openapi-examples-validator");
 | 
					const ExampleValidator = require("openapi-examples-validator");
 | 
				
			||||||
const Prettier = require("prettier");
 | 
					const Prettier = require("prettier");
 | 
				
			||||||
const SwaggerParser = require("swagger-parser");
 | 
					const SwaggerParser = require("swagger-parser");
 | 
				
			||||||
@@ -64,7 +65,20 @@ async function checkFile(file) {
 | 
				
			|||||||
                if (formatted !== node.value.value) {
 | 
					                if (formatted !== node.value.value) {
 | 
				
			||||||
                    ok = false;
 | 
					                    ok = false;
 | 
				
			||||||
                    const {line, col} = lineCounter.linePos(node.value.range[0]);
 | 
					                    const {line, col} = lineCounter.linePos(node.value.range[0]);
 | 
				
			||||||
                    console.error("%s:%d:%d: Format description with Prettier", file, line, col);
 | 
					                    console.error("%s:%d:%d: Format description with Prettier:", file, line, col);
 | 
				
			||||||
 | 
					                    let diff = "";
 | 
				
			||||||
 | 
					                    for (const part of Diff.diffLines(node.value.value, formatted)) {
 | 
				
			||||||
 | 
					                        const prefix = part.added
 | 
				
			||||||
 | 
					                            ? "\u001B[32m+"
 | 
				
			||||||
 | 
					                            : part.removed
 | 
				
			||||||
 | 
					                            ? "\u001B[31m-"
 | 
				
			||||||
 | 
					                            : "\u001B[34m ";
 | 
				
			||||||
 | 
					                        diff += prefix;
 | 
				
			||||||
 | 
					                        diff += part.value.replace(/\n$/, "").replace(/\n/g, "\n" + prefix);
 | 
				
			||||||
 | 
					                        diff += "\n";
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    diff += "\u001B[0m";
 | 
				
			||||||
 | 
					                    console.error(diff);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user