Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better detection of arrow functions #82

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/cheerioManipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,7 @@ function clearInlineFunctions ($, options) {
$('*').each(function (index, element) {
Object.keys(element.attribs).forEach(function (attributeName) {
let value = element.attribs[attributeName];
if (
(
value.startsWith('function ') ||
value.startsWith('function(')
) &&
value.endsWith('}') &&
helpers.functionRegex.test(value)
) {
if (helpers.isFunctionDeclaration(value)) {
element.attribs[attributeName] = '[function]';
}
});
Expand Down
9 changes: 9 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ const helpers = {
// }
functionRegex: /function( )*(.)*( )*\((.,* *){0,}\) *{(.*\n*)*}/,

isFunctionDeclaration: function (str) {
if (str.startsWith('function ') || str.startsWith('function(')) {
return str.endsWith('}') && helpers.functionRegex.test(str);
}

// Good enough to match most arrow functions
return /^\s*\w+\s*=>/.test(str) || /^\s*\([^)]*\)\s*=>/.test(str);
},

/**
* Console logs helper error messages if verbose mode is enabled.
*
Expand Down
16 changes: 14 additions & 2 deletions tests/__snapshots__/InlineFunctions.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ exports[`InlineFunctions.vue Functions kept 1`] = `
}">Multi-line</h2>
<h3 title="function (x) {
return !x;
}">Arrow</h3>
}">Arrow 1</h3>
<h3 title="function (x) {
return !x;
}">Arrow 2</h3>
<h3 title="function () {
return true;
}">Arrow 3</h3>
<h3 title="function (x, y) {
return x + y;
}">Arrow 4</h3>
<h4 title="function () { [native code] }">Reference</h4>
<h5 title="2">Run</h5>
<h6 title="function (a word used in programming) is typically followed by curly braces, ie {}">Red herring</h6>
Expand All @@ -35,7 +44,10 @@ exports[`InlineFunctions.vue Functions removed 1`] = `
<div>
<h1 title="[function]">Classic</h1>
<h2 title="[function]">Multi-line</h2>
<h3 title="[function]">Arrow</h3>
<h3 title="[function]">Arrow 1</h3>
<h3 title="[function]">Arrow 2</h3>
<h3 title="[function]">Arrow 3</h3>
<h3 title="[function]">Arrow 4</h3>
<h4 title="[function]">Reference</h4>
<h5 title="2">Run</h5>
<h6 title="function (a word used in programming) is typically followed by curly braces, ie {}">Red herring</h6>
Expand Down
5 changes: 4 additions & 1 deletion tests/components/InlineFunctions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
return true;
}"
>Multi-line</h2>
<h3 :title="(x) => !x">Arrow</h3>
<h3 :title="(x) => !x">Arrow 1</h3>
<h3 :title="x => !x">Arrow 2</h3>
<h3 :title="() => true">Arrow 3</h3>
<h3 :title="(x, y) => { return x + y; }">Arrow 4</h3>
<h4 :title="method">Reference</h4>
<h5 :title="method()">Run</h5>
<h6 title="function (a word used in programming) is typically followed by curly braces, ie {}">Red herring</h6>
Expand Down
52 changes: 42 additions & 10 deletions tests/src/helpers.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const helpers = require('../../src/helpers.js');

describe('Helpers', () => {
describe('functionRegex', () => {
let pattern = helpers.functionRegex;
describe('isFunctionDeclaration', () => {
let { isFunctionDeclaration } = helpers;

test('Patterns that match', () => {
let matchers = [
Expand Down Expand Up @@ -77,15 +77,49 @@ describe('Helpers', () => {
'function named(arg, arg2){}',
'function named(arg, arg2){ return; }',
'function named(arg, arg2){ return true; }',
'function named(arg, arg2){\n return true;\n}'
'function named(arg, arg2){\n return true;\n}',
'() => 1',
'() => true',
'() => 1;',
'() => true;',
'() => {}',
'() => {return;}',
'() => { return; }',
'() => { return true; }',
'() => { return true;\n}',
'arg => 1',
'arg => true',
'arg => 1;',
'arg => true;',
'arg => {}',
'arg => {return;}',
'arg => { return; }',
'arg => { return true; }',
'arg => { return true;\n}',
'(arg) => 1',
'(arg) => true',
'(arg) => 1;',
'(arg) => true;',
'(arg) => {}',
'(arg) => {return;}',
'(arg) => { return; }',
'(arg) => { return true; }',
'(arg) => { return true;\n}',
'(arg, arg2) => 1',
'(arg, arg2) => true',
'(arg, arg2) => 1;',
'(arg, arg2) => true;',
'(arg, arg2) => {}',
'(arg, arg2) => {return;}',
'(arg, arg2) => { return; }',
'(arg, arg2) => { return true; }',
'(arg, arg2) => { return true;\n}',
];

// Non-english symbols
matchers.push('function funkcjonować (spór, wywód) { return "powrót"; }');

let matched = matchers.filter((match) => {
return !pattern.test(match);
});
let matched = matchers.filter(v => !isFunctionDeclaration(v));

if (matched.length) {
console.log('The following should be matched by the Regex, but wasn\'t');
Expand All @@ -99,13 +133,11 @@ describe('Helpers', () => {
test('Patterns that don\'t match', () => {
let matchers = [
'junk',
'() => {}',
'(a) >= 1',
'function (a word used in programming) is typically followed by curly braces, ie {}'
];

let matched = matchers.filter((match) => {
return pattern.test(match);
});
let matched = matchers.filter(v => isFunctionDeclaration(v));

if (matched.length) {
console.log('The following should NOT be matched by the Regex, but was');
Expand Down