How to use esutils - 10 common examples

To help you get started, we’ve selected a few esutils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github yize / xswitch / src / astutils.ts View on Github external
export function adjustMultilineComment(value: string, base: string = '', specialBase: any) {
  var array, i, len, line, j, spaces, previousBase, sn;

  array = value.split(/\r\n|[\r\n]/);
  spaces = Number.MAX_VALUE;

  // first line doesn't have indentation
  for (i = 1, len = array.length; i < len; ++i) {
      line = array[i];
      j = 0;
      while (j < line.length && esutils.code.isWhiteSpace(line.charCodeAt(j))) {
          ++j;
      }
      if (spaces > j) {
          spaces = j;
      }
  }

  if (typeof specialBase !== 'undefined') {
      // pattern like
      // {
      //   var t = 20;  /*
      //                 * this is comment
      //                 */
      // }
      previousBase = base;
      if (array[1][spaces] === '*') {
github alibaba / rax / packages / babel-plugin-transform-jsx-to-html / src / index.js View on Github external
const value = convertAttributeValue(node.value || t.booleanLiteral(true));

  if (t.isStringLiteral(value) && !t.isJSXExpressionContainer(node.value)) {
    value.value = value.value.replace(/\n\s+/g, ' ');

    // "raw" JSXText should not be used from a StringLiteral because it needs to be escaped.
    if (value.extra && value.extra.raw) {
      delete value.extra.raw;
    }
  }

  if (t.isJSXNamespacedName(node.name)) {
    node.name = t.stringLiteral(
      node.name.namespace.name + ':' + node.name.name.name,
    );
  } else if (esutils.keyword.isIdentifierNameES6(node.name.name)) {
    node.name.type = 'Identifier';
  } else {
    node.name = t.stringLiteral(node.name.name);
  }

  return t.inherits(t.objectProperty(node.name, value), node);
}
github estools / esmangle / bin / esmangle.js View on Github external
if (comment.type !== 'Line') {
                this.finishCurrent();
                this.result.push(new CommentBlock(comment));
                return this;
            }

            if (!this.current) {
                this.current = new CommentBlock(comment);
                return this;
            }

            // Both comment and this.current are single line comments.
            for (i = this.current.comments[this.current.comments.length - 1].range[1], iz = comment.range[0]; i < iz; ++i) {
                code = source.charCodeAt(i);
                if (!esutils.code.isLineTerminator(code) && !esutils.code.isWhiteSpace(code)) {
                    // Not contiguous.
                    this.finishCurrent();
                    this.current = new CommentBlock(comment);
                    return this;
                }
            }

            this.current.append(comment);

            return this;
        };
github power-assert-js / espower / lib / create-node-with-loc.js View on Github external
const isValidIdentifier = (name) => {
  if (typeof name !== 'string' || keyword.isReservedWordES6(name, true)) {
    return false;
  } else if (name === 'await') {
    // invalid in module, valid in script; better be safe (see #4952)
    return false;
  } else {
    return keyword.isIdentifierNameES6(name);
  }
};
github estools / esvalid / index.js View on Github external
[].push.apply(result, fn(xs[i]));
  }
  return result;
}

// filter :: forall a. (a -> Boolean) -> [a] -> [a]
function filter(xs, predicate) {
  var filtered = [];
  for (var i = 0, l = xs.length; i < l; ++i) {
    if (predicate(xs[i])) filtered.push(xs[i]);
  }
  return filtered;
}

// isExpression :: Node -> Boolean
var isExpression = esutils.ast.isExpression;
// isStatement :: Node -> Boolean
var isStatement = esutils.ast.isStatement;
// isSourceElement :: Node -> Boolean
var isSourceElement = esutils.ast.isSourceElement;
// directives :: [Maybe Node] -> [Node]
function directives(stmts) {
  if (stmts && stmts.length > 0) {
    var s = stmts[0];
    if (s && s.type === "ExpressionStatement" && s.expression && s.expression.type === "Literal" && typeof s.expression.value === "string")
      return [s.expression.value].concat(directives([].slice.call(stmts, 1)));
  }
  return [];
}

var OBJECT_PROPERTY_KINDS = ["init", "get", "set"];
var VARIABLE_DECLARATION_KINDS = ["var", "let", "const"];
github power-assert-js / espower / lib / create-node-with-loc.js View on Github external
const isValidIdentifier = (name) => {
  if (typeof name !== 'string' || keyword.isReservedWordES6(name, true)) {
    return false;
  } else if (name === 'await') {
    // invalid in module, valid in script; better be safe (see #4952)
    return false;
  } else {
    return keyword.isIdentifierNameES6(name);
  }
};
github babel / babel / lib / 6to5 / transformation / transformers / jsx.js View on Github external
exports.XJSIdentifier = function (node) {
  if (esutils.keyword.isIdentifierName(node.name)) {
    node.type = "Identifier";
  } else {
    return t.literal(node.name);
  }
};
github sx1989827 / DOClever / Client / node_modules / babel-plugin-transform-vue-jsx / index.js View on Github external
function convertJSXIdentifier (node, parent) {
    if (t.isJSXIdentifier(node)) {
      if (node.name === 'this' && t.isReferenced(node, parent)) {
        return t.thisExpression()
      } else if (esutils.keyword.isIdentifierNameES6(node.name)) {
        node.type = 'Identifier'
      } else {
        return t.stringLiteral(node.name)
      }
    } else if (t.isJSXMemberExpression(node)) {
      return t.memberExpression(
        convertJSXIdentifier(node.object, node),
        convertJSXIdentifier(node.property, node)
      )
    }
    return node
  }
github forivall / tacoscript / src / validators.js View on Github external
export function isValidIdentifier(name: string): boolean {
  if (typeof name !== "string" || esutils.keyword.isReservedWordES6(name, true)) {
    return false;
  } else {
    return esutils.keyword.isIdentifierNameES6(name);
  }
}
github estools / esvalid / index.js View on Github external
strict = state.strict || any(function(d) { return d === "use strict"; }, directives(node.body.body));
            if (strict && !state.strict)
              recursionFn = errorsP(merge({}, state, {strict: true, inFunc: true}));
            [].push.apply(errors, recursionFn({type: "Program", body: node.body.body}));
          } else {
            [].push.apply(errors, recursionFn(node.body));
          }
        }
        break;

      case "Identifier":
        if (node.name == null)
          errors.push(new E(node, "Identifier `name` member must be non-null"));
        else if (!esutils.keyword.isIdentifierName(node.name))
          errors.push(new E(node, "Identifier `name` member must be a valid IdentifierName"));
        else if (esutils.keyword.isReservedWordES5(node.name, state.strict))
          errors.push(new E(node, "Identifier `name` member must not be a ReservedWord"));
        break;

      case "IfStatement":
        if (!isExpression(node.test))
          errors.push(new E(node, "IfStatement `test` member must be an expression node"));
        if (!isStatement(node.consequent))
          errors.push(new E(node, "IfStatement `consequent` member must be a statement node"));
        if (node.alternate != null && !isStatement(node.alternate))
          errors.push(new E(node, "IfStatement `alternate` member must be a statement node or null"));
        if (node.alternate != null && node.consequent != null && esutils.ast.isProblematicIfStatement(node))
          errors.push(new E(node, "IfStatement with null `alternate` must not be the `consequent` of an IfStatement with a non-null `alternate`"));
        if (node.test != null)
          [].push.apply(errors, recurse(node.test));
        if (node.consequent != null)
          [].push.apply(errors, recurse(node.consequent));