Skip to content

Commit

Permalink
Update no-nullable-attribute-binding fix for null attribute (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewJakubowicz authored Jan 5, 2024
1 parent 9f44f6f commit 6d7dbe8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,15 @@ const rule: RuleModule = {
if (htmlAttr.kind !== HtmlNodeAttrKind.ATTRIBUTE) return;

const { typeB } = extractBindingTypes(assignment, context);
const isAssignableToNull = isAssignableToSimpleTypeKind(typeB, "NULL");

// Test if removing "null" from typeB would work and suggest using "ifDefined(exp === null ? undefined : exp)".
if (isAssignableToSimpleTypeKind(typeB, "NULL")) {
// Test if removing "undefined" or "null" from typeB would work and suggest using "ifDefined".
if (isAssignableToNull || isAssignableToSimpleTypeKind(typeB, "UNDEFINED")) {
context.report({
location: rangeFromHtmlNodeAttr(htmlAttr),
message: `This attribute binds the type '${typeToString(typeB)}' which can end up binding the string 'null'.`,
fixMessage: "Use the 'ifDefined' directive and strict null check?",
fix: () => {
const newValue = `ifDefined(${assignment.expression.getText()} === null ? undefined : ${assignment.expression.getText()})`;

return {
message: `Use '${newValue}'`,
actions: [{ kind: "changeAssignment", assignment, newValue }]
};
}
});
}

// Test if removing "undefined" from typeB would work and suggest using "ifDefined".
else if (isAssignableToSimpleTypeKind(typeB, "UNDEFINED")) {
context.report({
location: rangeFromHtmlNodeAttr(htmlAttr),
message: `This attribute binds the type '${typeToString(typeB)}' which can end up binding the string 'undefined'.`,
message: `This attribute binds the type '${typeToString(typeB)}' which can end up binding the string '${
isAssignableToNull ? "null" : "undefined"
}'.`,
fixMessage: "Use the 'ifDefined' directive?",
fix: () => ({
message: `Use the 'ifDefined' directive.`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,17 @@ tsTest("Can assign 'null' in property binding", t => {
const { diagnostics } = getDiagnostics('html`<input .selectionEnd="${{} as number | null}" />`');
hasNoDiagnostics(t, diagnostics);
});

tsTest("Message for 'null' in attribute detects null type correctly", t => {
const { diagnostics } = getDiagnostics('html`<input maxlength="${{} as number | null}" />`');
hasDiagnostic(t, diagnostics, "no-nullable-attribute-binding");

t.true(diagnostics[0].message.includes("can end up binding the string 'null'"));
});

tsTest("Message for 'undefined' in attribute detects undefined type correctly", t => {
const { diagnostics } = getDiagnostics('html`<input maxlength="${{} as number | undefined}" />`');
hasDiagnostic(t, diagnostics, "no-nullable-attribute-binding");

t.true(diagnostics[0].message.includes("can end up binding the string 'undefined'"));
});

0 comments on commit 6d7dbe8

Please sign in to comment.