Skip to content

Commit

Permalink
eslint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Nov 7, 2017
1 parent 4cfff1f commit d62ebd6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 104 deletions.
71 changes: 3 additions & 68 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,73 +1,8 @@
{
"plugins": [
],
"extends": "trendmicro",
"parser": "babel-eslint",
"env": {
"browser": true,
"node": true,
"es6": true
},
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 7,
"sourceType": "module",
"ecmaFeatures": {
"experimentalObjectRestSpread": true,
"jsx": true
}
},
"rules": {
"arrow-body-style": 0,
"block-spacing": 2,
"brace-style": [2, "1tbs"],
"callback-return": [2, ["cb", "callback", "next"]],
"camelcase": [2, { "properties": "never" }],
"comma-spacing": 2,
"comma-style": [2, "last"],
"comma-dangle": [2, "never"],
"consistent-return": 2,
"curly": [2, "all"],
"default-case": 2,
"dot-notation": [2, { "allowKeywords": true }],
"eol-last": 2,
"eqeqeq": 2,
"func-names": 0,
"indent": [2, 4],
"key-spacing": [2, {
"beforeColon": false,
"afterColon": true
}],
"max-len": [1, 160, 2, { "ignoreComments": true }],
"new-cap": [2, { "newIsCap": true, "capIsNew": false }],
"new-parens": 2,
"no-alert": 2,
"no-array-constructor": 2,
"no-caller": 2,
"no-cond-assign": [2, "except-parens"],
"no-const-assign": 2,
"no-console": [1, { "allow": ["assert", "warn", "error"]}],
"no-else-return": 0,
"no-lone-blocks": 0,
"no-param-reassign": 0,
"no-shadow": 0,
"no-var": 1,
"no-unused-expressions": [2, {
"allowShortCircuit": true,
"allowTernary": true
}],
"no-unused-vars": [1, {
"vars": "local",
"args": "none"
}],
"no-use-before-define": 0,
"object-shorthand": 0,
"one-var": 0,
"one-var-declaration-per-line": 0,
"prefer-const": 0,
"prefer-template": 0,
"quote-props": [0, "as-needed"],
"quotes": [2, "single"],
"space-before-function-paren": 0,
"spaced-comment": 0,
"vars-on-top": 0
"node": true
}
}
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "i18next-scanner",
"version": "2.0.2",
"version": "2.0.3",
"description": "Scan your code, extract translation keys/values, and merge them into i18n resource files.",
"homepage": "https://github.com/i18next/i18next-scanner",
"author": "Cheton Wu <cheton@gmail.com>",
Expand Down Expand Up @@ -55,11 +55,15 @@
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.0.0",
"babel-eslint": "^8.0.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"coveralls": "^2.13.1",
"eslint": "^4.7.0",
"eslint": "^4.10.0",
"eslint-config-trendmicro": "^1.0.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-jsx-a11y": "^5.1.1",
"eslint-plugin-react": "^7.4.0",
"gulp": "^3.9.1",
"gulp-tap": "^1.0.1",
"gulp-util": "^3.0.8",
Expand Down
56 changes: 28 additions & 28 deletions src/jsx-parser.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,90 @@
import htmlparser from 'htmlparser2';

const jsExpr = /(.*?)({+[^]+?}+)(.*)/

const jsExpr = /(.*?)({+[^]+?}+)(.*)/;

export function parseJSX(fragment) {
const ast = {
nodeName: '#root',
childNodes: []
}
const stack = [ ast ]
};
const stack = [ast];

const handler = {
onopentag: (name) => {
const node = {
nodeName: name,
childNodes: []
}
stack[0].childNodes.push(node)
stack.unshift(node)
};
stack[0].childNodes.push(node);
stack.unshift(node);
},

onclosetag: () => {
stack.shift()
stack.shift();
},

ontext: (text) => {
let txt = text
let m = jsExpr.exec(txt)
let txt = text;
let m = jsExpr.exec(txt);
if (m) {
while ((m = jsExpr.exec(txt))) {
if (m[1]) {
stack[0].childNodes.push({
nodeName: '#text',
value: m[1],
childNodes: []
})
});
}
stack[0].childNodes.push({
nodeName: '#expression',
value: m[2],
childNodes: []
})
txt = m[3]
});
txt = m[3];
}
}
if (txt!=='') {
if (txt) {
stack[0].childNodes.push({
nodeName: '#text',
value: txt,
childNodes: []
})
});
}
}
};

const parser = new htmlparser.Parser(handler, {
xmlMode: true,
decodeEntities: true
})
parser.write(fragment)
return stack[0].childNodes
});
parser.write(fragment);

return stack[0].childNodes;
}


function astToText(ast) {
let output = ''
let output = '';

function walk(nodes) {
nodes.forEach((node, ix) => {
if (node.nodeName === '#text') {
output += node.value;
} else if (node.nodeName === '#expression') {
output += `<${ix}>${node.value}</${ix}>`
output += `<${ix}>${node.value}</${ix}>`;
} else {
output += `<${ix}>`
walk(node.childNodes)
output += `</${ix}>`
output += `<${ix}>`;
walk(node.childNodes);
output += `</${ix}>`;
}
})
});
}

walk(ast)
return output
walk(ast);
return output;
}

export default function jsxToText(fragment) {
const ast = parseJSX(fragment)
return astToText(ast)
const ast = parseJSX(fragment);
return astToText(ast);
}
12 changes: 7 additions & 5 deletions src/parser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint no-console: 0 */
/* eslint no-continue: 0 */
/* eslint no-eval: 0 */
import fs from 'fs';
import isArray from 'lodash/isArray';
import isFunction from 'lodash/isFunction';
Expand Down Expand Up @@ -397,13 +399,13 @@ class Parser {
}

setter(key);
}
};

const walk = (nodes) => {
nodes.forEach(node => {
if (node.attrs) {
node.attrs.forEach(attr => {
if (attrs.indexOf(attr.name)!==-1) {
if (attrs.indexOf(attr.name) !== -1) {
const values = attr.value.split(';');
values.forEach(parseAttributeValue);
}
Expand All @@ -412,10 +414,10 @@ class Parser {
if (node.childNodes) {
walk(node.childNodes);
}
})
}
});
};

walk(ast.childNodes)
walk(ast.childNodes);

return this;
}
Expand Down

0 comments on commit d62ebd6

Please sign in to comment.