UNPKG

2.14 kBJavaScriptView Raw
1'use strict';
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var BaseFormatter_1 = require("./utils/BaseFormatter");
14var Formatter = (function (_super) {
15 __extends(Formatter, _super);
16 function Formatter() {
17 return _super.call(this, 'no-require-imports', function (failure) {
18 var fileName = failure.getFileName();
19 var fileContents = this.readFile(fileName);
20 var start = failure.getStartPosition().getPosition();
21 var end = failure.getEndPosition().getPosition();
22 var importStartIndex = fileContents.lastIndexOf('\n', start) + 1;
23 if (importStartIndex === -1) {
24 importStartIndex = 0;
25 }
26 var importEndIndex = fileContents.indexOf('\n', end);
27 var leftSide = fileContents.substring(0, importStartIndex);
28 var middle = fileContents.substring(importStartIndex, importEndIndex).trim();
29 var rightSide = fileContents.substring(importEndIndex);
30 var newImport = middle.replace(/import\s+(.*)\s+=\s*require\(((.|\s)*)\);/m, 'import {$1} from $2;');
31 newImport = newImport.replace(/from \n/, 'from\n');
32 var newContent = leftSide + newImport + rightSide;
33 this.writeFile(fileName, newContent);
34 console.log('Automatically converting require-style import to an ES6 import. Please re-compile and re-lint: ' + fileName);
35 }) || this;
36 }
37 return Formatter;
38}(BaseFormatter_1.BaseFormatter));
39exports.Formatter = Formatter;
40//# sourceMappingURL=fixNoRequireImportsFormatter.js.map
\No newline at end of file