-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mark san components, write to js and run
- Loading branch information
Showing
14 changed files
with
325 additions
and
75 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,13 @@ | ||
export function getComponentClassIdentifier (sourceFile): string | undefined { | ||
const declaration = sourceFile.getImportDeclaration( | ||
node => node.getModuleSpecifierValue() === 'san' | ||
) | ||
if (!declaration) return | ||
const namedImports = declaration.getNamedImports() | ||
for (const namedImport of namedImports) { | ||
const propertyName = namedImport.getText() | ||
if (propertyName === 'Component') { | ||
return namedImport.getText() | ||
} | ||
} | ||
} |
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,59 @@ | ||
import { getComponentClassIdentifier } from './ast-util' | ||
import { Project, ts, SourceFile } from 'ts-morph' | ||
import { getDefaultConfigPath } from './tsconfig' | ||
|
||
export class ComponentParser { | ||
private componentFile: string | ||
private root: string | ||
private tsconfigPath: string | ||
private project: Project | ||
private idPropertyName: string | ||
private id: number = 0 | ||
|
||
constructor ( | ||
componentFile: string, | ||
tsconfigPath = getDefaultConfigPath(), | ||
idPropertyName = 'spsrId' | ||
) { | ||
this.idPropertyName = idPropertyName | ||
this.componentFile = componentFile | ||
this.tsconfigPath = tsconfigPath | ||
this.project = new Project({ | ||
tsConfigFilePath: tsconfigPath | ||
}) | ||
} | ||
|
||
parseComponent () { | ||
const files = this.getComponentFiles() | ||
for (const file of files.values()) { | ||
const componentClassIdentifier = getComponentClassIdentifier(file) | ||
if (!componentClassIdentifier) continue | ||
this.markComponents(file, componentClassIdentifier) | ||
} | ||
return files | ||
} | ||
|
||
private getComponentFiles () { | ||
return new Map([[ | ||
this.componentFile, | ||
this.project.getSourceFile(this.componentFile) | ||
]]) | ||
} | ||
|
||
private markComponents (sourceFile: SourceFile, componentClassIdentifier: string) { | ||
for (const clazz of sourceFile.getClasses()) { | ||
const extendClause = clazz.getHeritageClauseByKind(ts.SyntaxKind.ExtendsKeyword) | ||
if (!extendClause) return | ||
|
||
const typeNode = extendClause.getTypeNodes().find(x => x.getText() === componentClassIdentifier) | ||
if (!typeNode) return | ||
|
||
clazz.addProperty({ | ||
isStatic: true, | ||
name: this.idPropertyName, | ||
type: 'number', | ||
initializer: '' + (this.id++) | ||
}) | ||
} | ||
} | ||
} |
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,10 @@ | ||
import * as ts from 'typescript' | ||
import * as path from 'path' | ||
|
||
export function transformer (): ts.TransformerFactory<ts.SourceFile> { | ||
return (context: ts.TransformationContext) => (file: ts.SourceFile) => addMetadata(file, context) | ||
} | ||
|
||
function addMetadata (node: ts.SourceFile, context: ts.TransformationContext) { | ||
return node | ||
} |
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,15 +1,42 @@ | ||
import { resolve } from 'path' | ||
import { transpileModule, convertCompilerOptionsFromJson, TranspileOptions } from 'typescript' | ||
|
||
const tsConfigPath = resolve(__dirname, '../test/cases') | ||
const compilerOptions = convertCompilerOptionsFromJson({}, tsConfigPath).options | ||
|
||
export function ts2js (source) { | ||
const { diagnostics, outputText } = | ||
transpileModule(source, { compilerOptions }) | ||
if (diagnostics.length) { | ||
console.log(diagnostics) | ||
throw new Error('typescript compile error') | ||
import { transpileModule } from 'typescript' | ||
import { Project, SourceFile } from 'ts-morph' | ||
import { getDefaultConfigPath } from './tsconfig' | ||
import { sep } from 'path' | ||
|
||
export class Compiler { | ||
private root: string | ||
private tsconfig: object | ||
private project: Project | ||
|
||
constructor ( | ||
tsconfigPath = getDefaultConfigPath(), | ||
root = tsconfigPath.split(sep).slice(0, -1).join(sep) | ||
) { | ||
this.root = root | ||
this.tsconfig = require(tsconfigPath) | ||
this.project = new Project({ | ||
tsConfigFilePath: tsconfigPath | ||
}) | ||
} | ||
|
||
compileAndRun (source: SourceFile) { | ||
const js = this.compileToJS(source) | ||
const fn = new Function('module', 'exports', 'require', js) // eslint-disable-line | ||
const module = { | ||
exports: {} | ||
} | ||
fn(module, module.exports, require) | ||
return module.exports | ||
} | ||
|
||
compileToJS (source: SourceFile) { | ||
const compilerOptions = this.tsconfig['compilerOptions'] | ||
const { diagnostics, outputText } = | ||
transpileModule(source.getFullText(), { compilerOptions }) | ||
if (diagnostics.length) { | ||
console.log(diagnostics) | ||
throw new Error('typescript compile error') | ||
} | ||
return outputText | ||
} | ||
return outputText | ||
} |
Oops, something went wrong.