LoLBench / TypeScript_3 /instruction.md
lolbench26's picture
Publish LoLBench Harbor tasks from artifact branch
d0faac4 verified
|
Raw History Blame Contribute Delete
11.2 kB

You are working in /workspace/typescript, a source tree checked out at the base commit for this task. Implement the requested behavior in the source tree, then run:

lolbench-submit

Do not stop after editing files, running tests, or describing the solution. The task is complete only when lolbench-submit has created /logs/artifacts/solution.patch. If that file does not exist, continue working and run lolbench-submit again.

That command writes your implementation diff to /logs/artifacts/solution.patch, which is the artifact the Harbor verifier will grade. Before running lolbench-submit, clean or revert any test files you created or modified; test files must not be included in the final solution.patch.

This environment has no outbound internet access — curl/wget, git fetch/clone, package installs, and web fetch/search will all fail. Implement the requirements using only the code already in the workspace and your own knowledge; do not attempt to fetch or search external resources.

Now please implement the following requirements in the source tree:


Arbitrary Module Namespace Identifier Names

Arbitrary Module Namespace Identifier Names is an ECMAScript syntax feature (shipped in ES2022) that allows the imported and exported names of a module binding to be written as string literals rather than only as ECMAScript identifiers. This lets modules import and export bindings whose names are not valid ECMAScript identifiers — for example names containing spaces, ::, ., or other characters — which is required to faithfully interoperate with module systems (notably WebAssembly) that permit any well-formed Unicode string as an export/import name.

The canonical normative specification for this feature was merged into the ECMAScript Language Specification (ecma262) via PR #2154 "Normative: Arbitrary module namespace identifier names". There is no separate TC39 proposal repository for this feature; its substantive grammar and static semantics live in the ecma262 spec.html change introduced by that PR, reproduced inline below.

Summary

Before this feature, an import or export specifier could only name a binding using an IdentifierName:

import { foo } from "./mod.js";
export { foo as bar };

This feature generalizes the module export name — the name under which a binding is imported from, or exported to, a module — so that it may also be a StringLiteral:

import { "foo bar" as foo } from "./mod.js";
export { foo as "foo bar" };
export { foo as "foo bar" } from "./mod.js";
export * as "foo bar" from "./mod.js";

A string-literal module export name is purely a name on the module record: it is the name that other modules use to import the binding. When importing a string-literal name, the as ImportedBinding clause binds it to an ordinary local identifier (foo above), because a string literal cannot itself be referenced as a local variable. A string-literal name may also be re-exported (export { x as "string name" }) or accessed as a property of a namespace object obtained via import * as ns from "..." (e.g. ns["foo bar"]).

Motivation

ECMAScript modules need to interoperate with other module systems whose export/import names are not constrained to the ECMAScript identifier grammar. The motivating case is WebAssembly: the WebAssembly specification allows the string names of imports and exports to be any sequence of valid Unicode, including names that are not valid ECMAScript identifiers. To allow an ECMAScript module to import such a binding from (or export such a binding to) a WebAssembly module, the module-export-name grammar must admit arbitrary (well-formed Unicode) string literals.

The original ECMAScript change summarizes the goal as allowing:

import {"\0 any unicode" as foo} from "";
export {foo as "\0 any unicode"};

and notes that this matches the restriction that WASM has that the string names for exports must be valid Unicode.

Grammar

The feature extends the module import/export grammar by introducing a new ModuleExportName nonterminal that may be either an IdentifierName or a StringLiteral, and by routing import and export specifiers through it. The relevant productions (from the ecma262 spec.html change) are:

ModuleExportName :
  IdentifierName
  StringLiteral

ImportSpecifier :
  ImportedBinding
  ModuleExportName `as` ImportedBinding

ExportFromClause :
  `*`
  `*` `as` ModuleExportName
  NamedExports

ExportSpecifier :
  ModuleExportName
  ModuleExportName `as` ModuleExportName

NamedExports continues to be a brace-delimited, comma-separated list of ExportSpecifiers. An ImportSpecifier of the form ModuleExportName as ImportedBinding allows a string-literal import name to be bound to a local identifier; an ExportSpecifier of the form ModuleExportName as ModuleExportName and the ExportFromClause form * as ModuleExportName allow string-literal export names.

Static Semantics

The new StringLiteral form of ModuleExportName carries static-semantics rules that constrain it and that thread its string value (StringValue / SV) through the module's import and export entry records.

Well-formed Unicode requirement. A string-literal module export name must be well-formed Unicode (it may not contain unpaired surrogates):

ModuleExportName : StringLiteral
  - It is a Syntax Error if IsStringWellFormedUnicode(the SV of StringLiteral) is false.

