-
Notifications
You must be signed in to change notification settings - Fork 29
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
Emit deprecation warnings for legacy JS API #331
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,7 +2,7 @@ | |||||
// MIT-style license that can be found in the LICENSE file or at | ||||||
// https://opensource.org/licenses/MIT. | ||||||
|
||||||
import {DeprecationOrId} from './vendor/sass'; | ||||||
import {Deprecation, DeprecationOrId, Options} from './vendor/sass'; | ||||||
import {Version} from './version'; | ||||||
|
||||||
export {deprecations} from './vendor/deprecations'; | ||||||
|
@@ -15,12 +15,132 @@ export {Deprecation, DeprecationOrId, DeprecationStatus} from './vendor/sass'; | |||||
export function getDeprecationIds( | ||||||
arr: (DeprecationOrId | Version)[] | ||||||
): string[] { | ||||||
return arr.flatMap(item => { | ||||||
return arr.map(item => { | ||||||
if (item instanceof Version) { | ||||||
return arr.map(item => item.toString()); | ||||||
return item.toString(); | ||||||
} else if (typeof item === 'string') { | ||||||
return item; | ||||||
} | ||||||
return item.id; | ||||||
}); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Map between active compilations and the deprecation options they use. | ||||||
* | ||||||
* This is used to determine which options to use when handling host-side | ||||||
* deprecation warnings that aren't explicitly tied to a particular compilation. | ||||||
*/ | ||||||
export const activeDeprecationOptions: Map<Object, DeprecationOptions> = | ||||||
new Map(); | ||||||
|
||||||
/** | ||||||
* Shorthand for the subset of options related to deprecations. | ||||||
*/ | ||||||
export type DeprecationOptions = Pick< | ||||||
Options<'sync'>, | ||||||
'fatalDeprecations' | 'futureDeprecations' | 'silenceDeprecations' | ||||||
>; | ||||||
|
||||||
/** | ||||||
* Handles a host-side deprecation warning, either emitting a warning, throwing | ||||||
* an error, or doing nothing depending on the deprecation options used. | ||||||
* | ||||||
* If no specific deprecation options are passed here, then options will be | ||||||
* determined based on the options of the active compilations. | ||||||
*/ | ||||||
export function warnForHostSideDeprecation( | ||||||
message: string, | ||||||
deprecation: Deprecation, | ||||||
options?: DeprecationOptions | ||||||
): void { | ||||||
if ( | ||||||
deprecation.status === 'future' && | ||||||
!isEnabledFuture(deprecation, options) | ||||||
) { | ||||||
return; | ||||||
} | ||||||
const fullMessage = `Deprecation [${deprecation.id}]: ${message}`; | ||||||
if (isFatal(deprecation, options)) { | ||||||
throw Error(fullMessage); | ||||||
} | ||||||
if (!isSilent(deprecation, options)) { | ||||||
console.warn(fullMessage); | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Checks whether the given deprecation is included in the given list of silent | ||||||
* deprecations or is silenced by at least one active compilation. | ||||||
*/ | ||||||
function isSilent( | ||||||
deprecation: Deprecation, | ||||||
options?: DeprecationOptions | ||||||
): boolean { | ||||||
if (!options) { | ||||||
for (const potentialOptions of activeDeprecationOptions.values()) { | ||||||
if (isSilent(deprecation, potentialOptions)) return true; | ||||||
} | ||||||
return false; | ||||||
} | ||||||
return getDeprecationIds(options?.silenceDeprecations ?? []).includes( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There's no way for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||||||
deprecation.id | ||||||
); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Checks whether the given deprecation is included in the given list of future | ||||||
* deprecations that should be enabled or is enabled in all active compilations. | ||||||
*/ | ||||||
function isEnabledFuture( | ||||||
deprecation: Deprecation, | ||||||
options?: DeprecationOptions | ||||||
): boolean { | ||||||
if (!options) { | ||||||
for (const potentialOptions of activeDeprecationOptions.values()) { | ||||||
if (!isEnabledFuture(deprecation, potentialOptions)) return false; | ||||||
} | ||||||
return activeDeprecationOptions.size > 0; | ||||||
} | ||||||
return getDeprecationIds(options?.futureDeprecations ?? []).includes( | ||||||
deprecation.id | ||||||
); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Checks whether the given deprecation is included in the given list of | ||||||
* fatal deprecations or is marked as fatal in all active compilations. | ||||||
*/ | ||||||
function isFatal( | ||||||
deprecation: Deprecation, | ||||||
options?: DeprecationOptions | ||||||
): boolean { | ||||||
if (!options) { | ||||||
for (const potentialOptions of activeDeprecationOptions.values()) { | ||||||
if (!isFatal(deprecation, potentialOptions)) return false; | ||||||
} | ||||||
return activeDeprecationOptions.size > 0; | ||||||
} | ||||||
const versionNumber = | ||||||
deprecation.deprecatedIn === null | ||||||
? null | ||||||
: deprecation.deprecatedIn.major * 1000000 + | ||||||
deprecation.deprecatedIn.minor * 1000 + | ||||||
deprecation.deprecatedIn.patch; | ||||||
for (const fatal of options?.fatalDeprecations ?? []) { | ||||||
if (fatal instanceof Version) { | ||||||
if (versionNumber === null) continue; | ||||||
if ( | ||||||
versionNumber <= | ||||||
fatal.major * 1000000 + fatal.minor * 1000 + fatal.patch | ||||||
) { | ||||||
return true; | ||||||
} | ||||||
} else if (typeof fatal === 'string') { | ||||||
if (fatal === deprecation.id) return true; | ||||||
} else { | ||||||
if ((fatal as Deprecation).id === deprecation.id) return true; | ||||||
} | ||||||
} | ||||||
return false; | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If you're using
Symbol
s as the key here, you could actually just use aRecord<Symbol, DeprecationOptions>
. Either way, the key type should beSymbol
.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.
Changed the key type to Symbol, but keeping this as a Map because it's better for adding/removing and checking the size