-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
i18n-no-this-translate: Add 18n-no-this-translate (#28)
- Loading branch information
1 parent
433eb32
commit 4199cf7
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/eslint-plugin-wpcalypso/docs/rules/i18n-no-this-translate.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Disallow this.translate() | ||
Disallows the usage of `this.translate()` in favor of the high order component `localize()` from `i18n-calypso` repository. | ||
|
||
## Rule Details | ||
|
||
This rules aims to prevent the usage of `this.translate` that uses the mixin approach which makes the components harder to test. | ||
The following is considered a warning: | ||
```js | ||
const MyComponent = React.createClass( { | ||
render() { | ||
return this.translate( 'Hello World' ); | ||
} | ||
} ); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
import i18n from 'i18n-calypso'; | ||
i18n.translate('Hello World'); | ||
``` | ||
|
||
```js | ||
import { localize } from 'i18n-calypso'; | ||
const MyComponent = React.createClass( { | ||
render() { | ||
return this.props.translate( 'Hello World' ); | ||
} | ||
} ); | ||
export default localize( MyComponent ); | ||
``` |
35 changes: 35 additions & 0 deletions
35
packages/eslint-plugin-wpcalypso/lib/rules/i18n-no-this-translate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @fileoverview Disallow the use of this.translate | ||
* @author Automattic | ||
* @copyright 2016 Automattic. All rights reserved. | ||
* See LICENSE.md file in root directory for full license. | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
const ERROR_MESSAGE = 'Use localize( ReactComponent ) instead of this.translate'; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Disallow the use of this.translate', | ||
category: 'Deprecation', | ||
recommended: true | ||
}, | ||
schema: [] | ||
}, | ||
create: function( context ) { | ||
return { | ||
CallExpression: function( node ) { | ||
if ( node.callee.type === 'MemberExpression' && | ||
node.callee.object.type === 'ThisExpression' && | ||
node.callee.property.name === 'translate' ) { | ||
context.report( node, ERROR_MESSAGE ); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
35 changes: 35 additions & 0 deletions
35
packages/eslint-plugin-wpcalypso/tests/lib/rules/i18n-no-this-translate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @fileoverview Disallow the use of this.translate | ||
* @author Automattic | ||
* @copyright 2016 Automattic. All rights reserved. | ||
* See LICENSE.md file in root directory for full license. | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var rule = require( '../../../lib/rules/i18n-no-this-translate' ), | ||
RuleTester = require( 'eslint' ).RuleTester; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
( new RuleTester() ).run( 'i18n-no-this-translate', rule, { | ||
valid: [ | ||
'i18n.translate(\'hello\')', | ||
'translate(\'hello\')', | ||
'this.props.translate(\'hello\')' | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'this.translate(\'hello\')', | ||
errors: [ { | ||
message: 'Use localize( ReactComponent ) instead of this.translate' | ||
} ] | ||
} | ||
] | ||
} ); |