forked from MapComplete/MapComplete
		
	Add wrong default import from json files to code quality checks, fix those imports
This commit is contained in:
		
							parent
							
								
									71c815d37d
								
							
						
					
					
						commit
						ce44f34bf3
					
				
					 42 changed files with 167 additions and 148 deletions
				
			
		|  | @ -7,62 +7,86 @@ import { exec } from "child_process" | |||
|  * @param reason | ||||
|  * @private | ||||
|  */ | ||||
| function detectInCode(forbidden: string, reason: string) { | ||||
|     const excludedDirs = [ | ||||
|         ".git", | ||||
|         "node_modules", | ||||
|         "dist", | ||||
|         ".cache", | ||||
|         ".parcel-cache", | ||||
|         "assets", | ||||
|         "vendor", | ||||
|         ".idea/", | ||||
|     ] | ||||
| function detectInCode(forbidden: string, reason: string): (done: () => void) => void { | ||||
|     return (done: () => void) => { | ||||
|         const excludedDirs = [ | ||||
|             ".git", | ||||
|             "node_modules", | ||||
|             "dist", | ||||
|             ".cache", | ||||
|             ".parcel-cache", | ||||
|             "assets", | ||||
|             "vendor", | ||||
|             ".idea/", | ||||
|         ] | ||||
| 
 | ||||
|     exec( | ||||
|         'grep -n "' + | ||||
|             forbidden + | ||||
|             '" -r . ' + | ||||
|             excludedDirs.map((d) => "--exclude-dir=" + d).join(" "), | ||||
|         (error, stdout, stderr) => { | ||||
|             if (error?.message?.startsWith("Command failed: grep")) { | ||||
|                 console.warn("Command failed!") | ||||
|                 return | ||||
|             } | ||||
|             if (error !== null) { | ||||
|                 throw error | ||||
|             } | ||||
|             if (stderr !== "") { | ||||
|                 throw stderr | ||||
|             } | ||||
|         exec( | ||||
|             'grep -n "' + | ||||
|                 forbidden + | ||||
|                 '" -r . ' + | ||||
|                 excludedDirs.map((d) => "--exclude-dir=" + d).join(" "), | ||||
|             (error, stdout, stderr) => { | ||||
|                 if (error?.message?.startsWith("Command failed: grep")) { | ||||
|                     console.warn("Command failed!", error) | ||||
|                     return | ||||
|                 } | ||||
|                 if (error !== null) { | ||||
|                     throw error | ||||
|                 } | ||||
|                 if (stderr !== "") { | ||||
|                     throw stderr | ||||
|                 } | ||||
| 
 | ||||
|             const found = stdout | ||||
|                 .split("\n") | ||||
|                 .filter((s) => s !== "") | ||||
|                 .filter((s) => !s.startsWith("./test/")) | ||||
|             if (found.length > 0) { | ||||
|                 throw `Found a '${forbidden}' at \n    ${found.join("\n     ")}.\n ${reason}` | ||||
|                 const found = stdout | ||||
|                     .split("\n") | ||||
|                     .filter((s) => s !== "") | ||||
|                     .filter((s) => !s.startsWith("./test/")) | ||||
|                 if (found.length > 0) { | ||||
|                     const msg = `Found a '${forbidden}' at \n    ${found.join( | ||||
|                         "\n     " | ||||
|                     )}.\n ${reason}` | ||||
|                     console.error(msg) | ||||
|                     console.error(found.length, "issues found") | ||||
|                     throw msg | ||||
|                 } | ||||
|                 done() | ||||
|             } | ||||
|         } | ||||
|     ) | ||||
|         ) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| describe("Code quality", () => { | ||||
|     it("should not contain reverse", () => { | ||||
|     it( | ||||
|         "should not contain reverse", | ||||
|         detectInCode( | ||||
|             "reverse()", | ||||
|             "Reverse is stateful and changes the source list. This often causes subtle bugs" | ||||
|         ) | ||||
|     }) | ||||
|     ) | ||||
| 
 | ||||
|     it("should not contain 'constructor.name'", () => { | ||||
|     it( | ||||
|         "should not contain 'constructor.name'", | ||||
|         detectInCode("constructor\\.name", "This is not allowed, as minification does erase names.") | ||||
|     }) | ||||
|     ) | ||||
| 
 | ||||
|     it("should not contain 'innerText'", () => { | ||||
|     it( | ||||
|         "should not contain 'innerText'", | ||||
|         detectInCode( | ||||
|             "innerText", | ||||
|             "innerText is not allowed as it is not testable with fakeDom. Use 'textContent' instead." | ||||
|         ) | ||||
|     }) | ||||
|     ) | ||||
| 
 | ||||
|     it( | ||||
|         "should not contain 'import * as name from \"xyz.json\"'", | ||||
|         detectInCode( | ||||
|             'import \\* as [a-zA-Z0-9_]\\+ from \\"[.-_/a-zA-Z0-9]\\+\\.json\\"', | ||||
|             "With vite, json files have a default export. Use import name from file.json instead" | ||||
|         ) | ||||
|     ) | ||||
| 
 | ||||
|     it( | ||||
|         "should not contain '[\"default\"]'", | ||||
|         detectInCode('\\[\\"default\\"\\]', "Possible leftover of faulty default import") | ||||
|     ) | ||||
| }) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue