Skip to content

Commit

Permalink
feat: introduce php parser
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 31, 2021
1 parent 73b4d69 commit 2510faa
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 12 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"parcel-bundler": "^1.12.4",
"parcel-plugin-inliner": "^1.0.14",
"php-array-parser": "^1.0.1",
"php-parser": "^3.0.2",
"pug": "^3.0.0",
"rimraf": "^3.0.2",
"standard-version": "^9.1.0",
Expand Down Expand Up @@ -233,6 +234,11 @@
"category": "%extname%",
"title": "%refactor.extract_text%"
},
{
"command": "i18n-ally.extract-current-file",
"category": "%extname%",
"title": "Extract current file"
},
{
"command": "i18n-ally.open-url",
"category": "%extname%",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/detectHardStrings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ExtensionModule } from '~/modules'
import { Global } from '~/core'
import { Commands } from '~/commands'

async function DetectHardStrings() {
export async function DetectHardStrings() {
const doc = window.activeTextEditor?.document

if (!doc)
Expand Down
5 changes: 3 additions & 2 deletions src/commands/extractCurrentFile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { commands } from 'vscode'
import { DetectHardStrings } from './detectHardStrings'
import { ExtensionModule } from '~/modules'
import { Commands } from '~/commands'

async function ExtractAllInFileCommand() {

export async function ExtractAllInFileCommand() {
await DetectHardStrings()
}

const m: ExtensionModule = () => {
Expand Down
1 change: 1 addition & 0 deletions src/frameworks/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ScopeRange {

export interface HardStringInfo {
range: Range
value: string
}

export abstract class Framework {
Expand Down
54 changes: 45 additions & 9 deletions src/frameworks/laravel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Range, TextDocument } from 'vscode'
import Parser, { Node } from 'php-parser'
import { Framework, HardStringInfo } from './base'
import { LanguageId } from '~/utils'

Expand Down Expand Up @@ -47,24 +48,59 @@ class LaravelFramework extends Framework {

supportAutoExtraction = true

_engine: Parser = undefined!

getHardStrings(doc: TextDocument) {
if (doc.languageId !== 'php')
return undefined

const text = doc.getText()
const strings: HardStringInfo[] = []

for (const match of text.matchAll(/["'](.*?)['"]/g)) {
if (!match || match.index == null)
continue
const start = match.index
const end = start + match[0].length

strings.push({
range: new Range(doc.positionAt(start), doc.positionAt(end)),
const engine = this._engine = this._engine || new Parser({
parser: {
extractDoc: true,
},
ast: {
withPositions: true,
},
})

const ast = engine.parseCode(text)

console.log('AST', ast)

function searchFor(name: string, node: any = ast): Node[] {
if (!node)
return []
if (node.kind === name)
return [node]
if (node.expression)
return searchFor(name, node.expression)
if (node.left || node.right) {
return [
...searchFor(name, node.left),
...searchFor(name, node.right),
]
}
if (!node?.children?.length)
return []
return node.children.flatMap((i: any) => {
return searchFor(name, i)
})
}

const stringNodes = searchFor('string')

console.log('STRINGS', stringNodes)

const strings: HardStringInfo[] = stringNodes.map((i: any) => ({
range: new Range(
doc.positionAt(i.loc.start.offset),
doc.positionAt(i.loc.end.offset),
),
value: i.value,
}))

return strings
}
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7450,6 +7450,11 @@ php-parser@^2.0.3:
resolved "https://registry.yarnpkg.com/php-parser/-/php-parser-2.2.0.tgz#67384f0a5933dbbef40beab0ab31d0b8c582ff88"
integrity sha1-ZzhPClkz2770C+qwqzHQuMWC/4g=

php-parser@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/php-parser/-/php-parser-3.0.2.tgz#a86dbbc110e57378cba71ab4cd9b0d18f3872ac3"
integrity sha512-a7y1+odEGsceLDLpu7oNyspZ0pK8FMWJOoim4/yd82AtnEZNLdCLZ67arnOQZ9K0lHJiSp4/7lVUpGELVxE14w==

physical-cpu-count@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/physical-cpu-count/-/physical-cpu-count-2.0.0.tgz#18de2f97e4bf7a9551ad7511942b5496f7aba660"
Expand Down

0 comments on commit 2510faa

Please sign in to comment.