Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update no-nullable-attribute-binding fix for null attribute #339

Merged
merged 1 commit into from
Jan 5, 2024
Merged
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
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'"));
});