Skip to content

Instantly share code, notes, and snippets.

@seeflanigan
Created December 8, 2016 21:37
Show Gist options
  • Save seeflanigan/abdbffa755a29199a1ab5ce7cb9bb948 to your computer and use it in GitHub Desktop.
Save seeflanigan/abdbffa755a29199a1ab5ce7cb9bb948 to your computer and use it in GitHub Desktop.
`sed` onelines to fix `func-name` errors in `ESLint` per `airbnb/legacy` rules
#!/usr/bin/env bash
# Use cases
#
# Anonymous function expressions assigned by `=` operator to a variable:
# (with and without arguments)
#
# const aFunc = function () {};
#
# Anonymous function expressions declared as a property of an object:
#
# const anObj = {
# aFunc: function () {}
# };
#
# Anonymous function assigned by `=` to a property of a prototype:
#
# String.prototype.aFunc = function () {};
#
# Anonymous function expression as a callback:
#
# describe('Documentation', function () {});
#
EXCLUSIONS_REGEX='assets'
CALLBACK_AG_REGEX='[,(]\s+function\s*\('
OBJECT_PROPERTY_AG_REGEX='\w+:\s+function\s*\('
PROTOTYPE_AG_REGEX='prototype\.\w+\s+=\s+function\s*\(' # is this made redundant by the `VARIABLE` case?
VARIABLE_AG_REGEX='\w+\s+=\s+function\s*\('
CALLBACK_SUBSTITUTION='s/[,(] *function *(\([^)]*\))/, (\1) =>/'
OBJECT_PROPERTY_SUBSTITUTION='s/\([[:alnum:]]*\): *function *(/\1: function \1(/'
PROTOTYPE_SUBSTITUTION='s/prototype\.\([[:alnum:]]*\) *= *function *(/prototype\.\1 = function \1(/'
VARIABLE_SUBSTITUTION='s/\([[:alnum:]]\) *= *function *(/\1 = function \1('
# this looks like a generic map from one input set (files) to another
# can we map over tuples of [REGEX, SUBSTITUTIONS] and apply the 'map' to project
# from one set to the next set by composing `ag`, `grep`, and `sed`?
ag -l $PROTOTYPE_AG_REGEX | grep -v $EXCLUSIONS_REGEX | xargs sed -i '' $PROTOTYPE_SUBSTITUTION
ag -l $OBJECT_PROPERTY_AG_REGEX | grep -v $EXCLUSIONS_REGEX | xargs sed -i '' $OBJECT_PROPERTY_SUBSTITUTION
ag -l $CALLBACK_AG_REGEX | grep -v $EXCLUSIONS_REGEX | xargs sed -i '' $CALLBACK_SUBSTITUTION
ag -l $VARIABLE_AG_REGEX | grep -v $EXCLUSIONS_REGEX | xargs sed -i '' $VARIABLE_SUBSTITUTION
@seeflanigan
Copy link
Author

seeflanigan commented Dec 20, 2016

I think we're missing some cases in the above. For one, I think we're missing the case where a callback as a first argument.

We're also agnostic to assignment of a callback on an arbitrary property:

-exports.command = function (fn) {
+exports.command = function command(fn) {
   var error;

A callback can be the 0th, or 0+Nth argument to a function call, so we need to address 0th position as well as arbitrary position.

@seeflanigan
Copy link
Author

seeflanigan commented Dec 21, 2016

// Attempt at covering a generic case, peeling non camelcased terms from the lint output, and ultimately feeding them to `sed` as the pattern to replace.

npm run lint 2>/dev/null|ag camelcase|awk '{print $4}'| uniq | set PATTERN=$i; sed "s/$PATTERN/UNICORN/g"

// Unsure how to camelCase the expression inline, or using eg shell programming. Could shell to an installed interp eg `ruby`, `node`, etc and pass a script inline, at some point before `sed` in the pipeline.

// This works, given a specific pattern:
ag -l specific_search_identifier | xargs sed -i '' 's/specific_search_identifier/specificSearchIdentifier/g'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment