From fc19f1b2cd7a286f1346d9acfa5d99887a5c2003 Mon Sep 17 00:00:00 2001 From: Vitaly Date: Fri, 13 May 2022 16:00:28 +0300 Subject: [PATCH 1/3] fix: print deprecation warning only when plugin is used --- lib/postcss.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/postcss.js b/lib/postcss.js index d3f640e4e..080ee8378 100644 --- a/lib/postcss.js +++ b/lib/postcss.js @@ -27,25 +27,27 @@ function postcss(...plugins) { } postcss.plugin = function plugin(name, initializer) { - // eslint-disable-next-line no-console - if (console && console.warn) { + let warningPrinted = false + function creator(...args) { // eslint-disable-next-line no-console - console.warn( - name + - ': postcss.plugin was deprecated. Migration guide:\n' + - 'https://evilmartians.com/chronicles/postcss-8-plugin-migration' - ) - if (process.env.LANG && process.env.LANG.startsWith('cn')) { - /* c8 ignore next 7 */ + if (console && console.warn && !warningPrinted) { + warningPrinted = true // eslint-disable-next-line no-console console.warn( name + - ': 里面 postcss.plugin 被弃用. 迁移指南:\n' + - 'https://www.w3ctech.com/topic/2226' + ': postcss.plugin was deprecated. Migration guide:\n' + + 'https://evilmartians.com/chronicles/postcss-8-plugin-migration' ) + if (process.env.LANG && process.env.LANG.startsWith('cn')) { + /* c8 ignore next 7 */ + // eslint-disable-next-line no-console + console.warn( + name + + ': 里面 postcss.plugin 被弃用. 迁移指南:\n' + + 'https://www.w3ctech.com/topic/2226' + ) + } } - } - function creator(...args) { let transformer = initializer(...args) transformer.postcssPlugin = name transformer.postcssVersion = new Processor().version From 2295e280665431a14a3656b30d48841469cc98b8 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Fri, 13 May 2022 17:39:56 +0300 Subject: [PATCH 2/3] fix tests --- test/postcss.test.ts | 12 ++++++++---- test/processor.test.ts | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/test/postcss.test.ts b/test/postcss.test.ts index 47043fa69..279ea60a2 100755 --- a/test/postcss.test.ts +++ b/test/postcss.test.ts @@ -121,8 +121,6 @@ test('has deprecated method to creat plugins', () => { }) } }) - equal(warn.callCount, 1) - match(warn.calls[0][0], /postcss\.plugin was deprecated/) let func1: any = postcss(plugin).plugins[0] is(func1.postcssPlugin, 'test') @@ -134,9 +132,13 @@ test('has deprecated method to creat plugins', () => { let result1 = postcss(plugin('one')).process('a{ one: 1; two: 2 }') is(result1.css, 'a{ two: 2 }') + equal(warn.callCount, 1) let result2 = postcss(plugin).process('a{ one: 1; two: 2 }') is(result2.css, 'a{ one: 1 }') + + equal(warn.callCount, 1) + match(warn.calls[0][0], /postcss\.plugin was deprecated/) }) test('creates a shortcut to process css', async () => { @@ -148,7 +150,6 @@ test('creates a shortcut to process css', async () => { }) } }) - equal(warn.callCount, 1) let result1 = plugin.process('a{value:foo}') is(result1.css, 'a{value:bar}') @@ -159,6 +160,8 @@ test('creates a shortcut to process css', async () => { let result = await plugin.process('a{value:foo}', { from: 'a' }, 'baz') equal(result.opts, { from: 'a' }) is(result.css, 'a{value:baz}') + + equal(warn.callCount, 1) }) test('does not call plugin constructor', () => { @@ -169,13 +172,14 @@ test('does not call plugin constructor', () => { return () => {} }) is(calls, 0) - equal(warn.callCount, 1) postcss(plugin).process('a{}') is(calls, 1) postcss(plugin()).process('a{}') is(calls, 2) + + equal(warn.callCount, 1) }) test.run() diff --git a/test/processor.test.ts b/test/processor.test.ts index 23234ac38..09a579c2d 100755 --- a/test/processor.test.ts +++ b/test/processor.test.ts @@ -390,8 +390,8 @@ test('checks plugin compatibility', () => { throw new Error('Er') } }) - equal(warn.callCount, 1) let func = plugin() + equal(warn.callCount, 1) func.postcssVersion = '2.1.5' function processBy(version: string): void { @@ -562,9 +562,9 @@ test('supports plugins returning processors', () => { let other: any = (postcss as any).plugin('test', () => { return new Processor([a]) }) - equal(warn.callCount, 1) processor.use(other) equal(processor.plugins, [a]) + equal(warn.callCount, 1) }) test('supports plugin creators returning processors', () => { From 7cd8e273c03a94fb79e1eee0db58f4cd6d4cbf86 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Fri, 13 May 2022 17:45:44 +0300 Subject: [PATCH 3/3] improve warnings count testing --- test/postcss.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/postcss.test.ts b/test/postcss.test.ts index 279ea60a2..ee2b88dba 100755 --- a/test/postcss.test.ts +++ b/test/postcss.test.ts @@ -122,9 +122,12 @@ test('has deprecated method to creat plugins', () => { } }) + equal(warn.callCount, 0) + let func1: any = postcss(plugin).plugins[0] is(func1.postcssPlugin, 'test') match(func1.postcssVersion, /\d+.\d+.\d+/) + equal(warn.callCount, 1) let func2: any = postcss(plugin()).plugins[0] equal(func2.postcssPlugin, func1.postcssPlugin) @@ -132,7 +135,6 @@ test('has deprecated method to creat plugins', () => { let result1 = postcss(plugin('one')).process('a{ one: 1; two: 2 }') is(result1.css, 'a{ two: 2 }') - equal(warn.callCount, 1) let result2 = postcss(plugin).process('a{ one: 1; two: 2 }') is(result2.css, 'a{ one: 1 }')