forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add eslint custom plugin to prevent translation variables (apa…
- Loading branch information
1 parent
4cb30f8
commit cd1bcfb
Showing
9 changed files
with
184 additions
and
5 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
56 changes: 56 additions & 0 deletions
56
superset-frontend/tools/eslint-plugin-translation-vars/index.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,56 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/** | ||
* @fileoverview Rule to warn about translation template variables | ||
* @author Apache | ||
*/ | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
rules: { | ||
'no-template-vars': { | ||
create(context) { | ||
function handler(node) { | ||
if (node.arguments.length) { | ||
const firstArgs = node.arguments[0]; | ||
if ( | ||
firstArgs.type === 'TemplateLiteral' && | ||
firstArgs.expressions.length | ||
) { | ||
context.report({ | ||
node, | ||
message: | ||
"Don't use variables in translation string templates. Flask-babel is a static translation translation service, so it can’t handle strings that include variables", | ||
}); | ||
} | ||
} | ||
} | ||
return { | ||
"CallExpression[callee.name='t']": handler, | ||
"CallExpression[callee.name='tn']": handler, | ||
}; | ||
}, | ||
}, | ||
}, | ||
}; |
68 changes: 68 additions & 0 deletions
68
superset-frontend/tools/eslint-plugin-translation-vars/no-template-vars.test.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,68 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/** | ||
* @fileoverview Rule to warn about translation template variables | ||
* @author Apache | ||
*/ | ||
/* eslint-disable no-template-curly-in-string */ | ||
const { RuleTester } = require('eslint'); | ||
const plugin = require('.'); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } }); | ||
const rule = plugin.rules['no-template-vars']; | ||
|
||
const errors = [ | ||
{ | ||
type: 'CallExpression', | ||
}, | ||
]; | ||
|
||
ruleTester.run('no-template-vars', rule, { | ||
valid: [ | ||
't(`foo`)', | ||
'tn(`foo`)', | ||
't(`foo %s bar`)', | ||
'tn(`foo %s bar`)', | ||
't(`foo %s bar %s`)', | ||
'tn(`foo %s bar %s`)', | ||
], | ||
invalid: [ | ||
{ | ||
code: 't(`foo${bar}`)', | ||
errors, | ||
}, | ||
{ | ||
code: 't(`foo${bar} ${baz}`)', | ||
errors, | ||
}, | ||
{ | ||
code: 'tn(`foo${bar}`)', | ||
errors, | ||
}, | ||
{ | ||
code: 'tn(`foo${bar} ${baz}`)', | ||
errors, | ||
}, | ||
], | ||
}); |
20 changes: 20 additions & 0 deletions
20
superset-frontend/tools/eslint-plugin-translation-vars/package.json
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,20 @@ | ||
{ | ||
"name": "eslint-plugin-translation-vars", | ||
"version": "1.0.0", | ||
"description": "Warns about translation variables", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"license": "Apache-2.0", | ||
"author": "Apache", | ||
"dependencies": {}, | ||
"peerDependencies": { | ||
"eslint": ">=0.8.0" | ||
}, | ||
"engines": { | ||
"node": "^16.9.1", | ||
"npm": "^7.5.4" | ||
} | ||
} |