Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
Please do not merge. Current issues:

- My code editor changed tabs to spaces. Sorry. I think it can be easily fixed using a linter.
- Visibility sorter was commented out in doc.ts
- Part of type checking in ttf.ts was removed (T replaced with any)
- walker.ts and compiler.ts were changed in a try to make JSDoc adding work, but as it's not working it was commented out in compiler.ts
  • Loading branch information
qgustavor committed Jun 6, 2019
1 parent ee97d1d commit 218a095
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 67 deletions.
6 changes: 3 additions & 3 deletions build/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function flatten<T>(arr: T[][]): T[] {
}

const sorter = (() => {
function visibilitySorter(value1: { isPrivate?: boolean; isProtected?: boolean; }, value2: { isPrivate?: boolean; isProtected?: boolean; }) {
/*function visibilitySorter(value1: { isPrivate?: boolean; isProtected?: boolean; }, value2: { isPrivate?: boolean; isProtected?: boolean; }) {
if (value1.isPrivate === value2.isPrivate && value1.isProtected === value2.isProtected) {
return 0;
}
Expand All @@ -57,7 +57,7 @@ const sorter = (() => {
}
return 0;
}
}*/

const types = [AST.Property, AST.Function, AST.Interface, AST.Class, AST.Enum];
function typeSorter(value1: AST.ModuleMember | AST.NamespaceMember, value2: AST.ModuleMember | AST.NamespaceMember) {
Expand All @@ -81,7 +81,7 @@ const sorter = (() => {
return value1.name.localeCompare(value2.name);
}

const sorters: ((value1: AST.ModuleMember, value2: AST.ModuleMember) => number)[] = [visibilitySorter, typeSorter, nameSorter];
const sorters: ((value1: AST.ModuleMember, value2: AST.ModuleMember) => number)[] = [/*visibilitySorter, */typeSorter, nameSorter];

return (value1: AST.ModuleMember, value2: AST.ModuleMember) => {
for (const sorter of sorters) {
Expand Down
3 changes: 3 additions & 0 deletions build/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ declare var require: {

interface BufferConstructor {
new (str: string): Buffer;
from (arrayBuffer: ArrayBuffer, byteOffset?: number, length?: number): Buffer;
from (str: string, encoding?: string): Buffer;
from (data: ReadonlyArray<any> | Buffer): Buffer;
prototype: Buffer;
concat(list: Buffer[]): Buffer;
}
Expand Down
23 changes: 13 additions & 10 deletions build/typescript/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function createCompilerHost(options: ts.CompilerOptions): StreamingCompilerHost
host.writeFile = (fileName, data, writeByteOrderMark, onError?, sourceFiles?): void => {
_outputStream.push({
path: fileName,
contents: new Buffer(data)
contents: Buffer.from(data)
});
};

Expand Down Expand Up @@ -106,11 +106,11 @@ export class Compiler {
return this._program.getTypeChecker();
}

get sourceFiles(): ts.SourceFile[] {
get sourceFiles(): readonly ts.SourceFile[] {
return this._program.getSourceFiles();
}

private _reportDiagnostics(diagnostics: ts.Diagnostic[]) {
private _reportDiagnostics(diagnostics: readonly ts.DiagnosticWithLocation[]) {
for (const diagnostic of diagnostics) {
let message = "";

Expand All @@ -137,8 +137,9 @@ export function build(root: string, rootNamespaceName: string): FileTransform {

compiler.compile(projectConfigFile);

const walkResult = walk(compiler, root, rootNamespaceName);
addJSDocComments(walkResult.modules);
// BROKEN:
// const walkResult = walk(compiler, root, rootNamespaceName);
// addJSDocComments(walkResult.modules);

compiler.writeFiles(this);

Expand Down Expand Up @@ -243,7 +244,7 @@ class FakeSourceFile {

constructor(originalSourceFile: ts.SourceFile) {
this.text = originalSourceFile.text;
this.lineMap = ts.getLineStarts(originalSourceFile).slice();
this.lineMap = originalSourceFile.getLineStarts().slice();
}

addComment(originalComment: ts.CommentRange, newComments: string[]): ts.CommentRange & { sourceFile: FakeSourceFile } {
Expand Down Expand Up @@ -278,13 +279,15 @@ class FakeSourceFile {

var fakeSourceFiles: { [name: string]: FakeSourceFile } = Object.create(null);

export const oldGetLeadingCommentRangesOfNodeFromText: typeof ts.getLeadingCommentRangesOfNodeFromText = ts.getLeadingCommentRangesOfNodeFromText.bind(ts);
export function oldGetLeadingCommentRangesOfNodeFromText(node: ts.Node, text: string) {
return ts.getLeadingCommentRanges(text, node.pos);
}

ts.getLeadingCommentRangesOfNodeFromText = (node: ts.Node, text: string) => {
const originalComments = oldGetLeadingCommentRangesOfNodeFromText(node, text);

if (originalComments !== undefined && (<any>node)["typescript-new-comment"] !== undefined) {
const sourceFileOfNode = ts.getSourceFileOfNode(node);
const sourceFileOfNode = node.getSourceFile();
let fakeSourceFile = fakeSourceFiles[sourceFileOfNode.fileName];
if (fakeSourceFile === undefined) {
fakeSourceFile = fakeSourceFiles[sourceFileOfNode.fileName] = new FakeSourceFile(sourceFileOfNode);
Expand All @@ -297,12 +300,12 @@ ts.getLeadingCommentRangesOfNodeFromText = (node: ts.Node, text: string) => {
};

var oldWriteCommentRange: typeof ts.writeCommentRange = ts.writeCommentRange.bind(ts);
ts.writeCommentRange = (text: string, lineMap: number[], writer: ts.EmitTextWriter, comment: ts.CommentRange, newLine: string) => {
ts.writeCommentRange = (text: string, lineMap: ReadonlyArray<number>, writer: ts.EmitTextWriter, commentPos: number, commentEnd: number, newLine: string) => {
if ((<{ sourceFile: ts.SourceFile }><any>comment).sourceFile) {
const currentSourceFile = (<{ sourceFile: ts.SourceFile }><any>comment).sourceFile;
text = currentSourceFile.text;
lineMap = currentSourceFile.lineMap;
}

return oldWriteCommentRange(text, lineMap, writer, comment, newLine);
return oldWriteCommentRange(text, lineMap, writer, commentPos, commentEnd, newLine);
};
64 changes: 34 additions & 30 deletions build/typescript/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ import { Compiler, oldGetLeadingCommentRangesOfNodeFromText } from "./compiler";

import * as AST from "./ast";

function hasModifier(node: ts.Node, flags: ts.NodeFlags): boolean {
return (node.flags & flags) !== 0;
function hasModifier(node: ts.Node, flag: ts.SyntaxKind): boolean {
if (!node.modifiers) return false;
for (let modifier of node.modifiers) {
if (modifier.kind === flag) return true;
}
return false;
}

interface JSDoc {
Expand Down Expand Up @@ -186,18 +190,18 @@ class Walker {
}

private _visitProperty(node: ts.PropertyDeclaration, parent: AST.Class | AST.Interface) {
if (hasModifier(node, ts.NodeFlags.Private)) {
if (hasModifier(node, ts.SyntaxKind.PrivateKeyword)) {
return;
}

const jsDoc = this._parseJSDoc(node);

if (jsDoc.typeAnnotation === null) {
this._notifyIncorrectJsDoc(`Field ${ ts.getTextOfNode(node.name) } has no @type annotation.`);
this._notifyIncorrectJsDoc(`Field ${ node.getText() } has no @type annotation.`);
jsDoc.typeAnnotation = "*";
}

const property = this._scope.enter(new AST.Property(ts.getTextOfNode(node.name)));
const property = this._scope.enter(new AST.Property(node.getText()));
parent.members[property.name] = property;
property.getter = new AST.Getter(node, jsDoc.description, jsDoc.typeAnnotation, false);
property.setter = new AST.Setter(node, jsDoc.description, jsDoc.typeAnnotation, false);
Expand All @@ -208,31 +212,31 @@ class Walker {
const jsDoc = this._parseJSDoc(node);

const parameters = this._connectParameters(node.parameters, jsDoc.parameters,
parameterName => `Could not find @param annotation for ${ parameterName } on method ${ ts.getTextOfNode(node.name) }`
parameterName => `Could not find @param annotation for ${ parameterName } on method ${ node.getText() }`
);

if (jsDoc.returnType === null && (node.type === undefined || node.type.kind !== ts.SyntaxKind.VoidKeyword)) {
this._notifyIncorrectJsDoc(`Missing @return annotation for method ${ ts.getTextOfNode(node.name) }`);
this._notifyIncorrectJsDoc(`Missing @return annotation for method ${ node.getText() }`);
jsDoc.returnType = new AST.ReturnType("", "*");
}

const isPrivate = hasModifier(node, ts.NodeFlags.Private);
const isProtected = hasModifier(node, ts.NodeFlags.Protected);
const isStatic = hasModifier(node, ts.NodeFlags.Static);
const isPrivate = hasModifier(node, ts.SyntaxKind.PrivateKeyword);
const isProtected = hasModifier(node, ts.SyntaxKind.ProtectedKeyword);
const isStatic = hasModifier(node, ts.SyntaxKind.StaticKeyword);

const generics = this._getGenericsOfSignatureDeclaration(node);

const method = this._scope.enter(new AST.Function(ts.getTextOfNode(node.name), node, jsDoc.description, generics, parameters, jsDoc.returnType, jsDoc.isAbstract, isPrivate, isProtected, isStatic));
const method = this._scope.enter(new AST.Function(node.getText(), node, jsDoc.description, generics, parameters, jsDoc.returnType, jsDoc.isAbstract, isPrivate, isProtected, isStatic));
parent.members[method.name] = method;
this._scope.leave();
}

private _visitGetAccessor(node: ts.AccessorDeclaration, clazz: AST.Class): void {
const jsDoc = this._parseJSDoc(node);

const name = ts.getTextOfNode(node.name);
const name = node.getText();

const isPrivate = hasModifier(node, ts.NodeFlags.Private);
const isPrivate = hasModifier(node, ts.SyntaxKind.PrivateKeyword);

let property = clazz.members[name] as AST.Property;
if (property === undefined) {
Expand All @@ -253,9 +257,9 @@ class Walker {
private _visitSetAccessor(node: ts.AccessorDeclaration, clazz: AST.Class): void {
const jsDoc = this._parseJSDoc(node);

const name = ts.getTextOfNode(node.name);
const name = node.getText();

const isPrivate = hasModifier(node, ts.NodeFlags.Private);
const isPrivate = hasModifier(node, ts.SyntaxKind.PrivateKeyword);

let property = clazz.members[name] as AST.Property;
if (property === undefined) {
Expand All @@ -279,7 +283,7 @@ class Walker {
}

const declaration = node.declarationList.declarations[0];
if (hasModifier(declaration, ts.NodeFlags.Ambient)) {
if ((declaration.flags & ts.NodeFlags.Ambient) !== 0) {
return;
}

Expand All @@ -288,7 +292,7 @@ class Walker {
return;
}

const property = this._scope.enter(new AST.Property(ts.getTextOfNode(declaration.name)));
const property = this._scope.enter(new AST.Property(declaration.getText()));
property.getter = new AST.Getter(node, jsDoc.description, jsDoc.typeAnnotation, false);

parent.members[property.name] = property;
Expand All @@ -299,7 +303,7 @@ class Walker {
private _visitFunctionDeclaration(node: ts.FunctionDeclaration, parent: AST.Module): void {
const jsDoc = this._parseJSDoc(node);

const isPrivate = !hasModifier(node, ts.NodeFlags.Export);
const isPrivate = !hasModifier(node, ts.SyntaxKind.ExportKeyword);

const generics = this._getGenericsOfSignatureDeclaration(node);

Expand Down Expand Up @@ -330,7 +334,7 @@ class Walker {

const generics = this._getGenericsOfInterfaceType(type);

const baseTypeHeritageClauseElement = ts.getClassExtendsHeritageClauseElement(node) || null;
const baseTypeHeritageClauseElement = ts.getClassExtendsHeritageElement(node) || null;
let baseType: AST.UnresolvedType = null;
if (baseTypeHeritageClauseElement !== null) {
baseType = new AST.UnresolvedType(
Expand All @@ -344,12 +348,12 @@ class Walker {
this._getGenericsOfTypeReferenceNode(type, generics)
));

const isPrivate = !hasModifier(node, ts.NodeFlags.Export);
const isPrivate = !hasModifier(node, ts.SyntaxKind.ExportKeyword);

let parameters: AST.Parameter[] = [];

if (type.symbol.members["__constructor"] !== undefined) {
parameters = this._connectParameters((type.symbol.members["__constructor"].declarations[0] as ts.ConstructorDeclaration).parameters, jsDoc.parameters,
if (type.symbol.members.get("__constructor" as ts.__String) !== undefined) {
parameters = this._connectParameters((type.symbol.members.get("__constructor" as ts.__String).declarations[0] as ts.ConstructorDeclaration).parameters, jsDoc.parameters,
parameterName => `Could not find @param annotation for ${ parameterName } on constructor in class ${ node.name.text }`
);
}
Expand All @@ -361,7 +365,7 @@ class Walker {

parent.members[clazz.name] = clazz;

ts.forEachProperty(type.symbol.exports, symbol => {
type.symbol.exports.forEach((symbol: ts.Symbol) => {
if (symbol.name === "prototype") {
return;
}
Expand All @@ -371,7 +375,7 @@ class Walker {
}
});

ts.forEachProperty(type.symbol.members, symbol => {
type.symbol.members.forEach((symbol: ts.Symbol) => {
for (const declaration of symbol.declarations) {
this._walkClassMember(declaration, clazz);
}
Expand All @@ -397,12 +401,12 @@ class Walker {
return;
}

const isPrivate = !hasModifier(node, ts.NodeFlags.Export);
const isPrivate = !hasModifier(node, ts.SyntaxKind.ExportKeyword);

const interfase = this._scope.enter(new AST.Interface(node.name.text, node, jsDoc.description, generics, baseTypes, isPrivate));
parent.members[interfase.name] = interfase;

ts.forEachProperty(type.symbol.members, symbol => {
type.symbol.members.forEach((symbol: ts.Symbol) => {
for (const declaration of symbol.declarations) {
this._walkInterfaceMember(declaration, interfase);
}
Expand All @@ -419,14 +423,14 @@ class Walker {
return;
}

const isPrivate = !hasModifier(node, ts.NodeFlags.Export);
const isPrivate = !hasModifier(node, ts.SyntaxKind.ExportKeyword);

const type = this._typeChecker.getTypeAtLocation(node);

const enumType = this._scope.enter(new AST.Enum(node.name.text, node, jsDoc.description, isPrivate));
parent.members[enumType.name] = enumType;

ts.forEachProperty(type.symbol.exports, symbol => {
type.symbol.exports.forEach((symbol: ts.Symbol) => {
this._visitEnumMember(symbol.declarations[0] as ts.EnumMember, enumType);
});

Expand All @@ -438,7 +442,7 @@ class Walker {

const value = (node.initializer === undefined) ? null : parseInt((node.initializer as ts.LiteralExpression).text);

const enumMember = this._scope.enter(new AST.EnumMember(ts.getTextOfNode(node.name), (jsDoc === null) ? "" : jsDoc.description, value));
const enumMember = this._scope.enter(new AST.EnumMember(node.getText(), (jsDoc === null) ? "" : jsDoc.description, value));

parent.members.push(enumMember);

Expand Down Expand Up @@ -681,7 +685,7 @@ class Walker {
});
}

private _connectParameters(astParameters: ts.ParameterDeclaration[], jsDocParameters: { [name: string]: AST.Parameter }, onMissingMessageCallback: (parameterName: string) => string) {
private _connectParameters(astParameters: ts.NodeArray<ts.ParameterDeclaration>, jsDocParameters: { [name: string]: AST.Parameter }, onMissingMessageCallback: (parameterName: string) => string) {
return astParameters.map(parameter => {
let parameterName = (parameter.name as ts.Identifier).text;
if (parameterName[0] === "_") {
Expand Down
8 changes: 4 additions & 4 deletions build/uglify.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,12 @@ var Run = (function () {

outputStream.push({
path: this._outputLibraryName + ".js",
contents: Buffer.concat([new Buffer(stream.toString()), new Buffer("\n//# sourceMappingURL=" + this._outputLibraryName + ".js.map")])
contents: Buffer.concat([Buffer.from(stream.toString()), Buffer.from("\n//# sourceMappingURL=" + this._outputLibraryName + ".js.map")])
});

outputStream.push({
path: this._outputLibraryName + ".js.map",
contents: new Buffer(this._rootSourceMap.get().toString())
contents: Buffer.from(this._rootSourceMap.get().toString())
});

// Print unused variables
Expand Down Expand Up @@ -416,7 +416,7 @@ module.exports = {
codeFile.path = codeFile.path.replace(/\.js$/, ".min.js");
sourceMapFile.path = sourceMapFile.path.replace(/\.js\.map$/, ".min.js.map");

codeFile.contents = Buffer.concat([new Buffer(stream.toString()), new Buffer("\n//# sourceMappingURL="), new Buffer(sourceMapFile.path)]);
codeFile.contents = Buffer.concat([Buffer.from(stream.toString()), Buffer.from("\n//# sourceMappingURL="), Buffer.from(sourceMapFile.path)]);
this.push(codeFile);

var inputSourceMapObject = JSON.parse(sourceMapFile.contents.toString());
Expand All @@ -425,7 +425,7 @@ module.exports = {
outputSourceMapObject.setSourceContent(filename, inputSourceMapObject.sourcesContent[i]);
});

sourceMapFile.contents = new Buffer(output.source_map.toString());
sourceMapFile.contents = Buffer.from(output.source_map.toString());
this.push(sourceMapFile);

codeFile = null;
Expand Down
18 changes: 12 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
"name": "libjass",
"version": "0.12.0",
"description": "A library to render ASS subtitles on HTML5 video in the browser.",
"keywords": ["browser", "html5", "subtitles"],
"keywords": [
"browser",
"html5",
"subtitles"
],
"homepage": "https:/Arnavion/libjass",
"bugs": "https:/Arnavion/libjass/issues",
"license": "Apache-2.0",
"contributors": [{
"name": "Arnav Singh",
"email": "[email protected]"
}],
"contributors": [
{
"name": "Arnav Singh",
"email": "[email protected]"
}
],
"repository": {
"type": "git",
"url": "https:/Arnavion/libjass"
Expand All @@ -31,7 +37,7 @@
"npm": "4.x",
"pngjs": "3.x",
"sax": "1.x",
"typescript": "2.0.10",
"typescript": "^3.5.1",
"uglify-js": "2.x >=2.4.24"
},
"private": true
Expand Down
Loading

0 comments on commit 218a095

Please sign in to comment.