1 | /**
|
2 | * @license
|
3 | * Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
|
4 | * This code may only be used under the BSD style license found at
|
5 | * http://polymer.github.io/LICENSE.txt The complete set of authors may be found
|
6 | * at http://polymer.github.io/AUTHORS.txt The complete set of contributors may
|
7 | * be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by
|
8 | * Google as part of the polymer project is also subject to an additional IP
|
9 | * rights grant found at http://polymer.github.io/PATENTS.txt
|
10 | */
|
11 |
|
12 | export function quotePropertyName(name: string): string {
|
13 | // TODO We should escape reserved words, and there are many more safe
|
14 | // characters than are included in this RegExp.
|
15 | // See https://mathiasbynens.be/notes/javascript-identifiers-es6
|
16 | const safe = name.match(/^[_$a-zA-Z][_$a-zA-Z0-9]*$/);
|
17 | return safe ? name : JSON.stringify(name);
|
18 | }
|
19 |
|
20 | const indentSpaces = 2;
|
21 |
|
22 | export function indent(depth: number): string {
|
23 | return ' '.repeat(depth * indentSpaces);
|
24 | }
|
25 |
|
26 | export function formatComment(comment: string, depth: number): string {
|
27 | // Make sure we don't end our comment early by printing out the `*/` end
|
28 | // comment sequence if it is contained in the comment. Escape it as `*\/`
|
29 | // instead. One way this sequence could get here is if an HTML comment
|
30 | // embedded a JavaScript style block comment.
|
31 | comment = comment.replace(/\*\//g, '*\\/');
|
32 |
|
33 | // Indent the comment one space so that it doesn't touch the `*` we add next,
|
34 | // but only if there is a character there. If we also indented blank lines by
|
35 | // one space, then they would have an unneccessary space after the `*`.
|
36 | comment = comment.replace(/^(.)/gm, ' $1');
|
37 |
|
38 | // Indent to the given level and add the `*` character.
|
39 | const i = indent(depth);
|
40 | comment = comment.replace(/^/gm, `${i} *`);
|
41 |
|
42 | return `${i}/**\n${comment}\n${i} */\n`;
|
43 | }
|