Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hacking Typescript

Hacking Typescript

Hacking Typescript

Avatar for Djordje Lukic

Djordje Lukic

February 27, 2017
Tweet

Other Decks in Technology

Transcript

  1. Que génère TS avec ce code ? console.log('what'); <<<<<<< HEAD:file.txt

    console.log('the'); ======= console.log('fuck'); >>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.t 5 / 24
  2. Que génère TS avec ce code ? Reponse console.log('what'); console.log('the');

    console.log('what'); <<<<<<< HEAD:file.txt console.log('the'); ======= console.log('fuck'); >>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.t 7 / 24
  3. Stats 9937 fichiers 162466 lignes de code TS 300121 lignes

    de code JS 14649 lignes de JSON 53109 tests 477236 lignes de tests 113152 lignes de code source 13 / 24
  4. //// [ifElseWithStatements1.ts] if (true) f(); else f(); function foo(): boolean

    { if (true) return true; else return false; } //// [ifElseWithStatements1.js] if (true) f(); else f(); function foo() { if (true) return true; else return false; } 15 / 24
  5. //// [unlessElseWithStatements1.ts] unless (true) f(); else f(); function foo(): boolean

    { unless (true) return true; else return false; } //// [unlessElseWithStatements1.js] if (!(true)) f(); else f(); function foo() { if (!(true)) return true; else return false; } 16 / 24
  6. Scanner // scanner.ts const textToToken = createMapFromTemplate({ // ..... "enum":

    SyntaxKind.EnumKeyword, "export": SyntaxKind.ExportKeyword, "extends": SyntaxKind.ExtendsKeyword, "false": SyntaxKind.FalseKeyword, "finally": SyntaxKind.FinallyKeyword, "for": SyntaxKind.ForKeyword, "from": SyntaxKind.FromKeyword, "function": SyntaxKind.FunctionKeyword, "get": SyntaxKind.GetKeyword, "if": SyntaxKind.IfKeyword, "unless": SyntaxKind.UnlessKeyword, "implements": SyntaxKind.ImplementsKeyword, "import": SyntaxKind.ImportKeyword, "in": SyntaxKind.InKeyword, "instanceof": SyntaxKind.InstanceOfKeyword, "interface": SyntaxKind.InterfaceKeyword, // .... 18 / 24
  7. Parser // types.ts export interface UnlessStatement extends Statement { kind:

    SyntaxKind.UnlessStatement; expression: Expression; thenStatement: Statement; elseStatement?: Statement; } 19 / 24
  8. Parser // parser.ts function parseStatement(): Statement { switch (token()) {

    // case, case, case.... case SyntaxKind.FunctionKeyword: return parseFunctionDeclaration(scanner.getStartPos(), case SyntaxKind.ClassKeyword: return parseClassDeclaration(scanner.getStartPos(), case SyntaxKind.IfKeyword: return parseIfStatement(); case SyntaxKind.UnlessKeyword: return parseUnlessStatement(); case SyntaxKind.DoKeyword: return parseDoStatement(); case SyntaxKind.WhileKeyword: return parseWhileStatement(); // case, case, case 20 / 24
  9. Parser // parser.ts function parseUnlessStatement(): UnlessStatement { const node =

    <UnlessStatement>createNode(SyntaxKind.UnlessStatement); parseExpected(SyntaxKind.UnlessKeyword); parseExpected(SyntaxKind.OpenParenToken); node.expression = allowInAnd(parseExpression); parseExpected(SyntaxKind.CloseParenToken); node.thenStatement = parseStatement(); node.elseStatement = parseOptional(SyntaxKind.ElseKeyword) ? parseStatement return finishNode(node); } 21 / 24
  10. Emitter // emitter.ts function emitUnlessStatement(node: UnlessStatement) { const openParenPos =

    writeToken(SyntaxKind.IfKeyword, node.pos, node); write(" "); writeToken(SyntaxKind.OpenParenToken, openParenPos, node); write("!("); emitExpression(node.expression); write(")"); writeToken(SyntaxKind.CloseParenToken, node.expression.end, node); emitEmbeddedStatement(node, node.thenStatement); if (node.elseStatement) { writeLineOrSpace(node); writeToken(SyntaxKind.ElseKeyword, node.thenStatement.end, node); if (node.elseStatement.kind === SyntaxKind.IfStatement) { write(" "); emit(node.elseStatement); } else { emitEmbeddedStatement(node, node.elseStatement); } } } 22 / 24