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

Feature: translate descriptions #710

Open
wants to merge 30 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
74cf278
feat: start docgen i18n
elevatebart Nov 16, 2019
24dc651
feat(i18n): create traverse and getOrCreate
elevatebart Dec 24, 2019
a8fe12f
feat: add a little architecture in the mix
elevatebart Dec 24, 2019
bd5ea23
feat: make it all work with js traverse
elevatebart Dec 25, 2019
f4ad161
feat: make generateTrans
elevatebart Dec 28, 2019
744125f
fix: simplify it all
elevatebart Dec 28, 2019
81ffb3c
refactor: setatpath
elevatebart Dec 28, 2019
60daa68
test: add test
elevatebart Mar 13, 2020
8430c99
fix: allow to only specify validExtends
elevatebart Mar 14, 2020
d923a06
fix(i18n): make bin file and first tests
elevatebart Mar 16, 2020
881e128
test(i18n): move tests in e2e
elevatebart Mar 16, 2020
29777e4
feat: add errors in binary
elevatebart Mar 19, 2020
6e783f0
fix: add name the translation objects
elevatebart Mar 19, 2020
77d4bfa
test: translation in the API
elevatebart Mar 19, 2020
5572d02
feat: allow multiple componenet trans
elevatebart Mar 19, 2020
db41744
feat: add the translation option
elevatebart Mar 19, 2020
e3643b8
test: add mergeTranslations tests
elevatebart Mar 19, 2020
a313b7b
build: remane one
elevatebart Mar 19, 2020
318ef37
build: rename 2
elevatebart Mar 19, 2020
cd10b5e
build: fix build
elevatebart Oct 12, 2020
f337511
Merge branch 'dev' into feat-translate
elevatebart Oct 12, 2020
03f593e
Merge branch 'dev' into feat-translate
elevatebart Oct 16, 2022
7ab6f9d
fix build
elevatebart Oct 16, 2022
6c28eb1
fix all tests
elevatebart Oct 16, 2022
679744f
fix bin for i18n
elevatebart Oct 17, 2022
914e38e
Maybe this would work ?
elevatebart Oct 17, 2022
7735175
make sure the tests are running
elevatebart Oct 17, 2022
a6b1ac5
fix test
elevatebart Oct 17, 2022
e643117
add comment
elevatebart Oct 17, 2022
8f34739
update vue-component-meta
elevatebart Oct 24, 2022
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 examples/docgen-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"typescript": "~4.7.4",
"vite": "^2.9.14",
"vitepress": "^1.0.0-alpha.13",
"vue-component-meta": "^0.40.13",
"vue-component-meta": "^1.0.8",
"vue-docgen-cli": "^4",
"vue-live": "^2",
"vue-tsc": "^0.38.8"
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"vue": "2.7.11",
"vue-cli-plugin-docgen": "*",
"vue-cli-plugin-styleguidist": "*",
"vue-docgen-i18n": "*",
"vue-inbrowser-compiler-sucrase": "*",
"vue-property-decorator": "9.1.2",
"vue-server-renderer": "2.7.11",
Expand Down Expand Up @@ -158,4 +159,4 @@
"[email protected]": "patches/[email protected]"
}
}
}
}
4 changes: 3 additions & 1 deletion packages/vue-docgen-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
"lru-cache": "^4.1.5",
"pug": "^3.0.2",
"recast": "0.21.5",
"traverse": "^0.6.6",
"ts-map": "^1.0.3",
"vue-inbrowser-compiler-independent-utils": "^4.52.0"
},
"devDependencies": {
"@test-vue-docgen/mixins": "^4.44.15",
"@types/dedent": "0.7.0",
"@types/pug": "2.0.6",
"@types/traverse": "^0.6.32",
"dedent": "0.7.0",
"globby": "10.0.2",
"vue": "2.7.11",
Expand All @@ -53,4 +55,4 @@
"vue-property-decorator": "7.3.0",
"vuetify": "2.6.10"
}
}
}
17 changes: 12 additions & 5 deletions packages/vue-docgen-api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ import Documentation, {
ParamTag,
ParamType
} from './Documentation'
import { DocGenOptions, parseFile, ParseOptions, parseSource as _parseSource } from './parse'
import { DocGenOptions as DCOptions, parseFile, ParseOptions, parseSource as _parseSource } from './parse'
import * as ScriptHandlers from './script-handlers'
import * as TemplateHandlers from './template-handlers'
import mergeTranslations from './mergeTranslations'

