Remove all IIFEs

Mike Kowdley
3 min readMar 10, 2022

--

Just a quick script, but it illustrates a couple of interesting points. I have a bunch of JavaScript files that were written years ago, and each file wraps itself in an IIFE. The files all look like this:

(function() {
... do a bunch of stuff...
}());

I won’t spend time on the purpose of the IIFE; the link above to MDN should suffice.

We upgraded all our build to gulp+webpack years ago, and never removed these IIFEs, which now have no purpose since webpack provides the namespace isolation that these IIFEs were originally intended to provide. They don’t seem to hurt, but it’s visual noise that I’d like to get rid of. So I need some tools to:

  • find all files with IIFEs in them
  • remove any empty lines from the end of a file
  • remove the first and last line of a file
  • remove the first two spaces from a file (so that the indenting of the IIFE is removed, and the linter still passes).

It took a big of monkeying, but we got there. Note that there are a lot of assumptions in the following commands; here’s a non-exhaustive list of them:

  • the IIFE starts on the first line
  • the IIFE ends on the last line, with 0 or more empty lines afterward
  • intending is always two spaces (this is known to be true or the linter would be failing)

Of course not every assumption is correct every time, so be sure you’ve got backups! Commit your work to git before proceeding.

Find all files with IIFEs in them

Easy peasy, once you’ve made the assumptions above. In my case, all of my JavaScript files are in the path apps under my current directory. Using -n gives us the line number, and grepping on :1: allows us to only select the results from lines numbered 1.

egrep -Rn '^\(function\(\) {' apps | fgrep ':1:' | cut -f1 -d':' > /tmp/files

Check the contents of /tmp/files for sanity before proceeding.

Remove trailing blank lines in a file

This actually will remove multiple blank lines if they exist. Props to figuring out this one…

# https://stackoverflow.com/a/35453867/2476452
cat /tmp/files | xargs -n1 sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' -i.bak

Removing the first and last line of a file

Recall that $ denotes the last line of a file, as far as sed is concerned.

# https://unix.stackexchange.com/a/209070/502262
cat /tmp/files | xargs -n1 sed -e '1d;$d' -i .bak

Removing the first two spaces of every line in a file

This cleans up the indenting, and the linter is happy once again.

cat /tmp/files | xargs -n1 sed -e 's/^  //' -i .bak

Putting it all together

Here’s the whole script, after which I can verify the compiler and the linter were happy. In my case it was 129 files total, so it was still practical to visually review the PR and make sure nothing weird happened.

#!/bin/bash# make a list of all files that have an iife signature on line 1
egrep -Rn '^\(function\(\) {' apps | fgrep ':1:' | cut -f1 -d':' > /tmp/files
# remove all trailing empty lines from files in the list
# https://stackoverflow.com/a/35453867/2476452
cat /tmp/files | xargs -n1 sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' -i.bak
# remove the first and last line of each file
# https://unix.stackexchange.com/a/209070/502262
cat /tmp/files | xargs -n1 sed -e '1d;$d' -i .bak
# remove the first two spaces of each line that starts with two spaces
cat /tmp/files | xargs -n1 sed -e 's/^ //' -i .bak
# remove back up files
find . -name '*.bak' | xargs rm

And we’re done. Many IIFEs gone, and there’s two fewer lines in every file to spend any time thinking about during future reviews.

Hope this helps!

This was written on my MacOS box, which claims bash to be: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18) .

--

--

No responses yet