當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript UpdateRecorder.remove方法代碼示例

本文整理匯總了TypeScript中@angular-devkit/schematics.UpdateRecorder.remove方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript UpdateRecorder.remove方法的具體用法?TypeScript UpdateRecorder.remove怎麽用?TypeScript UpdateRecorder.remove使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在@angular-devkit/schematics.UpdateRecorder的用法示例。


在下文中一共展示了UpdateRecorder.remove方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: removePropertyInAstObject

export function removePropertyInAstObject(
  recorder: UpdateRecorder,
  node: JsonAstObject,
  propertyName: string,
) {
  // Find the property inside the object.
  const propIdx = node.properties.findIndex(prop => prop.key.value === propertyName);

  if (propIdx === -1) {
    // There's nothing to remove.
    return;
  }

  if (node.properties.length === 1) {
    // This is a special case. Everything should be removed, including indentation.
    recorder.remove(node.start.offset, node.end.offset - node.start.offset);
    recorder.insertRight(node.start.offset, '{}');

    return;
  }

  // The AST considers commas and indentation to be part of the preceding property.
  // To get around messy comma and identation management, we can work over the range between
  // two properties instead.
  const previousProp = node.properties[propIdx - 1];
  const targetProp = node.properties[propIdx];
  const nextProp = node.properties[propIdx + 1];

  let start, end;
  if (previousProp) {
    // Given the object below, and intending to remove the `m` property:
    // "{\n  \"a\": \"a\",\n  \"m\": \"m\",\n  \"z\": \"z\"\n}"
    //                        ^---------------^
    // Removing the range above results in:
    // "{\n  \"a\": \"a\",\n  \"z\": \"z\"\n}"
    start = previousProp.end;
    end = targetProp.end;
  } else {
    // If there's no previousProp there is a nextProp, since we've specialcased the 1 length case.
    // Given the object below, and intending to remove the `a` property:
    // "{\n  \"a\": \"a\",\n  \"m\": \"m\",\n  \"z\": \"z\"\n}"
    //       ^---------------^
    // Removing the range above results in:
    // "{\n  \"m\": \"m\",\n  \"z\": \"z\"\n}"
    start = targetProp.start;
    end = nextProp.start;
  }

  recorder.remove(start.offset, end.offset - start.offset);
  if (!nextProp) {

    recorder.insertRight(start.offset, '\n');
  }
}
開發者ID:angular,項目名稱:angular-cli,代碼行數:54,代碼來源:json-utils.ts

示例2: replacePropertyInAstObject

function replacePropertyInAstObject(
    recorder: UpdateRecorder, node: JsonAstObject, propertyName: string, value: JsonValue,
    indent: number) {
  const property = findPropertyInAstObject(node, propertyName);
  if (property === null) {
    throw new Error(`Property ${propertyName} does not exist in JSON object`);
  }
  const {start, text} = property;
  recorder.remove(start.offset, text.length);
  const indentStr = '\n' +
      ' '.repeat(indent);
  const content = JSON.stringify(value, null, '  ').replace(/\n/g, indentStr);
  recorder.insertLeft(start.offset, content);
}
開發者ID:cyrilletuzi,項目名稱:angular,代碼行數:14,代碼來源:index.ts

示例3: removeKeyValueInAstObject

export function removeKeyValueInAstObject(
    recorder: UpdateRecorder, content: string, node: JsonAstObject, key: string) {
  for (const [i, prop] of node.properties.entries()) {
    if (prop.key.value === key) {
      const start = prop.start.offset;
      const end = prop.end.offset;
      let length = end - start;
      const match = content.slice(end).match(/[,\s]+/);
      if (match) {
        length += match.pop() !.length;
      }
      recorder.remove(start, length);
      if (i === node.properties.length - 1) {  // last property
        let offset = 0;
        while (/(,|\s)/.test(content.charAt(start - offset - 1))) {
          offset++;
        }
        recorder.remove(start - offset, offset);
      }
      return;
    }
  }
}
開發者ID:BobChao87,項目名稱:angular,代碼行數:23,代碼來源:json-utils.ts

示例4: recordQueryUsageTransformation

/**
 * Transforms the query decorator by explicitly specifying the timing based on the
 * determined timing. The changes will be added to the specified update recorder.
 */
function recordQueryUsageTransformation(
    query: NgQueryDefinition, recorder: UpdateRecorder, timing: QueryTiming, printer: ts.Printer,
    sourceFile: ts.SourceFile) {
  const queryExpr = query.decorator.node.expression as ts.CallExpression;
  const queryArguments = queryExpr.arguments;
  const timingPropertyAssignment = ts.createPropertyAssignment(
      'static', timing === QueryTiming.STATIC ? ts.createTrue() : ts.createFalse());
  let newCallText = '';

  // If the query decorator is already called with two arguments, we need to
  // keep the existing options untouched and just add the new property if needed.
  if (queryArguments.length === 2) {
    const existingOptions = queryArguments[1] as ts.ObjectLiteralExpression;

    // In case the options already contains a property for the "static" flag, we just
    // skip this query and leave it untouched.
    if (existingOptions.properties.some(
            p => !!p.name && getPropertyNameText(p.name) === 'static')) {
      return;
    }

    const updatedOptions = ts.updateObjectLiteral(
        existingOptions, existingOptions.properties.concat(timingPropertyAssignment));
    const updatedCall = ts.updateCall(
        queryExpr, queryExpr.expression, queryExpr.typeArguments,
        [queryArguments[0], updatedOptions]);
    newCallText = printer.printNode(ts.EmitHint.Unspecified, updatedCall, sourceFile);
  } else {
    const newCall = ts.updateCall(
        queryExpr, queryExpr.expression, queryExpr.typeArguments,
        [queryArguments[0], ts.createObjectLiteral([timingPropertyAssignment])]);
    newCallText = printer.printNode(ts.EmitHint.Unspecified, newCall, sourceFile);
  }

  recorder.remove(queryExpr.getStart(), queryExpr.getWidth());
  recorder.insertRight(queryExpr.getStart(), newCallText);
}
開發者ID:zackarychapple,項目名稱:angular,代碼行數:41,代碼來源:migration.ts


注:本文中的@angular-devkit/schematics.UpdateRecorder.remove方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。