Skip to content

Commit

Permalink
fix: 修正轉換時 constructor 變成 function Object() { [native code] } 的 BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
bluelovers committed Feb 20, 2022
1 parent 41b5d76 commit 731e5fd
Show file tree
Hide file tree
Showing 7 changed files with 594 additions and 64 deletions.
99 changes: 70 additions & 29 deletions lib/build/properties-replace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import { outputJSON } from 'fs-extra';
import { createSingleBar } from '../cli-progress';
import { textIncludeCJK } from '../util/include-cjk';
import { sortObjectKeys } from 'sort-object-keys2';
import { parse, ITree, stringify, IStringifyOptions, ILine, parseLines } from 'dot-properties2';

export const defaultPropertiesStringifyOptions: IStringifyOptions = Object.freeze({
latin1: false,
keySep: '=',
});

export const LAZY_PROPERTIES = new DotProperties({
file: __dict_properties_lazy,
Expand Down Expand Up @@ -48,40 +54,17 @@ export function replaceProperties(lang: string | 'zh')
.mapSeries(async (file: string, index) =>
{
bar?.update(index, { filename: file });
const fullpath = join(cwd, file);

const dp = new DotProperties({
file: fullpath,
});

let _changed = false;

const tree = dp.tree;

LAZY_PROPERTIES_KEYS
.forEach(key =>
{

if (key in tree)
{
dp.set(key, LAZY_PROPERTIES.get(key) as any);

_changed = true;
}

})
;
const {
fullpath,
dp,
_changed,
} = handlePropertiesFile(file, cwd)

if (_changed)
{
changedList.push(file);
dp.save({
file: fullpath,
options: {
latin1: false,
keySep: '=',
},
});
saveProperties(dp, fullpath);
}

for (let key in dp.tree)
Expand Down Expand Up @@ -119,3 +102,61 @@ export function replaceProperties(lang: string | 'zh')
})
;
}

export function _handlePropertiesFileCore(dp: DotProperties)
{
let _changed = false;

const tree = dp.tree;

LAZY_PROPERTIES_KEYS
.forEach(key =>
{

if (key in tree)
{
dp.set(key, LAZY_PROPERTIES.get(key) as any);

_changed = true;
}

})
;

return {
dp,
_changed,
}
}

export function handlePropertiesFile(file: string, cwd: string)
{
const fullpath = join(cwd, file);

const dp = new DotProperties({
file: fullpath,
});

const { _changed } = _handlePropertiesFileCore(dp);

return {
file,
cwd,
fullpath,
dp,
_changed,
}
}

export function stringifyProperties(dp: DotProperties)
{
return dp.stringify(defaultPropertiesStringifyOptions)
}

export function saveProperties(dp: DotProperties, fullpath: string)
{
return dp.save({
file: fullpath,
options: defaultPropertiesStringifyOptions,
})
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@
"markdown-it": "^12.3.2",
"micromatch": "^4.0.4",
"moment": "^2.29.1",
"novel-segment": "^2.7.89",
"novel-segment-cli": "^1.1.107",
"novel-segment": "^2.7.90",
"novel-segment-cli": "^1.1.108",
"progress-estimator": "^0.3.0",
"regexp-cjk": "^3.3.106",
"regexp-cjk-with-plugin-enabled": "^1.0.12",
Expand Down
441 changes: 441 additions & 0 deletions test/__snapshots__/properties.spec.ts.snap

Large diffs are not rendered by default.

20 changes: 1 addition & 19 deletions test/handle-text.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { readFile } from 'fs-extra';
import { join } from 'upath2';
import { __plugin_dev_raw_dir, __plugin_downloaded_dir_unzip } from '../lib/const';
import { cn2tw_min } from '@lazy-cjk/zh-convert/min';
import { cn2tw } from '@lazy-cjk/zh-convert';
import { array_unique_overwrite } from 'array-hyper-unique';
import { handleText } from '../lib/handleText';
import { zhRegExpWithPluginEnabled } from 'regexp-cjk-with-plugin-enabled';
import escapeStringRegexp from 'escape-string-regexp';
import { textToRegexp } from '../lib/util/text-to-regexp';
import { ITSArrayListMaybeReadonly, ITSValueOrArrayMaybeReadonly } from 'ts-type/lib/type/base';
import { handleFile } from './lib/handle.file';

jest.setTimeout(60 * 1000);

Expand Down Expand Up @@ -91,16 +86,3 @@ function _doTests([file, words, not]: ITestInput, cwd: string)
})
}

export async function handleFile(file: string, cwd: string)
{
const text = await readFile(join(cwd, file)).then(m => m.toString());

const actual = await handleText(text, {
file,
});

return {
text,
actual,
}
}
22 changes: 22 additions & 0 deletions test/lib/handle.file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { readFile } from 'fs-extra';
import { join } from 'upath2';
import { handleText } from '../../lib/handleText';

if (typeof jest !== 'undefined')
{
jest.setTimeout(60 * 1000);
}

export async function handleFile(file: string, cwd: string)
{
const text = await readFile(join(cwd, file)).then(m => m.toString());

const actual = await handleText(text, {
file,
});

return {
text,
actual,
}
}
44 changes: 44 additions & 0 deletions test/properties.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//@noUnusedParameters:false

import { basename, extname } from 'path';
import { handlePropertiesFile, stringifyProperties } from '../lib/build/properties-replace';
import { __plugin_downloaded_dir_unzip } from '../lib/const';
import { join } from 'upath2';
import { handleFile } from './lib/handle.file';