export { ScriptHandlers }
export { TemplateHandlers }

export interface DocGenOptions extends DCOptions {
translation?: string
}

export { TemplateParserOptions } from './parse-template'
export { ScriptHandler, TemplateHandler } from './parse'
export {
ComponentDoc,
DocGenOptions,
ParseOptions,
Documentation,
BlockTag,
Expand Down Expand Up @@ -77,8 +81,9 @@ export async function parseSource(
function isOptionsObject(opts: any): opts is DocGenOptions {
return (
!!opts &&
(!!opts.alias ||
opts.jsx !== undefined ||
(opts.jsx !== undefined ||
!!opts.alias ||
!!opts.validExtends ||
!!opts.addScriptHandlers ||
!!opts.addTemplateHandlers)
)
Expand All @@ -101,5 +106,7 @@ async function parsePrimitive(
validExtends: (fullFilePath: string) => !/[\\/]node_modules[\\/]/.test(fullFilePath)
}
const docs = await createDocs(options)
return docs.map(d => d.toObject())
return docs
.map(d => d.toObject())
.map(d => (opts && opts.translation ? mergeTranslations(d, filePath, opts.translation) : d))
}
68 changes: 68 additions & 0 deletions packages/vue-docgen-api/src/mergeTranslations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { expect } from 'vitest'
import { mergeTranslationsAsObject } from './mergeTranslations'

const trad = [
{
name: 'default',
description: "un example de traduction d'un composant",
props: [
{
name: 'size',
description: 'taille du bouton'
},
{
name: 'span',
description: 'nombre de colomnes'
}
]
}
]

const orig = {
exportName: 'default',
displayName: 'origin',
tags: {},
description:
'This is an example of creating a reusable button component and using it with external data.',
props: [
{
name: 'span',
description: 'Number of columns (1-12) the column should span.'
},
{
name: 'spanSm',
description: 'Sm breakpoint and above'
},
{
name: 'size',
description: 'The size of the button'
}
]
}

describe('mergeTranslationsAsObject', () => {
it('should match the object', () => {
expect(mergeTranslationsAsObject(orig, trad)).toMatchInlineSnapshot(`
{
"description": "un example de traduction d'un composant",
"displayName": "origin",
"exportName": "default",
"props": [
{
"description": "nombre de colomnes",
"name": "span",
},
{
"description": "Sm breakpoint and above",
"name": "spanSm",
},
{
"description": "taille du bouton",
"name": "size",
},
],
"tags": {},
}
`)
})
})
61 changes: 61 additions & 0 deletions packages/vue-docgen-api/src/mergeTranslations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as path from 'path'
import traverse from 'traverse'
import { ComponentDoc } from './Documentation'

export function getTranslationsObjectFromFile(fullFilePath: string, translation: string) {
const transFileName = `${path.basename(fullFilePath).replace(/\.\w+$/, '')}.${translation}.js`
return require(path.join(path.dirname(fullFilePath), transFileName))
}

export function mergeTranslationsAsObject(docs: ComponentDoc, transObjects: any[]): ComponentDoc {
const transObject = transObjects.find((tr: { name: string }) => tr.name === docs.exportName)
traverse(transObject).forEach(function(traversedItem) {
if (!traversedItem.description) {
return
}
// if the element is in array
if (this.key && !isNaN(parseInt(this.key, 10)) && this.parent) {
if (!traversedItem.name) return
// find path of array
const arrayPath = this.parent.path
// get element in doc
let i = 0
let obj = docs
while (i < arrayPath.length) {
obj = obj[arrayPath[i++]]
// if element does not exist quit
if (!obj) return
}
const arrayOfProps = obj
const prop = arrayOfProps.find((p: any) => p.name === traversedItem.name)
// update its description with the one provided
if (traversedItem.description && prop) {
prop.description = traversedItem.description
}
} else {
const arrayPath = this.path
// get element in doc
let i = 0
let obj = docs
while (i < arrayPath.length) {
obj = obj[arrayPath[i++]]
// if element does not exist quit
if (!obj) return
}
// update its description with the one provided
if (traversedItem.description) {
obj.description = traversedItem.description
}
}
})
return docs
}

export default function mergeTranslations(
docs: ComponentDoc,
fullFilePath: string,
translation: string
): ComponentDoc {
const transObjects = getTranslationsObjectFromFile(fullFilePath, translation)
return mergeTranslationsAsObject(docs, transObjects)
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('slotHandler', () => {
)
traverse(ast.children[0], doc, [slotHandler], ast.children, { functional: false })
const slots = doc.toObject().slots || []
expect(slots.filter(s => s.name === 'bound')[0].bindings).toMatchObject([
expect(slots.filter((s: any) => s.name === 'bound')[0].bindings).toMatchObject([
{
name: 'v-bind'
}
Expand All @@ -198,7 +198,7 @@ describe('slotHandler', () => {
)
traverse(ast.children[0], doc, [slotHandler], ast.children, { functional: false })
const slots = doc.toObject().slots || []
expect(slots.filter(s => s.name === 'bound')[0].bindings).toMatchObject([
expect(slots.filter((s: any) => s.name === 'bound')[0].bindings).toMatchObject([
{
name: 'item'
},
Expand All @@ -223,7 +223,7 @@ describe('slotHandler', () => {
)
traverse(ast.children[0], doc, [slotHandler], ast.children, { functional: false })
const slots = doc.toObject().slots || []
expect(slots.filter(s => s.name === 'bound')[0].bindings).toMatchObject([
expect(slots.filter((s: any) => s.name === 'bound')[0].bindings).toMatchObject([
{
name: 'item',
description: 'menu item'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default async function documentRequiredComponents(
for (const varName of Object.keys(varToFilePath)) {
const { filePath, exportName } = varToFilePath[varName]
filePath.forEach(p => {
if (!opt.validExtends(p)) return
const fullFilePath = pathResolver(p)
if (fullFilePath && opt.validExtends(fullFilePath)) {
const vars = files.get(fullFilePath) || []
Expand Down Expand Up @@ -88,6 +89,7 @@ async function enrichDocumentation(
// long as we follow the variables order
await Promise.all(
filePath.map(async p => {
if (!opt.validExtends(p)) return
const fullFilePath = pathResolver(p)

if (fullFilePath && opt.validExtends(fullFilePath)) {
Expand Down
2 changes: 2 additions & 0 deletions packages/vue-docgen-api/src/utils/recursiveResolveIEV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ export async function resolveIEV(
}
})
try {
if (!validExtends(filePath)) return
const fullFilePath = pathResolver(filePath)
if (!fullFilePath || !validExtends(fullFilePath)) {
return
}

const source = await read(fullFilePath, {
encoding: 'utf-8'
})
Expand Down
92 changes: 92 additions & 0 deletions packages/vue-docgen-api/tests/components/button/Button.FR.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
module.exports = [
{
name: 'default',
/* @orig: This is an example of creating a reusable button component and using it with external data.*/
description:
'This is an example of creating a reusable button component and using it with external data.',
props: [
{
name: 'span',
/* @orig: Number of columns (1-12) the column should span.*/
description: 'Number of columns (1-12) the column should span.'
},
{
name: 'spanSm',
/* @orig: Sm breakpoint and above*/
description: 'Sm breakpoint and above'
},
{
name: 'size',
/* @orig: The size of the button*/
description: 'taille du bouton'
},
{
name: 'spanMd',
/* @orig: Md breakpoint and above*/
description: 'Md breakpoint and above'
},
{
name: 'example',
/* @orig: The example props*/
description: 'The example props'
},
{
name: 'v-model',
/* @orig: Model example2*/
description: 'Model example2'
},
{
name: 'example3',
/* @orig: The example3 props*/
description: 'The example3 props'
},
{
name: 'onCustomClick',
/* @orig: Add custom click actions.*/
description: 'Add custom click actions.'
},
{
name: 'funcDefault',
/* @orig: Function default*/
description: 'Function default'
},
{
name: 'propE',
/* @orig: Object or array defaults must be returned from
a factory function*/
description: 'Object or array defaults must be returned from\na factory function'
}
],
events: [
{
name: 'success',
/* @orig: Success event.*/
description: 'Success event.',
properties: [
{
name: 'demo',
/* @orig: example*/
description: 'example'
},
{
name: 'called',
/* @orig: test called*/
description: 'test called'
},
{
name: 'isPacked',
/* @orig: Indicates whether the snowball is tightly packed.*/
description: 'Indicates whether the snowball is tightly packed.'
}
]
}
],
slots: [
{
name: 'default',
/* @orig: Use this slot default*/
description: 'Use this slot default'
}
]
}
]
Loading