-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compiler): Stencil decorator import aliasing (#5161)
* create map of original imports to their aliased import this commit creates a map of original import names to aliased import names. This will be used to get the aliased name of a decorator when parsing Stencil decorators to static component metadata * change `importAliasMap` to class This commit creates an `ImportAliasMap` class that extends the `Map` class. This makes it easy to instantiate, reference the underlying type, and clearly override the default getter * pass aliased name to decorator parsers The commit passes the aliased import name from the `ImportAliasMap` to each respective decorator transformer Fixes: #3137 STENCIL-263 * add e2e tests for some aliased imports * check namedBindings exist * add rest of decorators to test
- Loading branch information
1 parent
3feee7c
commit 97dcb45
Showing
15 changed files
with
302 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
42 changes: 42 additions & 0 deletions
42
src/compiler/transformers/decorators-to-static/import-alias-map.ts
This file contains 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,42 @@ | ||
import ts from 'typescript'; | ||
|
||
import { STENCIL_DECORATORS, StencilDecorator } from './decorators-constants'; | ||
|
||
export class ImportAliasMap extends Map<StencilDecorator, string> { | ||
constructor(sourceFile: ts.SourceFile) { | ||
super(); | ||
this.generateImportAliasMap(sourceFile); | ||
} | ||
|
||
/** | ||
* Parses a {@link ts.SourceFile} and generates a map of all imported Stencil decorators | ||
* to their aliases import name (if one exists). | ||
* | ||
* @param sourceFile The source file to parse | ||
*/ | ||
private generateImportAliasMap(sourceFile: ts.SourceFile) { | ||
const importDeclarations = sourceFile.statements.filter(ts.isImportDeclaration); | ||
|
||
for (const importDeclaration of importDeclarations) { | ||
if (importDeclaration.moduleSpecifier.getText().includes('@stencil/core')) { | ||
const namedBindings = importDeclaration.importClause?.namedBindings; | ||
|
||
if (namedBindings && ts.isNamedImports(namedBindings)) { | ||
for (const element of namedBindings.elements) { | ||
const importName = element.name.getText(); | ||
const originalImportName = element.propertyName?.getText() ?? importName; | ||
|
||
// We only care to generate a map for the Stencil decorators | ||
if (STENCIL_DECORATORS.includes(originalImportName as StencilDecorator)) { | ||
this.set(originalImportName as StencilDecorator, importName); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
override get(key: StencilDecorator): string { | ||
return super.get(key) ?? key; | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.