Skip to content

Commit

Permalink
literal init
Browse files Browse the repository at this point in the history
  • Loading branch information
Bacra committed Mar 23, 2020
1 parent c47b51d commit d7b4bec
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 48 deletions.
21 changes: 14 additions & 7 deletions packages/i18nc-ast/lib/ast_util.js
@@ -1,7 +1,7 @@
'use strict';

const esprima = require('esprima');
const escodegen = require('escodegen');
const babelParse = require('@babel/parser').parse;
const babelGenerate = require('@babel/generator').default;
const astTpl = require('./ast_tpl');
const config = require('./config');
const debug = require('debug')('i18nc-ast:ast_util');
Expand All @@ -19,15 +19,22 @@ exports.checkAstFlag = function checkAstFlag(ast, flag) {
};

exports.parse = function parse(code) {
return esprima.parse(code, config.esprimaOptions);
return babelParse(code, config.esparseOptions).program;
};

exports.mincode = function mincode(code) {
return escodegen.generate(exports.parse(code), config.escodegenMinOptions);
return babelGenerate(exports.parse(code), config.escodegenMinOptions).code;
};

exports.tocode = function tocode(ast) {
return escodegen.generate(ast, config.escodegenOptions);
return babelGenerate(ast, config.escodegenOptions).code;
};

exports.isLiteral = function isLiteral(ast) {
// @see https://babeljs.io/docs/en/babel-parser#output
if (ast && ast.type) {
return ast.type == 'Literal' || ast.type == 'StringLiteral';
}
};

