• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript schematics.UpdateRecorder类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中@angular-devkit/schematics.UpdateRecorder的典型用法代码示例。如果您正苦于以下问题:TypeScript UpdateRecorder类的具体用法?TypeScript UpdateRecorder怎么用?TypeScript UpdateRecorder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了UpdateRecorder类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1: appendPropertyInAstObject

export function appendPropertyInAstObject(
  recorder: UpdateRecorder,
  node: JsonAstObject,
  propertyName: string,
  value: JsonValue,
  indent: number,
) {
  const indentStr = _buildIndent(indent);
  let index = node.start.offset + 1;
  if (node.properties.length > 0) {
    // Insert comma.
    const last = node.properties[node.properties.length - 1];
    const { text, end } = last;
    const commaIndex = text.endsWith('\n') ? end.offset - 1 : end.offset;
    recorder.insertRight(commaIndex, ',');
    index = end.offset;
  }
  const content = JSON.stringify(value, null, indent).replace(/\n/g, indentStr);
  recorder.insertRight(
    index,
    (node.properties.length === 0 && indent ? '\n' : '')
    + ' '.repeat(indent)
    + `"${propertyName}":${indent ? ' ' : ''}${content}`
    + indentStr.slice(0, -indent),
  );
}
开发者ID:angular,项目名称:angular-cli,代码行数:26,代码来源:json-utils.ts


示例2: 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


示例3: 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


示例4: appendValueInAstArray

export function appendValueInAstArray(
  recorder: UpdateRecorder,
  node: JsonAstArray,
  value: JsonValue,
  indent = 4,
) {
  const indentStr = _buildIndent(indent);

  if (node.elements.length > 0) {
    // Insert comma.
    const last = node.elements[node.elements.length - 1];
    recorder.insertRight(last.start.offset + last.text.replace(/\s+$/, '').length, ',');
  }

  recorder.insertLeft(
    node.end.offset - 1,
    '  '
    + JSON.stringify(value, null, 2).replace(/\n/g, indentStr)
    + indentStr.slice(0, -2),
  );
}
开发者ID:fmalcher,项目名称:angular-cli,代码行数:21,代码来源:json-utils.ts


示例5: insertPropertyInAstObjectInOrder

export function insertPropertyInAstObjectInOrder(
  recorder: UpdateRecorder,
  node: JsonAstObject,
  propertyName: string,
  value: JsonValue,
  indent: number,
) {

  if (node.properties.length === 0) {
    appendPropertyInAstObject(recorder, node, propertyName, value, indent);

    return;
  }

  // Find insertion info.
  let insertAfterProp: JsonAstKeyValue | null = null;
  let prev: JsonAstKeyValue | null = null;
  let isLastProp = false;
  const last = node.properties[node.properties.length - 1];
  for (const prop of node.properties) {
    if (prop.key.value > propertyName) {
      if (prev) {
        insertAfterProp = prev;
      }
      break;
    }
    if (prop === last) {
      isLastProp = true;
      insertAfterProp = last;
    }
    prev = prop;
  }

  if (isLastProp) {
    appendPropertyInAstObject(recorder, node, propertyName, value, indent);

    return;
  }

  const indentStr = _buildIndent(indent);

  const insertIndex = insertAfterProp === null
    ? node.start.offset + 1
    : insertAfterProp.end.offset + 1;

  recorder.insertRight(
    insertIndex,
    `${indentStr}`
    + `"${propertyName}": ${JSON.stringify(value, null, 2).replace(/\n/g, indentStr)}`
    + ',',
  );
}
开发者ID:fmalcher,项目名称:angular-cli,代码行数:52,代码来源:json-utils.ts


示例6: appendPropertyInAstObject

function appendPropertyInAstObject(
  recorder: UpdateRecorder,
  node: JsonAstObject,
  propertyName: string,
  value: JsonValue,
  indent = 4,
) {
  const indentStr = '\n' + new Array(indent + 1).join(' ');

  if (node.properties.length > 0) {
    // Insert comma.
    const last = node.properties[node.properties.length - 1];
    recorder.insertRight(last.start.offset + last.text.replace(/\s+$/, '').length, ',');
  }

  recorder.insertLeft(
    node.end.offset - 1,
    '  '
    + `"${propertyName}": ${JSON.stringify(value, null, 2).replace(/\n/g, indentStr)}`
    + indentStr.slice(0, -2),
  );
}
开发者ID:angular,项目名称:angular-cli,代码行数:22,代码来源:factory.ts


示例7: 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


示例8: appendValueInAstArray

export function appendValueInAstArray(
  recorder: UpdateRecorder,
  node: JsonAstArray,
  value: JsonValue,
  indent = 4,
) {
  const indentStr = _buildIndent(indent);
  let index = node.start.offset + 1;
  if (node.elements.length > 0) {
    // Insert comma.
    const last = node.elements[node.elements.length - 1];
    recorder.insertRight(last.end.offset, ',');
    index = indent ? last.end.offset + 1 : last.end.offset;
  }

  recorder.insertRight(
    index,
    (node.elements.length === 0 && indent ? '\n' : '')
    + ' '.repeat(indent)
    + JSON.stringify(value, null, indent).replace(/\n/g, indentStr)
    + indentStr.slice(0, -indent),
  );
}
开发者ID:angular,项目名称:angular-cli,代码行数:23,代码来源:json-utils.ts


示例9: 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类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript testing.SchematicTestRunner类代码示例发布时间:2022-05-28
下一篇:
TypeScript schematics.Tree类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap