Skip to content

Instantly share code, notes, and snippets.

@binki
Last active June 11, 2018 17:33
Show Gist options
  • Save binki/01f78df7d0a364740d773e533fb3ecb5 to your computer and use it in GitHub Desktop.
Save binki/01f78df7d0a364740d773e533fb3ecb5 to your computer and use it in GitHub Desktop.
Find old-style angular files likely to fail injection during minification
#!/bin/sh
# The find ignores any VS build folders and minified files. It
# then grabs any *.js and passes them to xargs.
#
# The xargs runs a script per each file which is unfortunately inside
# of "" so everything has to be escaped, making the inside unreadable.
# The sed takes any line ending in opening square bracket ‘[’, comma ‘,’,
# space ‘ ’ and removes the newline. It took me a long time to figure out
# how to get sed to join lines together. It involves some ridiculousness
# and usage of the hold space. If I knew how to use something like awk or
# just wrote a custom script I probably would have finished this faster.
#
# Once those lines are joined together, it does a grep which is supposed
# to match any functions with at least one argument starting with a dollar sign
# not preceeded by an array opener such as ‘[’. For example, it should reject
# code like this:
#
# .module(function ($scope, $http) {})
#
# but accept code like this:
#
# .module(['$scope', '$http', function ($scope, $http) {
# })
#
# If a problem is found, the file containing it is printed. It is expected that
# the file would be short enough that the dev can just search for all “function”
# in the file and judge whether or not there are any issues.
#
# However, there are some places where one might write arguments with $event
# which are not injected which will result in it falsely reporting a problem.
# And there are times when an injectable module is not named starting with a ‘$’.
# For example, an app-custom service is normally not named with a ‘$’. If a module
# only imports such services, it will be missed by this heuristic.
find . \( -name obj -o -name bin -o -name '*.min.*' \) -prune -o -name '*.js' -print0 | xargs -0 -n
1 sh -c "sed -n -e H -e x -e 's/\\n//' -e '/[[, ]\$/{h; d;}' -e p -e 's/.*//' -e x -e d \"\${1}\" | grep -qe '^[^[
]*function[^(]*(\\\$[^,) ]' && echo \"\${1}\"" arg0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment