Skip to content

Commit 1fcdc4f

Browse files
author
Kent C. Dodds
committed
fix: update all deps and fix cosmiconfig usage
1 parent e7ea8eb commit 1fcdc4f

File tree

6 files changed

+64
-57
lines changed

6 files changed

+64
-57
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ dist
44
.opt-in
55
.opt-out
66
package-lock.json
7-
.eslintcache
7+
yarn.lock

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@
3232
"author": "Kent C. Dodds <[email protected]> (http://kentcdodds.com/)",
3333
"license": "MIT",
3434
"dependencies": {
35-
"@babel/runtime": "^7.4.2",
36-
"cosmiconfig": "^5.2.0",
37-
"resolve": "^1.10.0"
35+
"@babel/runtime": "^7.7.2",
36+
"cosmiconfig": "^6.0.0",
37+
"resolve": "^1.12.0"
3838
},
3939
"devDependencies": {
40-
"@babel/core": "^7.4.0",
41-
"@babel/parser": "^7.4.2",
42-
"@babel/types": "^7.4.0",
40+
"@babel/core": "^7.7.2",
41+
"@babel/parser": "^7.7.3",
42+
"@babel/types": "^7.7.2",
4343
"ast-pretty-print": "^2.0.1",
44-
"babel-plugin-tester": "^6.0.1",
44+
"babel-plugin-tester": "^7.0.4",
4545
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
46-
"cpy": "^7.1.0",
47-
"kcd-scripts": "^1.1.3"
46+
"cpy": "^7.3.0",
47+
"kcd-scripts": "^1.11.0"
4848
},
4949
"eslintConfig": {
5050
"extends": "./node_modules/kcd-scripts/eslint.js"

src/__tests__/__snapshots__/index.js.snap

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ const Div = STYLED.div\`
2727
↓ ↓ ↓ ↓ ↓ ↓
2828
2929
const red = "background-color: red;";
30-
const Div = STYLED.div\`undefined\`;
30+
const Div = STYLED.div\`composes: background-color: red;
31+
color: blue;\`;
3132
3233
`;
3334

@@ -49,7 +50,7 @@ errorThrower('hi')
4950
5051
↓ ↓ ↓ ↓ ↓ ↓
5152
52-
Error: not helpful Learn more: https://www.npmjs.com/package/babel-plugin-macros-test-error-thrower
53+
Error: babel-plugin-macros-test-error-thrower/macro: not helpful Learn more: https://www.npmjs.com/package/babel-plugin-macros-test-error-thrower
5354
5455
`;
5556

@@ -60,7 +61,7 @@ errorThrower('hi')
6061
6162
↓ ↓ ↓ ↓ ↓ ↓
6263
63-
Error: not helpful Learn more: https://www.npmjs.com/package/babel-plugin-macros-test-error-thrower.macro
64+
Error: babel-plugin-macros-test-error-thrower.macro: not helpful Learn more: https://www.npmjs.com/package/babel-plugin-macros-test-error-thrower.macro
6465
6566
`;
6667

@@ -133,8 +134,15 @@ const red = macro('noop')
133134
134135
↓ ↓ ↓ ↓ ↓ ↓
135136
136-
import macro from './fixtures/keep-imports.macro';
137-
const red = macro('noop');
137+
"use strict";
138+
139+
var _keepImports = require("./fixtures/keep-imports.macro");
140+
141+
var _keepImports2 = _interopRequireDefault(_keepImports);
142+
143+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
144+
145+
const red = (0, _keepImports2.default)('noop');
138146
139147
`;
140148

@@ -145,7 +153,7 @@ errorThrower('hey')
145153
146154
↓ ↓ ↓ ↓ ↓ ↓
147155
148-
Error: very unhelpful
156+
Error: ./fixtures/error-thrower.macro: very unhelpful
149157
150158
`;
151159

@@ -202,7 +210,7 @@ unwrapped('hey')
202210
203211
↓ ↓ ↓ ↓ ↓ ↓
204212
205-
Error: https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/author.md#writing-a-macro
213+
Error: The macro imported from "./fixtures/non-wrapped.macro" must be wrapped in "createMacro" which you can get from "babel-plugin-macros". Please refer to the documentation to see how to do this properly: https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/author.md#writing-a-macro
206214
207215
`;
208216
@@ -346,7 +354,8 @@ const Div = STYLED.div\`
346354
↓ ↓ ↓ ↓ ↓ ↓
347355
348356
const red = "background-color: red;";
349-
const Div = STYLED.div\`undefined\`;
357+
const Div = STYLED.div\`composes: background-color: red;
358+
color: blue;\`;
350359
351360
`;
352361
@@ -365,7 +374,8 @@ const Div = styled.div\`
365374
↓ ↓ ↓ ↓ ↓ ↓
366375
367376
const red = "background-color: red;";
368-
const Div = styled.div\`undefined\`;
377+
const Div = styled.div\`composes: background-color: red;
378+
color: blue;\`;
369379
370380
`;
371381

src/__tests__/fixtures/emotion.macro.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ function emotionMacro({references, babel}) {
2222
if (styledRef.parentPath.parentPath.type === 'TaggedTemplateExpression') {
2323
const quasi = styledRef.parentPath.parentPath.get('quasi')
2424
const val = quasi.evaluate().value.trim()
25-
const replacement = t.templateLiteral([t.templateElement(val)], [])
25+
const replacement = t.templateLiteral(
26+
[t.templateElement({raw: val, cooked: val})],
27+
[],
28+
)
2629
quasi.replaceWith(replacement)
2730
}
2831
})

