Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for .cjs and .mjs input files #39840

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
@@ -248,6 +248,16 @@ namespace ts {

if (!file.locals) {
bind(file);
// `bindSourceFileAsExternalModule` is called in `setCommonJsModuleIndicator`,
// but if `setCommonJsModuleIndicator` was not called, as it won't be for `.cjs`
// files with no imports or exports, then `bindSourceFileAsExternalModule`
// will not have been called, which would cause the checker to crash.

// This also can't be moved to `bindSourceFileIfExternalModule`,
// as that gets called after CommonJS exports are bound.
if (isCommonJsModule(file) && !file.commonJsModuleIndicator) {
bindSourceFileAsExternalModule();
}
file.symbolCount = symbolCount;
file.classifiableNames = classifiableNames;
delayedBindJSDocTypedefTag();
@@ -283,7 +293,7 @@ namespace ts {
return true;
}
else {
return !!file.externalModuleIndicator;
return isExternalModule(file);
}
}

@@ -2154,7 +2164,7 @@ namespace ts {
return Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode;
}

if (file.externalModuleIndicator) {
if (isExternalModule(file)) {
return Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Modules_are_automatically_in_strict_mode;
}

@@ -2223,7 +2233,7 @@ namespace ts {
return Diagnostics.Invalid_use_of_0_Class_definitions_are_automatically_in_strict_mode;
}

if (file.externalModuleIndicator) {
if (isExternalModule(file)) {
return Diagnostics.Invalid_use_of_0_Modules_are_automatically_in_strict_mode;
}

@@ -2244,7 +2254,7 @@ namespace ts {
return Diagnostics.Function_declarations_are_not_allowed_inside_blocks_in_strict_mode_when_targeting_ES3_or_ES5_Class_definitions_are_automatically_in_strict_mode;
}

if (file.externalModuleIndicator) {
if (isExternalModule(file)) {
return Diagnostics.Function_declarations_are_not_allowed_inside_blocks_in_strict_mode_when_targeting_ES3_or_ES5_Modules_are_automatically_in_strict_mode;
}

@@ -2460,7 +2470,7 @@ namespace ts {
bindSpecialPropertyDeclaration(expr);
}
if (isInJSFile(expr) &&
file.commonJsModuleIndicator &&
isCommonJsModule(file) &&
isModuleExportsAccessExpression(expr) &&
!lookupSymbolForNameWorker(blockScopeContainer, "module" as __String)) {
declareSymbol(file.locals!, /*parent*/ undefined, expr.expression,
@@ -2743,7 +2753,7 @@ namespace ts {
}

function setCommonJsModuleIndicator(node: Node) {
if (file.externalModuleIndicator) {
if (isExternalModule(file)) {
return false;
}
if (!file.commonJsModuleIndicator) {
@@ -2866,7 +2876,7 @@ namespace ts {
if (hasDynamicName(node)) {
break;
}
else if ((thisContainer as SourceFile).commonJsModuleIndicator) {
else if (isCommonJsModule(thisContainer as SourceFile)) {
declareSymbol(thisContainer.symbol.exports!, thisContainer.symbol, node, SymbolFlags.Property | SymbolFlags.ExportValue, SymbolFlags.None);
}
else {
@@ -3133,7 +3143,7 @@ namespace ts {
function bindCallExpression(node: CallExpression) {
// We're only inspecting call expressions to detect CommonJS modules, so we can skip
// this check if we've already seen the module indicator
if (!file.commonJsModuleIndicator && isRequireCall(node, /*checkArgumentIsStringLiteralLike*/ false)) {
if (!file.commonJsModuleIndicator && !fileExtensionIs(file.fileName, Extension.Mjs) && isRequireCall(node, /*checkArgumentIsStringLiteralLike*/ false)) {
setCommonJsModuleIndicator(node);
}
}
35 changes: 21 additions & 14 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
@@ -1382,7 +1382,7 @@ namespace ts {
const useFile = getSourceFileOfNode(usage);
const declContainer = getEnclosingBlockScopeContainer(declaration);
if (declarationFile !== useFile) {
if ((moduleKind && (declarationFile.externalModuleIndicator || useFile.externalModuleIndicator)) ||
if ((moduleKind && (isExternalModule(declarationFile) || isExternalModule(useFile))) ||
(!outFile(compilerOptions)) ||
isInTypeQuery(usage) ||
declaration.flags & NodeFlags.Ambient) {
@@ -1717,7 +1717,7 @@ namespace ts {
isInExternalModule = true;
// falls through
case SyntaxKind.ModuleDeclaration:
const moduleExports = getSymbolOfNode(location as SourceFile | ModuleDeclaration).exports || emptySymbols;
const moduleExports = getSymbolOfNode(location as SourceFile | ModuleDeclaration)?.exports || emptySymbols;
if (location.kind === SyntaxKind.SourceFile || (isModuleDeclaration(location) && location.flags & NodeFlags.Ambient && !isGlobalScopeAugmentation(location))) {

// It's an external module. First see if the module has an export default and if the local
@@ -1751,7 +1751,7 @@ namespace ts {

// ES6 exports are also visible locally (except for 'default'), but commonjs exports are not (except typedefs)
if (name !== InternalSymbolName.Default && (result = lookup(moduleExports, name, meaning & SymbolFlags.ModuleMember))) {
if (isSourceFile(location) && location.commonJsModuleIndicator && !result.declarations.some(isJSDocTypeAlias)) {
if (isSourceFile(location) && isCommonJsModule(location) && !result.declarations.some(isJSDocTypeAlias)) {
result = undefined;
}
else {
@@ -1945,7 +1945,7 @@ namespace ts {
if (!result) {
if (lastLocation) {
Debug.assert(lastLocation.kind === SyntaxKind.SourceFile);
if ((lastLocation as SourceFile).commonJsModuleIndicator && name === "exports" && meaning & lastLocation.symbol.flags) {
if (isCommonJsModule(lastLocation as SourceFile) && name === "exports" && meaning & lastLocation.symbol.flags) {
return lastLocation.symbol;
}
}
@@ -2467,7 +2467,7 @@ namespace ts {
return hasExportAssignmentSymbol(moduleSymbol);
}
// JS files have a synthetic default if they do not contain ES2015+ module syntax (export = is not valid in js) _and_ do not have an __esModule marker
return !file.externalModuleIndicator && !resolveExportByName(moduleSymbol, escapeLeadingUnderscores("__esModule"), /*sourceNode*/ undefined, dontResolveAlias);
return !isExternalModule(file) && !resolveExportByName(moduleSymbol, escapeLeadingUnderscores("__esModule"), /*sourceNode*/ undefined, dontResolveAlias);
}

function getTargetOfImportClause(node: ImportClause, dontResolveAlias: boolean): Symbol | undefined {
@@ -8568,7 +8568,7 @@ namespace ts {
declaration.parent.kind === SyntaxKind.BinaryExpression)) {
return getWidenedTypeForAssignmentDeclaration(symbol);
}
else if (symbol.flags & SymbolFlags.ValueModule && declaration && isSourceFile(declaration) && declaration.commonJsModuleIndicator) {
else if (symbol.flags & SymbolFlags.ValueModule && declaration && isSourceFile(declaration) && isCommonJsModule(declaration)) {
const resolvedModule = resolveExternalModuleSymbol(symbol);
if (resolvedModule !== symbol) {
if (!pushTypeResolution(symbol, TypeSystemPropertyName.Type)) {
@@ -22523,11 +22523,11 @@ namespace ts {

if (isSourceFile(container)) {
// look up in the source file's locals or exports
if (container.commonJsModuleIndicator) {
if (isCommonJsModule(container)) {
const fileSymbol = getSymbolOfNode(container);
return fileSymbol && getTypeOfSymbol(fileSymbol);
}
else if (container.externalModuleIndicator) {
else if (isExternalModule(container)) {
// TODO: Maybe issue a better error than 'object is possibly undefined'
return undefinedType;
}
@@ -22893,7 +22893,7 @@ namespace ts {
// Don't contextually type `this` as `exports` in `exports.Point = function(x, y) { this.x = x; this.y = y; }`
if (inJs && isIdentifier(expression)) {
const sourceFile = getSourceFileOfNode(parent);
if (sourceFile.commonJsModuleIndicator && getResolvedSymbol(expression) === sourceFile.symbol) {
if (isCommonJsModule(sourceFile) && getResolvedSymbol(expression) === sourceFile.symbol) {
return undefined;
}
}
@@ -27788,12 +27788,18 @@ namespace ts {
}

function checkImportMetaProperty(node: MetaProperty) {
if (moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.System) {
error(node, Diagnostics.The_import_meta_meta_property_is_only_allowed_when_the_module_option_is_esnext_or_system);
}
const file = getSourceFileOfNode(node);
if (fileExtensionIs(file.fileName, Extension.Cjs)) {
error(node, Diagnostics.The_import_meta_meta_property_is_not_allowed_in_CommonJS_module_files);
Debug.assert(file.externalModuleIndicator === undefined, "Containing `.cjs` file should not be a module.");
}
else {
if (moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.System) {
error(node, Diagnostics.The_import_meta_meta_property_is_only_allowed_when_the_module_option_is_esnext_or_system);
}
Debug.assert(!!file.externalModuleIndicator, "Containing file should be a module.");
}
Debug.assert(!!(file.flags & NodeFlags.PossiblyContainsImportMeta), "Containing file is missing import meta node flag.");
Debug.assert(!!file.externalModuleIndicator, "Containing file should be a module.");
return node.name.escapedText === "meta" ? getGlobalImportMetaType() : errorType;
}

@@ -35466,7 +35472,8 @@ namespace ts {
}

function checkGrammarModuleElementContext(node: Statement, errorMessage: DiagnosticMessage): boolean {
const isInAppropriateContext = node.parent.kind === SyntaxKind.SourceFile || node.parent.kind === SyntaxKind.ModuleBlock || node.parent.kind === SyntaxKind.ModuleDeclaration;
const isInAppropriateContext = (node.parent.kind === SyntaxKind.SourceFile && !fileExtensionIs((<SourceFile>node.parent).fileName, Extension.Cjs))
|| node.parent.kind === SyntaxKind.ModuleBlock || node.parent.kind === SyntaxKind.ModuleDeclaration;
if (!isInAppropriateContext) {
grammarErrorOnFirstToken(node, errorMessage);
}
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
@@ -1180,6 +1180,10 @@
"category": "Error",
"code": 1388
},
"The 'import.meta' meta-property is not allowed in CommonJS module files.": {
"category": "Error",
"code": 1389
},

"The types of '{0}' are incompatible between these types.": {
"category": "Error",
14 changes: 13 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
@@ -116,9 +116,11 @@ namespace ts {
return Extension.Json;
}

const { fileName } = sourceFile;

if (options.jsx === JsxEmit.Preserve) {
if (isSourceFileJS(sourceFile)) {
if (fileExtensionIs(sourceFile.fileName, Extension.Jsx)) {
if (fileExtensionIs(fileName, Extension.Jsx)) {
return Extension.Jsx;
}
}
@@ -127,6 +129,16 @@ namespace ts {
return Extension.Jsx;
}
}

// Preserve the output extension for `.cjs` and `.mjs` files
if (fileExtensionIs(fileName, Extension.Cjs)) {
return Extension.Cjs;
}

if (fileExtensionIs(fileName, Extension.Mjs)) {
return Extension.Mjs;
}

return Extension.Js;
}

5 changes: 3 additions & 2 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ namespace ts {
*/
enum Extensions {
TypeScript, /** '.ts', '.tsx', or '.d.ts' */
JavaScript, /** '.js' or '.jsx' */
JavaScript, /** '.js', '.cjs', '.mjs' or '.jsx' */
Json, /** '.json' */
TSConfig, /** '.json' with `tsconfig` used instead of `index` */
DtsOnly /** Only '.d.ts' */
@@ -1096,6 +1096,7 @@ namespace ts {
case Extensions.TypeScript:
return tryExtension(Extension.Ts) || tryExtension(Extension.Tsx) || tryExtension(Extension.Dts);
case Extensions.JavaScript:
// `.cjs` and `.mjs` file extensions must always have the file extension
return tryExtension(Extension.Js) || tryExtension(Extension.Jsx);
case Extensions.TSConfig:
case Extensions.Json:
@@ -1235,7 +1236,7 @@ namespace ts {
function extensionIsOk(extensions: Extensions, extension: Extension): boolean {
switch (extensions) {
case Extensions.JavaScript:
return extension === Extension.Js || extension === Extension.Jsx;
return extension === Extension.Js || extension === Extension.Cjs || extension === Extension.Mjs || extension === Extension.Jsx;
case Extensions.TSConfig:
case Extensions.Json:
return extension === Extension.Json;
11 changes: 10 additions & 1 deletion src/compiler/moduleSpecifiers.ts
Original file line number Diff line number Diff line change
@@ -17,6 +17,10 @@ namespace ts.moduleSpecifiers {
ending: getEnding(),
};
function getEnding(): Ending {
// `.mjs` files must always use file extensions in import specifiers
if (fileExtensionIs(importingSourceFile.fileName, Extension.Mjs)) {
return Ending.JsExtension;
}
switch (importModuleSpecifierEnding) {
case "minimal": return Ending.Minimal;
case "index": return Ending.Index;
@@ -309,6 +313,8 @@ namespace ts.moduleSpecifiers {
const relativePath = normalizedSourcePath !== undefined ? ensurePathIsNonModuleName(getRelativePathFromDirectory(normalizedSourcePath, normalizedTargetPath, getCanonicalFileName)) : normalizedTargetPath;
return getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeJs
? removeExtensionAndIndexPostFix(relativePath, ending, compilerOptions)
// `.cjs` and `.mjs` files must always be imported with the file extension:
: fileExtensionIsOneOf(relativePath, [Extension.Cjs, Extension.Mjs]) ? relativePath
: removeFileExtension(relativePath);
}

@@ -490,7 +496,8 @@ namespace ts.moduleSpecifiers {
}

function removeExtensionAndIndexPostFix(fileName: string, ending: Ending, options: CompilerOptions): string {
if (fileExtensionIs(fileName, Extension.Json)) return fileName;
// `.cjs` and `.mjs` files must always be imported with the file extension, as Node doesn't resolve them by default:
if (fileExtensionIsOneOf(fileName, [Extension.Cjs, Extension.Mjs, Extension.Json])) return fileName;
const noExtension = removeFileExtension(fileName);
switch (ending) {
case Ending.Minimal:
@@ -513,6 +520,8 @@ namespace ts.moduleSpecifiers {
case Extension.Tsx:
return options.jsx === JsxEmit.Preserve ? Extension.Jsx : Extension.Js;
case Extension.Js:
case Extension.Cjs:
case Extension.Mjs:
case Extension.Jsx:
case Extension.Json:
return ext;
12 changes: 10 additions & 2 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
@@ -636,7 +636,11 @@ namespace ts {

// See also `isExternalOrCommonJsModule` in utilities.ts
export function isExternalModule(file: SourceFile): boolean {
return file.externalModuleIndicator !== undefined;
return file.externalModuleIndicator !== undefined || fileExtensionIs(file.fileName, Extension.Mjs);
}

export function isCommonJsModule(file: SourceFile): boolean {
return file.commonJsModuleIndicator !== undefined || fileExtensionIs(file.fileName, Extension.Cjs);
}

// Produces a new SourceFile for the 'newText' provided. The 'textChangeRange' parameter
@@ -1157,6 +1161,8 @@ namespace ts {
// this is quite rare comparing to other nodes and createNode should be as fast as possible
let sourceFile = factory.createSourceFile(statements, endOfFileToken, flags);
setTextRangePosWidth(sourceFile, 0, sourceText.length);

sourceFile.fileName = fileName;
setExternalModuleIndicator(sourceFile);

// If we parsed this as an external module, it may contain top-level await
@@ -1168,7 +1174,6 @@ namespace ts {
sourceFile.bindDiagnostics = [];
sourceFile.bindSuggestionDiagnostics = undefined;
sourceFile.languageVersion = languageVersion;
sourceFile.fileName = fileName;
sourceFile.languageVariant = getLanguageVariant(scriptKind);
sourceFile.isDeclarationFile = isDeclarationFile;
sourceFile.scriptKind = scriptKind;
@@ -7047,6 +7052,9 @@ namespace ts {
}

function setExternalModuleIndicator(sourceFile: SourceFile) {
// `.cjs` files are never ECMAScript modules
if (fileExtensionIs(sourceFile.fileName, Extension.Cjs)) return;

// Try to use the first top-level import/export when available, then
// fall back to looking for an 'import.meta' somewhere in the tree if necessary.
sourceFile.externalModuleIndicator =
Loading