wrapper.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # JSLint doesn't allow/support disabling the requirement for braces around
  2. # blocks, and that's one uglification I refuse to perform in the service of a
  3. # lint.
  4. #
  5. # There are of course lots of other intelligent things JSLint has to say
  6. # because it's just too easy in JS to do something that (e.g.) IE won't like.
  7. # So, this dumb script filters out any output messages from running JSLint
  8. # that look like they're complaints about '{' around braces.
  9. #
  10. # Also, takes an optional file describing the line mapping from the source
  11. # files to the combined distribution file, and outputs error messages in a
  12. # format suitable for vim (or whatever) pointing back to the source file.
  13. from subprocess import Popen, PIPE
  14. import sys
  15. import re
  16. def remapError(linestarts, line, col, err):
  17. if len(linestarts) == 0:
  18. # none supplied on command line
  19. return "%s:%d:%d: %s" % (sys.argv[1], line, col - 1, err)
  20. for i in range(len(linestarts)):
  21. if line > linestarts[i][0] and (i + 1 == len(linestarts) or (line <= linestarts[i + 1][0])):
  22. return "%s:%d:%d: %s" % (linestarts[i][1],
  23. line - linestarts[i][0],
  24. col - 1,
  25. err)
  26. raise Exception("Couldn't remap error!\n%s\n%s\n%s\n%s" % (linestarts, line, col, err))
  27. def main():
  28. p = Popen("support/d8/d8 support/jslint/fulljslint.js support/jslint/d8.js -- %s" % sys.argv[1],
  29. shell=True, stdout=PIPE)
  30. linestarts = []
  31. if len(sys.argv) > 2:
  32. linemaps = open(sys.argv[2]).read().split("\n")
  33. for l in linemaps:
  34. if len(l.strip()) == 0: continue
  35. start, fn = l.split(":")
  36. linestarts.append((int(start), fn))
  37. out, err = p.communicate()
  38. result = []
  39. report = []
  40. skip = 0
  41. mode = 'errors'
  42. for line in out.split("\n"):
  43. if line == "---REPORT":
  44. mode = 'report'
  45. continue
  46. if mode == "report":
  47. report.append(line)
  48. else:
  49. if skip > 0:
  50. skip -= 1
  51. else:
  52. if re.search(r"^.*Expected '{' and instead saw.*", line):
  53. skip = 2
  54. else:
  55. m = re.search("^Lint at line (\d+) character (\d+): (.*)$", line)
  56. if m:
  57. result.append(remapError(linestarts, int(m.group(1)),
  58. int(m.group(2)), m.group(3)))
  59. else:
  60. if line.strip() != "":
  61. result.append(line)
  62. if len(result) > 0:
  63. print '\n'.join(result)
  64. sys.exit(p.returncode)
  65. #open("dist/report.html", "w").write('\n'.join(report))
  66. sys.exit(0)
  67. if __name__ == "__main__": main()