-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
1,112 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* Code generation: Create Node Polyfills | ||
* -------------------------------------- | ||
* This creates a file that is being used as a temporary workaround so that this library | ||
* can be used with versions of the compiler < v2.7. In the future, a better solution should be devised. | ||
* -------------------------------------- | ||
*/ | ||
import * as path from "path"; | ||
import * as ts from "typescript"; | ||
import {rootFolder} from "./config"; | ||
import {InspectorFactory} from "./inspectors"; | ||
import {EnumDeclaration, EnumMemberStructure} from "./../src/main"; | ||
|
||
const nodesToPolyfillNames = ["JsxFragment", "JsxOpeningFragment", "JsxClosingFragment"]; | ||
const enumsToPolyfillNames = ["ScriptElementKind"]; | ||
|
||
export function createNodePolyfills(factory: InspectorFactory) { | ||
const tsInspector = factory.getTsInspector(); | ||
const sourceFile = getSourceFile(); | ||
|
||
sourceFile.addImportDeclaration({ | ||
namespaceImport: "ts", | ||
moduleSpecifier: "typescript" | ||
}); | ||
|
||
fillInterfaces(); | ||
fillEnums(); | ||
|
||
sourceFile.insertStatements(0, writer => { | ||
writer.writeLine("// DO NOT EDIT - This file is automatically generated by createNodePolyfills.ts"); | ||
writer.writeLine("// This file is necessary because old supported versions of the compiler won't have these nodes."); | ||
}); | ||
|
||
sourceFile.save(); | ||
|
||
function getSourceFile() { | ||
const ast = factory.getAst(); | ||
const filePath = path.join(rootFolder, "src/compiler/polyfills.ts"); | ||
if (ast.getSourceFile(filePath) != null) | ||
ast.getSourceFileOrThrow(filePath).deleteSync(); | ||
return ast.createSourceFile(filePath); | ||
} | ||
|
||
function fillInterfaces() { | ||
const nodesToPolyfill = tsInspector.getTsNodes().filter(n => nodesToPolyfillNames.indexOf(n.getName()) >= 0); | ||
for (const node of nodesToPolyfill) { | ||
const nodeInterface = node.getInterface(); | ||
const newInterface = sourceFile.addInterface({ | ||
name: nodeInterface.getName(), | ||
isExported: true, | ||
extends: nodeInterface.getExtends().map(e => e.getText()) | ||
}); | ||
|
||
for (const nodeProp of nodeInterface.getProperties()) { | ||
// don't include this because old versions of the compiler won't have the syntax kind | ||
if (nodeProp.getName() === "kind") | ||
continue; | ||
|
||
newInterface.addProperty({ | ||
name: nodeProp.getName(), | ||
type: nodeProp.getTypeNodeOrThrow().getText(), | ||
hasQuestionToken: nodeProp.hasQuestionToken() | ||
}); | ||
} | ||
} | ||
|
||
for (const newInterface of sourceFile.getInterfaces()) | ||
newInterface.rename(newInterface.getName() + "CompilerPolyfill"); | ||
|
||
// this is sketch, but good enough for now | ||
const prefix = "ts."; | ||
let offset = 0; | ||
for (const cannotFindNameDiagnostic of sourceFile.getDiagnostics().filter(d => d.getCode() === 2304)) { | ||
sourceFile.insertText(cannotFindNameDiagnostic.getStart()! + offset, prefix); | ||
offset += prefix.length; | ||
} | ||
} | ||
|
||
function fillEnums() { | ||
const enums = tsInspector.getDeclarationFile().getDescendantsOfKind(ts.SyntaxKind.EnumDeclaration) as EnumDeclaration[]; | ||
const enumsToPolyfill = enums.filter(e => enumsToPolyfillNames.indexOf(e.getName()) >= 0); | ||
|
||
for (const enumDec of enumsToPolyfill) { | ||
sourceFile.addEnum({ | ||
name: enumDec.getName(), | ||
isExported: true, | ||
members: enumDec.getMembers().map(m => ({ | ||
name: m.getName(), | ||
docs: m.getJsDocs().map(d => ({ description: d.getInnerText().replace(/\r?\n/g, "\r\n") })), | ||
value: m.getValue() | ||
}) as EnumMemberStructure) | ||
}); | ||
} | ||
} | ||
} |
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,17 +1,15 @@ | ||
import * as path from "path"; | ||
import * as fs from "fs"; | ||
import {rootFolder} from "./config"; | ||
import {createGetStructureFunctions} from "./createGetStructureFunctions"; | ||
import {createTypeGuardsUtility} from "./createTypeGuardsUtility"; | ||
import {createNodePolyfills} from "./createNodePolyfills"; | ||
import {InspectorFactory} from "./inspectors"; | ||
|
||
// setup | ||
const factory = new InspectorFactory(); | ||
const inspector = factory.getTsSimpleAstInspector(); | ||
|
||
// get structure functions | ||
const getStructureCode = createGetStructureFunctions(inspector.getStructures()); | ||
fs.writeFileSync(path.join(rootFolder, "src/manipulation/helpers/getStructureFunctions.ts"), getStructureCode, { encoding: "utf-8" }); | ||
|
||
// create the TypeGuard class | ||
// create | ||
createGetStructureFunctions(inspector.getStructures()); | ||
createTypeGuardsUtility(inspector); | ||
createNodePolyfills(factory); |
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,6 +1,5 @@ | ||
import * as ts from "typescript"; | ||
import {UpdateExpression} from "./UpdateExpression"; | ||
|
||
export const LeftHandSideExpressionBase = UpdateExpression; | ||
export class LeftHandSideExpression<T extends ts.LeftHandSideExpression = ts.LeftHandSideExpression> extends LeftHandSideExpressionBase<T> { | ||
export class LeftHandSideExpression<T extends ts.LeftHandSideExpression = ts.LeftHandSideExpression> extends UpdateExpression<T> { | ||
} |
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,6 +1,5 @@ | ||
import * as ts from "typescript"; | ||
import {LeftHandSideExpression} from "./LeftHandSideExpression"; | ||
|
||
export const MemberExpressionBase = LeftHandSideExpression; | ||
export class MemberExpression<T extends ts.MemberExpression = ts.MemberExpression> extends MemberExpressionBase<T> { | ||
export class MemberExpression<T extends ts.MemberExpression = ts.MemberExpression> extends LeftHandSideExpression<T> { | ||
} |
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,6 +1,5 @@ | ||
import * as ts from "typescript"; | ||
import {MemberExpression} from "./MemberExpression"; | ||
|
||
export const PrimaryExpressionBase = MemberExpression; | ||
export class PrimaryExpression<T extends ts.PrimaryExpression = ts.PrimaryExpression> extends PrimaryExpressionBase<T> { | ||
export class PrimaryExpression<T extends ts.PrimaryExpression = ts.PrimaryExpression> extends MemberExpression<T> { | ||
} |
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,6 +1,5 @@ | ||
import * as ts from "typescript"; | ||
import {Expression} from "./Expression"; | ||
|
||
export const UnaryExpressionBase = Expression; | ||
export class UnaryExpression<T extends ts.UnaryExpression = ts.UnaryExpression> extends UnaryExpressionBase<T> { | ||
export class UnaryExpression<T extends ts.UnaryExpression = ts.UnaryExpression> extends Expression<T> { | ||
} |
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,6 +1,5 @@ | ||
import * as ts from "typescript"; | ||
import {UnaryExpression} from "./UnaryExpression"; | ||
|
||
export const UpdateExpressionBase = UnaryExpression; | ||
export class UpdateExpression<T extends ts.UpdateExpression = ts.UpdateExpression> extends UpdateExpressionBase<T> { | ||
export class UpdateExpression<T extends ts.UpdateExpression = ts.UpdateExpression> extends UnaryExpression<T> { | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as ts from "typescript"; | ||
import * as errors from "./../../errors"; | ||
import {NamedNode} from "./../base"; | ||
import {Node} from "./../common"; | ||
import {StringLiteral} from "./../literal"; | ||
import {JsxExpression} from "./JsxExpression"; | ||
|
||
export class JsxAttribute extends NamedNode(Node)<ts.JsxAttribute> { | ||
/** | ||
* Gets the JSX attribute's initializer or throws if it doesn't exist. | ||
*/ | ||
getInitializerOrThrow() { | ||
return errors.throwIfNullOrUndefined(this.getInitializer(), `Expected to find an initializer for the JSX attribute '${this.getName()}'`); | ||
} | ||
|
||
/** | ||
* Gets the JSX attribute's initializer or returns undefined if it doesn't exist. | ||
*/ | ||
getInitializer() { | ||
return this.getNodeFromCompilerNodeIfExists<StringLiteral | JsxExpression>(this.compilerNode.initializer); | ||
} | ||
} |
Oops, something went wrong.