Skip to content
This repository was archived by the owner on Aug 13, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/ast-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

"use strict";

const path = require("path");

module.exports = {
isTypeScriptParserServices(parserServices) {
// Check properties specific to @typescript-eslint/parser
Expand Down Expand Up @@ -39,6 +41,15 @@ module.exports = {
}
return "any";
},
hasDeclarationInFile(type, fileName) {
const symbol = type.getSymbol();
return Boolean(
symbol &&
symbol
.getDeclarations()
?.some((declaration) => path.basename(declaration.getSourceFile().fileName) === fileName)
);
},
isDocumentObject(node, context, fullTypeChecker) {
if (fullTypeChecker) {
const type = this.getNodeTypeAsString(fullTypeChecker, node, context);
Expand Down
15 changes: 13 additions & 2 deletions lib/rules/no-inner-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,19 @@ module.exports = {
const fullTypeChecker = astUtils.getFullTypeChecker(context);

function mightBeHTMLElement(node) {
const type = astUtils.getNodeTypeAsString(fullTypeChecker, node, context);
return type.match(/HTML.*Element/) || type === "any";
if (!fullTypeChecker) {
return true;
}

const tsNode = context.sourceCode.parserServices.esTreeNodeToTSNodeMap.get(node);
const tsType = fullTypeChecker.getTypeAtLocation(tsNode);
const type = fullTypeChecker.typeToString(tsType);
return (
type.match(/HTML.*Element/) ||
type === "any" ||
// A local type can also be named Element, so only trust the DOM library declaration.
(type === "Element" && astUtils.hasDeclarationInFile(tsType, "lib.dom.d.ts"))
);
}

return {
Expand Down
17 changes: 15 additions & 2 deletions lib/rules/no-postmessage-star-origin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@

const astUtils = require("../ast-utils");

function isWindowOrAny(fullTypeChecker, type) {
const typeName = fullTypeChecker.typeToString(type);
return (
typeName === "any" ||
// A local type can also be named Window, so only trust the DOM library declaration.
(typeName === "Window" && astUtils.hasDeclarationInFile(type, "lib.dom.d.ts"))
);
}

module.exports = {
meta: {
type: "suggestion",
Expand Down Expand Up @@ -40,8 +49,12 @@ module.exports = {
node.callee.object
);
const tsType = fullTypeChecker.getTypeAtLocation(tsNode);
const type = fullTypeChecker.typeToString(tsType);
if (type !== "any" && type !== "Window") {
// Unions such as Window | null must be checked one constituent at a time.
if (tsType.isUnionOrIntersection()) {
if (tsType.types.every((t) => !isWindowOrAny(fullTypeChecker, t))) {
return;
}
} else if (!isWindowOrAny(fullTypeChecker, tsType)) {
return;
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"compilerOptions": {
"strictNullChecks": true
},
"include": ["estree.ts"]
}
21 changes: 21 additions & 0 deletions tests/lib/rules/no-inner-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ ruleTester.run(ruleId, rule, {
test.innerHTML = test;
test.outerHTML = test;
`
},
{
languageOptions: testUtils.tsLanguageOptions,
code: `
function main() {
class Element {
innerHTML = "";
}
const element = new Element();
element.innerHTML = "test";
}
`
}
],
invalid: [
Expand All @@ -48,6 +60,15 @@ ruleTester.run(ruleId, rule, {
{ messageId: "noInsertAdjacentHTML", line: 5 }
]
},
{
languageOptions: testUtils.tsLanguageOptions,
code: `
function main(element: Element) {
element.innerHTML = "test";
}
`,
errors: [{ messageId: "noInnerHtml", line: 3 }]
},
{
code: `
element.innerHTML = 'test';
Expand Down
21 changes: 21 additions & 0 deletions tests/lib/rules/no-postmessage-star-origin.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ class WindowLike {
function main() {
var w: WindowLike = new WindowLike();
w.postMessage('test', '*');
}
`
},
{
languageOptions: testUtils.tsLanguageOptions,
code: `
function main() {
class Window {
postMessage(): void {
};
}
const target = new Window();
target.postMessage('test', '*');
}
`
}
Expand All @@ -52,6 +65,14 @@ function main() {
{ messageId: "default", line: 2 },
{ messageId: "default", line: 4 }
]
},
{
languageOptions: testUtils.tsLanguageOptions,
code: `
declare const target: Window | null;
target.postMessage(message, "*");
`,
errors: [{ messageId: "default", line: 3 }]
}
]
});
Loading