eval javascript, check for syntax error

后端 未结 6 495
失恋的感觉
失恋的感觉 2020-12-08 09:50

I wanted to know if it is possible to find through javascript if a call to eval() has a syntax error or undefined variable, etc... so lets say I use eval for some arbitrary

相关标签:
6条回答
  • 2020-12-08 10:13

    This Below code posted by go-oleg thanks to him

    This code validate the correct syntax otherwise return error

    Note:code is not vaildate run time error because it uses ast parser to analyze the correct syntax.

    To Install

    npm install esprima --save
    

    code:

    var esprima = require('esprima');
    var userStringToTest = 'var a = 50;';
    
    var isValid = isValidJs(userStringToTest);
    
    if(isValid) {
      alert('its validated!');
    }
    else {
      console.log('its NOT valid syntax!');
    }
    
    function isValidJs(testString) {
      var isValid = true;
      try {
        esprima.parse(testString);
      }
      catch(e) {
        isValid = false;
      }
      return isValid;
    }
    
    0 讨论(0)
  • 2020-12-08 10:21

    To continue using the code after validation, I use the following example:

    var validCode = 1;
    try {
      eval( jsCode );        /* Code test */
    } catch (e) {
      if (e instanceof SyntaxError) {
        validCode = 0;
        console.warn(e.message);
      }
    } finally {
      if(validCode){
        "do some magic"
      }
    }
    
    0 讨论(0)
  • 2020-12-08 10:24

    You can test to see if an error is indeed a SyntaxError.

    try {
        eval(code); 
    } catch (e) {
        if (e instanceof SyntaxError) {
            alert(e.message);
        }
    }
    
    0 讨论(0)
  • 2020-12-08 10:27

    According to the Mozilla documentation for eval:

    eval returns the value of the last expression evaluated.

    So I think you may be out of luck. This same document also recommends against using eval:

    eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, third party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways of which the similar Function is not susceptible.

    So regardless, please be aware of the risks before using this function.

    0 讨论(0)
  • 2020-12-08 10:32

    You can use JsLint which contains a javascript parser written in javascript. It will give you lots of information about your code, it can be configured to be more relaxed or not, etc...

    0 讨论(0)
  • 2020-12-08 10:37

    When using try-catch for catching a particular type of error one should ensure that other types of exceptions are not suppressed. Otherwise if the evaluated code throws a different kind of exception it could disappear and cause unexpected behaviour of the code.

    I would suggest writing code like this:

    try {
        eval(code); 
    } catch (e) {
        if (e instanceof SyntaxError) {
            alert(e.message);
        } else {
            throw e;
        }
    }
    

    Please note the "else" section.

    0 讨论(0)
提交回复
热议问题