Ejemplo n.º 1
0
/**
 * This takes in a require node and reprints it. This should remove whitespace
 * and allow us to have a consistent formatting of all requires.
 */
function reprintRequireHelper(node: Node): Node {
  if (jscs.ExpressionStatement.check(node)) {
    return statement`${node.expression}`;
  }

  if (jscs.VariableDeclaration.check(node)) {
    const kind = node.kind || 'const';
    const declaration = node.declarations[0];
    if (jscs.Identifier.check(declaration.id)) {
      return jscs.variableDeclaration(
        kind,
        [jscs.variableDeclarator(declaration.id, declaration.init)],
      );
    } else if (jscs.ObjectPattern.check(declaration.id)) {
      declaration.id.properties.sort((prop1, prop2) => {
        return compareStrings(prop1.key.name, prop2.key.name);
      });
      return jscs.variableDeclaration(
        kind,
        [jscs.variableDeclarator(
          oneLineObjectPattern(declaration.id),
          declaration.init,
        )],
      );
    } else if (jscs.ArrayPattern.check(declaration.id)) {
      return jscs.variableDeclaration(
        kind,
        [jscs.variableDeclarator(declaration.id, declaration.init)],
      );
    }
  }

  if (jscs.ImportDeclaration.check(node) && node.importKind === 'type') {
    // Sort the specifiers.
    node.specifiers.sort((one, two) => compareStrings(
      one.local.name,
      two.local.name
    ));
    // TODO: Properly remove new lines from the node.
    return node;
  }

  return node;
}
Ejemplo n.º 2
0
/**
 * Tests if a variable declaration is a valid require declaration.
 */
function isValidRequireDeclaration(node: Node): boolean {
  if (!hasOneRequireDeclaration(node)) {
    return false;
  }
  const declaration = node.declarations[0];
  if (jscs.Identifier.check(declaration.id)) {
    return true;
  }
  if (jscs.ObjectPattern.check(declaration.id)) {
    return declaration.id.properties.every(
      prop => prop.shorthand && jscs.Identifier.check(prop.key)
    );
  }
  if (jscs.ArrayPattern.check(declaration.id)) {
    return declaration.id.elements.every(
      element => jscs.Identifier.check(element)
    );
  }
  return false;
}
Ejemplo n.º 3
0
 element => jscs.Identifier.check(element)
Ejemplo n.º 4
0
 prop => prop.shorthand && jscs.Identifier.check(prop.key)
Ejemplo n.º 5
0
 nodes.forEach(node => {
   if (jscs.Identifier.check(node)) {
     ids.add(node.name);
   }
 });