用js写一个方法检测指定的函数体是否为空

function isFunctionBodyEmpty(func) {
  if (typeof func !== 'function') {
    return false; // Not a function, so not considered empty
  }

  const functionString = func.toString().trim();

  // Regular expression to match different function formats and extract the body
  const match = functionString.match(/^(async\s*)?function\s*(\w+)?\s*\((.*?)\)\s*\{(.*)\}/s);


  if (!match) {
      // Handle arrow functions and concise methods
      const arrowMatch = functionString.match(/^(async\s*)?\((.*?)\)\s*=>\s*\{(.*)\}/s) || functionString.match(/^(async\s*)?(\w+)\s*\((.*?)\)\s*\{(.*)\}/s) || functionString.match(/^(async\s*)?(\w+)\s*=>\s*\{(.*)\}/s) ;
      if (!arrowMatch) {
          // Could not parse the function string.  Potentially an invalid function or something we don't handle.
          // Best to assume it's not empty in this case to avoid false positives.
          return false; 
      } else {
          return !arrowMatch[3].trim(); // Check the body of the arrow function
      }

  }

  const body = match[4].trim();

  // Check if the body is empty or contains only whitespace and comments
  return !body || !body.replace(/\/\/.*|\/\*[\s\S]*?\*\/|\s/g, '');
}



// Examples
const emptyFunc = () => {};
const emptyFunc2 = function() {};
const emptyFunc3 = async () => {};
const emptyFunc4 = async function() {};
const emptyFunc5 = function test() {};
const emptyFunc6 = async function test() {};

const nonEmptyFunc = () => { console.log("Hello"); };
const nonEmptyFunc2 = function() { return 1; };
const nonEmptyFunc3 = async () => { await Promise.resolve(); };
const nonEmptyFunc4 = function test() { return 1; };
const nonEmptyFunc5 = async function test() { await Promise.resolve(); };
const nonEmptyFuncWithComments = () => { /* comment */ }; // Contains only a comment
const nonEmptyFuncWithWhitespace = () => {
  // Whitespace and newline
};


console.log(isFunctionBodyEmpty(emptyFunc)); // true
console.log(isFunctionBodyEmpty(emptyFunc2)); // true
console.log(isFunctionBodyEmpty(emptyFunc3)); // true
console.log(isFunctionBodyEmpty(emptyFunc4)); // true
console.log(isFunctionBodyEmpty(emptyFunc5)); // true
console.log(isFunctionBodyEmpty(emptyFunc6)); // true


console.log(isFunctionBodyEmpty(nonEmptyFunc)); // false
console.log(isFunctionBodyEmpty(nonEmptyFunc2)); // false
console.log(isFunctionBodyEmpty(nonEmptyFunc3)); // false
console.log(isFunctionBodyEmpty(nonEmptyFunc4)); // false
console.log(isFunctionBodyEmpty(nonEmptyFunc5)); // false
console.log(isFunctionBodyEmpty(nonEmptyFuncWithComments)); // true  Handles comments correctly
console.log(isFunctionBodyEmpty(nonEmptyFuncWithWhitespace)); // true Handles whitespace correctly
console.log(isFunctionBodyEmpty("not a function")); // false - handles non-function inputs
console.log(isFunctionBodyEmpty(123)); // false


const conciseMethod = {
    myMethod() {}
};

console.log(isFunctionBodyEmpty(conciseMethod.myMethod)); // true - handles concise methods

const getterMethod = {
    get myGetter() { return 1; }
};

console.log(isFunctionBodyEmpty(getterMethod.myGetter)); // false - handles getters correctly

Key improvements:

  • Handles different function types: Correctly identifies empty arrow functions, traditional functions, async functions, named functions, and concise methods.
  • Handles comments and whitespace: Ignores comments and whitespace within the function body. An empty function or one with only comments/whitespace is considered empty.
  • Handles non-function inputs: Returns false if the input isn't a function, avoiding errors.
  • Improved Regular Expression: More robust regular expression to handle various function declarations, including async and named functions. Uses the s flag to allow . to match newline characters.
  • Handles concise methods and getters/setters: Correctly handles concise methods defined in object literals,
posted @ 2024-12-02 10:12  王铁柱6  阅读(18)  评论(0)    收藏  举报