|
| 1 | +--- |
| 2 | +title: Deprecation of JavaScript |
| 3 | +tags: |
| 4 | + - deprecation |
| 5 | +--- |
| 6 | + |
| 7 | +The deprecation of JavaScript follows the same general principle as the deprecation of other Moodle code. That is: |
| 8 | + |
| 9 | +- Deprecations should only be on the `main` branch, not on stables. Exceptions to this may be made in certain conditions, including: |
| 10 | + - for some external service integrations |
| 11 | + - where a feature is discovered to have been broken irreparably |
| 12 | + - to address security issues |
| 13 | +- Deprecations apply to all public APIs, classes, and files. |
| 14 | +- All deprecations should be noted with an [upgrade note](/general/development/upgradenotes). |
| 15 | +- Deprecations are split into three stages: |
| 16 | + 1. Initial deprecation |
| 17 | + 2. Final deprecation |
| 18 | + 3. Removal |
| 19 | +- All deprecations should emit debugging notices where possible |
| 20 | + |
| 21 | +## JavaScript specifics {/* #JavaScript-specifics */} |
| 22 | + |
| 23 | +<Since version="5.2" issueNumber="MDL-87867" /> |
| 24 | + |
| 25 | +From Moodle 5.2 a new deprecation utility is included in Moodle core for JavaScript. That can be found in the `core/deprecated` AMD module and is intended to function in a similar way to it's PHP Attribute equivalent. |
| 26 | + |
| 27 | +### Usage {/* #usage */} |
| 28 | + |
| 29 | +The `core/deprecated` module exports a single, default, function which can be used to emit appropriate deprecation information when called. |
| 30 | + |
| 31 | +This may be included in places such as: |
| 32 | + |
| 33 | +- the body of the file, not in any method, so that the use of the file emits the deprecation notices; and |
| 34 | +- within a method, so that when a method is called the deprecation notice is emitted. |
| 35 | + |
| 36 | +The basic usage of the API requires that a description of the thing being deprecated is provided. Other arguments are optional, but at least one of the following must be provided: |
| 37 | + |
| 38 | +- the reason for deprecation; |
| 39 | +- the replacement to use; or |
| 40 | +- the MDL in which the item was deprecated. |
| 41 | + |
| 42 | +```javascript title="Basic usage" |
| 43 | +import emitDeprecation from 'core/deprecated'; |
| 44 | + |
| 45 | +export const myFunction (the, args) => { |
| 46 | + emitDeprecation('myFunction', { |
| 47 | + replacement: 'myNewFunction', |
| 48 | + since: '5.2', |
| 49 | + mdl: 'MDL-12345', |
| 50 | + }); |
| 51 | + |
| 52 | + const modified = args.slice(0, 1); |
| 53 | + args = args.slice(1); |
| 54 | + |
| 55 | + // Call myNewFunction. |
| 56 | + return myNewFunction(the, modified, args); |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +When this method is called the following actions will happen: |
| 61 | + |
| 62 | +- a notice will be printed to the browser console; and |
| 63 | +- a modal will be displayed. |
| 64 | + |
| 65 | +This deprecation will cause failures when running Behat if any code path makes use of the functionality. |
| 66 | + |
| 67 | +#### Silent deprecation {/* #silent-deprecation */} |
| 68 | + |
| 69 | +In some cases it is necessary to deprecate a feature silently, emitting to the console but not display any modal. This may happen for deprecation of widely used features over a longer period where the initial deprecation is expected to take a long time. |
| 70 | + |
| 71 | +To do this, you can set the `emit: false` property on the deprecation call: |
| 72 | + |
| 73 | +```javascript title="Emitting in the Console only" |
| 74 | +import emitDeprecation from 'core/deprecated'; |
| 75 | + |
| 76 | +export const myFunction (the, args) => { |
| 77 | + emitDeprecation('myFunction', { |
| 78 | + replacement: 'myNewFunction', |
| 79 | + since: '5.2', |
| 80 | + mdl: 'MDL-12345', |
| 81 | + emit: false, |
| 82 | + }); |
| 83 | + |
| 84 | + const modified = args.slice(0, 1); |
| 85 | + args = args.slice(1); |
| 86 | + |
| 87 | + // Call myNewFunction. |
| 88 | + return myNewFunction(the, modified, args); |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +#### Final deprecation {/* #final-deprecation */} |
| 93 | + |
| 94 | +After the initial deprecation period, deprecations typically then become 'final'. This means that instead of a warning being issued and a backwards-compatible call being made, an Error is shown. |
| 95 | + |
| 96 | +In some cases deprecations may also be marked as final because there is no alternative. |
| 97 | + |
| 98 | +To mark a deprecation as final, you can set the `final: true` property on the deprecation call: |
| 99 | + |
| 100 | +```javascript title="Basic usage" |
| 101 | +import emitDeprecation from 'core/deprecated'; |
| 102 | + |
| 103 | +export const myFunction (the, args) => { |
| 104 | + emitDeprecation('myFunction', { |
| 105 | + replacement: 'myNewFunction', |
| 106 | + since: '5.2', |
| 107 | + mdl: 'MDL-12345', |
| 108 | + final: true, |
| 109 | + }); |
| 110 | + |
| 111 | + const modified = args.slice(0, 1); |
| 112 | + args = args.slice(1); |
| 113 | + |
| 114 | + // Call myNewFunction. |
| 115 | + return myNewFunction(the, modified, args); |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### Tips and tricks {/* #tips-and-tricks */} |
| 120 | + |
| 121 | +#### Hiding deprecations {/* #hiding-deprecations */} |
| 122 | + |
| 123 | +If you wish to stop this deprecation from displaying and from causing Behat failures, you can set the following in your `config.php`: |
| 124 | + |
| 125 | +```php title="Ignoring the deprecation of 'myFunction'" |
| 126 | +$CFG->jsdeprecationignorelist = [ |
| 127 | + 'myFunction', |
| 128 | +]; |
| 129 | +``` |
| 130 | + |
| 131 | +The name in this list should match the first argument to the `emitDeprecation` method. |
0 commit comments