-
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
7 changed files
with
258 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
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,152 @@ | ||
"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 extractKeysFromObjectExpression(node) { | ||
const keys = []; | ||
|
||
function traverseProperties(properties) { | ||
if (!properties) return; // Handle undefined or null properties | ||
|
||
for (const property of properties) { | ||
if ( | ||
property.type === "Property" && | ||
property.value.type === "ObjectExpression" | ||
) { | ||
traverseProperties(property.value.properties); | ||
} else if ( | ||
property.type === "Property" && | ||
property.value.type === "ArrayExpression" | ||
) { | ||
traverseArrayElements(property.value.elements); | ||
} else if ( | ||
property.type === "Property" && | ||
property.key.type === "Identifier" | ||
) { | ||
keys.push(property.key.name); | ||
} | ||
} | ||
} | ||
|
||
function traverseArrayElements(elements) { | ||
if (!elements) return; // Handle undefined or null elements | ||
|
||
for (const element of elements) { | ||
if (element.type === "ObjectExpression") { | ||
traverseProperties(element.properties); | ||
} | ||
} | ||
} | ||
|
||
traverseProperties(node.properties); | ||
|
||
return keys; | ||
} | ||
|
||
function extractKeysFromFile(filePath) { | ||
const fileContent = fs.readFileSync(filePath, "utf8"); | ||
const ast = parse(fileContent, { | ||
sourceType: "module", // or 'script' depending on your file | ||
plugins: ["typescript", "jsx"] | ||
}); | ||
const keys = []; | ||
|
||
const properties = ast.program.body[0].declaration.properties; | ||
|
||
function traverseProperties(properties) { | ||
for (const property of properties) { | ||
if ( | ||
property.type === "ObjectProperty" && | ||
property.value.type === "ObjectExpression" | ||
) { | ||
traverseProperties(property.value.properties); | ||
} else if ( | ||
property.type === "ObjectProperty" && | ||
property.value.type === "ArrayExpression" | ||
) { | ||
traverseArrayElements(property.value.elements); | ||
} else if ( | ||
property.type === "ObjectProperty" && | ||
property.key.type === "Identifier" | ||
) { | ||
keys.push(property.key.name); | ||
} | ||
} | ||
} | ||
|
||
function traverseArrayElements(elements) { | ||
if (!elements) return; // Handle undefined or null elements | ||
|
||
for (const element of elements) { | ||
if (element.type === "ObjectExpression") { | ||
traverseProperties(element.properties); | ||
} | ||
} | ||
} | ||
|
||
traverseProperties(properties); | ||
|
||
return keys; | ||
} | ||
|
||
return { | ||
Program(node) { | ||
for (const statement of node.body) { | ||
const relativePath = path.relative(process.cwd(), context.getFilename()) | ||
const fallbackFilePath = path | ||
.relative(process.cwd(), context.getFilename()) | ||
.replace( | ||
/\/i18n\/\w+\/translations\.ts$/, | ||
"/i18n/en/translations.ts" | ||
); | ||
|
||
const keys = extractKeysFromObjectExpression( | ||
statement.declaration | ||
); | ||
|
||
const enKeys = extractKeysFromFile(fallbackFilePath); | ||
// Report missing keys and incorrect order | ||
enKeys.forEach((enKey, index) => { | ||
if (!keys.includes(enKey)) { | ||
context.report({ | ||
node: node, | ||
message: `missing key '${enKey}'` | ||
}); | ||
} else if (keys.indexOf(enKey) !== index) { | ||
context.report({ | ||
node: node, | ||
message: `incorrect key location '${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,38 @@ | ||
{ | ||
"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:eslint-docs": "npm-run-all \"update:eslint-docs -- --check\"", | ||
"lint:js": "eslint .", | ||
"test": "mocha tests --recursive", | ||
"update:eslint-docs": "eslint-doc-generator" | ||
}, | ||
"dependencies": { | ||
"requireindex": "^1.2.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^8.19.0", | ||
"eslint-doc-generator": "^1.0.0", | ||
"eslint-plugin-eslint-plugin": "^5.0.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"mocha": "^10.0.0", | ||
"npm-run-all": "^4.1.5" | ||
}, | ||
"engines": { | ||
"node": "^14.17.0 || ^16.0.0 || >= 18.0.0" | ||
}, | ||
"peerDependencies": { | ||
"eslint": ">=7" | ||
}, | ||
"license": "ISC" | ||
} |