Repository navigation
JavaScript and TypeScript
The rules of the method stereotypes, the free-function stereotypes, and the class stereotypes hold in JavaScript and TypeScript as in every language of Stereocode. Since srcML marks a TypeScript unit as JavaScript, each fact that this page states for JavaScript holds for TypeScript as well. The section Data Members and Calls of the method stereotypes states the conventions by which Stereocode determines the facts of these rules in every language. JavaScript code need not declare the data members of a class or the types of its variables, while TypeScript declares a type only where the code writes an annotation. The section Language Facts therefore replaces or adds to these conventions for JavaScript and TypeScript. This page also gives the built-in lists that Stereocode reads for JavaScript and TypeScript and one labeled example of each stereotype.
- Every named class declaration outside the bodies of functions is a type, also an abstract or an exported one. Its parents are the names after
extendsandimplements. A TypeScript interface is a type that Stereocode labels only with-i, as in C# and Java. Enums, type aliases, object literals, and class expressions are no types. - A function in the body of a class is a method unless the keyword
staticmarks it, a private method such as#notify()included.constructorgets the stereotype constructor, while JavaScript has no destructor and no copy constructor. - A method named
getorset, such asget(key), is an ordinary method, unlike an accessor such asget size(). - A function without a body, such as an abstract method, an overload signature, or a
declare function, gets no stereotype. - With
-f, the function declarations at the top level of a file, the function declarations in a TypeScript namespace, and thestaticmethods of a class get the stereotypes of free functions.thisin a static method names the class, asclsin a Python class method does. - A function assigned to a variable, such as
const f = () => { ... }, and a method added to a prototype get no stereotype. - A body is empty when it holds nothing but comments.
- A class defined in a function is a local class and gets no stereotype.
- A method accesses its object through
this. The#of a private name belongs to the name.this.#countthus names the data member#count. - Every name that a method reaches through the object, such as
this.total, is a data member of the class, unless the class or an ancestor in the archive defines a method or a class of that name. The body of a class declares further data members, namely the fields of the class and the parameter properties of a TypeScript constructor, such aswidthofconstructor(protected readonly width: number). - The code reaches a
staticfield, such asstatic count = 0;, through the name of the class.this.countnames a data member, also where the class declares astaticfield of that name. - An accessor
get size()orset size(value)is a method with stereotypes of its own. For the other methods, the name of such an accessor is a data member. - Since srcML marks a template literal as one literal, Stereocode reads the names in its placeholders from the text.
${this.total}thus reads the data membertotal.
- The type of a parameter, a local variable, a data member, or a returned value is the type that its annotation names. Without an annotation, a variable that the code assigns a newly created object has the class of that object. A data member assigned an annotated parameter has the type of that parameter. Any other type is unknown. An unknown type counts as primitive and thus makes no method a collaborator.
- The type of a function counts as primitive, as do the types of the built-in list of primitive types. A union with
undefinedornull, such asEngine | undefined, names the classEngine. The return typesvoid,undefined, andneverare void, whilebooleanis Boolean. - Every parameter other than the object shares its object with the caller, unless its annotation names a primitive type or the parameter gathers arguments, as
...restdoes. A TypeScript parameter of an array, a tuple, or an object type shares its object also where its elements have a primitive type, such asnumber[]or{ x: number }. A write through a shared parameter changes an object of the caller, as does a call on the parameter unless its method is a query method. - A write through a member or an index of a parameter or a local variable, such as
p.x = 1orp[i] = 1, changes the object that the variable refers to. - A local variable is a name that
var,let, orconstdeclares, a parameter of an arrow function, acatchparameter, or a loop variable. A destructuring pattern declares the names of its keys and its elements, such asbof{ a: b }andfirstof[first, ...others]. - The parameters and the local variables of a nested function or an arrow function are local variables of the enclosing function.
- A variable at the top level of a file is a global of the module of the file. A name without a qualifier refers only to the globals of its own file, while code in another file accesses a global through the name of the module, such as
settings.level. An imported name is no variable. A module thatrequireloads from outside the archive defines no global of the archive. - A call on a
constglobal of a primitive value changes no global.
- An expression is Boolean by its form when it is a Boolean literal, a negation, a comparison, a call of a built-in function that tests its argument, such as
Array.isArray, or a call of a method of a built-in type that tests its object, such asincludes. A conjunction, a disjunction, or a conditional expression is Boolean by its form when each of its results is Boolean by its form. The value of any other call is not Boolean by its form, since the called function may return any value. - A
yieldwith a value counts as a return of that value, while ayieldin a nested function returns no value of the enclosing function.return undefinedcounts as areturnwithout a value. - A function without a return annotation returns
voidwhen noreturnand noyieldgives a value. Otherwise, such a function returns a Boolean when each returned expression is Boolean by its form and the class of an object when it returns an object that it creates. Any other such function returns an unknown type. - An
asyncfunction returns the value to which its promise resolves. Its return typePromise<Matrix>thus counts asMatrix.
-
this.m()calls a method of the object, unlessmnames a data member of the class and no method of the class or of its ancestors has that name.this.onClick()of a data member that stores a function is thus a call of a variable.super.m()calls a method of a parent. - Optional chaining, as in
this.fuel?.fill(), names the same member as the plain access operator. -
f(),Name.f(), andmodule.f()are calls of free functions.new A()creates an object, also without an argument list.
Stereocode reads four built-in lists for JavaScript and TypeScript. The list files of the options -p, -c, -s, and -q add entries to these lists under the section [JavaScript], as the README states. Each list below opens with a click.
Primitive types (18 entries)
A type of this list makes no method a collaborator and no return type of a factory.
any, BigInt, bigint, Boolean, boolean, Function, never, null, Number, number, object, String, string, Symbol, symbol, undefined, unknown, void
Ignored calls (9 entries)
The rules ignore a call of these names. A name without a qualifier, such as alert, is ignored only as the whole name of a call.
alert, console.debug, console.error, console.info, console.log, console.trace, console.warn, process.exit, require
Specifiers and containers (32 entries)
Stereocode strips these entries from a written type before it reads the names of the type. A wrapper or a container thus counts by the types in its arguments, such as Engine of Readonly<Engine> or Map<string, Engine>. Each entry is a regular expression, such as \.\.\. for ....
&, <, >, \?, \|, Array, AsyncGenerator, AsyncIterable, AsyncIterableIterator, AsyncIterator, Awaited, Generator, Iterable, IterableIterator, Iterator, keyof, Map, NonNullable, Partial, Promise, PromiseLike, Readonly, readonly, ReadonlyArray, ReadonlyMap, ReadonlySet, Record, Required, Set, typeof, WeakMap, WeakSet
Query methods (52 entries)
A call of these methods only reads the variable that the call is on. The list holds the methods of strings and the methods of arrays, maps, and sets that change nothing.
at, charAt, charCodeAt, codePointAt, concat, endsWith, entries, every, filter, find, findIndex, findLast, findLastIndex, flat, flatMap, forEach, get, has, includes, indexOf, join, keys, lastIndexOf, localeCompare, map, match, matchAll, normalize, padEnd, padStart, reduce, reduceRight, repeat, replace, replaceAll, search, slice, some, split, startsWith, substring, toLowerCase, toReversed, toSorted, toSpliced, toString, toUpperCase, trim, trimEnd, trimStart, valueOf, values
Each example below is the declaration of an element from the stereotype cases of Stereocode, whose expected stereotypes are written by hand in @stereotype comments. Each link opens the case on the branch develop. The second column gives every stereotype of the element. A stereotype without an example has no case in JavaScript and TypeScript.
| Stereotype | Stereotypes of the element | Declaration |
|---|---|---|
| get | get |
getValue(): number | undefined |
| predicate | predicate |
isFreezing(): boolean |
| property | property |
read(count?: number): string |
| void-accessor | void-accessor |
readCelsius(values: number[]): void |
| set | set |
setRadius(radius: number): void |
| command | command |
increment(): void |
| non-void-command | property non-void-command |
withdrawAll() |
| factory | factory stateless |
find(count: number): Matrix | undefined |
| collaborator | set collaborator |
attach(printer: Printer): void |
| controller | controller |
sendSecond(printer: Printer): void |
| wrapper | wrapper |
announce(): void |
| incidental | incidental |
greeting(name) |
| stateless | stateless |
fail() |
| empty | empty |
refresh() {} |
| constructor | constructor |
constructor(printer: Printer, pages: number) |
| copy-constructor | No case | |
| destructor | No case | |
| unclassified | unclassified |
show() |
TABLE 1. Examples of the method stereotypes in JavaScript and TypeScript.
| Stereotype | Stereotypes of the element | Declaration |
|---|---|---|
| main | No case | |
| predicate | predicate |
async function isReady(value) |
| property | property |
export function square(value: number): number |
| global-command | global-command |
export function raiseLevel() |
| command | command |
function swap([left, right], pair) |
| factory | property factory |
function makeWidget(style) |
| literal | literal |
function copiedLevel() |
| wrapper | wrapper |
function run(callback) |
| empty | empty |
function noop(value) {} |
| unclassified | unclassified |
function show(value) |
TABLE 2. Examples of the free-function stereotypes in JavaScript and TypeScript, which the option -f assigns.
| Stereotype | Stereotypes of the element | Declaration |
|---|---|---|
| boundary | entity boundary |
class Measure |
| commander | minimal-entity commander |
class Counter |
| controller | factory controller |
class WidgetFactory |
| data-class | data-class |
class Pair |
| data-provider | data-provider |
abstract class Base<T> implements Shape |
| empty | empty |
interface Named |
| entity | entity boundary |
class Measure |
| factory | factory controller |
class WidgetFactory |
| minimal-entity | minimal-entity commander |
class Counter |
| pure-controller | pure-controller |
class Dispatcher |
| small-class | data-provider small-class |
class Square<T> extends Base<T> |
TABLE 3. Examples of the class stereotypes in JavaScript and TypeScript, where large-class, lazy-class, data-class, and small-class of a type of another kind take that kind in place of class, such as data-struct.