Skip to content

Commit 193c99f

Browse files
committed
Fix various codemods + tests + docs
1 parent 0c2aea0 commit 193c99f

File tree

42 files changed

+1081
-632
lines changed

Some content is hidden

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

42 files changed

+1081
-632
lines changed

community/javascript/codeshift.config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ module.exports = {
44
description: 'Codemods for javascript',
55
transforms: {},
66
presets: {
7-
'import-from-root': require.resolve('./import-from-root/transform'),
7+
'remove-console-log': require.resolve('./remove-console-log/transform'),
88
'remove-debugger': require.resolve('./remove-debugger/transform'),
99
'sort-object-props': require.resolve('./sort-object-props/transform'),
10-
'use-named-exports': require.resolve('./use-named-exports/transform'),
11-
'use-named-imports': require.resolve('./use-named-imports/transform'),
1210
'var-to-let': require.resolve('./var-to-let/transform'),
1311
},
1412
};

community/javascript/import-from-root/README.md

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

community/javascript/import-from-root/transform.spec.ts

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

community/javascript/import-from-root/transform.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# remove-console-log
2+
3+
Removes all `console.log` statements.
4+
5+
```js
6+
/* INPUT */
7+
console.log('hello world');
8+
foo('bar');
9+
10+
/* OUTPUT */
11+
foo('bar');
12+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { applyTransform } from '@codeshift/test-utils';
2+
import * as transformer from './transform';
3+
4+
describe('javascript#remove-console-log transform', () => {
5+
it('should not modify files with no console.log statements', async () => {
6+
const result = await applyTransform(transformer, `foo();`, {
7+
parser: 'tsx',
8+
});
9+
10+
expect(result).toMatchInlineSnapshot(`"foo();"`);
11+
});
12+
13+
it('should remove top-level console.log statement', async () => {
14+
const result = await applyTransform(transformer, 'console.log("fff");', {
15+
parser: 'tsx',
16+
});
17+
18+
expect(result).toMatchInlineSnapshot(`""`);
19+
});
20+
21+
it('should console.logs in function statements', async () => {
22+
const result = await applyTransform(
23+
transformer,
24+
`
25+
function foo () {
26+
setTimeout(() => { debugger; console.log('foo') });
27+
}
28+
`,
29+
{ parser: 'tsx' },
30+
);
31+
32+
expect(result).toMatchInlineSnapshot(`
33+
"function foo () {
34+
setTimeout(() => {
35+
debugger;
36+
});
37+
}"
38+
`);
39+
});
40+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { API, FileInfo, Options } from 'jscodeshift';
2+
3+
export default function transformer(
4+
file: FileInfo,
5+
{ jscodeshift: j }: API,
6+
options: Options,
7+
) {
8+
const source = j(file.source);
9+
const consoleLogStatements = source.find(j.ExpressionStatement, {
10+
expression: {
11+
type: 'CallExpression',
12+
callee: {
13+
type: 'MemberExpression',
14+
object: {
15+
type: 'Identifier',
16+
name: 'console',
17+
},
18+
property: {
19+
type: 'Identifier',
20+
name: 'log',
21+
},
22+
},
23+
},
24+
});
25+
26+
if (consoleLogStatements.length === 0) {
27+
return file.source;
28+
}
29+
30+
consoleLogStatements.remove();
31+
32+
return source.toSource(options.printOptions);
33+
}

community/javascript/remove-debugger/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Removes all `debugger` statements.
44

5+
_Credit_: [https://github.com/JamieMason/codemods](https://github.com/JamieMason/codemods)
6+
57
```js
68
/* INPUT */
79
console.log('hello world');
Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
import { applyTransform } from '@codeshift/test-utils';
22
import * as transformer from './transform';
33

4-
describe('react@1.0.0 transform', () => {
5-
it('should not modify files with no debugger statements', () => {
6-
const result = applyTransform(transformer, `console.log('foooo');`, {
4+
describe('javascript#remove-debugger transform', () => {
5+
it('should not modify files with no debugger statements', async () => {
6+
const result = await applyTransform(transformer, `console.log('foooo');`, {
77
parser: 'tsx',
88
});
99

10-
expect(result).toMatchInlineSnapshot();
10+
expect(result).toMatchInlineSnapshot(`"console.log('foooo');"`);
1111
});
1212

13-
it('should remove top-level debugger statement', () => {
14-
const result = applyTransform(transformer, 'debugger;', { parser: 'tsx' });
13+
it('should remove top-level debugger statement', async () => {
14+
const result = await applyTransform(transformer, 'debugger;', {
15+
parser: 'tsx',
16+
});
1517

16-
expect(result).toMatchInlineSnapshot();
18+
expect(result).toMatchInlineSnapshot(`""`);
1719
});
1820

19-
it('should debuggers in function statements', () => {
20-
const result = applyTransform(
21+
it('should debuggers in function statements', async () => {
22+
const result = await applyTransform(
2123
transformer,
2224
`
23-
function foo () {
24-
debugger;
25-
setTimeout(() => { debugger; console.log('foo') });debugger;
26-
}
25+
function foo () {
26+
debugger;
27+
setTimeout(() => { debugger; console.log('foo') });debugger;
28+
}
2729
`,
2830
{ parser: 'tsx' },
2931
);
3032

31-
expect(result).toMatchInlineSnapshot();
33+
expect(result).toMatchInlineSnapshot(`
34+
"function foo () {
35+
setTimeout(() => {
36+
console.log('foo')
37+
});
38+
}"
39+
`);
3240
});
3341
});

community/javascript/sort-object-props/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Sort members of Object Literals alphabetically.
44

5+
_Credit_: [https://github.com/JamieMason/codemods](https://github.com/JamieMason/codemods)
6+
57
```js
68
/* INPUT */
79
const players = { messi: true, bergkamp: true, ginola: true };

0 commit comments

Comments
 (0)