| 116 | } |
| 117 | |
| 118 | toString(normalize = true) { |
| 119 | const content = this.contents |
| 120 | .filter((x) => x.content !== EMPTY) |
| 121 | .map((x) => x.toString()) |
| 122 | .join('\n') |
| 123 | if (!normalize) { |
| 124 | return content |
| 125 | } |
| 126 | |
| 127 | const name = this.options.filename || 'index.js' |
| 128 | const isTs = /\.tsx?$/.test(name) |
| 129 | |
| 130 | try { |
| 131 | return format(content, { |
| 132 | ...config, |
| 133 | filepath: this.options.filename, |
| 134 | parser: isTs ? 'typescript' : 'babel', |
| 135 | plugins: [isTs ? parserTs : parserBabel] |
| 136 | }) |
| 137 | .replace(/^;/, '') |
| 138 | .trim() |
| 139 | } catch (err) { |
| 140 | err.message = `${content} |
| 141 | |
| 142 | ${err.message}` |
| 143 | throw err |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | export function createLineContentsByContent(content: string, options?: ContentParserOptions) { |