Skip to content

Commit

Permalink
chore: apply prettier code style to the whole project (lingui#1398)
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko authored Feb 6, 2023
1 parent 31316f9 commit be527a5
Show file tree
Hide file tree
Showing 111 changed files with 1,599 additions and 1,300 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ jobs:
- name: Linting & Types
run: yarn lint:all

- name: Check Prettier Formatting
run: yarn prettier:check

- name: Build packages
run: yarn release:build

Expand Down
4 changes: 2 additions & 2 deletions jest.config.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
testPathIgnorePatterns: ["/node_modules/"],
// Redirect imports to the compiled bundles
moduleNameMapper: {},
setupFiles: ['set-tz/utc'],
setupFiles: ["set-tz/utc"],

// Exclude the build output from transforms
transformIgnorePatterns: ["/node_modules/", "<rootDir>/packages/*/build/"],
Expand All @@ -20,5 +20,5 @@ module.exports = {

haste: {
throwOnModuleCollision: false,
}
},
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"lint:types": "tsc",
"lint:eslint": "eslint './packages/**/*.{ts,tsx,js,jsx}'",
"lint:all": "yarn lint:eslint && yarn lint:types",
"prettier": "prettier --write '**/*.{ts,tsx,js,jsx}'",
"prettier:check": "prettier --check '**/*.{ts,tsx,js,jsx}'",
"release:build": "node -r @swc-node/register ./scripts/build/index.ts",
"release:test": "node -r @swc-node/register ./scripts/test.ts",
"version:next": "lerna version --exact --force-publish --no-private --preid next --create-release github --conventional-commits --conventional-prerelease --yes",
Expand Down
112 changes: 73 additions & 39 deletions packages/babel-plugin-extract-messages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,65 +35,80 @@ function addMessage(
"Different defaults for the same message ID."
)
}

if (newDefault) {
message.message = newDefault
}

;[].push.apply(message.origin, origin)

if (comment) {
;[].push.apply(message.extractedComments, [comment])
}
} else {
existingContext.set(id, { ...props, message: newDefault, origin, extractedComments })
existingContext.set(id, {
...props,
message: newDefault,
origin,
extractedComments,
})
messages.set(context, existingContext)
}
} else {
const newContext = new Map();
newContext.set(id, { ...props, message: newDefault, origin, extractedComments })
const newContext = new Map()
newContext.set(id, {
...props,
message: newDefault,
origin,
extractedComments,
})
messages.set(context, newContext)
}
} else {
if (messages.has(id)) {
const message = messages.get(id)

// only set/check default language when it's defined.
if (message.message && newDefault && message.message !== newDefault) {
throw path.buildCodeFrameError(
"Different defaults for the same message ID."
)
}

if (newDefault) {
message.message = newDefault
}

;[].push.apply(message.origin, origin)
if (comment) {
;[].push.apply(message.extractedComments, [comment])
}
} else {
messages.set(id, { ...props, message: newDefault, origin, extractedComments })
messages.set(id, {
...props,
message: newDefault,
origin,
extractedComments,
})
}
}
}

/**
* An ES6 Map type is not possible to encode with JSON.stringify,
* so we can instead use a replacer function as an argument to
* so we can instead use a replacer function as an argument to
* tell the JSON parser how to serialize / deserialize the Maps
* it encounters.
*/
function mapReplacer(key, value) {
if (value instanceof Map) {
const object = {}
value.forEach((v, k) => {
return object[k] = v;
});
return object;
return (object[k] = v)
})
return object
}
return value;
return value
}

