-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
preset-18.js
74 lines (68 loc) · 2.43 KB
/
preset-18.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const contextAnyTransform = require("./context-any");
const deprecatedReactTypeTransform = require("./deprecated-react-type");
const deprecatedSFCTransform = require("./deprecated-sfc");
const deprecatedSFCElementTransform = require("./deprecated-sfc-element");
const deprecatedStatelessComponentTransform = require("./deprecated-stateless-component");
const implicitChildrenTransform = require("./implicit-children");
const implicitAnyTransform = require("./useCallback-implicit-any");
/**
* @type {import('jscodeshift').Transform}
*
* Summary for Klarna's klapp@19fc4dafed84670398644298bf19c8c2a781dcf8/clients
* 28172 Files unmodified
* 11 skipped
* 5 Ok
* 1 false-positive due to `this.props.context` access
* 2 false-positive due to `path['context']` access
* 1 false-positive due to `constructor(props, context)`
*/
const transform = (file, api, options) => {
const { preset18Transforms } = options;
const transformNames = new Set(preset18Transforms.split(","));
/**
* @type {import('jscodeshift').Transform[]}
*/
const transforms = [];
if (transformNames.has("context-any")) {
transforms.push(contextAnyTransform);
}
if (transformNames.has("deprecated-react-type")) {
transforms.push(deprecatedReactTypeTransform);
}
if (transformNames.has("deprecated-sfc")) {
transforms.push(deprecatedSFCTransform);
}
if (transformNames.has("deprecated-sfc-element")) {
transforms.push(deprecatedSFCElementTransform);
}
if (transformNames.has("deprecated-stateless-component")) {
transforms.push(deprecatedStatelessComponentTransform);
}
if (transformNames.has("implicit-children")) {
transforms.push(implicitChildrenTransform);
}
if (transformNames.has("useCallback-implicit-any")) {
transforms.push(implicitAnyTransform);
}
let wasAlwaysSkipped = true;
const newSource = transforms.reduce((currentFileSource, transform) => {
// TODO: currently we parse -> transform -> print on every transform
// Instead, we could parse and prince once in the preset and the transformers just deal with an AST.
// That requires refactoring of every transform into source-transformer and ast-transformer
const transformResult = transform(
{ path: file.path, source: currentFileSource },
api,
options,
);
if (transformResult == null) {
return currentFileSource;
} else {
wasAlwaysSkipped = false;
return transformResult;
}
}, file.source);
if (!wasAlwaysSkipped) {
return newSource;
}
};
module.exports = transform;