Skip to content

Commit

Permalink
Merge branch 'main' of github.com:thx/gogocode
Browse files Browse the repository at this point in the history
  • Loading branch information
yexi-xf committed Jul 11, 2022
2 parents 502610c + 82c5664 commit 6cc1b66
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 44 deletions.
2 changes: 1 addition & 1 deletion packages/gogocode-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const transform = require('./src/commands/transform');
.option('-o, --out <path>', 'output file path, if not input use src path instead')
.option('-s, --src <path>', 'source file path')
.option('-d, --dry', 'dry run (no changes are made to files)')
.option('-p, --params <key=value>','params direct to plugin, eg: format=true&test=false')
.option('-p, --params <key=value>','params direct to plugin, eg: format=true#test=false, use \'#\' to join params')
.option('-i, --info', 'show transform log info')
.action((options) => {
transform(options).then(() => { console.log(); }).catch(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/gogocode-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gogocode-cli",
"version": "0.2.26",
"version": "0.2.27",
"description": "gogocode-cli tools",
"bin": {
"gogocode": "index.js"
Expand All @@ -26,7 +26,7 @@
"commander": "^7.1.0",
"cross-spawn": "^7.0.3",
"fs-extra": "^9.1.0",
"gogocode": "^1.0.48",
"gogocode": "^1.0.53",
"inquirer": "^8.1.0",
"progress": "^2.0.3",
"semver": "^7.3.5"
Expand Down
2 changes: 1 addition & 1 deletion packages/gogocode-cli/src/commands/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ function paramsToOptions(params, options) {
if (!params) {
return;
}
const arr = params.split('&');
const arr = params.split('#');
arr.forEach(kv => {
const kvArr = kv.split('=');
if (kvArr.length === 1) {
Expand Down
2 changes: 1 addition & 1 deletion packages/gogocode-core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ declare module 'gogocode' {
* @param selector 代码选择器,可以是代码也可以将代码中的部分内容挖空替换为通配符
* @param options
*/
find(selector: Selector, options?: FindOption): GoGoAST;
find(selector: Selector | string[], options?: FindOption): GoGoAST;
/**
* 获取某个父节点
* @param level 自内向外第n层父元素,默认值 0
Expand Down
164 changes: 125 additions & 39 deletions packages/gogocode-plugin-vue/src/filters.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
const scriptUtils = require('../utils/scriptUtils');
const _ = require('lodash');

function callSeq(scopedFilterList, arr, index) {
const item = arr[index];
const name = item.name;
const args = item.args;
if (index === arr.length - 1) {
return name;
}

const filterName = scopedFilterList.includes(name) ? `${name}_filter` : `$filters.${name}`;

return `${filterName}(${callSeq(scopedFilterList, arr, index + 1)}${args.length ? `, ${args.join(', ')}` : ''})`;
}

module.exports = function (ast, api) {
const $ = api.gogocode;
const vueAppName = 'window.$vueApp';
const isVueFile = ast.parseOptions && ast.parseOptions.language === 'vue';
const script = isVueFile ? ast.find('<script></script>') : ast;
Expand Down Expand Up @@ -65,34 +51,134 @@ module.exports = function (ast, api) {

// x | A | B | C => C(B(A(x)))
function converFilterExpression(filterExpression) {
const filterExpressionArr = filterExpression.split(' | ');
if (filterExpressionArr.length === 1) {
return filterExpression;
// https://github.com/vuejs/core/blob/0683a022ec83694e29636f64aaf3c04012e9a7f0/packages/compiler-core/src/compat/transformFilter.ts#L62
const validDivisionCharRE = /[\w).+\-_$\]]/

function parseFilter(exp) {
let inSingle = false
let inDouble = false
let inTemplateString = false
let inRegex = false
let curly = 0
let square = 0
let paren = 0
let lastFilterIndex = 0
let c,
prev,
i,
expression,
filters = []

for (i = 0; i < exp.length; i++) {
prev = c
c = exp.charCodeAt(i)
if (inSingle) {
if (c === 0x27 && prev !== 0x5c) inSingle = false
} else if (inDouble) {
if (c === 0x22 && prev !== 0x5c) inDouble = false
} else if (inTemplateString) {
if (c === 0x60 && prev !== 0x5c) inTemplateString = false
} else if (inRegex) {
if (c === 0x2f && prev !== 0x5c) inRegex = false
} else if (
c === 0x7c && // pipe
exp.charCodeAt(i + 1) !== 0x7c &&
exp.charCodeAt(i - 1) !== 0x7c &&
!curly &&
!square &&
!paren
) {
if (expression === undefined) {
// first filter, end of expression
lastFilterIndex = i + 1
expression = exp.slice(0, i).trim()
} else {
pushFilter()
}
} else {
switch (c) {
case 0x22:
inDouble = true
break // "
case 0x27:
inSingle = true
break // '
case 0x60:
inTemplateString = true
break // `
case 0x28:
paren++
break // (
case 0x29:
paren--
break // )
case 0x5b:
square++
break // [
case 0x5d:
square--
break // ]
case 0x7b:
curly++
break // {
case 0x7d:
curly--
break // }
}
if (c === 0x2f) {
// /
let j = i - 1
let p
// find first non-whitespace prev char
for (; j >= 0; j--) {
p = exp.charAt(j)
if (p !== ' ') break
}
if (!p || !validDivisionCharRE.test(p)) {
inRegex = true
}
}
}
}

if (expression === undefined) {
expression = exp.slice(0, i).trim()
} else if (lastFilterIndex !== 0) {
pushFilter()
}

function pushFilter() {
filters.push(exp.slice(lastFilterIndex, i).trim())
lastFilterIndex = i + 1
}

if (filters.length) {
for (i = 0; i < filters.length; i++) {
expression = wrapFilter(expression, filters[i])
}
}
return expression
}
const matchArr = filterExpressionArr.map((e) => {
// A or B(x)
const expItem = e.trim();
// https://play.gogocode.io/#code/N4IglgdgDgrgLgYQPYBMCmIBcICGAKADwBoBPIgLyIHIBnKgShCJAHckAnAa2XSxADMYEAMZwwSCAAI47HBBr8OAWzz8wAGzQBJCIqKScUMPqRQxEmvUnAAOlMnCLcSQBJJAXgNGAdAHMk-o7odpIOTpI0SDDswmgekmqaOorekdGxIWHyzjg0zp4ueGkxaPrAYDQACuwBskqYCTjqNGgAvvSZjtnSJFBxnrlw3jhwMnhUaARQ7Gg0NOIQ3nC9aAyd4fwQAHI4Sv0GecOj7OOT07PzEt7CTZpo3hC7qx32XXkOtzt7NPGDR2MTKYzOYLYbsXwwPYQOB0ejeJSGPA4cEeAB8riR4LhvjQEDQsjgaDw9BeoRmcGiUk2XzQdladiY4Gg8AAMnJfHxln0aMJ2GAzIyABa5apoUZgfFYGQwUogGgwABGADUJSwACorTkzDCtIA
const expressionStatementAst = $(expItem, { isProgram: false });
const expressionType = expressionStatementAst.attr('expression.type') || 'Identifier';
if (expressionType === 'CallExpression') {
const args = (expressionStatementAst.attr('expression.arguments') || []).map((arg) =>
$(arg).generate()
);
const fnName = expressionStatementAst.attr('expression.callee.name') || '';
return {
name: fnName,
args,
};

function wrapFilter(exp, filter) {
const getName = (name) => {
if (scopedFilterList.includes(name)) {
return `${name}_filter`
}
return `$filters.${name}`
}

const i = filter.indexOf('(')
if (i < 0) {
return `${getName(filter)}(${exp})`
} else {
return {
name: expItem,
args: [],
};
const name = getName(filter.slice(0, i))
const args = filter.slice(i + 1)
return `${name}(${exp}${args !== ')' ? ',' + args : args}`
}
});
const newCallExpression = callSeq(scopedFilterList, _.reverse(matchArr), 0);
return newCallExpression;
}

return parseFilter(filterExpression)
}

if (isVueFile) {
Expand Down

0 comments on commit 6cc1b66

Please sign in to comment.