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

Exclude symbols when checking binding types #364

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -50,6 +50,10 @@ export function extractBindingTypes(assignment: HtmlNodeAttrAssignment, context:
typeB = directiveType;
}

// Handle `nothing` and `noChange` symbols
// Since it's not possible to check the details of a symbol due to various restrictions (it's treated as a `unique symbol` or `Symbol()`), all symbols are excluded.
typeB = excludeSymbolsFromUnion(typeB);

// Cache the result
const result = { typeA, typeB };
cache.set(assignment, result);
Expand Down Expand Up @@ -82,6 +86,17 @@ export function inferTypeFromAssignment(assignment: HtmlNodeAttrAssignment, chec
}
}

function excludeSymbolsFromUnion(type: SimpleType): SimpleType {
if (type.kind !== "UNION") {
return type;
}

return {
...type,
types: type.types.filter(t => t.kind !== "ES_SYMBOL" && t.kind !== "ES_SYMBOL_UNIQUE")
};
}

/**
* Relax the type so that for example "string literal" become "string" and "function" become "any"
* This is used for javascript files to provide type checking with Typescript type inferring
Expand Down
12 changes: 11 additions & 1 deletion packages/lit-analyzer/src/test/helpers/compile-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,17 @@ export function compileFiles(inputFiles: TestFile[] | TestFile = []): { program:

const program = ts.createProgram({
//rootNames: [...files.map(file => file.fileName!), "node_modules/typescript/lib/lib.dom.d.ts"],
rootNames: [...files.map(file => file.fileName!), ...(includeLib ? ["node_modules/typescript/lib/lib.dom.d.ts"] : [])],
rootNames: [
...files.map(file => file.fileName!),
...(includeLib
? [
"node_modules/typescript/lib/lib.dom.d.ts",
// We need this to enable `Symbol`.
// We cannot use "esnext" since it cannot be loaded by TS 4.8.4.
"node_modules/typescript/lib/lib.es2015.d.ts"
]
: [])
],
//rootNames: files.map(file => file.fileName!),
options: compilerOptions,
host: compilerHost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,39 @@ tsTest("Attribute binding: the target attribute is correctly type checked when g

hasNoDiagnostics(t, diagnostics);
});

tsTest("Attribute binding: any symbols are ignored on type checking", t => {
const { diagnostics } = getDiagnostics(`
declare const value: boolean | unique symbol;

html\`<div aria-expanded=\${userInput}></div>\`
`);

hasNoDiagnostics(t, diagnostics);
});

tsTest("Attribute binding: symbols are not treated as any type", t => {
const { diagnostics } = getDiagnostics(`
declare const value: "invalid" | unique symbol;

html\`<div aria-expanded=\${value}></div>\`
`);

hasDiagnostic(t, diagnostics, "no-incompatible-type-binding");
});

tsTest("Attribute binding: lit's nothing is ignored on type checking when returned by a function", t => {
const { diagnostics } = getDiagnostics(`
declare const nothing: unique symbol;

function customIfDef<T>(value: T | null | undefined): T | typeof nothing {
return value ?? nothing;
}

declare const value: boolean | null;

html\`<div aria-expanded=\${customIfDef(value)}></div>\`
`);

hasNoDiagnostics(t, diagnostics);
});