@instructure/ui-codemods 8.17.1-snapshot.30 → 8.17.1-snapshot.71

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,111 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { findAttribute, findElements } from '../helpers/v7PropsUpdateHelpers'
26
+ import { Collection, JSCodeshift, Literal } from 'jscodeshift'
27
+
28
+ /**
29
+ * Does the following updates on a <Button>:
30
+ * - `variant="default"` -> removed
31
+ * - `variant="primary"` -> `color="primary"`
32
+ * - `variant="success"` -> `color="success"`
33
+ * - `variant="danger"` -> `color="danger"`
34
+ * - `variant="light"` -> `color="primary-inverse"`
35
+ * - `variant="ghost"` -> `color="ghost" withBackground={false}`
36
+ * - `variant="ghost-inverse"` -> `color="primary-inverse" withBackground={false}`
37
+ */
38
+ export default function updateV7ButtonsWithText(
39
+ j: JSCodeshift,
40
+ root: Collection,
41
+ importedName: string,
42
+ filePath: string
43
+ ) {
44
+ const buttons = findElements(filePath, j, root, importedName)
45
+
46
+ // remove variant="default"
47
+ findAttribute(filePath, j, buttons, 'variant', 'default').remove()
48
+ // replace <Button variant="primary" with <Button color="primary"
49
+ findAttribute(filePath, j, buttons, 'variant', 'primary').replaceWith(
50
+ (nodePath) => {
51
+ const { node } = nodePath
52
+ node.name.name = 'color'
53
+ return nodePath.node
54
+ }
55
+ )
56
+ // replace <Button variant="success" with <Button color="success"
57
+ findAttribute(filePath, j, buttons, 'variant', 'success').replaceWith(
58
+ (nodePath) => {
59
+ const { node } = nodePath
60
+ node.name.name = 'color'
61
+ return nodePath.node
62
+ }
63
+ )
64
+ // replace <Button variant="danger" with <Button color="danger"
65
+ findAttribute(filePath, j, buttons, 'variant', 'danger').replaceWith(
66
+ (nodePath) => {
67
+ const { node } = nodePath
68
+ node.name.name = 'color'
69
+ return nodePath.node
70
+ }
71
+ )
72
+ // replace <Button variant="light" with color="primary-inverse"
73
+ findAttribute(filePath, j, buttons, 'variant', 'light').replaceWith(
74
+ (nodePath) => {
75
+ const { node } = nodePath
76
+ node.name.name = 'color'
77
+ ;(node.value as Literal).value = 'primary-inverse'
78
+ return nodePath.node
79
+ }
80
+ )
81
+ // replace <Button variant="ghost" with <Button color="primary" withBackground={false}
82
+ findAttribute(filePath, j, buttons, 'variant', 'ghost')
83
+ .replaceWith((nodePath) => {
84
+ const { node } = nodePath
85
+ node.name.name = 'color'
86
+ ;(node.value as Literal).value = 'primary'
87
+ return nodePath.node
88
+ })
89
+ .insertAfter(
90
+ j.jsxAttribute(
91
+ j.jsxIdentifier('withBackground'),
92
+ j.jsxExpressionContainer(j.jsxIdentifier('false'))
93
+ )
94
+ )
95
+
96
+ // replace <Button variant="ghost-inverse" with
97
+ // <Button color="primary-inverse" withBackground={false}
98
+ findAttribute(filePath, j, buttons, 'variant', 'ghost-inverse')
99
+ .replaceWith((nodePath) => {
100
+ const { node } = nodePath
101
+ node.name.name = 'color'
102
+ ;(node.value as Literal).value = 'primary-inverse'
103
+ return nodePath.node
104
+ })
105
+ .insertAfter(
106
+ j.jsxAttribute(
107
+ j.jsxIdentifier('withBackground'),
108
+ j.jsxExpressionContainer(j.jsxIdentifier('false'))
109
+ )
110
+ )
111
+ }
@@ -0,0 +1,105 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import type { Collection, JSCodeshift } from 'jscodeshift'
26
+ import {
27
+ addImportIfNeeded,
28
+ findAttribute,
29
+ findElements,
30
+ findImport,
31
+ printWarning,
32
+ renameElements
33
+ } from '../helpers/v7PropsUpdateHelpers'
34
+ import { Literal } from 'jscodeshift'
35
+
36
+ /**
37
+ * Does the following updates on a <FocusableView>:
38
+ * - `<FocusableView/>` -> `<View/>`
39
+ * - focused -> withFocusOutline
40
+ * - shape -> delete and display warning, that focus ring shape cannot be set
41
+ * - color="primary" -> delete, its the default
42
+ * - color="error" -> focusColor="danger"
43
+ * - color="inverse" -> focusColor="inverse"
44
+ *
45
+ **/
46
+ export default function updateV7FocusableView(
47
+ j: JSCodeshift,
48
+ root: Collection,
49
+ filePath: string
50
+ ) {
51
+ const importName = findImport(j, root, 'FocusableView', [
52
+ '@instructure/ui-focusable',
53
+ '@instructure/ui'
54
+ ])
55
+ if (!importName) {
56
+ return false
57
+ }
58
+ const views = findElements(filePath, j, root, importName)
59
+
60
+ ///// `<FocusableView/>` -> `<View/>`
61
+ renameElements(filePath, views, 'FocusableView', 'View')
62
+ addImportIfNeeded(j, root, 'View', '@instructure/ui-view')
63
+
64
+ ///// focused -> withFocusOutline
65
+ findAttribute(filePath, j, views, 'focused').replaceWith((nodePath) => {
66
+ const { node } = nodePath
67
+ node.name.name = 'withFocusOutline'
68
+ return nodePath.node
69
+ })
70
+
71
+ ///// shape -> delete and display warning, that focus ring shape cannot be set
72
+ findAttribute(filePath, j, views, 'shape')
73
+ .forEach((path) => {
74
+ printWarning(
75
+ filePath,
76
+ path.value.loc?.start.line,
77
+ `The 'shape' property was removed, focus ring shape
78
+ cannot be set in InstUI v8`
79
+ )
80
+ })
81
+ .remove()
82
+
83
+ //// color="primary" -> delete, its the default
84
+ findAttribute(filePath, j, views, 'color', 'primary').remove()
85
+
86
+ //// color="error" -> focusColor="danger"
87
+ findAttribute(filePath, j, views, 'color', 'error').replaceWith(
88
+ (nodePath) => {
89
+ const { node } = nodePath
90
+ node.name.name = 'focusColor'
91
+ ;(node.value as Literal).value = 'danger'
92
+ return nodePath.node
93
+ }
94
+ )
95
+
96
+ ///// color="inverse" -> focusColor="inverse"
97
+ findAttribute(filePath, j, views, 'color', 'inverse').replaceWith(
98
+ (nodePath) => {
99
+ const { node } = nodePath
100
+ node.name.name = 'focusColor'
101
+ return nodePath.node
102
+ }
103
+ )
104
+ return true
105
+ }
@@ -0,0 +1,145 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import type {
26
+ Collection,
27
+ JSCodeshift,
28
+ JSXAttribute,
29
+ JSXElement
30
+ } from 'jscodeshift'
31
+ import {
32
+ addImportIfNeeded,
33
+ findElements,
34
+ findImport,
35
+ isJSXAttribue,
36
+ isJSXExpressionContainer,
37
+ isLiteral,
38
+ printWarning
39
+ } from '../helpers/v7PropsUpdateHelpers'
40
+
41
+ /**
42
+ * Does the following update on a <Heading>:
43
+ * `<Heading ellipsis>abc</Heading>` ->
44
+ * `<Heading><TruncateText>abc</TruncateText></Heading>
45
+ */
46
+ export default function updateV7Heading(
47
+ j: JSCodeshift,
48
+ root: Collection,
49
+ filePath: string
50
+ ) {
51
+ const importName = findImport(j, root, 'Heading', [
52
+ '@instructure/ui-heading',
53
+ '@instructure/ui'
54
+ ])
55
+ if (!importName) {
56
+ return false
57
+ }
58
+ const tags = findElements(filePath, j, root, importName, {
59
+ name: 'ellipsis'
60
+ })
61
+
62
+ tags.forEach((path) => {
63
+ if (path.value.openingElement.attributes) {
64
+ for (const attr of path.value.openingElement.attributes) {
65
+ if (isJSXAttribue(attr) && attr.name.name === 'ellipsis') {
66
+ if (!attr.value) {
67
+ // <Heading ellipsis>abc</Heading>
68
+ wrapInTruncateText(j, root, attr, path.value)
69
+ } else if (isJSXExpressionContainer(attr.value)) {
70
+ // <Heading ellipsis={expr}>abc</Heading>
71
+ if (isLiteral(attr.value.expression)) {
72
+ if (typeof attr.value.expression.value === 'boolean') {
73
+ if (attr.value.expression.value === true) {
74
+ // <Heading ellipsis={true}>abc</Heading>
75
+ wrapInTruncateText(j, root, attr, path.value)
76
+ } else {
77
+ // <Heading ellipsis={false}>abc</Heading>
78
+ // remove ellipsis attribute
79
+ path.value.openingElement.attributes.splice(
80
+ path.value.openingElement.attributes.indexOf(attr),
81
+ 1
82
+ )
83
+ return
84
+ }
85
+ } else {
86
+ printWarning(
87
+ filePath,
88
+ path.value.loc?.start.line,
89
+ 'Heading ellipsis ' +
90
+ 'parameter has non-boolean value, please refactor manually'
91
+ )
92
+ return
93
+ }
94
+ } else {
95
+ printWarning(
96
+ filePath,
97
+ path.value.loc?.start.line,
98
+ 'Heading ellipsis ' +
99
+ 'parameter has non-boolean value, please refactor manually'
100
+ )
101
+ return
102
+ }
103
+ } else {
104
+ printWarning(
105
+ filePath,
106
+ path.value.loc?.start.line,
107
+ 'Heading ellipsis ' +
108
+ 'parameter has non-boolean value, please refactor manually'
109
+ )
110
+ return
111
+ }
112
+ }
113
+ }
114
+ }
115
+ })
116
+ if (tags) {
117
+ return true
118
+ }
119
+ return false
120
+ }
121
+
122
+ function wrapInTruncateText(
123
+ j: JSCodeshift,
124
+ root: Collection,
125
+ attr: JSXAttribute,
126
+ elem: JSXElement
127
+ ) {
128
+ elem.openingElement.attributes!.splice(
129
+ elem.openingElement.attributes!.indexOf(attr),
130
+ 1
131
+ )
132
+ addImportIfNeeded(j, root, 'TruncateText', [
133
+ '@instructure/ui-truncate-text',
134
+ '@instructure/ui'
135
+ ])
136
+ // move children under a new TruncateText
137
+ const children = elem.children
138
+ const tt = j.jsxElement(
139
+ j.jsxOpeningElement(j.jsxIdentifier('TruncateText')),
140
+ j.jsxClosingElement(j.jsxIdentifier('TruncateText')),
141
+ children
142
+ )
143
+ // eslint-disable-next-line no-param-reassign
144
+ elem.children = [tt]
145
+ }
@@ -0,0 +1,113 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { Collection, JSCodeshift } from 'jscodeshift'
26
+ import {
27
+ addImportIfNeeded,
28
+ findAttribute,
29
+ findElements,
30
+ findImport,
31
+ printWarning,
32
+ renameElements
33
+ } from '../helpers/v7PropsUpdateHelpers'
34
+
35
+ /**
36
+ * Does the following update on a <List>:
37
+ * - `<List variant="default"` -> `<List`
38
+ * - `<List variant="unstyled"` -> `<List isUnstyled={true}`
39
+ * - `<List variant="inline"` -> `<InlineList`
40
+ * - `<List delimiter=` -> display warning
41
+ * - `<List.Item delimiter=` -> display warning
42
+ */
43
+ export default function updateV7Lists(
44
+ j: JSCodeshift,
45
+ root: Collection,
46
+ filePath: string
47
+ ) {
48
+ const importName = findImport(j, root, 'List', [
49
+ '@instructure/ui-lists',
50
+ '@instructure/ui'
51
+ ])
52
+ if (!importName) {
53
+ return false
54
+ }
55
+ const tags = findElements(filePath, j, root, importName, {
56
+ name: 'variant'
57
+ })
58
+
59
+ ///// `<List variant="default"` -> `<List`
60
+ findAttribute(filePath, j, tags, 'variant', 'default').remove()
61
+
62
+ ///// `<List variant="unstyled"` -> `<List isUnstyled`
63
+ findAttribute(filePath, j, tags, 'variant', 'unstyled')
64
+ .remove()
65
+ .insertAfter(j.jsxAttribute(j.jsxIdentifier('isUnstyled')))
66
+
67
+ ///// `<List variant="inline"` -> `<InlineList`
68
+ const inlineLists = findElements(filePath, j, root, 'List', {
69
+ name: 'variant',
70
+ value: 'inline'
71
+ })
72
+ if (inlineLists.length > 0) {
73
+ findAttribute(filePath, j, inlineLists, 'variant').remove()
74
+ renameElements(filePath, inlineLists, 'List', 'InlineList')
75
+ inlineLists.forEach((path) => {
76
+ if (path.value.children) {
77
+ // rename List.Item to InlineList.Item
78
+ renameElements(
79
+ filePath,
80
+ path.value.children,
81
+ 'List.Item',
82
+ 'InlineList.Item'
83
+ )
84
+ }
85
+ })
86
+ addImportIfNeeded(j, root, 'InlineList', [
87
+ '@instructure/ui',
88
+ '@instructure/ui-lists'
89
+ ])
90
+ }
91
+
92
+ ///// `<List delimiter=` -> display warning
93
+ ///// `<List.Item delimiter=` -> display warning
94
+ findElements(filePath, j, root, importName, { name: 'delimiter' }).forEach(
95
+ (path) => {
96
+ printWarning(
97
+ filePath,
98
+ path.value.loc?.start.line,
99
+ `List's delimiter prop is only available for 'InlineList' in InstUI v8, please check.`
100
+ )
101
+ }
102
+ )
103
+ findElements(filePath, j, root, 'List.Item', { name: 'delimiter' }).forEach(
104
+ (path) => {
105
+ printWarning(
106
+ filePath,
107
+ path.value.loc?.start.line,
108
+ `List's delimiter prop is only available for 'InlineList' in InstUI v8, please check.`
109
+ )
110
+ }
111
+ )
112
+ return tags.length > 0
113
+ }
@@ -0,0 +1,83 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { Collection, JSCodeshift } from 'jscodeshift'
26
+ import {
27
+ findElements,
28
+ findImport,
29
+ isJSXAttribue,
30
+ printWarning
31
+ } from '../helpers/v7PropsUpdateHelpers'
32
+
33
+ /**
34
+ * Does the following updates on a <Pill>:
35
+ * - `<Pill text="abc"/>` -> `<Pill>abc</Pill>`
36
+ **/
37
+ export default function updateV7Pill(
38
+ j: JSCodeshift,
39
+ root: Collection,
40
+ filePath: string
41
+ ) {
42
+ const importName = findImport(j, root, 'Pill', [
43
+ '@instructure/ui-pill',
44
+ '@instructure/ui'
45
+ ])
46
+ if (!importName) {
47
+ return false
48
+ }
49
+ ///// `<Pill text="abc"/>` -> `<Pill>abc</Pill>`
50
+ let isUpdated = false
51
+ findElements(filePath, j, root, importName, { name: 'text' }).forEach(
52
+ (path) => {
53
+ const tag = path.value
54
+ for (const attr of tag.openingElement.attributes!) {
55
+ if (isJSXAttribue(attr) && attr.name.name === 'text') {
56
+ if (tag.children && tag.children.length !== 0) {
57
+ printWarning(
58
+ filePath,
59
+ tag.loc?.start.line,
60
+ "Pill had both `text` attribute and children. I've " +
61
+ 'moved the value of `text` under its `children`, please double ' +
62
+ 'check.'
63
+ )
64
+ }
65
+ if (tag.openingElement.selfClosing) {
66
+ tag.openingElement.selfClosing = false
67
+ tag.closingElement = j.jsxClosingElement(j.jsxIdentifier('Pill'))
68
+ }
69
+ if (!tag.children) {
70
+ tag.children = []
71
+ }
72
+ tag.children!.push(attr.value!)
73
+ tag.openingElement.attributes!.splice(
74
+ tag.openingElement.attributes!.indexOf(attr),
75
+ 1
76
+ )
77
+ isUpdated = true
78
+ }
79
+ }
80
+ }
81
+ )
82
+ return isUpdated
83
+ }
@@ -0,0 +1,137 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import type {
26
+ Collection,
27
+ JSCodeshift,
28
+ JSXElement,
29
+ JSXExpressionContainer,
30
+ StringLiteral
31
+ } from 'jscodeshift'
32
+ import {
33
+ findElements,
34
+ findImport,
35
+ getVisibleChildren,
36
+ isJSXElement,
37
+ isJSXIdentifier,
38
+ isJSXMemberExpression,
39
+ isJSXText,
40
+ printWarning
41
+ } from '../helpers/v7PropsUpdateHelpers'
42
+
43
+ /**
44
+ * Does the following updates on a <Popover>:
45
+ * - `<Popover><Popover.Trigger>{t}</Popover.Trigger></Popover>` ->
46
+ * `<Popover renderTrigger={t} />`
47
+ * - `<Popover><Popover.Content>c</Popover.Content></Popover>` ->
48
+ * `<Popover>c</Popover>`
49
+ * - `<Popover onToggle=` -> Display warning that it needs to be done
50
+ * manually
51
+ **/
52
+ export default function updateV7Popover(
53
+ j: JSCodeshift,
54
+ root: Collection,
55
+ filePath: string
56
+ ) {
57
+ const importName = findImport(j, root, 'Popover', [
58
+ '@instructure/ui-popover',
59
+ '@instructure/ui'
60
+ ])
61
+ if (!importName) {
62
+ return false
63
+ }
64
+ let isUpdated = false
65
+ ///// `<Popover><Popover.Trigger>{t}</Popover.Trigger></Popover>` ->
66
+ ///// `<Popover renderTrigger={t} />`
67
+ findElements(filePath, j, root, 'Popover').forEach((path) => {
68
+ const trigger = getVisibleChildren(
69
+ getChildrenByName('Popover', 'Trigger', path.value.children)
70
+ )
71
+ if (trigger.length > 0) {
72
+ // should have 0 or 1 child
73
+ isUpdated = true
74
+ const theChild = trigger[0]
75
+ let toAdd = theChild as JSXExpressionContainer | StringLiteral
76
+ if (isJSXText(theChild)) {
77
+ toAdd = j.stringLiteral(theChild.value)
78
+ }
79
+ if (isJSXElement(theChild)) {
80
+ toAdd = j.jsxExpressionContainer(theChild)
81
+ } // hope these conversions are enough
82
+ path.value.openingElement.attributes!.push(
83
+ j.jsxAttribute(j.jsxIdentifier('renderTrigger'), toAdd)
84
+ )
85
+ }
86
+ })
87
+ ///// `<Popover><Popover.Content>c</Popover.Content></Popover>` ->
88
+ ///// `<Popover>c</Popover>`
89
+ findElements(filePath, j, root, 'Popover').forEach((path) => {
90
+ const trigger = getChildrenByName('Popover', 'Content', path.value.children)
91
+ if (trigger) {
92
+ // should have 0 or 1 child
93
+ isUpdated = true
94
+ path.value.children!.push(...trigger)
95
+ }
96
+ })
97
+ ///// `<Popover onToggle=` -> Display warning that it needs to be done manually
98
+ findElements(filePath, j, root, 'Popover', { name: 'onToggle' }).forEach(
99
+ (path) => {
100
+ printWarning(
101
+ filePath,
102
+ path.value.loc?.start.line,
103
+ "Popover's onToggle needs to be converted manually " +
104
+ 'to onShowContent and onHideContent'
105
+ )
106
+ }
107
+ )
108
+ return isUpdated
109
+ }
110
+
111
+ /**
112
+ * Removes the child of the given array that is named like `Name1.Name2`
113
+ * It returns the first child of the child that has this name (if any)
114
+ */
115
+ function getChildrenByName(
116
+ name1: string,
117
+ name2: string,
118
+ array?: JSXElement['children']
119
+ ) {
120
+ if (!array) {
121
+ return
122
+ }
123
+ for (const child of array) {
124
+ if (
125
+ isJSXElement(child) &&
126
+ child.children &&
127
+ isJSXMemberExpression(child.openingElement.name) &&
128
+ isJSXIdentifier(child.openingElement.name.object) &&
129
+ child.openingElement.name.object.name === name1 &&
130
+ child.openingElement.name.property.name === name2
131
+ ) {
132
+ array.splice(array.indexOf(child), 1)
133
+ return child.children
134
+ }
135
+ }
136
+ return
137
+ }