function extractStringContatentation(t, node, error): string {
Expand All @@ -112,7 +127,7 @@ function extractStringContatentation(t, node, error): string {
function extractCommentString(t, path, valuePath, valueObj): string {
if (t.isStringLiteral(valueObj)) {
// Comment is a single line string
return valueObj.value;
return valueObj.value
}

// Comment is a multi-line string.
Expand All @@ -121,11 +136,7 @@ function extractCommentString(t, path, valuePath, valueObj): string {
.buildCodeFrameError("Only strings are supported as comments.")

if (t.isBinaryExpression(valueObj)) {
return extractStringContatentation(
t,
valueObj,
errorIfNotAString
)
return extractStringContatentation(t, valueObj, errorIfNotAString)
} else {
throw errorIfNotAString
}
Expand Down Expand Up @@ -202,7 +213,12 @@ export default function ({ types: t }) {

const props = attrs.reduce((acc, item) => {
const key = item.name.name
if (key === "id" || key === "message" || key === "comment" || key === "context") {
if (
key === "id" ||
key === "message" ||
key === "comment" ||
key === "context"
) {
if (item.value.value) {
acc[key] = item.value.value
} else if (
Expand Down Expand Up @@ -242,29 +258,38 @@ export default function ({ types: t }) {
)
}
)

const isNonMacroI18n = isI18nMethod(path.node.callee) && !hasComment && path.node.arguments[0] && !path.node.arguments[0].leadingComments;
if (!hasComment && !isNonMacroI18n) return;

const isNonMacroI18n =
isI18nMethod(path.node.callee) &&
!hasComment &&
path.node.arguments[0] &&
!path.node.arguments[0].leadingComments
if (!hasComment && !isNonMacroI18n) return
const props = {
id: path.node.arguments[0].value
};
id: path.node.arguments[0].value,
}

if (!props.id) {
console.warn("Missing message ID, skipping.");
console.warn("Missing message ID, skipping.")
console.warn(generate(path.node).code)
return;
return
}

const copyOptions = ["message", "comment", "context"]

if (t.isObjectExpression(path.node.arguments[2])) {
path.node.arguments[2].properties.forEach(({key, value}, i) => {
path.node.arguments[2].properties.forEach(({ key, value }, i) => {
if (!copyOptions.includes(key.name)) return

let valueToExtract = value.value;
let valueToExtract = value.value

if (key.name === "comment") {
valueToExtract = extractCommentString(t, path, `arguments.2.properties.${i}.value`, value);
valueToExtract = extractCommentString(
t,
path,
`arguments.2.properties.${i}.value`,
value
)
}

props[key.name] = valueToExtract
Expand Down Expand Up @@ -325,17 +350,22 @@ export default function ({ types: t }) {
.filter(({ key }) => copyProps.indexOf(key.name) !== -1)
.forEach(({ key, value }, i) => {
// By default, the value is just the string value of the object property.
let valueToExtract = value.value;
let valueToExtract = value.value

if (key.name === "comment") {
valueToExtract = extractCommentString(t, path, `properties.${i}.value`, value);
valueToExtract = extractCommentString(
t,
path,
`properties.${i}.value`,
value
)
} else if (key.name === "id") {
const isIdLiteral = !value.value && t.isTemplateLiteral(value)
if (isIdLiteral) {
valueToExtract = value?.quasis[0]?.value?.cooked;
}
const isIdLiteral = !value.value && t.isTemplateLiteral(value)
if (isIdLiteral) {
valueToExtract = value?.quasis[0]?.value?.cooked
}
}
props[key.name] = valueToExtract;
props[key.name] = valueToExtract
})

collectMessage(path, file, props)
Expand All @@ -349,7 +379,11 @@ export default function ({ types: t }) {
// Config was already validated in CLI.
file.set(
CONFIG,
getConfig({ cwd: file.opts.filename, skipValidation: true, configPath: this.opts.configPath })
getConfig({
cwd: file.opts.filename,
skipValidation: true,
configPath: this.opts.configPath,
})
)

// Ignore else path for now. Collision is possible if other plugin is
Expand Down
22 changes: 11 additions & 11 deletions packages/babel-plugin-extract-messages/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ function testCase(testName, assertion) {
configFile: false,
plugins: [
"@babel/plugin-syntax-jsx",
["macros", {
// macro plugin uses package `resolve` to find a path of macro file
// this will not follow jest pathMapping and will resolve path from ./build
// instead of ./src which makes testing & developing hard.
// here we override resolve and provide correct path for testing
resolvePath: (source: string) => require.resolve(source)
}],
[
"macros",
{
// macro plugin uses package `resolve` to find a path of macro file
// this will not follow jest pathMapping and will resolve path from ./build
// instead of ./src which makes testing & developing hard.
// here we override resolve and provide correct path for testing
resolvePath: (source: string) => require.resolve(source),
},
],
[
plugin,
{
Expand Down Expand Up @@ -119,10 +122,7 @@ describe("@lingui/babel-plugin-extract-messages", function () {
).toBeFalsy()
})

testCase(
"should handle duplicate ids",
"duplicate-id-valid.js"
)
testCase("should handle duplicate ids", "duplicate-id-valid.js")

testCase(
"should extract all messages from JSX files",
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/api/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,9 @@ describe("getCatalogForFile", function () {
)
const catalogs = [catalog]

expect(getCatalogForFile("./src/locales/en/messages_en.po", catalogs)).toEqual({
expect(
getCatalogForFile("./src/locales/en/messages_en.po", catalogs)
).toEqual({
locale: "en",
catalog,
})
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/api/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ export class Catalog {
})

// Map over all locales and post-process each catalog
const cleanAndSort = (R.map(
const cleanAndSort = R.map(
R.pipe(
// Clean obsolete messages
options.clean ? cleanObsolete : R.identity,
// Sort messages
order(options.orderBy)
)
) as unknown) as (catalog: AllCatalogsType) => AllCatalogsType
) as unknown as (catalog: AllCatalogsType) => AllCatalogsType

const sortedCatalogs = cleanAndSort(catalogs)

Expand Down
Loading

0 comments on commit be527a5

Please sign in to comment.