Skip to content

Commit

Permalink
finalize logic
Browse files Browse the repository at this point in the history
  • Loading branch information
benalleng committed Feb 19, 2024
1 parent 0445234 commit 9b498c6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 104 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ module.exports = {
}
},
{
files: ['src/i18n/**/translations.ts'], // Specify the path pattern for the files you want to apply the rule to
files: ['src/i18n/**/translations.ts'],
rules: {
"internal-rules/check-i18n-keys": "off"
"internal-rules/check-i18n-keys": "warn"
}
},
],
Expand Down
107 changes: 41 additions & 66 deletions tools/internal-rules/lib/rules/check-i18n-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,97 +18,76 @@ module.exports = {
schema: []
},
create: function (context) {
function extractKeysFromObjectExpression(node) {
function extractKeysFromObjectExpression(node, parentKey = "") {
const keys = [];

function traverseProperties(properties) {
if (!properties) return; // Handle undefined or null properties

for (const property of properties) {
function traverseObjectProperties(properties, parentKey) {
properties.forEach((property) => {
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);
const currentKey = parentKey
? `${parentKey}.${property.key.name}`
: property.key.name;
keys.push(currentKey);
if (property.value.type === "ObjectExpression") {
traverseObjectProperties(
property.value.properties,
currentKey
);
}
}
}
});
}

traverseProperties(node.properties);
traverseObjectProperties(node.properties, parentKey);

return keys;
}

function extractKeysFromFile(filePath) {
function extractKeysFromFile(filePath, parentKey = "") {
const fileContent = fs.readFileSync(filePath, "utf8");
const ast = parse(fileContent, {
sourceType: "module", // or 'script' depending on your file
sourceType: "module",
plugins: ["typescript", "jsx"]
});
const keys = [];

const properties = ast.program.body[0].declaration.properties;

function traverseProperties(properties) {
for (const property of properties) {
function traverseFileProperties(properties, parentKey) {
properties.forEach((property) => {
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);
const currentKey = parentKey
? `${parentKey}.${property.key.name}`
: property.key.name;
keys.push(currentKey);
if (property.value.type === "ObjectExpression") {
traverseFileProperties(
property.value.properties,
currentKey
);
}
}
}
});
}

traverseProperties(properties);
traverseFileProperties(properties, parentKey);

return keys;
}

return {
Program(node) {
for (const statement of node.body) {
const relativePath = path.relative(process.cwd(), context.getFilename())
const relativePath = path.relative(
process.cwd(),
context.getFilename()
);
const fallbackFilePath = path
.relative(process.cwd(), context.getFilename())
.replace(
Expand All @@ -121,27 +100,23 @@ module.exports = {
);

const enKeys = extractKeysFromFile(fallbackFilePath);
// Report missing keys and incorrect order
enKeys.forEach((enKey, index) => {

// Report missing keys
enKeys.forEach((enKey) => {
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}'`
message: `missing key '${enKey}' ${relativePath}`
});
}
});

// Report extra keys
keys.forEach(key => {
keys.forEach((key) => {
if (!enKeys.includes(key)) {
context.report({
node: node,
message: `extra key '${key}'`
message: `extra key '${key}' ${relativePath}`
});
}
});
Expand Down
72 changes: 36 additions & 36 deletions tools/internal-rules/package.json
Original file line number Diff line number Diff line change
@@ -1,38 +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"
"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"
}

0 comments on commit 9b498c6

Please sign in to comment.