Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
436 changes: 73 additions & 363 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wp-blocks/make-pot",
"version": "1.6.0",
"version": "1.6.1",
"license": "GPL-3.0-or-later",
"homepage": "https://wp-blocks.github.io/make-pot/",
"description": "A Node.js script for generating a POT file from source code",
Expand Down Expand Up @@ -47,7 +47,7 @@
],
"scripts": {
"postinstall": "npm rebuild tree-sitter tree-sitter-typescript tree-sitter-php tree-sitter-javascript --force",
"build": "npx esbuild ./src/**/* --format=cjs --minify --outdir=lib --platform=node",
"build": "npx esbuild ./src/**/* ./src/*.ts --format=cjs --minify --outdir=lib --platform=node",
"watch": "tsc --watch",
"lint": "npx @biomejs/biome check --write src",
"rm": "rmdir /s /q lib",
Expand All @@ -66,10 +66,10 @@
"gettext-parser": "^4.0.4",
"glob": "^11.0.2",
"tannin": "^1.2.0",
"tree-sitter": "^0.20.6",
"tree-sitter-javascript": "^0.20.4",
"tree-sitter-php": "^0.20.0",
"tree-sitter-typescript": "^0.20.5",
"tree-sitter": "^0.21.1",
"tree-sitter-javascript": "^0.23.1",
"tree-sitter-php": "^0.23.12",
"tree-sitter-typescript": "^0.23.2",
"yargs": "^17.7.1"
},
"devDependencies": {
Expand Down
12 changes: 7 additions & 5 deletions src/fs/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import path from "node:path";
import { Glob, type Path } from "glob";
import { minimatch } from "minimatch";
// @ts-ignore
import * as Javascript from "tree-sitter-javascript";
import * as javascript from "tree-sitter-javascript";
// @ts-ignore
import * as php from "tree-sitter-php";
// @ts-ignore
import * as ts from "tree-sitter-typescript";
import type { Args, Patterns } from "../types.js";
import { detectPatternType } from "../utils/common.js";
import { detectPatternType, getFileExtension } from "../utils/common.js";

/**
* Return the parser based on the file extension
Expand All @@ -19,7 +19,7 @@ import { detectPatternType } from "../utils/common.js";
export function getParser(
file: string,
): string | { name: string; language: unknown } | null {
const ext = file.split(".").pop();
const ext = getFileExtension(file);
switch (ext) {
case "ts":
return ts.typescript;
Expand All @@ -29,9 +29,11 @@ export function getParser(
case "jsx":
case "mjs":
case "cjs":
return Javascript.default;
return javascript;
case "php":
return php.default;
return php.php;
case "blade.php":
return php.php_only;
default:
return null;
}
Expand Down
21 changes: 7 additions & 14 deletions src/parser/makeJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,18 @@ export class MakeJsonCommand {
}

if (typeof this.scriptName === "string") {
const pot = this.addPot(file, this.scriptName);
this.scriptName = [this.scriptName];
}

for (const script of this.scriptName) {
const pot = this.addPot(file, script);
if (pot.data) {
output[pot.filename] = pot.data;
} else {
console.log(
`❌ Translation strings not found in Script ${this.scriptName} in ${file} po file`,
`❌ Translation strings not found in Script ${script} in ${file} po file`,
);
}
} else if (Array.isArray(this.scriptName)) {
for (const script of this.scriptName) {
const pot = this.addPot(file, script);
if (pot.data) {
output[pot.filename] = pot.data;
} else {
console.log(
`❌ Translation strings not found in Script ${script} in ${file} po file`,
);
}
}
}
}

Expand Down Expand Up @@ -459,7 +452,7 @@ export class MakeJsonCommand {
],
}).code as string;

return doTree(transformedScript, script);
return doTree(transformedScript, script, this.debug);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/parser/patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function getPatterns(args: Args) {
if (args.options) {
// js typescript mjs cjs etc
if (args.options.skip.blade) {
pattern.exclude.push("**/blade.php");
pattern.exclude.push("**/*.blade.php");
} else if (args.options.skip.php) {
pattern.exclude.push("**/*.php", "**/*.blade.php");
}
Expand Down
4 changes: 3 additions & 1 deletion src/parser/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export async function processFiles(
);
} else if (allowedFormats.includes(ext)) {
tasks.push(
readFileAsync(fileRealPath).then((content) => doTree(content, file)),
readFileAsync(fileRealPath).then((content) =>
doTree(content, file, args.debug),
),
);
}

Expand Down
17 changes: 12 additions & 5 deletions src/parser/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ function collectComments(node: SyntaxNode): string | undefined {
*
* @param {string} sourceCode - The source code to be parsed.
* @param {string} filepath - The path to the file being parsed.
* @param {boolean} debugEnabled - Whether debug mode is enabled.
* @return {SetOfBlocks} An array of translation strings.
*/
export function doTree(sourceCode: string, filepath: string): SetOfBlocks {
export function doTree(
sourceCode: string,
filepath: string,
debugEnabled?: boolean,
): SetOfBlocks {
// set up the parser
const parser = new Parser();
const parserExt = getParser(filepath);
Expand Down Expand Up @@ -139,10 +144,12 @@ export function doTree(sourceCode: string, filepath: string): SetOfBlocks {
// unquote the strings
nodeValue = nodeValue.slice(1, -1);
} else {
// unexpected node type this string is not translatable and should be skipped
console.warn(
`Unexpected node type: ${node?.type} is ${translationKeys[translationKeyIndex]} for ${nodeValue} in ${filepath}`,
);
if (debugEnabled) {
// Whenever we get an unexpected node type this string is not translatable and should be skipped
console.warn(
`Unexpected node type ${node?.type} identified as ${translationKeys[translationKeyIndex]} with value ${nodeValue} in ${filepath} at ${node.startPosition.row + 1} pos ${node.startPosition.column + 1}`,
);
}
continue;
}

Expand Down
12 changes: 12 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ export function detectPatternType(
return "glob";
}

/**
* Gets the file extension from a filename.
* @param filename - The name of the file to extract the extension from.
* @returns The file extension, or 'blade.php' for Blade templates.
*/
export function getFileExtension(filename: string): string {
if (filename.endsWith(".blade.php")) {
return "blade.php";
}
return filename.split(".").pop() || "";
}

/**
* Generates a copyright comment for the specified slug and license.
*
Expand Down
38 changes: 38 additions & 0 deletions tests/extract.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,44 @@ describe("getStrings", () => {
assert.deepStrictEqual(result.blocks[0].toJson(), expected.toJson());
});

it("should extract translations from blade", () => {
const content = `<h1>{{ __('Hello World', 'greeting') }}</h1>`;

const filename = "filename.blade.php";

const result = doTree(content, filename);

const expected = new Block([]);
expected.msgid = "Hello World";
expected.msgstr = [""];
expected.comments = {
reference: ["filename.blade.php:1"],
translator: [""],
};

assert.deepStrictEqual(result.blocks[0].toJson(), expected.toJson());
});

it("should extract translations from blade @php block", () => {
const content = `@php
$greeting = __('Hello World', 'greeting');
@endphp`;

const filename = "filename.blade.php";

const result = doTree(content, filename);

const expected = new Block([]);
expected.msgid = "Hello World";
expected.msgstr = [""];
expected.comments = {
reference: ["filename.blade.php:2"],
translator: [""],
};

assert.deepStrictEqual(result.blocks[0].toJson(), expected.toJson());
});

it("should extract translations with context", () => {
const content = `<?php __('Hello World', 'greeting'); ?>`;
const filename = "filename.php";
Expand Down
Loading