forked from quizlet/ts-migration
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinsertIgnore.js
131 lines (131 loc) · 5.04 KB
/
insertIgnore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.insertIgnore = exports.getMissingTypePackages = void 0;
const typescript_1 = __importDefault(require("typescript"));
const utils = __importStar(require("tsutils"));
const IGNORE_TEXT = "// @ts-ignore";
const missingTypesPackages = new Set();
// JsxElement = 260,
// JsxSelfClosingElement = 261,
// JsxOpeningElement = 262,
// JsxClosingElement = 263,
// JsxFragment = 264,
// JsxOpeningFragment = 265,
// JsxClosingFragment = 266,
// JsxAttribute = 267,
// JsxAttributes = 268,
// JsxSpreadAttribute = 269,
// JsxExpression = 270,
function findParentJSX(n) {
if (n) {
if (n.kind >= typescript_1.default.SyntaxKind.JsxElement &&
n.kind <= typescript_1.default.SyntaxKind.JsxExpression) {
return [n.kind, n];
}
return findParentJSX(n.parent);
}
return null;
}
function getLine(diagnostic, position) {
const { line } = diagnostic.file.getLineAndCharacterOfPosition(position || diagnostic.start);
return line;
}
function specificIgnoreText(diagnostic) {
const message = typescript_1.default.flattenDiagnosticMessageText(diagnostic.messageText, ";");
const code = diagnostic.code ? ` (${diagnostic.code})` : "";
const missingTypes = message.match(/^Could not find a declaration file for module '(([a-z]|[A-Z]|[0-9]|\-|\.|\@|\/)*)'/);
if (missingTypes) {
const packageName = `@types/${missingTypes[1]}`;
missingTypesPackages.add(packageName);
return `Missing "${packageName}"${code}`;
}
if (message.endsWith(" has no default export.")) {
return `Use "import * as Foo from 'foo'" syntax if 'foo' does not export a default value.${code}`;
}
return `${message}${code}`;
}
function nodeContainsTSIgnore(node) {
return typescript_1.default.isJsxText(node) && node.text.includes(IGNORE_TEXT);
}
function ignoreText(diagnostic, rootDir) {
const specificText = specificIgnoreText(diagnostic);
return specificText == null
? IGNORE_TEXT
: `${IGNORE_TEXT} -- ${specificText.replace(rootDir, "")}`;
}
function getMissingTypePackages() {
return [...missingTypesPackages].sort();
}
exports.getMissingTypePackages = getMissingTypePackages;
function insertIgnore(diagnostic, codeSplitByLine, includeJSX, rootDir) {
const convertedAST = utils.convertAst(diagnostic.file);
const n = utils.getWrappedNodeAtPosition(convertedAST.wrapped, diagnostic.start);
const line = getLine(diagnostic);
const isInJSX = findParentJSX(n);
if (!includeJSX) {
// Don't add ignores in JSX since it's too hard.
return codeSplitByLine;
}
const ignoreComment = ignoreText(diagnostic, rootDir);
const maybeResult = [
...codeSplitByLine.slice(0, line),
IGNORE_TEXT,
...codeSplitByLine.slice(line)
];
if (isInJSX) {
const sourceFile = typescript_1.default.createSourceFile(diagnostic.file.fileName, maybeResult.join("\n"), typescript_1.default.ScriptTarget.ESNext);
const newConvertedAst = utils.convertAst(sourceFile);
if (newConvertedAst.flat.some(nodeContainsTSIgnore)) {
return [
...codeSplitByLine.slice(0, line),
"{ /*",
`${ignoreComment} */ }`,
...codeSplitByLine.slice(line)
];
}
}
// Ensure proper sequencing of eslint ignores and ts-ignores
if (codeSplitByLine.length > 0 &&
line > 0 &&
codeSplitByLine[line - 1].includes("// eslint-disable-next-line")) {
return [
...codeSplitByLine.slice(0, line - 1),
ignoreComment,
codeSplitByLine[line - 1],
...codeSplitByLine.slice(line)
];
}
return [
...codeSplitByLine.slice(0, line),
ignoreComment,
...codeSplitByLine.slice(line)
];
}
exports.insertIgnore = insertIgnore;
//# sourceMappingURL=insertIgnore.js.map