Skip to content

Commit 291b7ea

Browse files
feat(babel): update babel to major version 8 (#6104)
This updates Babel to version 8, which was a long time in the works. If you're using the type-safety feature of the ignorer plugin, you will need to use a different approach. ```ts import { PluginKind, declareValuePlugin } from '@stryker-mutator/api/plugin'; import { babel } from '@stryker-mutator/core; export const strykerPlugins = [declareValuePlugin(PluginKind.Ignore, 'MyIgnorer', { shouldIgnore(path: babel.NodePath) { // Define the conditions for which you want to ignore mutants } })]; ``` See the [ignorer-plugin documentation](https://stryker-mutator.io/docs/stryker-js/disable-mutants/#using-an-ignore-plugin).
1 parent 6a8865d commit 291b7ea

60 files changed

Lines changed: 2221 additions & 1310 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/disable-mutants.md‎

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,16 @@ The comment always starts with `// Stryker`, followed by either `disable` or `re
182182

183183
## Using an ignore-plugin
184184

185-
_Available since Stryker 7.3_
185+
<details>
186+
187+
<summary>History</summary>
188+
189+
| Version | Changes |
190+
| ------- | ------------------------------------------------------------ |
191+
| 7.3 | Added the `'ignorer` plugin option |
192+
| 9.7 | Added the `type babel` to the export `@stryker-mutator/core` |
193+
194+
</details>
186195

187196
You might not be interested in testing specific code patterns in some projects. You can use `// Stryker disable` comments for these. However, this gets tedious quickly.
188197

@@ -213,23 +222,25 @@ Now add a "stryker-console-ignorer.js" to your project:
213222
// stryker-console-ignorer.js
214223
import { PluginKind, declareValuePlugin } from '@stryker-mutator/api/plugin';
215224

216-
export const strykerPlugins = [declareValuePlugin(PluginKind.Ignore, 'console.debug', {
217-
shouldIgnore(path) {
218-
// Define the conditions for which you want to ignore mutants
219-
if (
220-
path.isExpressionStatement() &&
221-
path.node.expression.type === 'CallExpression' &&
222-
path.node.expression.callee.type === 'MemberExpression' &&
223-
path.node.expression.callee.object.type === 'Identifier' &&
224-
path.node.expression.callee.object.name === 'console' &&
225-
path.node.expression.callee.property.type === 'Identifier' &&
226-
path.node.expression.callee.property.name === 'debug'
227-
) {
228-
// Return the ignore reason
229-
return "We're not interested in testing `console.debug` statements, see ADR 648.";
230-
}
231-
}
232-
})];
225+
export const strykerPlugins = [
226+
declareValuePlugin(PluginKind.Ignore, 'console.debug', {
227+
shouldIgnore(path) {
228+
// Define the conditions for which you want to ignore mutants
229+
if (
230+
path.isExpressionStatement() &&
231+
path.node.expression.type === 'CallExpression' &&
232+
path.node.expression.callee.type === 'MemberExpression' &&
233+
path.node.expression.callee.object.type === 'Identifier' &&
234+
path.node.expression.callee.object.name === 'console' &&
235+
path.node.expression.callee.property.type === 'Identifier' &&
236+
path.node.expression.callee.property.name === 'debug'
237+
) {
238+
// Return the ignore reason
239+
return "We're not interested in testing `console.debug` statements, see ADR 648.";
240+
}
241+
},
242+
}),
243+
];
233244
```
234245

235246
In the above example, you declare an ignore-plugin with the name `'console.debug'` and an `Ignorer`. An `Ignorer` is an object with a `shouldIgnore(path)` method. Stryker will execute this method on each node of the abstract syntax tree (AST). To ignore mutants in the current node and child nodes, return a non-empty ignore reason as a string here. The `path` parameter is a babel `NodePath` object, [see 'visiting' in the babel handbook](https://github.com/jamiebuilds/babel-handbook/blob/master/translations/en/plugin-handbook.md#user-content-visiting) for more information on the `NodePath` API.
@@ -249,17 +260,44 @@ After rerunning Stryker, your report will look like this.
249260

250261
:::tip
251262

252-
If you want TypeScript type-safety on the `path` being passed into your ignore-plugin, you will need to install the babel types yourself: `npm i -D @types/babel__core` and add this TypeScript file somewhere in your project:
253-
254-
```ts
255-
/// <reference types="@stryker-mutator/api/ignore" />
256-
import type babel from '@babel/core';
257-
258-
declare module '@stryker-mutator/api/ignore' {
259-
export interface NodePath extends babel.NodePath {}
260-
}
261-
```
262-
263-
If you want to write the plugin itself as TypeScript as well, you will need to transpile it to JavaScript yourself. Either by doing it before you run Stryker, or using a just-in-time compiler like `tsx`.
263+
If you want TypeScript to type-check the `path` passed to your ignore plugin, use `NodePath` from `@stryker-mutator/core`.
264+
265+
1. Rename your plugin to `.ts`.
266+
- Don't forget to also update the `"plugins"` in your Stryker config file.
267+
```diff
268+
{
269+
"ignorers": ["console.debug"],
270+
- "plugins": ["@stryker-mutator/*", "./stryker-console-ignorer.js"]
271+
+ "plugins": ["@stryker-mutator/*", "./stryker-console-ignorer.ts"]
272+
}
273+
```
274+
2. Import type types and use them:
275+
276+
```diff
277+
// stryker-console-ignorer.ts
278+
import { PluginKind, declareValuePlugin } from '@stryker-mutator/api/plugin';
279+
+ import type { babel } from '@stryker-mutator/core';
280+
281+
export const strykerPlugins = [
282+
declareValuePlugin(PluginKind.Ignore, 'console.debug', {
283+
- shouldIgnore(path) {
284+
+ shouldIgnore(path: babel.NodePath) {
285+
// Define the conditions for which you want to ignore mutants
286+
if (
287+
path.isExpressionStatement() &&
288+
path.node.expression.type === 'CallExpression' &&
289+
path.node.expression.callee.type === 'MemberExpression' &&
290+
path.node.expression.callee.object.type === 'Identifier' &&
291+
path.node.expression.callee.object.name === 'console' &&
292+
path.node.expression.callee.property.type === 'Identifier' &&
293+
path.node.expression.callee.property.name === 'debug'
294+
) {
295+
// Return the ignore reason
296+
return "We're not interested in testing `console.debug` statements, see ADR 648.";
297+
}
298+
},
299+
}),
300+
];
301+
```
264302

265303
:::

‎e2e/package.json‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
"type": "module",
66
"packageManager": "pnpm@10.34.4",
77
"devDependencies": {
8-
"@babel/cli": "7.29.7",
9-
"@babel/core": "7.29.7",
8+
"@babel/cli": "8.0.1",
9+
"@babel/core": "8.0.1",
1010
"@babel/plugin-proposal-class-properties": "7.18.6",
11-
"@babel/plugin-proposal-pipeline-operator": "7.29.7",
12-
"@babel/preset-env": "7.29.7",
13-
"@babel/preset-flow": "7.29.7",
11+
"@babel/plugin-proposal-pipeline-operator": "8.0.1",
12+
"@babel/preset-env": "8.0.2",
13+
"@babel/preset-flow": "8.0.1",
1414
"@cucumber/cucumber": "12.9.0",
1515
"@stryker-mutator/api": "workspace:*",
1616
"@stryker-mutator/core": "workspace:*",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"presets": ["@babel/env"],
3-
"plugins": [["@babel/plugin-proposal-pipeline-operator", { "proposal": "minimal" }]]
3+
"plugins": [["@babel/plugin-proposal-pipeline-operator", { "proposal": "fsharp" }]]
44
}

‎e2e/test/babel-transpiling/src/Casino.js‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
// We use a require statement to see if stryker will work if users use require statements
2-
var Bank = require('./Bank').default;
1+
import Bank from './Bank.js';
32

43
// Use a pipeline operator to test the `mutator.plugins` option
5-
function capitalize (str) {
4+
function capitalize(str) {
65
return str[0].toUpperCase() + str.substring(1);
76
}
8-
let result = "hello"
9-
|> capitalize;
7+
let result = 'hello' |> capitalize;
108

119
export default class Casino extends Bank {
1210
constructor(chips, money) {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Bank from './Bank';
1+
import Bank from './Bank.js';
22

33
export default class User extends Bank {
44
constructor(name, age, money) {
@@ -7,4 +7,4 @@ export default class User extends Bank {
77
this.age = age;
88
this.chips = 0;
99
}
10-
}
10+
}

‎e2e/test/babel-transpiling/test/BankSpec.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'assert';
2-
import Bank from '../src/Bank';
2+
import Bank from '../src/Bank.js';
33

44
describe('Bank', () => {
55
it('should set money', () => {
@@ -51,4 +51,4 @@ describe('Bank', () => {
5151
assert.equal(bank.money, 500);
5252
});
5353
});
54-
});
54+
});

‎e2e/test/babel-transpiling/test/CasinoSpec.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from 'assert';
2-
import Casino from '../src/Casino';
3-
import User from '../src/User';
2+
import Casino from '../src/Casino.js';
3+
import User from '../src/User.js';
44

55
describe('Casino', () => {
66
describe('user over 18', () => {
@@ -25,4 +25,4 @@ describe('Casino', () => {
2525
assert.equal(user.chips, chipsToBuy);
2626
});
2727
});
28-
});
28+
});

e2e/test/ignore-project/stryker-plugins/ignorers/console-ignorer.js renamed to e2e/test/ignore-project/stryker-plugins/ignorers/console-ignorer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// @ts-check
1+
import type { babel } from '@stryker-mutator/core';
22
import { PluginKind, declareValuePlugin } from '@stryker-mutator/api/plugin';
33

44
export const strykerPlugins = [
55
declareValuePlugin(PluginKind.Ignore, 'ConsoleIgnorer', {
6-
shouldIgnore(path) {
6+
shouldIgnore(path: babel.NodePath) {
77
if (
88
path.isExpressionStatement() &&
99
path.node.expression.type === 'CallExpression' &&

‎e2e/test/ignore-project/stryker-plugins/ignorers/global.types.ts‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎e2e/test/ignore-project/stryker.conf.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"ignorers": ["ConsoleIgnorer"],
1616
"plugins": [
1717
"@stryker-mutator/mocha-runner",
18-
"./stryker-plugins/ignorers/console-ignorer.js"
18+
"./stryker-plugins/ignorers/console-ignorer.ts"
1919
],
2020
"allowConsoleColors": false
2121
}

0 commit comments

Comments
 (0)