How to use the tslint.Replacement function in tslint

To help you get started, we’ve selected a few tslint 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 mgechev / codelyzer / src / angularWhitespaceRule.ts View on Github external
} else {
      replacements.push(new Lint.Replacement(exprEnd + 1, 0, ' '));
    }

    // Handling the left side of the pipe
    if (exprText[exprText.length - 1] === ' ') {
      let ignoreSpace = 1;
      while (exprText[exprText.length - 1 - ignoreSpace] === ' ') {
        ignoreSpace += 1;
      }
      if (ignoreSpace > 1) {
        replacements.push(new Lint.Replacement(exprEnd - ignoreSpace, ignoreSpace, ' '));
      }
    } else {
      if (!parentheses) {
        replacements.push(new Lint.Replacement(exprEnd, 0, ' '));
      }
    }

    if (replacements.length) {
      context.addFailureAt(
        ast.exp.span.end - 1,
        3,
        'The pipe operator should be surrounded by one space on each side, i.e. " | ".',
        replacements
      );
    }
    super.visitPipe(ast, context);
    return null;
  }
github angular / angular / packages / core / schematics / migrations / google3 / injectablePipeRule.ts View on Github external
visitor.missingInjectablePipes.forEach(data => {
      const {pipeDecorator, importDeclarationMissingImport} = data;
      const fixes = [new Replacement(
          pipeDecorator.getStart(), pipeDecorator.getWidth(),
          `@${INJECTABLE_DECORATOR_NAME}()\n${pipeDecorator.getText()}`)];

      if (importDeclarationMissingImport) {
        const namedImports = getNamedImports(importDeclarationMissingImport);

        // Add another fix that'll add the missing import.
        if (namedImports) {
          fixes.push(new Replacement(
              namedImports.getStart(), namedImports.getWidth(),
              printer.printNode(
                  ts.EmitHint.Unspecified, addImport(namedImports, INJECTABLE_DECORATOR_NAME),
                  sourceFile)));
        }
      }
github ajafff / tslint-consistent-codestyle / rules / oddnessCheckRule.ts View on Github external
const cb = (node: ts.Node): void => {
            if (utils.isBinaryExpression(node) &&
                node.operatorToken.kind === ts.SyntaxKind.PercentToken &&
                utils.isNumericLiteral(node.right) &&
                node.right.text === '2') {
                // TODO if this is part of a comparison with a negative value, this failure would be a false positive
                const start = node.operatorToken.getStart(sourceFile);
                this.addFailure(start, node.right.end, FAILURE_STRING, [
                    new Lint.Replacement(start, 1, '&'),
                    new Lint.Replacement(node.right.end - 1, 1, '1'),
                ]);
            }
            return ts.forEachChild(node, cb);
        };
        return ts.forEachChild(sourceFile, cb);
github jonaskello / tslint-immutable / src / noLetRule.ts View on Github external
// "let" to "const" or not? For now we change to const if at least one variable is invalid.
    let addFix = true;
    for (const variableDeclarationNode of declarationList.declarations) {
      if (
        !Ignore.shouldIgnore(
          variableDeclarationNode,
          ctx.options,
          ctx.sourceFile
        )
      ) {
        invalidVariableDeclarationNodes.push(
          createInvalidNode(
            variableDeclarationNode,
            addFix
              ? [
                  new Lint.Replacement(
                    declarationList.getStart(ctx.sourceFile),
                    "let".length,
                    "const"
                  )
                ]
              : []
          )
        );
        addFix = false;
      }
    }
    return { invalidNodes: invalidVariableDeclarationNodes };
  }
  return { invalidNodes: [] };
}
github jonaskello / tslint-immutable / src / readonlyArrayRule.ts View on Github external
function getReadonlyKeywordFix(
  node: ts.ArrayTypeNode | ts.TupleTypeNode,
  ctx: Lint.WalkContext
): Lint.Replacement[] {
  // Nested shorthand syntax array?
  if (utils.isArrayTypeNode(node.parent)) {
    return [
      new Lint.Replacement(node.getStart(ctx.sourceFile), 0, "(readonly "),
      new Lint.Replacement(node.end, 0, ")")
    ];
  }
  return [new Lint.Replacement(node.getStart(ctx.sourceFile), 0, "readonly ")];
}
github intershop / intershop-pwa / tslint-rules / src / banSpecificImportsRule.ts View on Github external
return;
        }

        if (pattern.import) {
          importList
            .filter(token => new RegExp(pattern.import).test(token.getText()))
            .forEach(token =>
              ctx.addFailureAtNode(
                token,
                pattern.message || `Using '${token.getText()}' from '${fromStringText}' is banned.`
              )
            );
        } else {
          let fix;
          if (pattern.fix) {
            fix = new Lint.Replacement(
              fromStringToken.getStart(),
              fromStringToken.getWidth(),
              `'${fromStringText.replace(new RegExp(pattern.from), pattern.fix)}'`
            );
          }
          ctx.addFailureAtNode(fromStringToken, pattern.message || `Importing from '${fromStringText} is banned.`, fix);
        }
      }
    });
  }
github mgechev / codelyzer / src / angularWhitespaceRule.ts View on Github external
location: 'start' | 'end',
        fixTo: string,
        position: number,
        absolutePosition: number,
        lengthFix: number
      ) => {
        const { length } = subMatch;
        if (length === 1) {
          return;
        }
        const errorText = length === 0 ? 'Missing' : 'Extra';
        context.addFailureAt(
          position,
          length + lengthFix,
          `${errorText} whitespace in interpolation ${location}; expecting ${InterpolationOpen} expr ${InterpolationClose}`,
          [new Lint.Replacement(absolutePosition, length + lengthFix, fixTo)]
        );
      };
github jonaskello / tslint-immutable / src / readonlyArrayRule.ts View on Github external
function getReadonlyArrayFix(
  node: ts.ArrayTypeNode,
  ctx: Lint.WalkContext
): Lint.Replacement[] {
  return [
    new Lint.Replacement(node.getStart(ctx.sourceFile), 0, "ReadonlyArray<"),
    new Lint.Replacement(node.end - 2, 2, ">")
  ];
}
github angular / components / src / cdk / schematics / ng-update / upgrade-rules / attribute-selectors / attributeSelectorsTemplateRule.ts View on Github external
        .map(start => new Replacement(start, selector.replace.length, selector.replaceWith))
        .forEach(replacement => replacements.push({replacement, failureMessage}));
github palantir / blueprint / packages / tslint-config / src / rules / utils / replaceTagName.ts View on Github external
export function replaceTagName(tagName: JsxTagNameExpression, newTagName: string) {
    return new Replacement(tagName.getFullStart(), tagName.getFullWidth(), newTagName);
}