This relies on the abstract operation IsStringWellFormedUnicode(string), which interprets string as a sequence of UTF-16 encoded code points and returns false if any code point is an unpaired surrogate, otherwise true.

No string literals as local references. A string-literal name is only meaningful as a module export name, not as a local binding reference. In particular, in an export { ... } clause that re-exports local bindings (a NamedExports not followed by a FromClause), the referenced names must be identifiers:

NamedExports (without a FromClause)
  - It is a Syntax Error if ReferencedBindings of NamedExports contains any StringLiterals.

StringValue / import & export entries. The string value of a ModuleExportName is its SV when it is a StringLiteral and its StringValue when it is an IdentifierName. This value flows into the module record:

ModuleExportName : StringLiteral
  StringValue : Return the SV of StringLiteral.

ImportSpecifier : ModuleExportName `as` ImportedBinding
  ImportEntriesForModule : the [[ImportName]] is the StringValue of ModuleExportName.

ExportSpecifier : ModuleExportName
  ExportedBindings / ReferencedBindings : a List whose sole element is the StringValue of ModuleExportName.

ExportSpecifier : ModuleExportName `as` ModuleExportName
  - the source name is the StringValue of the first ModuleExportName.
  - the exported name is the StringValue of the second ModuleExportName.

ExportFromClause : `*` `as` ModuleExportName
  - the exported name is the StringValue of ModuleExportName.

Concretely, for import { "x" as y } the module-record [[ImportName]] is "x" and the local binding is y; for export { y as "x" } the local source name is y and the module-record export name is "x"; and for export * as "x" from "mod" the re-exported namespace is published under the name "x".

Examples

A WebAssembly-style import/export, binding a string export name to a usable local identifier:

import { "Foo::new" as Foo_new } from "./foo.wasm"

const foo = Foo_new()

export { Foo_new as "Foo::new" }

A real-world use is esbuild's inject feature, which treats each named export of a file as an expression to substitute at build time. String-literal export names let a build-time replacement target a member expression such as Math.sin, which is concise and tree-shaking friendly:

// This file is passed to esbuild using "--inject:./inject.js"

// Minify calls to math functions
const sin = Math.sin, cos = Math.cos, tan = Math.tan
export { sin as 'Math.sin', cos as 'Math.cos', tan as 'Math.tan' }

// Polyfill 32-bit integer multiply if needed
const imul = Math.imul || ((x, y) => {
  const xlo = x & 0xffff, ylo = y & 0xffff
  return xlo * ylo + (((x >>> 16) * ylo + (y >>> 16) * xlo) << 16 >>> 0) | 0
})
export { imul as 'Math.imul' }

TypeScript Implementation Scope

The implementing change in TypeScript (PR-58640) adds support for this ES2022 syntax across the compiler and language service. Per the PR description, it does the following:

  • The syntax can be parsed and emitted
  • It works with type checking and formatting
  • Seems to work with declaration file generation
  • Analogous syntax also works for type imports+exports
  • Converts to various module formats (ESNext, CommonJS, System, AMD, UMD)
  • Errors when the module format is ES2015 or ES2020: string-literal import and export names are unsupported for those two targets, so the checker reports error TS18057 — String literal import and export names are not supported when the '--module' flag is set to 'es2015' or 'es2020'. — at each offending string-literal name.
  • Can be used with "go to definition" and "find all references"
  • Has basic support for renaming quoted export names across files
  • Works with the "convert named imports to namespace import" refactoring
  • Provides completions in import {} and export {} lists. A string-literal module export name (an export name that is not a valid identifier) is offered as a JSON-quoted string, and the inserted text depends on position: in an import {} list it is the quoted name followed by an as clause binding it to a generated local identifier — for an export named "__some value" the completion inserts "__some value" as __some_value, deriving the identifier from the name by keeping the characters that are legal in an identifier and replacing each run of other characters (such as spaces) with a single underscore — whereas in an export {} list it is just the quoted name ("__some value", with no as clause). When the caret is already inside the quotes of a string-literal specifier, the completion inserts the bare name (__some value) without the surrounding quotes.

What the PR intentionally does not do: syntax highlighting (handled in a separate TextMate grammar PR) and the inverse "convert namespace import to named imports" refactoring (introducing this syntax when it is absent may not be desirable).

Linked ecma262 PR #2154 — Normative: Arbitrary module namespace identifier names

The original ECMAScript change that introduced this feature stated:

I do not believe this affects any integration downstream.

This PR aims to allow:

import {"\0 any unicode" as foo} from "";
export {foo as "\0 any unicode"};

This matches the restriction that WASM has that the string names for exports must be valid Unicode.

Depends on #2155

Source: https://github.com/tc39/ecma262/pull/2154