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 --allowArbitraryExtensions, a flag for allowing arbitrary extensions on import paths #51435

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Rename to allowArbitraryExtensions
weswigham committed Jan 9, 2023
commit 8014ef7e681511bbf96961021624771087ddb634
2 changes: 1 addition & 1 deletion src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
@@ -1207,7 +1207,7 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
defaultValueDescription: false,
},
{
name: "allowNonJsExtensions",
name: "allowArbitraryExtensions",
type: "boolean",
affectsModuleResolution: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was gathering module-resolution-affecting compiler options for docs, and noticed this. AFAICT it doesn’t affect module resolution, just whether an error is issued. This should probably be swapped for affectsSemanticDiagnostics.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something that should be in 5.0 beta? I just got to this comment in my email and noticed a PR wasn't sent for this (but I don't know how these two settings matter myself)

category: Diagnostics.Modules,
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
@@ -5194,7 +5194,7 @@
"category": "Message",
"code": 6262
},
"Module '{0}' was resolved to '{1}', but '--allowNonJsExtensions' is not set.": {
"Module '{0}' was resolved to '{1}', but '--allowArbitraryExtensions' is not set.": {
"category": "Error",
"code": 6263
},
4 changes: 2 additions & 2 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
@@ -4981,8 +4981,8 @@ export function getResolutionDiagnostic(options: CompilerOptions, { extension }:
return getResolveJsonModule(options) ? undefined : Diagnostics.Module_0_was_resolved_to_1_but_resolveJsonModule_is_not_used;
}
function needAllowNonJsExtensions() {
// But don't report the allowNonJsExtensions error from declaration files (no reason to report it, since the import doesn't have a runtime component)
return isDeclarationFile || options.allowNonJsExtensions ? undefined : Diagnostics.Module_0_was_resolved_to_1_but_allowNonJsExtensions_is_not_set;
// But don't report the allowArbitraryExtensions error from declaration files (no reason to report it, since the import doesn't have a runtime component)
return isDeclarationFile || options.allowArbitraryExtensions ? undefined : Diagnostics.Module_0_was_resolved_to_1_but_allowArbitraryExtensions_is_not_set;
}
}

