“This is the 8th day of my participation in the August Gwen Challenge.
Architectural layering
-
Core: TS Compiler
-
Parser:string —> AST
-
Binder: Declarations associated with a unified structure using symbolic links, that is, different declarations of the same interface or module, or modules and functions with the same name. This allows the type system to reason about these named declarations
-
Type resolution/checking: Parses the types of each construct, checks semantic operations, and generates diagnostic information as needed
-
Emitter: Generates a series of files from input files (.ts and.d.ts) : js,.d.ts, or sourcemap
-
Pre-processor: “Compilation context” refers to all files involved in a program. The context is created by examining all the files passed to the compiler at the command line. The order in which they are created is determined by direct or indirect references, e.g., import and ///. The result of facilitating all references is an ordered list of source files. When parsing imports, parse ts files over.d.ts files to ensure that most recent files are processed. The compiler interprets imports similarly to Node, as described earlier. A failed import does not cause an error because an Ambient module may already have been declared.
-
Standalone Compiler (TSC) : Batch compiled CLI. It mainly supports file read and write of different engines.
-
Language Service: “Language Service” exposes an extra layer on the core compilation pipeline, which is best suited for editors. The Language Service supports a range of editor operations, such as statement completion, function signing, code formatting and outline, code coloring, and more. Basic refactorings such as renaming; Debug interface assistance, such as verification of breakpoints and TS-specific features: incremental compilation (set by command line –watch). Language services are designed to handle file changes in a long-term compilation context; Therefore, unlike other compilers, the Language Service provides an interface between handlers and source files. As shown in the
-
Individual services: TsServer contains compilation and service layers, which are exposed to the outside world through JSON-formatted communication. LSP
The data structure
-
Node: base Node of the AST. Usually nodes represent non-terminals in language syntax; Some terminators, such as identifiers and literals, are stored in the AST tree.
-
SourceFile: AST of a given sourceFile. A sourceFile is itself a node. It provides a series of interfaces to obtain information about source files, such as text, references, a set of identifiers, and mappings between position and line/character numbers in the file.
-
Program: sourceFile and a collection of compilation operations. Program is the entry point to the type system and code artifacts.
-
Symbol: A named Symbol. Symbol is the result of binding. Symbols connects nodes declared in the AST tree with other entities that are related to the nodes. Symbol is the basic structure of the semantic system.
-
Type: Type is the other part of the semantic system. Types can be named, such as classes and interfaces; It can also be anonymous, such as object types.
-
Signature: There are three types of signatures in the language: invocation Signature, construction Signature, and index Signature.
An overview of the compilation process
preprocess
Handles reference relationships, that is, /// and import to determine which files are to be compiled.
parser
Obtain the AST tree. The AST tree is a tree data structure to represent the text edited by the user. A SourceFile object represents an AST tree for a given file, containing additional information such as the file name and file content.
binder
Binder traverses AST nodes to generate (???) Bind Symbols. Each named entity creates a Symbol. Some declared nodes can name the same entity (type alias?) . This means that several different nodes will have the same Symbol, and each Symbol can trace its declared nodes, i.e., one-to-many relationships. For example, a class with the same name and a namespace can be combined and have the same Symbol. Binder controls scoping and ensures that each Symbol is created in the correct domain.
Program
The createSoureceFile API can create a SourceFile.
So far, Symbols stands for named entities (s) in a single file. But some lives can merge multiple files, so the next step is to build all of them, known as programs.
TypeChecker
A TypeChecker can be created from a Program instance. TypeChecker is the core of the TS type system. TypeChecker is responsible for specifying symbols for different files, assigning types to symbols, and generating diagnostic information such as error messages.
The first thing TypeChecker does is combine symbols from different SourceFile to form a single Symbol table.
After initialization, TypeChecker is ready to answer the following questions?
- What is Symbol for Node?
- What is Type for Symbol?
- Which symbols are visible in the AST?
- What signatures are available for a function declaration?
- What errors are reportable for a file?
All calculations of the TypeChecker are lazy. He parses only the information necessary to answer the question; Only Nodes/Symbols/Types related to the problem at hand are checked and no other views are attempted.
Emitter
Emitter can be created from a given Program. Emitter is responsible for creating.js/.jsx/.d.ts/.js.map files from the given SourceFile.
The term
Full Start/Token Start
Ts.node. getStart –> gets the location of the first token from the Node
Ts.node. getFullStart –> First token location owned from Node
Stackoverflow.com/a/24391813/…
Trivia
Trivia stands for scores such as Spaces, comments, and even dash marks that are generally not important to understanding the code.
Because trival is not part of the standard language, it can be used between any token. And it’s not included in the syntax tree. However, because they are important for refactoring and maintaining the fidelity of the original text, they are still accessible through the TS API.
A little… Is not important
The last
Much more difficult to translate than handbook… Do your best