-
-
Notifications
You must be signed in to change notification settings - Fork 216
feat: HTML lexer supports vue bind attributes #1094
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
Open
PBK-B
wants to merge
6
commits into
i18next:master
Choose a base branch
from
PBK-B:feat_vue_bind_attr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e3337e9
feat: html lexer supports vue binding attributes #617
PBK-B e591c9f
chore: add test case for supports vue bind attributes
PBK-B 943c592
chore: add document for vue
PBK-B 60a4c86
fix: use more explicit `v-bind` syntax
PBK-B 203c392
feat: add vue-template-lexer #617
PBK-B 49cd876
chore(vue-template-lexer): modify `computed properties` misunderstand…
PBK-B File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * @Author: Bin | ||
| * @Date: 2025-01-04 | ||
| * @FilePath: /i18next-parser/src/lexers/vue-template-lexer.js | ||
| */ | ||
| import HTMLLexer from './html-lexer.js' | ||
| import JavascriptLexer from './javascript-lexer.js' | ||
| import * as cheerio from 'cheerio' | ||
|
|
||
| export default class VueTemplateLexer extends HTMLLexer { | ||
| constructor(options = {}) { | ||
| super(options) | ||
| this.vueBindAttr = options.vueBindAttr || true | ||
| this.jsLexer = new JavascriptLexer(options) | ||
| } | ||
|
|
||
| extract(content, filename = '__default.vue') { | ||
| const $ = cheerio.load(content) | ||
| const jsLexer = this.jsLexer | ||
| let keys = [] | ||
|
|
||
| // use HTMLLexer | ||
| const HTMLkeys = super.extract(content) | ||
| keys.push(...HTMLkeys) | ||
|
|
||
| // use JavascriptLexer | ||
| const JSKeys = jsLexer.extract(content, filename) | ||
| keys.push(...JSKeys) | ||
|
|
||
| // support vue bind attribute | ||
| if (this.vueBindAttr) { | ||
| // traverse all tags to filter bind attribute | ||
| $('*').each((_, node) => { | ||
| const attributes = node.attributes.filter( | ||
| (item) => item.name.startsWith(':') || item.name.startsWith('v-bind:') | ||
| ) | ||
| if (attributes.length > 0) { | ||
| // there are computed properties, it may have simple javascript logic | ||
| attributes.forEach((content) => { | ||
| const items = jsLexer.extract(content.value, filename) | ||
| keys.push(...items) | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| return keys | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * @Author: Bin | ||
| * @Date: 2025-01-04 | ||
| * @FilePath: /i18next-parser/test/lexers/vue-template-lexer.test.js | ||
| */ | ||
| import { assert } from 'chai' | ||
| import VueTemplateLexer from '../../src/lexers/vue-template-lexer.js' | ||
|
|
||
| describe('VueTemplateLexer', () => { | ||
| it('supports vue bind attributes', (done) => { | ||
| const Lexer = new VueTemplateLexer({ | ||
| functions: ['t', '$t'], | ||
| vueBindAttr: true, | ||
| }) | ||
| const content = ` | ||
| <template> | ||
| <button :aria-label="t('b_a_l', 'button aria label')"> | ||
| button label | ||
| </button> | ||
| <button v-bind:aria-label="$t('button v-bind aria label')"> | ||
| button label form v-bind | ||
| </button> | ||
| </template> | ||
| ` | ||
| assert.deepEqual(Lexer.extract(content), [ | ||
| { key: 'b_a_l', defaultValue: 'button aria label' }, | ||
| { key: 'button v-bind aria label' }, | ||
| ]) | ||
| done() | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Why are you extending the HTMLLexer and then compose the jsLexer as well. For better maintainability, shouldn't it be extending from the BaseLexer and compose both the JSLexer and the HTMLLexer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@geertklop Yes, by expanding BaseLexer, you can theoretically get better maintainability, but you will eventually find that it is actually a re-realization of HTMLLexer and JSLexer (excellent template syntax 🐶)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think that with the maintainability, I would expect to extend from BaseLexer. However, curious what regular maintainers like @karellm think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@geertklop If you want, you can complete the follow-up work based on my branch