index.js 840 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. var acorn = require('acorn');
  3. var objectAssign = require('object-assign');
  4. module.exports = isExpression;
  5. var DEFAULT_OPTIONS = {
  6. throw: false,
  7. strict: false,
  8. lineComment: false
  9. };
  10. function isExpression(src, options) {
  11. options = objectAssign({}, DEFAULT_OPTIONS, options);
  12. try {
  13. var parser = new acorn.Parser(options, src, 0);
  14. if (options.strict) {
  15. parser.strict = true;
  16. }
  17. if (!options.lineComment) {
  18. parser.skipLineComment = function (startSkip) {
  19. this.raise(this.pos, 'Line comments not allowed in an expression');
  20. };
  21. }
  22. parser.nextToken();
  23. parser.parseExpression();
  24. if (parser.type !== acorn.tokTypes.eof) {
  25. parser.unexpected();
  26. }
  27. } catch (ex) {
  28. if (!options.throw) {
  29. return false;
  30. }
  31. throw ex;
  32. }
  33. return true;
  34. }