-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(compiler): FeatureSourcePath + small TS helpers (#74)
- Loading branch information
Showing
11 changed files
with
154 additions
and
105 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
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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import type ts from "typescript"; | ||
import type { FeatureMeta } from "../../../Util/Feature/Meta/FeatureMeta"; | ||
import type { FeatureSourcePath } from "../../../Util/Feature/FeatureModuleDiscoverer"; | ||
|
||
export interface FeatureTransformContext { | ||
featureSourcePath: FeatureSourcePath; | ||
feature: FeatureMeta; | ||
sourceFilePathWithoutRoot: string; | ||
tsContext: ts.TransformationContext; | ||
diagnostics: ts.Diagnostic[]; | ||
} |
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
61 changes: 61 additions & 0 deletions
61
src/Compiler/Transformer/Helper/ImportDeclarationWrapper.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,61 @@ | ||
import ts from "typescript"; | ||
import { TsTransfromerHelper } from "../TsTransformerHelper"; | ||
|
||
export class ImportDeclarationWrapper { | ||
public constructor( | ||
public declaration: ts.ImportDeclaration, | ||
private context: ts.TransformationContext | ||
) { | ||
|
||
} | ||
|
||
public static create(importName: string | string[], importPath: string, context: ts.TransformationContext): ImportDeclarationWrapper { | ||
return ImportDeclarationWrapper.from(TsTransfromerHelper.createImportDeclaration(importName, importPath), context); | ||
} | ||
|
||
public static from(importDecl: ts.ImportDeclaration, context: ts.TransformationContext): ImportDeclarationWrapper { | ||
return new this(importDecl, context); | ||
} | ||
|
||
public get(name: string): ts.PropertyAccessChain | ts.Identifier { | ||
return TsTransfromerHelper.createNamedImportAccess(this.declaration, name, this.context); | ||
} | ||
|
||
public hasNamedAccess(name: string): boolean { | ||
if (!this.declaration.importClause?.namedBindings) { | ||
return false; | ||
} | ||
|
||
return (this.declaration.importClause.namedBindings as ts.NamedImports).elements.filter(i => i.name.text === name).length > 0; | ||
} | ||
|
||
public getEntityName(name: string): ts.EntityName { | ||
const current = this.get(name); | ||
if (ts.isIdentifier(current)) { | ||
return current; | ||
} | ||
|
||
return ts.factory.createQualifiedName(current.expression as ts.Identifier, current.name as ts.Identifier); | ||
} | ||
|
||
public addNamedImports(elements: (ts.ImportSpecifier | string)[]): ts.ImportDeclaration { | ||
const newNamedImports = ts.factory.createNamedImports([ | ||
...(this.declaration.importClause?.namedBindings as ts.NamedImports).elements.map(e => ts.factory.createImportSpecifier(e.isTypeOnly, e.propertyName, ts.factory.createIdentifier(e.name.text))), | ||
...elements.map(e => typeof e === 'string' ? ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier(e)) : e), | ||
]); | ||
|
||
this.declaration = ts.factory.updateImportDeclaration( | ||
this.declaration, | ||
undefined, | ||
ts.factory.updateImportClause(this.declaration.importClause!, | ||
this.declaration.importClause!.isTypeOnly, undefined, newNamedImports), | ||
this.declaration.moduleSpecifier, this.declaration.attributes | ||
); | ||
|
||
return this.declaration; | ||
} | ||
|
||
public get importNames(): string[] { | ||
return (this.declaration.importClause?.namedBindings as ts.NamedImports).elements.map(e => e.name.text); | ||
} | ||
} |
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.