-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add eslint rule for localization issues #10397
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
109 changes: 109 additions & 0 deletions
109
dev-packages/private-eslint-plugin/rules/localization-check.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,109 @@ | ||
// @ts-check | ||
/******************************************************************************** | ||
* Copyright (C) 2021 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
const levenshtein = require('js-levenshtein'); | ||
|
||
const metadata = require('@theia/core/src/common/i18n/nls.metadata.json'); | ||
const messages = new Set(Object.values(metadata.messages) | ||
.reduceRight((prev, curr) => prev.concat(curr), []) | ||
.map(e => e.replace(/&&/g, ''))); | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
fixable: 'code', | ||
docs: { | ||
description: 'prevent incorrect use of \'nls.localize\'.', | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
const callee = node.callee; | ||
if (callee.type === 'Super') { | ||
return; | ||
} | ||
const { value, byDefault, node: localizeNode } = evaluateLocalize(node); | ||
if (value !== undefined) { | ||
if (byDefault && !messages.has(value)) { | ||
let lowestDistance = Number.MAX_VALUE; | ||
let lowestMessage = ''; | ||
for (const message of messages) { | ||
const distance = levenshtein(value, message); | ||
if (distance < lowestDistance) { | ||
lowestDistance = distance; | ||
lowestMessage = message; | ||
} | ||
} | ||
if (lowestMessage) { | ||
context.report({ | ||
node: localizeNode, | ||
message: `'${value}' is not a valid default value. Did you mean '${lowestMessage}'?`, | ||
fix: function (fixer) { | ||
const updatedCall = `'${lowestMessage.replace(/'/g, "\\'")}'`; | ||
return fixer.replaceText(localizeNode, updatedCall); | ||
} | ||
}); | ||
} else { | ||
context.report({ | ||
node: localizeNode, | ||
message: `'${value}' is not a valid default value.` | ||
}); | ||
} | ||
} else if (!byDefault && messages.has(value)) { | ||
context.report({ | ||
node, | ||
message: `'${value}' can be translated using the 'nls.localizeByDefault' function.`, | ||
fix: function (fixer) { | ||
const code = context.getSourceCode(); | ||
const args = node.arguments.slice(1); | ||
const argsCode = args.map(e => code.getText(e)).join(', '); | ||
const updatedCall = `nls.localizeByDefault(${argsCode})`; | ||
return fixer.replaceText(node, updatedCall); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
function evaluateLocalize(/** @type {import('estree').CallExpression} */ node) { | ||
const callee = node.callee; | ||
if ('object' in callee && 'name' in callee.object && 'property' in callee && 'name' in callee.property && callee.object.name === 'nls') { | ||
if (callee.property.name === 'localize') { | ||
const defaultTextNode = node.arguments[1]; // The default text node is the second argument for `nls.localize` | ||
if (defaultTextNode && defaultTextNode.type === 'Literal' && typeof defaultTextNode.value === 'string') { | ||
return { | ||
value: defaultTextNode.value, | ||
byDefault: false | ||
}; | ||
} | ||
} else if (callee.property.name === 'localizeByDefault') { | ||
const defaultTextNode = node.arguments[0]; // The default text node is the first argument for ``nls.localizeByDefault` | ||
if (defaultTextNode && defaultTextNode.type === 'Literal' && typeof defaultTextNode.value === 'string') { | ||
return { | ||
node: defaultTextNode, | ||
value: defaultTextNode.value, | ||
byDefault: true | ||
}; | ||
} | ||
} | ||
} | ||
return {}; | ||
} | ||
} | ||
}; |
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
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
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps add a second commit fixing these? it looks like it's mainly suggestions to use
byDefault
in place of the keyed version. For that violation, it might make sense to add a fixer to the plugin.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this file is a weird case anyway. This file uses it's own "implementation" (if one can say so), since it's in the
node
directory and has no actual localizations available:theia/packages/debug/src/node/vscode/vscode-debug-adapter-contribution.ts
Lines 27 to 31 in 89b8dd1
This code has been there for quite a long time, or at least way before I started working on i18n in Theia. Actually, looking further into it, nothing that's exported in this file is referenced somewhere else. It seems to be dead code from before the vscode-extension time of Theia. I would remove this in a separate PR.