Safety Analysis

The P42 Safety Analysis checks if refactorings can change the code's behavior (within limitations). The P42 context and quick-fix menu items often contain safety indicators:

  • No indicator: P42 has not evaluated the safety of the refactoring or code action. No indicator is the default for code actions such as adding console.log statements that intentionally change your program.
  • Safe ✅: The refactoring will not change the behavior of your code.
  • Information ℹ️: The refactoring might detrimentally affect comments or readability.
  • Warning ⚠️: The refactoring might change the behavior of your code. The text will include short hints at what you need to check to proceed safely. The documentation page for the code action might contain more details.
  • Error ⛔: The refactoring will change the behavior of your code and can introduce defects. You might still want to proceed, particularly if you want to change the behavior on purpose.

Limitations

When you are using TypeScript, the safety analysis relies on the accuracy of your type annotations. If your type annotations are not accurate, some refactorings deemed safe by P42 might lead to the introduction of defects.

The safety analysis analyzes single files without a larger type context. Some indirect types resolved by TypeScript in your program might be considered any or unknown. As a result, P42 might present warnings that are not relevant if you have the complete type information.

P42 identifies whether a file is run in an isolated context (i.e., var and function do not change the global scope) based on the file extension or the use of import or require. Files with a .cjs extension or a top-level require('someLibrary) or module.exports are assumed to run as Node.js CommonJS modules. Files with a .mjs extension or import or export keywords are assumed to run as ECMAScript modules. TypeScript files are assumed to be transpiled into isolated contexts. For other JavaScript files, P42 assumes that they may run in the browser, and var could affect the global scope.

For code modernizations, the safety analysis also relies on the correct behavior of the standard JavaScript functions. If there are defects in the polyfills that you are using (for polyfilled functions), or if they implement a different behavior than the ECMAScript specification, modernizing the code can introduce defects. The safety analysis also assumes that you have set up the required transpilation if you need to support older browsers.

The safety analysis does not cover runtime performance, which can be vital if you work on hotspot areas of your application. Refactorings might negatively impact the application performance in some cases.

The safety analysis does not cover comments. P42 tries to retain comment associations as much as possible, but there can be cases where it is unclear which source code belongs to which comment.

Example

Example Safety analysis: Array.includes() refactoring