규칙 8.17
8장, 함수와 진입점 테스트
A local function (a const that holds an arrow function or a function expression, or a function declaration, written inside a function’s body) is a function of the module named after the body that declares it (surface §14), and it may read and write the variables of the functions around it, as a TypeScript closure does.
A name its body reads is looked up as TypeScript looks it up, from the innermost block outward; one that lands on a let, a const or a parameter of a function around it is a capture, which the emitted function takes as a parameter ahead of its own and which every call of it passes: by value while neither it nor a local function it calls writes the variable, and by reference once one does; a const keeps its constant, so a loop it bounds is still counted (Rule 7.5).
A whole write to a captured value parameter shares the local copy of Rule 8.8 with the
enclosing body and every other closure; its reference never reaches the enclosing caller.
An object parameter that its function writes is captured as the caller’s object it names (Rule 8.25): by value while it is only
read, by reference once it is written, and handed on as itself; a variable a local function
writes counts as a reference in Rule 8.25’s check of the call that passes it.
this in an arrow function is the object of the method around it, captured the same way, and the class the call names in a static member (Rule 8.13).
A write through a capture follows the variable’s own declaration: a let may be written, a const only through what it holds and only when its initializer built the value (Rule 6.10), and a whole-rebound value parameter through its mutable local copy (Rule 8.8).
A function declaration may be called anywhere in its block, as TypeScript hoists it, and a local function may be declared in a generic function, once for each of its instances.
A call of a local function at a point where a variable it captures is not declared yet must be refused, since TypeScript throws there; so must a function named where a value is read (held in a variable, returned, compared or chosen at run time), there being no function value, other than as an argument of a parameter that takes a function (Rule 8.18).
A function named as the callback of a fold (any(xs, isBig), zip(xs, ys, f)) passes what it captures to every call the fold makes.
규칙 본문과 그 아래 항목은 설계 문서에 적힌 컴파일러 원문이라 한국어 페이지에서도 영어로 둡니다.
근거
a function is no value here: nothing can return one, store one or choose one at run time, so every call of a local function is written inside the scope that declares it, where every variable it reads is in scope too. Passing those variables at each call is all a closure’s environment holds, and passing a written one by reference is what makes the write visible to the function around it and to the next call, as it is in TypeScript. A call is where TypeScript would read a variable that is not declared yet, so that is where the refusal stands. Before this rule a local function that read a name from the function around it was refused, with the parameter to add instead.
출처
ECMAScript function environment records (a closure reads the binding, not a copy of it), FunctionDeclarationInstantiation (a function declaration is instantiated before its block runs), let and const declarations (a binding may not be read before its declaration is evaluated) and arrow functions (this is the enclosing one); Rule 6.10; Rule 8.8; Rule 8.10; Rule 8.13; surface §14.
검증 방식
테스트로 확인합니다. 테스트나 게이트 스크립트, CI 워크플로가 이 규칙을 가리키고, 아래 파일 가운데 하나라도 규칙을 더는 가리키지 않으면 추적 검사가 실패합니다.
컴파일러가 이 규칙을 적용하는 곳을 규칙이 직접 적은 내용입니다.
TS8022 UNKNOWN_NAME for a call before a captured variable’s declaration ("f" reads "y", which is not declared yet where "f" is called: a let or a const is not there before its declaration, and TypeScript throws. Call "f" after "y" is declared.), TS8099 for a function named as a value ("f" is a function, and a shader has no function values: nothing at run time can hold one, return one or choose between two. Call it where its value is needed, "f(...)".), TS8018 ASSIGN_TARGET and TS8005 CONST_ASSIGN for a write through a parameter or a const; pinned by src/compiler/ts/closures.test.ts, which holds WGSL, GLSL ES 3.00, the CPU oracle, the codegen and the debugger to one value for each form; examples/gallery/closures/closures.shade.ts in the compile gate.
커밋 af54ddad5에서 이 규칙을 확인하는 파일입니다. 파일마다 규칙을 처음 가리키는 줄로 연결했습니다.
설명하는 절
커밋 af54ddad5의 언어 문서에서 이 규칙을 설명하는 절입니다.
적용하는 오류 코드
규칙이 Enforced by 항목에서 가리키는 진단 코드와, 레지스트리 설명에서 이 규칙을 가리키는 진단 코드입니다.
-
TS8005CONST_ASSIGN - 바뀔 수 없는 이름이나 자리에 값을 대입합니다.
const에 담은 수나 벡터,const벡터나 행렬의 성분,readonly필드나 static, 그리고readonly나"read"로 쓰지 않겠다고 약속한 스토리지 바인딩에는 값을 쓸 수 없습니다.const객체의 필드나 원소에 쓰는 것은 여기에 해당하지 않습니다.const는 객체를 담고 있을 뿐이고, 객체는 JavaScript에서처럼 계속 쓸 수 있기 때문입니다(규칙 6.10). -
TS8018ASSIGN_TARGET - 임시 값처럼 쓸 수 있는 자리가 아닌 곳에 대입하거나
++/--를 씁니다. 모르는 이름은UNKNOWN_NAME이고, 알려진 불변 바인딩에 쓰는 것은CONST_ASSIGN입니다. -
TS8022UNKNOWN_NAME - TypeShade가 풀지 못하는 이름을 가리킵니다. 함수 호출(
UNKNOWN_FN)이나 타입 이름(UNKNOWN_TYPE)은 아닙니다. 라이브러리가 TypeScript 자체를 위해 선언한 이름(Object,Math,Symbol)이나 파일이 enum, 네임스페이스, 클래스, 타입으로 선언한 이름을 값으로 읽으면, 메시지가 그 이름이 무엇인지 알려 줍니다. -
TS8099UNSUPPORTED - 자기 코드를 받을 만하지 않은 진단을 위한 포괄 코드입니다. 순차 범위 뒤인 TS8099에 따로 둡니다.
함께 보기
소스
커밋 af54ddad5의 규칙 원문:
docs/language-design.md:984(설계 문서)reqs/rules/RULE-0817.md(추적 항목)