Skip to content
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

Fix invalid translation files crashing the extension. #223

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion generate-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
$content = file_get_contents($template);

$content = str_replace('\\', '\\\\', $content);
$content = str_replace('<?php', '', $content);
$content = substr_replace($content, '', strpos($content,'<?php'), strlen('<?php'));
$content = implode("\n", [
"// This file was generated from {$template}, do not edit directly",
'export default `',
Expand Down
36 changes: 35 additions & 1 deletion php-templates/translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,43 @@ function vsCodeGetJsonTranslationsFromFile($lang, $key, $value, $file, $lines)
->filter()
];
}
function validateFile($file){
if (pathinfo($file, PATHINFO_EXTENSION) === 'json'){
try{
$decoded = json_decode(file_get_contents($file));
if (is_null($decoded) || json_last_error() !== JSON_ERROR_NONE) {
throw new Exception('Invalid JSON');
}
}catch(Throwable $e){
echo 'Translation Files: Invalid JSON translation file at: '.$file;
exit(0);
}

}
if (pathinfo($file, PATHINFO_EXTENSION) === 'php'){
try{
$content = file_get_contents($file);
if(str_contains($content,'<?php') === false){
throw new Exception('<?php not included');
}

$content = str_replace('<?php','',$content);
$content = str_replace('?>','',$content);

$result = eval($content);
if(is_array($result) === false){
throw new Exception('return type is not an array');
}

}catch(Throwable $e){
echo 'Translation Files: Invalid PHP translation file at: '.$file;
exit(0);
}
}
}
function vsCodeGetTranslationsFromFile($file, $path, $namespace)
{
validateFile($file);
$fileLines = \Illuminate\Support\Facades\File::lines($file);
$lines = [];
$inComment = false;
Expand Down Expand Up @@ -200,5 +234,5 @@ function vscodeCollectTranslations(string $path, ?string $namespace = null)

echo json_encode([
'default' => \Illuminate\Support\Facades\App::currentLocale(),
'translations' => $final,
'translations' => $final
]);
9 changes: 8 additions & 1 deletion src/support/php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,14 @@ export const runInLaravel = <T>(

throw new Error(result);
})
.catch((e) => {
.catch((e) => {
if(e.toString().includes('Translation Files: Invalid')){
error(e.toString().replace(toTemplateVar("start_output"),''));
return {
default:"",
translations:{}
};
}
if (e.toString().includes("ParseError")) {
// If we it's a parse error let's not show the popup,
// probably just a momentary syntax error
Expand Down
35 changes: 34 additions & 1 deletion src/templates/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,42 @@ function vsCodeGetJsonTranslationsFromFile($lang, $key, $value, $file, $lines)
->filter()
];
}
function validateFile($file){
if (pathinfo($file, PATHINFO_EXTENSION) === 'json'){
try{
$decoded = json_decode(file_get_contents($file));
if (is_null($decoded) || json_last_error() !== JSON_ERROR_NONE) {
throw new Exception('Invalid JSON');
}
}catch(Throwable $e){
echo 'Translation Files: Invalid JSON translation file at: '.$file;
exit(0);
}

}
if (pathinfo($file, PATHINFO_EXTENSION) === 'php'){
try{
$content = file_get_contents($file);
if(str_contains($content,'<?php') === false){
throw new Exception('<?php not included');
}

$content = str_replace('<?php','',$content);
$content = str_replace('?>','',$content);

$result = eval($content);
if(is_array($result) === false){
throw new Exception('return type is not an array');
}
}catch(Throwable $e){
echo 'Translation Files: Invalid PHP translation file at: '.$file;
exit(0);
}
}
}
function vsCodeGetTranslationsFromFile($file, $path, $namespace)
{
validateFile($file);
$fileLines = \\Illuminate\\Support\\Facades\\File::lines($file);
$lines = [];
$inComment = false;
Expand Down Expand Up @@ -200,6 +233,6 @@ foreach ($default->merge($namespaced) as $value) {

echo json_encode([
'default' => \\Illuminate\\Support\\Facades\\App::currentLocale(),
'translations' => $final,
'translations' => $final
]);
`;