exports.codeIndent = function codeIndent(ast, code) {
Expand All @@ -41,7 +48,7 @@ exports.codeIndent = function codeIndent(ast, code) {
};

exports.ast2constVal = function ast2constVal(ast) {
if (ast.type == 'Literal') return ast.value;
if (exports.isLiteral(ast)) return ast.value;
if (ast.type == 'Identifier') {
switch (ast.name) {
case 'undefined':
Expand All @@ -58,7 +65,7 @@ exports.ast2constVal = function ast2constVal(ast) {
};

exports.ast2constKey = function(ast) {
if (ast.type == 'Literal') return ast.value;
if (exports.isLiteral(ast)) return ast.value;
if (ast.type == 'Identifier') return ast.name;
};

Expand Down
51 changes: 28 additions & 23 deletions packages/i18nc-ast/lib/config.js
Expand Up @@ -2,33 +2,38 @@

exports.escodegenOptions = {
comment: true,
format: {
// escapeless 为true的时候,会把 \u0000 这样的字符直接以字符的形式输出
// 不开启,又会导致一些普通字符转移输出,比如“,”
escapeless: true,
newline: '\n',
indent: {
style: '\t'
}
}
// format: {
// // escapeless 为true的时候,会把 \u0000 这样的字符直接以字符的形式输出
// // 不开启,又会导致一些普通字符转移输出,比如“,”
// escapeless: true,
// newline: '\n',
// indent: {
// style: '\t'
// }
// }
};

exports.escodegenMinOptions = {
comment: false,
format: {
// escapeless 为true的时候,会把 \u0000 这样的字符直接以字符的形式输出
// 不开启,又会导致一些普通字符转移输出,比如“,”
escapeless: true,
newline: '\n',
quotes: 'auto',
compact: true,
indent: {
style: ''
}
}
// format: {
// // escapeless 为true的时候,会把 \u0000 这样的字符直接以字符的形式输出
// // 不开启,又会导致一些普通字符转移输出,比如“,”
// escapeless: true,
// newline: '\n',
// quotes: 'auto',
// compact: true,
// indent: {
// style: ''
// }
// }
};

exports.esprimaOptions = {
range: true,
loc: true
exports.esparseOptions = {
sourceType: 'module',
ranges: true,

plugins: [
'jsx',
'typescript'
]
};
6 changes: 3 additions & 3 deletions packages/i18nc-ast/package.json
Expand Up @@ -4,9 +4,9 @@
"description": "I18N AST Tool for JS files",
"main": "index.js",
"dependencies": {
"debug": "^4.1.0",
"escodegen": "^1.13.0",
"esprima": "^4.0.1"
"@babel/generator": "^7.9.3",
"@babel/parser": "^7.9.3",
"debug": "^4.1.0"
},
"repository": {
"type": "git",
Expand Down
2 changes: 2 additions & 0 deletions packages/i18nc-ast/test/files/literal.js
@@ -0,0 +1,2 @@
var string = 'string1';
var string2 = `string2`;
5 changes: 5 additions & 0 deletions packages/i18nc-ast/test/test_parse.js
@@ -0,0 +1,5 @@
const astUtils = require('../lib/ast_util');
const fs = require('fs');


console.log(astUtils.parse(fs.readFileSync(__dirname + '/files/literal.js', 'utf8')));
13 changes: 4 additions & 9 deletions packages/i18nc-core/lib/ast_collector.js
Expand Up @@ -2,7 +2,6 @@

const _ = require('lodash');
const debug = require('debug')('i18nc-core:ast_collector');
const estraverse = require('estraverse');
const emitter = require('./emitter');
const i18ncAst = require('i18nc-ast');
const astUtil = i18ncAst.util;
Expand All @@ -11,7 +10,6 @@ const ASTScope = require('./ast_scope').ASTScope;
const LiteralHandler = require('./ast_literal_handler').LiteralHandler;
const ArrayPush = Array.prototype.push;

const VISITOR_KEYS = estraverse.VisitorKeys;
const BLOCK_MODIFIER = DEF.BLOCK_MODIFIER;
const AST_FLAGS = i18ncAst.AST_FLAGS;
const UNSUPPORT_AST_TYPS = DEF.UNSUPPORT_AST_TYPS;
Expand Down Expand Up @@ -51,7 +49,7 @@ _.extend(ASTCollector.prototype, {
// 获取subkey ast
// 两种情况:字符串,或者options.subkey
if (maybeSubkeyAst) {
if (maybeSubkeyAst.type == 'Literal') {
if (astUtil.isLiteral(maybeSubkeyAst)) {
result.subkeyAst = maybeSubkeyAst;
} else if (
maybeSubkeyAst.type == 'ObjectExpression' &&
Expand All @@ -70,6 +68,7 @@ _.extend(ASTCollector.prototype, {

scan: function(scope, ast) {
const self = this;
if (typeof ast != 'object') return;

if (Array.isArray(ast)) {
const result = [];
Expand Down Expand Up @@ -280,11 +279,7 @@ _.extend(ASTCollector.prototype, {
return;
}

let scanKeys = VISITOR_KEYS[ast.type];
if (!scanKeys) {
debug('undefined ast type:%s', ast.type);
scanKeys = _.keys(ast);
}
const scanKeys = _.keys(ast);
if (!scanKeys.length) return;

const result = [];
Expand Down Expand Up @@ -326,7 +321,7 @@ _.extend(ASTCollector.prototype, {
astBodyFirst &&
astBodyFirst.type == 'ExpressionStatement' &&
astBodyFirst.expression &&
astBodyFirst.expression.type == 'Literal' &&
astUtil.isLiteral(astBodyFirst.expression) &&
astBodyFirst.expression.value;

if (val)
Expand Down
5 changes: 3 additions & 2 deletions packages/i18nc-core/lib/i18n_func/parser.js
Expand Up @@ -5,6 +5,7 @@ const debug = require('debug')('i18nc-core:i18n_func_parser');
const emitter = require('../emitter');
const escodegen = require('escodegen');
const jsoncode = require('i18nc-jsoncode');
const astUtil = require('i18nc-ast').util;

/**
* 分析i18n函数,提取相关信息:
Expand Down Expand Up @@ -48,7 +49,7 @@ function paseI18nHandlerInfo(ast, options, result) {
lastBodyArgument.type == 'BinaryExpression' &&
lastBodyArgument.operator == '+' &&
lastBodyArgument.left &&
lastBodyArgument.left.type == 'Literal' &&
astUtil.isLiteral(lastBodyArgument.left) &&
lastBodyArgument.left.value === ''
) {
const rightAst = lastBodyArgument.right;
Expand Down Expand Up @@ -102,7 +103,7 @@ function paseI18nHandlerInfo(ast, options, result) {
}[name] || name;
result[name + 'ast'] = initVal;

if (initVal.type == 'Literal') {
if (astUtil.isLiteral(initVal)) {
if (result[name])
throw new Error(name + ' IS DEFINED TWICE');
result[name] = initVal.value;
Expand Down
1 change: 0 additions & 1 deletion packages/i18nc-core/package.json
Expand Up @@ -21,7 +21,6 @@
"escodegen": "^1.11.1",
"esprima": "^4.0.1",
"esshorten4node11": "^1.1.2",
"estraverse": "^4.2.0",
"i18nc-ast": "^2.0.0",
"i18nc-db": "^2.0.0",
"i18nc-jsoncode": "^2.0.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/i18nc-core/test/test_example.js
Expand Up @@ -4,6 +4,7 @@ const debug = require('debug')('i18nc-core:test_example');
const expect = require('expect.js');
const i18nc = require('../');
const autoTestUtils = require('./auto_test_utils');
const astUtil = require('i18nc-ast').util;
const dbTranslateWords = require('./files/casefile/translate_words_db');

describe('#example', function() {
Expand Down Expand Up @@ -86,7 +87,7 @@ describe('#example', function() {
ast.callee.name == 'require' &&
ast.arguments &&
ast.arguments[0] &&
ast.arguments[0].type == 'Literal'
astUtil.isLiteral(ast.arguments[0])
) {
expect(emitData.options.originalOptions.mainFile).to.be(
mainFile
Expand Down
3 changes: 2 additions & 1 deletion packages/i18nc-jsoncode1/lib/parser.js
Expand Up @@ -87,6 +87,7 @@ function _wordAst2json(ast) {
}

switch (val_ast.type) {
case 'StringLiteral':
case 'Literal': {
let val = astUtil.ast2constVal(val_ast);
if (val) result[name] = val;
Expand All @@ -102,7 +103,7 @@ function _wordAst2json(ast) {
break;

default:
throw new Error('Error ast type for translate JSON');
throw new Error('Error ast type for translate JSON:' + val_ast.type);
}
});

Expand Down
2 changes: 1 addition & 1 deletion packages/i18nc-key-combo/keycombo.js
Expand Up @@ -540,6 +540,6 @@ function _getArgs0IfI18NLiteral(ast, options) {
options.I18NHandlerAlias.indexOf(calleeName) != -1
) {
const arg0ast = ast.arguments && ast.arguments[0];
return arg0ast && arg0ast.type == 'Literal' && arg0ast.value;
return arg0ast && astUtil.isLiteral(arg0ast) && arg0ast.value;
}
}

0 comments on commit d7b4bec

Please sign in to comment.