-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,165 additions
and
4 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"use strict"; | ||
|
||
module.exports = { | ||
root: true, | ||
extends: [ | ||
"eslint:recommended", | ||
"plugin:eslint-plugin/recommended", | ||
"plugin:node/recommended" | ||
], | ||
env: { | ||
node: true | ||
}, | ||
overrides: [ | ||
{ | ||
files: ["tests/**/*.js"], | ||
env: { mocha: true } | ||
} | ||
] | ||
}; |
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,18 @@ | ||
/** | ||
* @fileoverview internal eslint rules | ||
* @author | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const requireIndex = require("requireindex"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Plugin Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
// import all rules in lib/rules | ||
module.exports.rules = requireIndex(__dirname + "/rules"); |
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,98 @@ | ||
"use strict"; | ||
|
||
const fs = require("fs"); | ||
const path = require("path"); | ||
const { parse } = require("@babel/parser"); | ||
|
||
module.exports = { | ||
meta: { | ||
name: "check-i18n-keys", | ||
type: "suggestion", | ||
docs: { | ||
description: | ||
"Ensure translation keys in other language files match the keys in the English translation file.", | ||
category: "Best Practices", | ||
recommended: true | ||
}, | ||
fixable: null, | ||
schema: [] | ||
}, | ||
create: function (context) { | ||
function extractKeys(node, parentKey = "") { | ||
const keys = []; | ||
let properties = node.properties; | ||
|
||
if (typeof node === "string") { | ||
const fileContent = fs.readFileSync(node, "utf8"); | ||
const ast = parse(fileContent, { | ||
sourceType: "module", | ||
plugins: ["typescript", "jsx"] | ||
}); | ||
properties = | ||
!!ast && ast.program.body[0].declaration.properties; | ||
} | ||
|
||
function traverseProperties(properties, parentKey) { | ||
properties.forEach((property) => { | ||
if ( | ||
property.type === "ObjectProperty" || | ||
(property.type === "Property" && | ||
property.key.type === "Identifier") | ||
) { | ||
const currentKey = parentKey | ||
? `${parentKey}.${property.key.name}` | ||
: property.key.name; | ||
keys.push(currentKey); | ||
if (property.value.type === "ObjectExpression") { | ||
traverseProperties( | ||
property.value.properties, | ||
currentKey | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
traverseProperties(properties, parentKey); | ||
|
||
return keys; | ||
} | ||
|
||
return { | ||
Program(node) { | ||
for (const statement of node.body) { | ||
const fallbackFilePath = path | ||
.relative(process.cwd(), context.getFilename()) | ||
.replace( | ||
/\/i18n\/\w+\/translations\.ts$/, | ||
"/i18n/en/translations.ts" | ||
); | ||
|
||
const keys = extractKeys(statement.declaration); | ||
|
||
const enKeys = extractKeys(fallbackFilePath); | ||
|
||
// Report missing keys | ||
enKeys.forEach((enKey) => { | ||
if (!keys.includes(enKey)) { | ||
context.report({ | ||
node: node, | ||
message: `missing key '${enKey}'` | ||
}); | ||
} | ||
}); | ||
|
||
// Report extra keys | ||
keys.forEach((key) => { | ||
if (!enKeys.includes(key)) { | ||
context.report({ | ||
node: node, | ||
message: `extra key '${key}'` | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
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,32 @@ | ||
{ | ||
"name": "eslint-plugin-internal-rules", | ||
"version": "0.0.0", | ||
"description": "internal eslint rules", | ||
"keywords": [ | ||
"eslint", | ||
"eslintplugin", | ||
"eslint-plugin" | ||
], | ||
"author": "", | ||
"main": "./lib/index.js", | ||
"exports": "./lib/index.js", | ||
"scripts": { | ||
"lint": "npm-run-all \"lint:*\"", | ||
"lint:js": "eslint ." | ||
}, | ||
"dependencies": { | ||
"requireindex": "^1.2.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^8.19.0", | ||
"eslint-plugin-eslint-plugin": "^5.0.0", | ||
"eslint-plugin-node": "^11.1.0" | ||
}, | ||
"engines": { | ||
"node": "^14.17.0 || ^16.0.0 || >= 18.0.0" | ||
}, | ||
"peerDependencies": { | ||
"eslint": ">=7" | ||
}, | ||
"license": "ISC" | ||
} |
Oops, something went wrong.