TypeScript tsutils/util isAssignmentKind示例

说明

typescript tsutils/util isassignmentkind示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: TypeScript

命名空间/包名称: tsutils/util

示例#1
文件: noArrayMutationRule.ts 项目: jonaskello/tslint-immutable

/**
 * No assignment with array[index] on the left.
 * No assignment with array.property on the left (e.g. array.length).
 */
function checkBinaryExpression(
  node: ts.BinaryExpression,
  ctx: Lint.WalkContext<Options>,
  checker: ts.TypeChecker
): CheckNodeResult {
  if (
    !Ignore.isIgnoredPrefix(
      node.getText(node.getSourceFile()),
      ctx.options.ignorePrefix
    ) &&
    isAssignmentKind(node.operatorToken.kind) &&
    isAccessExpression(node.left)
  ) {
    const leftExpressionType = checker.getTypeAtLocation(
      getRootAccessExpression(node.left).expression
    );

    if (isArrayType(leftExpressionType)) {
      return { invalidNodes: [createInvalidNode(node, [])] };
    }
  }
  return { invalidNodes: [] };
}

示例#2
文件: noObjectMutationRule.ts 项目: jonaskello/tslint-immutable

function checkNode(
  node: ts.Node,
  ctx: Lint.WalkContext<Options>,
  checker: ts.TypeChecker
): CheckNodeResult {
  let invalidNodes: Array<InvalidNode> = [];

  // No assignment with object.property on the left
  if (
    utils.isBinaryExpression(node) &&
    isAccessExpression(node.left) &&
    utils.isBinaryExpression(node) &&
    isAssignmentKind(node.operatorToken.kind) &&
    !Ignore.isIgnoredPrefix(
      node.getText(node.getSourceFile()),
      ctx.options.ignorePrefix
    ) &&
    !inConstructor(node)
  ) {
    invalidNodes = [...invalidNodes, createInvalidNode(node, [])];
  }

  // No deleting object properties
  if (
    utils.isDeleteExpression(node) &&
    isAccessExpression(node.expression) &&
    !Ignore.isIgnoredPrefix(
      node.expression.getText(node.getSourceFile()),
      ctx.options.ignorePrefix
    )
  ) {
    invalidNodes = [...invalidNodes, createInvalidNode(node, [])];
  }

  // No prefix inc/dec
  if (
    utils.isPrefixUnaryExpression(node) &&
    isAccessExpression(node.operand) &&
    forbidUnaryOps.some(o => o === node.operator) &&
    !Ignore.isIgnoredPrefix(
      node.operand.getText(node.getSourceFile()),
      ctx.options.ignorePrefix
    )
  ) {
    invalidNodes = [...invalidNodes, createInvalidNode(node, [])];
  }

  // No postfix inc/dec
  if (
    utils.isPostfixUnaryExpression(node) &&
    isAccessExpression(node.operand) &&
    forbidUnaryOps.some(o => o === node.operator) &&
    !Ignore.isIgnoredPrefix(
      node.getText(node.getSourceFile()),
      ctx.options.ignorePrefix
    )
  ) {
    invalidNodes = [...invalidNodes, createInvalidNode(node, [])];
  }

  // No Object.assign on identifiers.
  if (
    utils.isCallExpression(node) &&
    utils.isPropertyAccessExpression(node.expression) &&
    utils.isIdentifier(node.expression.name) &&
    node.expression.name.text === "assign" &&
    node.arguments.length >= 2 &&
    (utils.isIdentifier(node.arguments[0]) ||
      utils.isPropertyAccessExpression(node.arguments[0])) &&
    !Ignore.isIgnoredPrefix(
      node.arguments[0].getText(node.arguments[0].getSourceFile()),
      ctx.options.ignorePrefix
    ) &&
    // Do type checking as late as possible as it is expensive.
    isObjectConstructorType(
      checker.getTypeAtLocation(node.expression.expression)
    )
  ) {
    invalidNodes = [...invalidNodes, createInvalidNode(node, [])];
  }

  return { invalidNodes };
}

展开阅读全文