src/__tests__/index.js

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
/* eslint no-console:0 */
21
import path from 'path'
3-
import cosmiconfigMock from 'cosmiconfig'
2+
import {cosmiconfigSync as cosmiconfigSyncMock} from 'cosmiconfig'
43
import cpy from 'cpy'
54
import babel from '@babel/core'
65
import pluginTester from 'babel-plugin-tester'
@@ -9,28 +8,13 @@ import plugin from '../'
98
const projectRoot = path.join(__dirname, '../../')
109

1110
jest.mock('cosmiconfig', () => {
12-
const mockSearchSync = jest.fn()
13-
Object.assign(mockSearchSync, {
14-
mockReset() {
15-
return mockSearchSync.mockImplementation(
16-
(filename, configuredCosmiconfig) =>
17-
configuredCosmiconfig.searchSync(filename),
18-
)
19-
},
20-
})
21-
22-
mockSearchSync.mockReset()
23-
24-
const _cosmiconfigMock = (...args) => ({
25-
searchSync(filename) {
26-
return mockSearchSync(
27-
filename,
28-
require.requireActual('cosmiconfig')(...args),
29-
)
30-
},
31-
})
32-
33-
return Object.assign(_cosmiconfigMock, {mockSearchSync})
11+
const cosmiconfigExports = jest.requireActual('cosmiconfig')
12+
const actualCosmiconfigSync = cosmiconfigExports.cosmiconfigSync
13+
function fakeCosmiconfigSync(...args) {
14+
fakeCosmiconfigSync.explorer = actualCosmiconfigSync(...args)
15+
return fakeCosmiconfigSync.explorer
16+
}
17+
return {...cosmiconfigExports, cosmiconfigSync: fakeCosmiconfigSync}
3418
})
3519

3620
beforeAll(() => {
@@ -43,10 +27,13 @@ beforeAll(() => {
4327
})
4428
})
4529

30+
beforeEach(() => {
31+
jest.spyOn(console, 'error').mockImplementation(() => {})
32+
})
33+
4634
afterEach(() => {
47-
// eslint-disable-next-line
48-
require('babel-plugin-macros-test-fake/macro').innerFn.mockClear()
49-
cosmiconfigMock.mockSearchSync.mockReset()
35+
console.error.mockRestore()
36+
jest.clearAllMocks()
5037
})
5138

5239
expect.addSnapshotSerializer({
@@ -308,18 +295,20 @@ pluginTester({
308295
error: true,
309296
fixture: path.join(__dirname, 'fixtures/config/code.js'),
310297
setup() {
311-
cosmiconfigMock.mockSearchSync.mockImplementationOnce(() => {
312-
throw new Error('this is a cosmiconfig error')
313-
})
314-
const originalError = console.error
315-
console.error = jest.fn()
298+
jest
299+
.spyOn(cosmiconfigSyncMock.explorer, 'search')
300+
.mockImplementationOnce(() => {
301+
throw new Error('this is a cosmiconfig error')
302+
})
303+
jest.spyOn(console, 'error').mockImplementationOnce(() => {})
316304
return function teardown() {
317305
try {
318306
expect(console.error).toHaveBeenCalledTimes(1)
319307
expect(console.error.mock.calls[0]).toMatchSnapshot()
320-
console.error = originalError
308+
console.error.mockClear()
321309
} catch (e) {
322310
console.error(e)
311+
console.error.mockClear()
323312
throw e
324313
}
325314
}
@@ -329,7 +318,11 @@ pluginTester({
329318
title: 'when there is no config to load, then no config is passed',
330319
fixture: path.join(__dirname, 'fixtures/config/code.js'),
331320
setup() {
332-
cosmiconfigMock.mockSearchSync.mockImplementationOnce(() => null)
321+
jest
322+
.spyOn(cosmiconfigSyncMock.explorer, 'search')
323+
.mockImplementationOnce(() => {
324+
return null
325+
})
333326
return function teardown() {
334327
try {
335328
const configurableMacro = require('./fixtures/config/configurable.macro')
@@ -450,3 +443,5 @@ pluginTester({
450443
},
451444
],
452445
})
446+
447+
/* eslint no-console:0 */

src/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function getConfigExporer() {
2323
return (_configExplorer =
2424
_configExplorer ||
2525
// Lazy load cosmiconfig since it is a relatively large bundle
26-
require('cosmiconfig')('babel-plugin-macros', {
26+
require('cosmiconfig').cosmiconfigSync('babel-plugin-macros', {
2727
searchPlaces: [
2828
'package.json',
2929
'.babel-plugin-macrosrc',
@@ -34,7 +34,6 @@ function getConfigExporer() {
3434
'babel-plugin-macros.config.js',
3535
],
3636
packageProp: 'babelMacros',
37-
sync: true,
3837
}))
3938
}
4039

@@ -253,7 +252,7 @@ function applyMacros({
253252

254253
function getConfigFromFile(configName, filename) {
255254
try {
256-
const loaded = getConfigExporer().searchSync(filename)
255+
const loaded = getConfigExporer().search(filename)
257256

258257
if (loaded) {
259258
return {

0 commit comments

Comments
 (0)