-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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 'lib' reference directive #15780
Conversation
src/compiler/commandLineParser.ts
Outdated
@@ -7,6 +7,39 @@ | |||
namespace ts { | |||
/* @internal */ | |||
export const compileOnSaveCommandLineOption: CommandLineOption = { name: "compileOnSave", type: "boolean" }; | |||
|
|||
/* @internal */ | |||
export const libMap = createMapFromTemplate({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This just maps x
to lib.x.d.ts
. Could be a function instead, combined with a set of allowed libs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This must be a Map to be used in the compiler option I pulled it from, below. If I added a function then we would have yet another place to maintain this list (in addition to the jakefile/gulpfile).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would still be nice to generate this instead of writing it explicitly. Start with const libs = ["es5", ..., "esnext.asynciterable"]
, generate the map with lib.${x}.d.ts
, and you'll no longer need to use arrayFrom(libMap.keys())
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Except for the outliers like "es6"
and "es7"
which are aliases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, we leave libMap
as is, and add: export const libs = arrayFrom(libMap.keys());
src/compiler/utilities.ts
Outdated
@@ -9,6 +9,7 @@ namespace ts { | |||
diagnosticMessage?: DiagnosticMessage; | |||
isNoDefaultLib?: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These three should really be kind?: "no-default-lib" | "types" | "lib"
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or kind: "no-default-lib" | "path" | "types" | "lib"
.
src/compiler/program.ts
Outdated
function processLibReferenceDirectives(file: SourceFile) { | ||
const libDirectory = getLibDirectory(); | ||
forEach(file.libReferenceDirectives, libReference => { | ||
const libName = libReference.fileName.toLocaleLowerCase(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need to lower-case it? If they try accessing /// <reference lib="es2017.Object" />
we will correct their spelling to "es2017.object"
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what we do for --lib
, so its consistent.
src/compiler/utilities.ts
Outdated
export function getFileReferenceFromReferencePath(comment: string, commentRange: CommentRange): ReferencePathMatchResult { | ||
const simpleReferenceRegEx = /^\/\/\/\s*<reference\s+/gim; | ||
const isNoDefaultLibRegEx = /^(\/\/\/\s*<reference\s+no-default-lib\s*=\s*)('|")(.+?)\2\s*\/>/gim; | ||
if (simpleReferenceRegEx.test(comment)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good place for an early return.
We'll also want to support services (goto definition at least, find-all-references isn't as important) on these, but that should wait for after both this and #15737 are in. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ignoreNoDefaultLib
parameter is never provided the value true. Is it needed?
src/compiler/utilities.ts
Outdated
*/ | ||
export function getSpellingSuggestion<T>(name: string, choices: T[], exclusions: string[] | undefined, getName: (candidate: T) => string | undefined): T | undefined; | ||
export function getSpellingSuggestion<T>(name: string, choices: T[], exclusions?: string[], getName: (candidate: T) => string | undefined = identity): T | undefined { | ||
// If there is a candidate that's the same except for case, return that. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this back to the JSDoc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't want to repeat the rather lengthy description for both overloads, and its a lot of text to show when you mouse over a reference. Its too bad we don't support the @summary
jsdoc tag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ouch. I didn't realise that both overloads had separate jsdoc. And @summary
is exactly what I was thinking of. Isn't there the opposite, something like @remarks
? (Later: nope, I guess I am thinking of XML doc comments in C#)
src/compiler/program.ts
Outdated
} | ||
else { | ||
const libDirectory = host.getDefaultLibLocation ? host.getDefaultLibLocation() : getDirectoryPath(host.getDefaultLibFileName(options)); | ||
const libDirectory = getLibDirectory(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this shadows the outer libDirectory
. Can you change it so that the code is less confusing?
src/compiler/program.ts
Outdated
const libFileName = libMap.get(libName); | ||
if (libFileName) { | ||
// we ignore any 'no-default-lib' reference set on this import. | ||
processRootFile(combinePaths(libDirectory, libFileName), /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't be ignoreNoDefaultLib
be true in order to ignore 'no-default-lib'
references?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it should.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't flipping false to true for ignoreNoDefaultLib in the last commit change some tests?
@ahejlsberg: per your comments in the design meeting, this should now be loading each file only once. This means that lib.d.ts and lib.es6.d.ts are no longer concatenated, but instead use See src/lib/default.es5.d.ts for an example of the file that is now used to build lib.d.ts. |
This came up today in Google style discussion, are you still planning to land this? |
@DanielRosenwasser we discussed this one next week, would still be nice to be able to use this for our |
Any progress on this? |
Superseded by #23893 |
This change adds support for a
/// <reference lib="name" />
directive, allowing a file to explicitly include an existing built-in lib file.lib
reference directive, anyno-default-lib
reference directive in the lib file is ignored. This is so that alib
reference in an imported package won't cause your current project to lose its default lib if you haven't specified one."lib"
compiler option in tsconfig.json (e.g. uselib="es2015"
and notlib="lib.es2015.d.ts"
, etc.).In the long term this can help polyfill/shim packages like core-js or es6-shim.
Fixes #15732.