You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

152 lines
4.5 KiB

  1. #!/usr/bin/python3
  2. import os
  3. class ScopeVars:
  4. def __init__(self, depth):
  5. self.variables = []
  6. self.depth = depth
  7. def exists(self, string):
  8. return string in self.variables
  9. def add(self, string):
  10. self.variables.append(string)
  11. def check_file_for_dupe_lets(file):
  12. found = False
  13. line_num = 0
  14. depth = 0
  15. scope_vars = []
  16. for line in file:
  17. line_num += 1
  18. for char in line:
  19. if char == "{":
  20. depth += 1
  21. scope_vars.append(ScopeVars(depth))
  22. elif char == "}":
  23. for obj in scope_vars:
  24. if obj.depth == depth:
  25. scope_vars.remove(obj)
  26. del obj
  27. depth -= 1
  28. if " let " in line:
  29. let_start = line.index(" let ")
  30. index = let_start + 5
  31. for i in line[index:]:
  32. match_found = False
  33. if i != " ":
  34. index += 1
  35. continue
  36. let_var = line[let_start + 5 : index]
  37. match_found = True
  38. for obj in scope_vars:
  39. if obj.depth == depth:
  40. if obj.exists(let_var):
  41. print("Dupe let declaration: '%s' in '%s' at line %d" % (let_var, file.name, line_num))
  42. found = True
  43. else:
  44. obj.add(let_var)
  45. break
  46. break
  47. return found
  48. def check_file_for_missing_commas(file):
  49. found = False
  50. line_num = 0
  51. char_num = 0
  52. depth = 0
  53. scope_vars = []
  54. in_comment = False
  55. in_proto = False
  56. in_method = False
  57. need_comma_or_final_closure = False
  58. for line in file:
  59. line_num += 1
  60. line = line.strip("\n")
  61. if line.strip().startswith("//"):
  62. continue
  63. char_num = 0
  64. while char_num < len(line):
  65. char = line[char_num]
  66. if line[char_num:char_num + 2] == "/*":
  67. in_comment = True
  68. elif line[char_num:char_num + 2] == "*/":
  69. in_comment = False
  70. if in_comment:
  71. char_num += 1
  72. continue
  73. elif char == "{":
  74. depth += 1
  75. if need_comma_or_final_closure:
  76. print("Missing comma after prototype method - '%s' at line %d" % (file.name, line_num))
  77. found = True
  78. need_comma_or_final_closure = False
  79. if "prototype={" in line[:char_num + 1].replace(" ", ""):
  80. in_proto = True
  81. elif depth > 1 and in_proto:
  82. in_method = True
  83. elif char == "}":
  84. depth -= 1
  85. if depth == 0:
  86. if in_proto:
  87. in_proto = False
  88. if need_comma_or_final_closure:
  89. need_comma_or_final_closure = False
  90. elif depth == 1 and in_proto and in_method:
  91. in_method = False
  92. need_comma_or_final_closure = True
  93. elif char == ",":
  94. if in_proto and need_comma_or_final_closure:
  95. need_comma_or_final_closure = False
  96. elif need_comma_or_final_closure and (char not in (" ", "\n", "\t")):
  97. print("Missing comma after prototype method - '%s' at line %d" % (file.name, line_num))
  98. found = True
  99. need_comma_or_final_closure = False
  100. char_num += 1
  101. return found
  102. ##########################################
  103. print("Checking for duplicate 'let' declarations - mozjs38 crashes if one is encountered within the same scope...")
  104. found = False
  105. for root, dirs, files in os.walk("."):
  106. for name in files:
  107. if name.endswith(".js"):
  108. with open(os.path.join(root, name), "r") as file:
  109. if check_file_for_dupe_lets(file):
  110. found = True
  111. if not found:
  112. print("... no problems found")
  113. print("\nChecking for missing comma separators between prototype methods - cinnamon crashes if one is missed...")
  114. found = False
  115. for root, dirs, files in os.walk("."):
  116. for name in files:
  117. if name.endswith(".js"):
  118. with open(os.path.join(root, name), "r") as file:
  119. if check_file_for_missing_commas(file):
  120. found = True
  121. if not found:
  122. print("... no problems found")
  123. quit()