-
Notifications
You must be signed in to change notification settings - Fork 1k
[lit-labs/compiler] only compile the valid html tag function imported from lit #4055
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --- | ||
| --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2023 Google LLC | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| */ | ||
|
|
||
| import ts from 'typescript'; | ||
|
|
||
| const compilerOptions = { | ||
| target: ts.ScriptTarget.ESNext, | ||
| module: ts.ModuleKind.ESNext, | ||
| skipDefaultLibCheck: true, | ||
| skipLibCheck: true, | ||
| moduleResolution: ts.ModuleResolutionKind.NodeNext, | ||
| }; | ||
|
|
||
| export const getTypeChecker = (filename: string, source: string) => { | ||
| const languageServiceHost = new SingleFileLanguageServiceHost( | ||
| filename, | ||
| source | ||
| ); | ||
| const languageService = ts.createLanguageService( | ||
| languageServiceHost, | ||
| ts.createDocumentRegistry() | ||
| ); | ||
|
|
||
| const program = languageService.getProgram(); | ||
| if (!program) { | ||
| throw new Error(`Internal Error: Could not start TypeScript program`); | ||
| } | ||
| return new TypeChecker(program.getTypeChecker()); | ||
| }; | ||
|
|
||
| /** | ||
| * Wrap ts.TypeChecker so we can provide a consistent and scoped API for the | ||
| * compiler. | ||
| */ | ||
| class TypeChecker { | ||
| private checker: ts.TypeChecker; | ||
| constructor(typeChecker: ts.TypeChecker) { | ||
| this.checker = typeChecker; | ||
| } | ||
|
|
||
| /** | ||
| * Use this method to find out if the passed in tagged template expression a | ||
| * compilable `lit` html template. | ||
| * | ||
| * The logic is strict, only marking a template compilable if it is | ||
| * | ||
| * @param node tagged template expression | ||
| * @returns if the tagged template expression is a lit template that can be | ||
| * compiled. | ||
| */ | ||
| isLitTaggedTemplateExpression(node: ts.TaggedTemplateExpression): boolean { | ||
| if (ts.isIdentifier(node.tag)) { | ||
| return this.isResolvedIdentifierLitHtmlTemplate(node.tag); | ||
| } | ||
| if (ts.isPropertyAccessExpression(node.tag)) { | ||
| return this.isResolvedPropertyAccessExpressionLitHtmlNamespace(node.tag); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the tag function identifier back to an import, returning true if | ||
| * the original reference was the `html` export from `lit` or `lit-html`. | ||
| * | ||
| * This check handles: aliasing and reassigning the import. | ||
| * | ||
| * ```ts | ||
| * import {html as h} from 'lit'; | ||
| * h``; | ||
| * // isResolvedIdentifierLitHtmlTemplate(<h ast node>) returns true | ||
| * ``` | ||
| * | ||
| * ```ts | ||
| * import {html} from 'lit-html/static.js'; | ||
| * html`false`; | ||
| * // isResolvedIdentifierLitHtmlTemplate(<html ast node>) returns false | ||
| * ``` | ||
| * | ||
| * @param node a TaggedTemplateExpression tag | ||
| */ | ||
| private isResolvedIdentifierLitHtmlTemplate(node: ts.Identifier): boolean { | ||
| const checker = this.checker; | ||
|
|
||
| const symbol = checker.getSymbolAtLocation(node); | ||
| if (!symbol) { | ||
| return false; | ||
| } | ||
| const templateImport = symbol.declarations?.[0]; | ||
| if (!templateImport || !ts.isImportSpecifier(templateImport)) { | ||
| return false; | ||
| } | ||
|
|
||
| if ( | ||
| templateImport.propertyName && | ||
| templateImport.propertyName.text !== 'html' | ||
| ) { | ||
| return false; | ||
| } | ||
| const namedImport = templateImport.parent; | ||
| if (!ts.isNamedImports(namedImport)) { | ||
| return false; | ||
| } | ||
| const importClause = namedImport.parent; | ||
| if (!ts.isImportClause(importClause)) { | ||
| return false; | ||
| } | ||
| const importDeclaration = importClause.parent; | ||
| if (!ts.isImportDeclaration(importDeclaration)) { | ||
| return false; | ||
| } | ||
| const specifier = importDeclaration.moduleSpecifier; | ||
| if (!ts.isStringLiteral(specifier)) { | ||
| return false; | ||
| } | ||
| return this.isLitTemplateModuleSpecifier(specifier.text); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve a common pattern of using the `html` identifier of a lit namespace | ||
| * import. | ||
| * | ||
| * E.g.: | ||
| * | ||
| * ```ts | ||
| * import * as identifier from 'lit'; | ||
| * identifier.html`<p>I am compiled!</p>`; | ||
| * ``` | ||
| */ | ||
| private isResolvedPropertyAccessExpressionLitHtmlNamespace( | ||
| node: ts.PropertyAccessExpression | ||
| ): boolean { | ||
| // Ensure propertyAccessExpression ends with `.html`. | ||
| if (ts.isIdentifier(node.name) && node.name.text !== 'html') { | ||
| return false; | ||
| } | ||
| // Expect a namespace preceding `html`, `<namespace>.html`. | ||
| if (!ts.isIdentifier(node.expression)) { | ||
| return false; | ||
| } | ||
|
|
||
| // Resolve the namespace if it has been aliased. | ||
| const checker = this.checker; | ||
| const symbol = checker.getSymbolAtLocation(node.expression); | ||
| if (!symbol) { | ||
| return false; | ||
| } | ||
| const namespaceImport = symbol.declarations?.[0]; | ||
| if (!namespaceImport || !ts.isNamespaceImport(namespaceImport)) { | ||
| return false; | ||
| } | ||
| const importDeclaration = namespaceImport.parent.parent; | ||
| const specifier = importDeclaration.moduleSpecifier; | ||
| if (!ts.isStringLiteral(specifier)) { | ||
| return false; | ||
| } | ||
| return this.isLitTemplateModuleSpecifier(specifier.text); | ||
| } | ||
|
|
||
| private isLitTemplateModuleSpecifier(specifier: string): boolean { | ||
| return ( | ||
| specifier === 'lit' || | ||
| specifier === 'lit-html' || | ||
| specifier === 'lit-element' | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A simple LanguageServiceHost used for simple, single file, one-time | ||
| * transforms. | ||
| */ | ||
| class SingleFileLanguageServiceHost implements ts.LanguageServiceHost { | ||
| private compilerOptions: ts.CompilerOptions = compilerOptions; | ||
|
|
||
| private filename: string; | ||
| private source: string; | ||
|
|
||
| constructor(filename: string, source: string) { | ||
| this.filename = filename; | ||
| this.source = source; | ||
| } | ||
|
|
||
| getCompilationSettings(): ts.CompilerOptions { | ||
| return this.compilerOptions; | ||
| } | ||
| getScriptFileNames(): string[] { | ||
| return [this.filename]; | ||
| } | ||
| getScriptVersion(_: string): string { | ||
| return '-1'; | ||
| } | ||
| getScriptSnapshot(filename: string): ts.IScriptSnapshot | undefined { | ||
| const contents = this.readFile(filename); | ||
| if (contents === undefined) { | ||
| return undefined; | ||
| } | ||
| return ts.ScriptSnapshot.fromString(contents); | ||
| } | ||
| getCurrentDirectory(): string { | ||
| return '.'; | ||
| } | ||
| getDefaultLibFileName(options: ts.CompilerOptions): string { | ||
| return ts.getDefaultLibFilePath(options); | ||
| } | ||
| readFile(filename: string): string | undefined { | ||
| if (!this.fileExists(filename)) { | ||
| return undefined; | ||
| } | ||
| return this.source; | ||
| } | ||
| fileExists(filename: string): boolean { | ||
| return this.filename === filename; | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
packages/labs/compiler/test_files/basic_litelement_import.golden.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { html } from 'lit-element'; | ||
| const b_1 = i => i; | ||
| const lit_template_1 = { h: b_1 `I compile`, parts: [] }; | ||
| ({ ["_$litType$"]: lit_template_1, values: [] }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| import {html} from 'lit-element'; | ||
| html`I compile`; |
9 changes: 9 additions & 0 deletions
9
packages/labs/compiler/test_files/dont_miscompile_shadowed_html.golden.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { html } from 'lit'; | ||
| import * as litStatic from 'lit-html/static.js'; | ||
| function trickyTemplate() { | ||
| const html = litStatic.html; | ||
| return html `Do not compile me, I am static!`; | ||
| } | ||
| const b_1 = i => i; | ||
| const lit_template_1 = { h: b_1 `Compile me!`, parts: [] }; | ||
| ({ ["_$litType$"]: lit_template_1, values: [] }); |
9 changes: 9 additions & 0 deletions
9
packages/labs/compiler/test_files/dont_miscompile_shadowed_html.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { html } from 'lit'; | ||
| import * as litStatic from 'lit-html/static.js'; | ||
|
|
||
| function trickyTemplate() { | ||
| const html = litStatic.html; | ||
| return html`Do not compile me, I am static!`; | ||
| } | ||
|
|
||
| html`Compile me!`; |
5 changes: 5 additions & 0 deletions
5
packages/labs/compiler/test_files/handle_aliased_imports.golden.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { svg as html, html as svg } from 'lit'; | ||
| html `<text>Do not compile me, I am an svg</text>`; | ||
| const b_1 = i => i; | ||
| const lit_template_1 = { h: b_1 `<p>Compiled me, I am an html template</p>`, parts: [] }; | ||
| ({ ["_$litType$"]: lit_template_1, values: [] }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import {svg as html, html as svg} from 'lit'; | ||
|
|
||
| html`<text>Do not compile me, I am an svg</text>`; | ||
| svg`<p>Compiled me, I am an html template</p>`; | ||
9 changes: 9 additions & 0 deletions
9
packages/labs/compiler/test_files/handle_html_identifier_reassignment.golden.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { html as a } from 'lit'; | ||
| // TODO: Renaming import outside the import declaration is not supported. | ||
| // These templates will not be optimized. | ||
| const b = a; | ||
| const c = b; | ||
| let g = 1, d = c, f = 1; | ||
| d `Compile me!`; | ||
| d = (i) => i; | ||
| d `Do not compile me!`; |
16 changes: 16 additions & 0 deletions
16
packages/labs/compiler/test_files/handle_html_identifier_reassignment.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import {html as a} from 'lit'; | ||
| // TODO: Renaming import outside the import declaration is not supported. | ||
| // These templates will not be optimized. | ||
|
|
||
| const b = a; | ||
| const c = b; | ||
| let g = 1, | ||
| d = c, | ||
| f = 1; | ||
|
|
||
| d`Compile me!`; | ||
|
|
||
|
|
||
| d = (i) => i; | ||
| d`Do not compile me!`; | ||
|
|
9 changes: 9 additions & 0 deletions
9
packages/labs/compiler/test_files/import_from_namespace_compiles.golden.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import * as lit from 'lit'; | ||
| const b_1 = i => i; | ||
| const lit_template_1 = { h: b_1 `<p>Compile me!</p>`, parts: [] }; | ||
| ({ ["_$litType$"]: lit_template_1, values: [] }); | ||
| let renamed = lit; | ||
| // TODO: Renaming namespaces not currently handled. | ||
| renamed.html `Compile me!`; | ||
| renamed = { html: (i) => i }; | ||
| renamed.html `Do not compile me!`; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ha ha, great test!