Skip to content

Commit

Permalink
feat: support option words
Browse files Browse the repository at this point in the history
  • Loading branch information
edvardchen committed May 7, 2022
1 parent 1934216 commit 874a694
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
28 changes: 2 additions & 26 deletions lib/rules/no-literal-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,9 @@ module.exports = {
ignoreAttribute = [],
ignoreProperty = [],
ignoreCallee = [],
ignore = [],
words,
} = options;
const whitelists = [
/^[0-9!-/:-@[-`{-~]+$/, // ignore not-word string
...ignore,
].map(item => new RegExp(item));
const whitelists = words.exclude.map(generateFullMatchRegExp);

const message = 'disallow literal string';
//----------------------------------------------------------------------
Expand All @@ -68,27 +65,6 @@ module.exports = {
return whitelists.some(item => item.test(str));
}

const popularCallee = [
/^i18n(ext)?$/,
't',
'require',
'addEventListener',
'removeEventListener',
'postMessage',
'getElementById',
//
// ─── VUEX CALLEE ────────────────────────────────────────────────────────────────
//
'dispatch',
'commit',
// ────────────────────────────────────────────────────────────────────────────────

'includes',
'indexOf',
'endsWith',
'startsWith',
];

const validCalleeList = callees.exclude.map(generateFullMatchRegExp);

function isValidFunctionCall({ callee }) {
Expand Down
18 changes: 14 additions & 4 deletions tests/lib/rules/no-literal-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,14 @@ ruleTester.run('no-literal-string', rule, {
{ code: 'store.dispatch("hello");' },
{ code: 'store.commit("hello");' },
{ code: 'i18n.t("hello");' },
{ code: 'const a = "absfoo";', options: [{ ignore: ['foo'] }] },
{ code: 'const a = "fooabc";', options: [{ ignore: [/^foo/] }] },
{
code: 'const a = "absfoo";',
options: [{ words: { exclude: ['.*foo.*'] } }],
},
{
code: 'const a = "fooabc";',
options: [{ words: { exclude: ['^foo.*'] } }],
},
{
code: 'foo.bar("taa");',
options: [
Expand Down Expand Up @@ -162,7 +168,11 @@ ruleTester.run('no-literal-string', rule, {
{ code: 'const a = "foo";', errors },
{ code: 'const a = call("Ffo");', errors },
{ code: 'var a = {foo: "bar"};', errors },
{ code: 'const a = "afoo";', options: [{ ignore: ['^foo'] }], errors },
{
code: 'const a = "afoo";',
options: [{ words: { exclude: ['^foo'] } }],
errors,
},
{
code: 'var a = {foo: "foo"};',
options: [{ ignoreProperty: ['bar'] }],
Expand All @@ -176,7 +186,7 @@ ruleTester.run('no-literal-string', rule, {
{ code: '<div>foo</div>', errors },
{ code: '<div>foo</div>', options: [{ markupOnly: true }], errors },
{ code: '<>foo999</>', options: [{ markupOnly: true }], errors },
{ code: '<div>FOO</div>', errors },
// { code: '<div>FOO</div>', errors },
{
code: '<div>{"hello world"}</div>',
options: [{ markupOnly: true }],
Expand Down

0 comments on commit 874a694

Please sign in to comment.