2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
@@ -6974,7 +6974,7 @@ export interface CompilerOptions {
allowImportingTsExtensions?: boolean;
allowJs?: boolean;
/** @internal */ allowNonTsExtensions?: boolean;
allowNonJsExtensions?: boolean;
allowArbitraryExtensions?: boolean;
allowSyntheticDefaultImports?: boolean;
allowUmdGlobalAccess?: boolean;
allowUnreachableCode?: boolean;
2 changes: 1 addition & 1 deletion src/testRunner/compilerRunner.ts
Original file line number Diff line number Diff line change
@@ -166,7 +166,7 @@ class CompilerTest {
"resolvePackageJsonExports",
"resolvePackageJsonImports",
"resolveJsonModule",
"allowNonJsExtensions",
"allowArbitraryExtensions",
];
private fileName: string;
private justName: string;
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
@@ -7018,7 +7018,7 @@ declare namespace ts {
interface CompilerOptions {
allowImportingTsExtensions?: boolean;
allowJs?: boolean;
allowNonJsExtensions?: boolean;
allowArbitraryExtensions?: boolean;
allowSyntheticDefaultImports?: boolean;
allowUmdGlobalAccess?: boolean;
allowUnreachableCode?: boolean;
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
@@ -3083,7 +3083,7 @@ declare namespace ts {
interface CompilerOptions {
allowImportingTsExtensions?: boolean;
allowJs?: boolean;
allowNonJsExtensions?: boolean;
allowArbitraryExtensions?: boolean;
allowSyntheticDefaultImports?: boolean;
allowUmdGlobalAccess?: boolean;
allowUnreachableCode?: boolean;
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowNonJsExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowNonJsExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowNonJsExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowNonJsExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowNonJsExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowNonJsExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowNonJsExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowNonJsExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowNonJsExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowNonJsExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowNonJsExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"allowArbitraryExtensions": true
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/conformance/nonjsExtensions/file.ts(1,22): error TS6263: Module './component.html' was resolved to 'tests/cases/conformance/nonjsExtensions/component.d.html.ts', but '--allowNonJsExtensions' is not set.
tests/cases/conformance/nonjsExtensions/file.ts(1,22): error TS6263: Module './component.html' was resolved to 'tests/cases/conformance/nonjsExtensions/component.d.html.ts', but '--allowArbitraryExtensions' is not set.


==== tests/cases/conformance/nonjsExtensions/component.d.html.ts (0 errors) ====
@@ -18,7 +18,7 @@ tests/cases/conformance/nonjsExtensions/file.ts(1,22): error TS6263: Module './c
==== tests/cases/conformance/nonjsExtensions/file.ts (1 errors) ====
import * as mod from "./component.html";
~~~~~~~~~~~~~~~~~~
!!! error TS6263: Module './component.html' was resolved to 'tests/cases/conformance/nonjsExtensions/component.d.html.ts', but '--allowNonJsExtensions' is not set.
!!! error TS6263: Module './component.html' was resolved to 'tests/cases/conformance/nonjsExtensions/component.d.html.ts', but '--allowArbitraryExtensions' is not set.

window.customElements.define("my-html5-element", mod.HTML5Element);

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
tests/cases/conformance/nonjsExtensions/main.ts(1,22): error TS6263: Module './dir/native.node' was resolved to 'tests/cases/conformance/nonjsExtensions/dir/native.d.node.ts', but '--allowNonJsExtensions' is not set.
tests/cases/conformance/nonjsExtensions/main.ts(1,22): error TS6263: Module './dir/native.node' was resolved to 'tests/cases/conformance/nonjsExtensions/dir/native.d.node.ts', but '--allowArbitraryExtensions' is not set.


==== tests/cases/conformance/nonjsExtensions/main.ts (1 errors) ====
import mod = require("./dir/native.node");
~~~~~~~~~~~~~~~~~~~
!!! error TS6263: Module './dir/native.node' was resolved to 'tests/cases/conformance/nonjsExtensions/dir/native.d.node.ts', but '--allowNonJsExtensions' is not set.
!!! error TS6263: Module './dir/native.node' was resolved to 'tests/cases/conformance/nonjsExtensions/dir/native.d.node.ts', but '--allowArbitraryExtensions' is not set.
mod.doNativeThing("good");

==== tests/cases/conformance/nonjsExtensions/package.json (0 errors) ====
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ Output::
>> Screen clear
[12:00:21 AM] Starting compilation in watch mode...

src/project/main.ts:1:18 - error TS6263: Module './data.json' was resolved to '/src/project/data.d.json.ts', but '--allowNonJsExtensions' is not set.
src/project/main.ts:1:18 - error TS6263: Module './data.json' was resolved to '/src/project/data.d.json.ts', but '--allowArbitraryExtensions' is not set.

1 import data from "./data.json"; let x: string = data;
   ~~~~~~~~~~~~~
@@ -89,7 +89,7 @@ var x = data_json_1.default;


//// [/src/project/tsconfig.tsbuildinfo]
{"program":{"fileNames":["../../a/lib/lib.d.ts","./data.d.json.ts","./main.ts"],"fileInfos":[{"version":"-7698705165-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }","affectsGlobalScope":true},"2718060498-declare var val: string; export default val;","6961905452-import data from \"./data.json\"; let x: string = data;"],"referencedMap":[],"exportedModulesMap":[],"semanticDiagnosticsPerFile":[1,2,[3,[{"file":"./main.ts","start":17,"length":13,"messageText":"Module './data.json' was resolved to '/src/project/data.d.json.ts', but '--allowNonJsExtensions' is not set.","category":1,"code":6263}]]]},"version":"FakeTSVersion"}
{"program":{"fileNames":["../../a/lib/lib.d.ts","./data.d.json.ts","./main.ts"],"fileInfos":[{"version":"-7698705165-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }","affectsGlobalScope":true},"2718060498-declare var val: string; export default val;","6961905452-import data from \"./data.json\"; let x: string = data;"],"referencedMap":[],"exportedModulesMap":[],"semanticDiagnosticsPerFile":[1,2,[3,[{"file":"./main.ts","start":17,"length":13,"messageText":"Module './data.json' was resolved to '/src/project/data.d.json.ts', but '--allowArbitraryExtensions' is not set.","category":1,"code":6263}]]]},"version":"FakeTSVersion"}

//// [/src/project/tsconfig.tsbuildinfo.readable.baseline.txt]
{
@@ -130,7 +130,7 @@ var x = data_json_1.default;
"file": "./main.ts",
"start": 17,
"length": 13,
"messageText": "Module './data.json' was resolved to '/src/project/data.d.json.ts', but '--allowNonJsExtensions' is not set.",
"messageText": "Module './data.json' was resolved to '/src/project/data.d.json.ts', but '--allowArbitraryExtensions' is not set.",
"category": 1,
"code": 6263
}
@@ -139,7 +139,7 @@ var x = data_json_1.default;
]
},
"version": "FakeTSVersion",
"size": 925
"size": 929
}


@@ -158,7 +158,7 @@ Output::
>> Screen clear
[12:00:32 AM] File change detected. Starting incremental compilation...

src/project/main.ts:1:18 - error TS6263: Module './data.json' was resolved to '/src/project/data.d.json.ts', but '--allowNonJsExtensions' is not set.
src/project/main.ts:1:18 - error TS6263: Module './data.json' was resolved to '/src/project/data.d.json.ts', but '--allowArbitraryExtensions' is not set.

1 import data from "./data.json"; let x: string = data;
   ~~~~~~~~~~~~~
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @lib: es2020,dom
// @target: es2020
// @allowNonJsExtensions: true,false
// @allowArbitraryExtensions: true,false
// @filename: component.d.html.ts

// html modules were proposed at https://github.com/WICG/webcomponents/blob/gh-pages/proposals/html-modules-explainer.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @allowNonJsExtensions: true
// @allowArbitraryExtensions: true
// @resolveJsonModule: true,false
// @filename: main.ts
import data from "./data.json";
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @allowNonJsExtensions: true
// @allowArbitraryExtensions: true
// @module: nodenext
// @filename: package.json
{"type": "module"}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @lib: es2020,dom
// @target: es2020
// @allowNonJsExtensions: true,false
// @allowArbitraryExtensions: true,false
// @module: nodenext
// @filename: package.json
{"type": "module"}