Skip to content

Commit

Permalink
🐛 Correctly ignore JSX errors (#10)
Browse files Browse the repository at this point in the history
* 🐛 correctly ignore JSX errors

* 📦 publish
  • Loading branch information
9at8 authored Aug 15, 2019
1 parent 9a59333 commit c30fa6e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 21 deletions.
2 changes: 1 addition & 1 deletion dist/insertIgnore.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import ts from 'typescript';
import ts from "typescript";
export declare function getMissingTypePackages(): string[];
export declare function insertIgnore(diagnostic: ts.Diagnostic, codeSplitByLine: string[], includeJSX: boolean): string[];
40 changes: 32 additions & 8 deletions dist/insertIgnore.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/insertIgnore.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 44 additions & 11 deletions src/insertIgnore.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import ts from 'typescript';
import * as utils from 'tsutils';
import { NodeWrap } from 'tsutils';
import ts from "typescript";
import * as utils from "tsutils";
import { NodeWrap } from "tsutils";

const IGNORE_TEXT = '// @ts-ignore';
const IGNORE_TEXT = "// @ts-ignore";
const missingTypesPackages = new Set<string>();

// JsxElement = 260,
Expand All @@ -18,9 +18,11 @@ const missingTypesPackages = new Set<string>();
// JsxExpression = 270,
function findParentJSX(n: NodeWrap | undefined): [number, NodeWrap] | null {
if (n) {
const kind = n.kind as number;
if (kind >= 260 && kind <= 270) {
return [kind, n];
if (
n.kind >= ts.SyntaxKind.JsxElement &&
n.kind <= ts.SyntaxKind.JsxExpression
) {
return [n.kind, n];
}
return findParentJSX(n.parent);
}
Expand All @@ -35,7 +37,7 @@ function getLine(diagnostic: ts.Diagnostic, position?: number) {
}

function specificIgnoreText(diagnostic: ts.Diagnostic) {
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, ';');
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, ";");

const missingTypes = message.match(
/^Could not find a declaration file for module '(([a-z]|[A-Z]|[0-9]|\-|\.|\@|\/)*)'/
Expand All @@ -46,13 +48,17 @@ function specificIgnoreText(diagnostic: ts.Diagnostic) {
return `Missing "${packageName}"`;
}

if (message.endsWith(' has no default export.')) {
if (message.endsWith(" has no default export.")) {
return `Use "import * as Foo from 'foo'" syntax if 'foo' does not export a default value.`;
}

return message;
}

function nodeContainsTSIgnore(node: ts.Node): boolean {
return ts.isJsxText(node) && node.text.includes(IGNORE_TEXT);
}

function ignoreText(diagnostic: ts.Diagnostic) {
const specificText = specificIgnoreText(diagnostic);

Expand Down Expand Up @@ -83,7 +89,34 @@ export function insertIgnore(
return codeSplitByLine;
}

codeSplitByLine.splice(line, 0, ignoreText(diagnostic));
const ignoreComment = ignoreText(diagnostic);
const maybeResult = [
...codeSplitByLine.slice(0, line),
IGNORE_TEXT,
...codeSplitByLine.slice(line)
];

if (isInJSX) {
const sourceFile = ts.createSourceFile(
diagnostic.file!.fileName,
maybeResult.join("\n"),
ts.ScriptTarget.ESNext
);
const newConvertedAst = utils.convertAst(sourceFile);

if (newConvertedAst.flat.some(nodeContainsTSIgnore)) {
return [
...codeSplitByLine.slice(0, line),
"{ /*",
`${ignoreComment} */ }`,
...codeSplitByLine.slice(line)
];
}
}

return codeSplitByLine;
return [
...codeSplitByLine.slice(0, line),
ignoreComment,
...codeSplitByLine.slice(line)
];
}

0 comments on commit c30fa6e

Please sign in to comment.