describe(`messages/AnalysisBundle.properties`, () =>
{
const file = `messages/AnalysisBundle.properties`;
const cwd = join(__plugin_downloaded_dir_unzip, 'zh');

test(`handlePropertiesFile`, () =>
{

let {
dp,
} = handlePropertiesFile(file, cwd);

const actual = stringifyProperties(dp);

_chk(actual);

});

test(`handleFile`, async () =>
{

let { actual } = await handleFile(file, cwd);

_chk(actual);

});

function _chk(actual: string)
{
expect(actual).toContain(`inspection.dead.code.problem.synopsis29.constructor`);
expect(actual).not.toContain(`[native code]`);
expect(actual).toMatchSnapshot();
}

})

28 changes: 14 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2800,10 +2800,10 @@ normalize-url@^4.1.0:
resolved "https://registry.npmjs.org/normalize-url/-/normalize-url-4.5.1.tgz#0dd90cf1288ee1d1313b87081c9a5932ee48518a"
integrity sha512-9UZCFRHQdNrfTpGg8+1INIg93B6zE0aXMVFkw1WFwvO4SlZywU6aLg5Of0Ap/PgcbSw4LNxvMWXMeugwMCX0AA==

novel-segment-cli@^1.1.107:
version "1.1.107"
resolved "https://registry.npmjs.org/novel-segment-cli/-/novel-segment-cli-1.1.107.tgz#62b34d334d83cf62f03110c29991fbedfbe961b9"
integrity sha512-aVO4pySo6SWrsn2Zy4yoOfRfMUZ1ZTGVd/kyTsb2u4n55rPBqSXSbsyCDyVE+zzD5DJbddL5s8LKwmgKkf3P8g==
novel-segment-cli@^1.1.108:
version "1.1.108"
resolved "https://registry.npmjs.org/novel-segment-cli/-/novel-segment-cli-1.1.108.tgz#2ed51af664963d85c1478de5e5f0dfdd9aa606f5"
integrity sha512-O8brCm/xDPZymPHRFuJREXbv1OZ9SwzMuk6Yt6hAJzzidFXXmtMSH/zRrbCleijti6PIFM5U1ee1r68GjoaP4g==
dependencies:
"@bluelovers/fast-glob" "^3.0.4"
"@novel-segment/types" "^1.0.3"
Expand All @@ -2818,18 +2818,18 @@ novel-segment-cli@^1.1.107:
iconv-jschardet "^2.0.24"
lazy-cacache "^2.0.4"
lodash "^4.17.21"
novel-segment "^2.7.89"
novel-segment "^2.7.90"
pkg-up "<4 >= 3.1.0"
prettyuse "^0.0.0"
ts-type "^2.1.4"
tslib "^2.3.1"
upath2 "^3.1.12"
yargs "^17"

novel-segment@^2.7.89:
version "2.7.89"
resolved "https://registry.npmjs.org/novel-segment/-/novel-segment-2.7.89.tgz#a397b56e5f9e22a1192abf2b7f7965e12388d1a4"
integrity sha512-QIbi9dOWDnyfFibpweLvNwgzsFRNO+I4/eOz1OXuNSfh8vprfTzuiLmc3lUUUpVEHiSygvJScgosOOpcl8CyFw==
novel-segment@^2.7.90:
version "2.7.90"
resolved "https://registry.npmjs.org/novel-segment/-/novel-segment-2.7.90.tgz#eab2aa6844d4ad37728ebfd1a31d3054ae2b0ec9"
integrity sha512-BYYQ0nDpXU8JcpKMSKz3oUeKZh6LKW/AZ7dS+zxIgcAS1PyuTy1g4GGYDk/vGpTKKva7sCV47minT/tgGehqQA==
dependencies:
"@bluelovers/fast-glob" "^3.0.4"
"@lazy-cjk/zh-table-list" "^1.0.80"
Expand All @@ -2844,7 +2844,7 @@ novel-segment@^2.7.89:
deepmerge-plus "^2.1.3"
lodash "^4.17.21"
regexp-cjk "^3.3.106"
segment-dict "^2.3.170"
segment-dict "^2.3.171"
sort-object-keys2 "^2.0.3"
str-util "^2.3.28"
ts-enum-util "4.0.2"
Expand Down Expand Up @@ -3513,10 +3513,10 @@ safe-buffer@~5.2.0:
resolved "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==

segment-dict@^2.3.170:
version "2.3.170"
resolved "https://registry.npmjs.org/segment-dict/-/segment-dict-2.3.170.tgz#1948a72764c9fae09d757ab9c998fc243d559891"
integrity sha512-194rbqcZnOhTArWUhEh2/1lMVrB/xGsRJsJIfhAvSB2arGfioCqgx7CmmmXfv+9Nzmiiq9EO2BsRyHp+7soMNA==
segment-dict@^2.3.171:
version "2.3.171"
resolved "https://registry.npmjs.org/segment-dict/-/segment-dict-2.3.171.tgz#5c26c3d4c9237001725bef1229721ec06a112064"
integrity sha512-zus001lMcJMkN6/0GpYJfcWX44dZ4sdgChoa7NrzvyxWseCNEmdk8O89MnVC3moe3Ypf67PUBJDX86zpmDHlFw==
dependencies:
"@bluelovers/fast-glob" "^3.0.4"
"@novel-segment/dict-loader-core" "^1.0.15"
Expand Down

0 comments on commit 731e5fd

Please sign in to comment.