1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.getFixedSource = exports.fixInPlace = void 0;
|
7 | const fs_1 = __importDefault(require("fs"));
|
8 | const util_1 = require("util");
|
9 | function fixInPlace(file, src, info) {
|
10 | const fixedSrc = getFixedSource(src, info);
|
11 | return (0, util_1.promisify)(fs_1.default.writeFile)(file, fixedSrc, "utf8");
|
12 | }
|
13 | exports.fixInPlace = fixInPlace;
|
14 | function getFixedSource(src, info) {
|
15 | const buf = [];
|
16 | let bodyStarted = false;
|
17 | if (info.provideEnd === 0) {
|
18 | writeDeclarationHeader(buf, info);
|
19 | buf.push(src);
|
20 | }
|
21 | else {
|
22 | src.split("\n").forEach((line, index) => {
|
23 | const lineNum = index + 1;
|
24 | if (lineNum < info.provideStart) {
|
25 | buf.push(line);
|
26 | }
|
27 | else if (lineNum === info.provideStart) {
|
28 | writeDeclarationHeader(buf, info);
|
29 | }
|
30 | else if (lineNum <= info.provideEnd) {
|
31 |
|
32 | }
|
33 | else if (bodyStarted || line) {
|
34 | bodyStarted = true;
|
35 | buf.push(line);
|
36 | }
|
37 | });
|
38 | }
|
39 | return buf.join("\n");
|
40 | }
|
41 | exports.getFixedSource = getFixedSource;
|
42 | function writeDeclarationHeader(buf, info) {
|
43 | const provides = getProvideSrc(info);
|
44 | const requires = getRequireSrc(info);
|
45 | const requireTypes = getRequireTypeSrc(info);
|
46 | const forwardDeclares = getForwardDeclareSrc(info);
|
47 | if (provides.length > 0) {
|
48 | buf.push(...provides);
|
49 | buf.push("");
|
50 | }
|
51 | if (requires.length > 0) {
|
52 | buf.push(...requires);
|
53 | buf.push("");
|
54 | }
|
55 | if (requireTypes.length > 0) {
|
56 | buf.push(...requireTypes);
|
57 | buf.push("");
|
58 | }
|
59 | if (forwardDeclares.length > 0) {
|
60 | buf.push(...forwardDeclares);
|
61 | buf.push("");
|
62 | }
|
63 | }
|
64 | function getProvideSrc(info) {
|
65 | return getDeclarationSrc(info.toProvide, info.ignoredProvide, "goog.provide");
|
66 | }
|
67 | function getRequireSrc(info) {
|
68 | return getDeclarationSrc(info.toRequire, info.ignoredRequire, "goog.require");
|
69 | }
|
70 | function getRequireTypeSrc(info) {
|
71 | return getDeclarationSrc(info.toRequireType, info.ignoredRequireType, "goog.requireType");
|
72 | }
|
73 | function getForwardDeclareSrc(info) {
|
74 | return getDeclarationSrc(info.toForwardDeclare, info.ignoredForwardDeclare, "goog.forwardDeclare");
|
75 | }
|
76 | function getDeclarationSrc(to, ignored, method) {
|
77 | const declarations = to.map((namespace) => `${method}('${namespace}');`);
|
78 | const ignores = ignored.map((namespace) => `${method}('${namespace}'); // fixclosure: ignore`);
|
79 | return declarations.concat(ignores).sort();
|
80 | }
|