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

Fix failing eslint tests #407

Merged
merged 11 commits into from
Mar 10, 2023
60 changes: 50 additions & 10 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,58 @@ jobs:
eslint:
name: eslint
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: install node v16

- name: Cache node_modules
id: cache-node-modules
uses: actions/cache@v3
env:
cache-name: cache-node-modules
with:
path: node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}

- name: Setup node v16 and npm cache
uses: actions/setup-node@v3
with:
node-version: 16
- name: npm install
run: npm install
- name: eslint
uses: icrawl/action-eslint@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
cache: 'npm'

- name: Install Node dependencies
if: steps.cache-node-modules.outputs.cache-hit != 'true'
run: npm ci --no-optional

- name: Get updated JS files
id: changed-files
uses: tj-actions/changed-files@v35
with:
files: |
**/*.js

- name: Run JS linting
run: |
if [[ "${{ steps.changed-files.outputs.any_changed }}" == 'true' && "${{ github.event_name }}" == "pull_request" ]]; then
npx wp-scripts lint-js ${{ steps.changed-files.outputs.all_changed_files }}
elif [[ "${{ github.event_name }}" == "push" ]]; then
npm run lint-js
fi

phpcs:
name: phpcs
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2.4.0
uses: actions/checkout@v3

- name: Set standard 10up cache directories
run: |
composer config -g cache-dir "${{ env.COMPOSER_CACHE }}"

- name: Prepare composer cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ${{ env.COMPOSER_CACHE }}
key: composer-${{ env.COMPOSER_VERSION }}-${{ hashFiles('**/composer.lock') }}
Expand All @@ -59,15 +85,29 @@ jobs:
- name: Install dependencies
run: composer install

- name: Get updated PHP files
id: changed-files
uses: tj-actions/changed-files@v35
with:
files: |
**/*.php

- name: PHPCS check
run: vendor/bin/phpcs . --runtime-set testVersion 7.4-
run: |
if [[ "${{ steps.changed-files.outputs.any_changed }}" == 'true' && "${{ github.event_name }}" == "pull_request" ]]; then
./vendor/bin/phpcs ${{ steps.changed-files.outputs.all_changed_files }} --runtime-set testVersion 7.4-
elif [[ "${{ github.event_name }}" == "push" ]]; then
./vendor/bin/phpcs . --runtime-set testVersion 7.4-
fi

vipcs:
name: vipcs
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: VIPCS check
uses: 10up/wpcs-action@stable
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ function setupRewardCall( blockId ) {
const contentLinks = document.querySelectorAll(
`#${ blockId } .classifai-send-reward`
);
contentLinks.forEach( function ( contentLink ) {
contentLink.addEventListener( 'click', function ( event ) {
contentLinks.forEach( function( contentLink ) {
contentLink.addEventListener( 'click', function( event ) {
event.preventDefault();
const eventId = this.getAttribute( 'data-eventid' );
const isRewarded = this.getAttribute( 'data-rewarded' );
Expand Down Expand Up @@ -44,18 +44,18 @@ function classifaiSessionSet( key, value, expirationInMin ) {
key,
JSON.stringify( {
expiresAt: new Date(
new Date().getTime() + 60000 * expirationInMin
new Date().getTime() + ( 60000 * expirationInMin )
),
value,
} )
);
}

document.addEventListener( 'DOMContentLoaded', function () {
document.addEventListener( 'DOMContentLoaded', function() {
const classifaiBlocks = document.querySelectorAll(
'.classifai-recommended-block-wrap'
);
classifaiBlocks.forEach( function ( classifaiBlock ) {
classifaiBlocks.forEach( function( classifaiBlock ) {
const blockId = classifaiBlock.getAttribute( 'id' );
const cached = classifaiSessionGet( blockId );
if ( cached !== null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ const termsPerPage = 100;
const getTermIdByTermValue = ( termsMappedByName, termValue ) => {
// First we check for exact match by `term.id` or case sensitive `term.name` match.
const termId = termValue?.id || termsMappedByName[ termValue ]?.id;
if ( termId ) return termId;

if ( termId ) {
return termId;
}

/**
* Here we make an extra check for entered terms in a non case sensitive way,
* to match user expectations, due to `FormTokenField` behaviour that shows
Expand Down Expand Up @@ -57,14 +61,22 @@ const TaxonomyControls = ( { onChange, query } ) => {
const taxonomyInfo = taxonomiesInfo.find(
( { slug } ) => slug === taxonomySlug
);
if ( ! taxonomyInfo ) return;

if ( ! taxonomyInfo ) {
return;
}

const termIds = Array.from(
newTermValues.reduce( ( accumulator, termValue ) => {
const termId = getTermIdByTermValue(
taxonomyInfo.terms.mapByName,
termValue
);
if ( termId ) accumulator.add( termId );

if ( termId ) {
accumulator.add( termId );
}

return accumulator;
}, new Set() )
);
Expand All @@ -81,7 +93,11 @@ const TaxonomyControls = ( { onChange, query } ) => {
const taxonomyInfo = taxonomiesInfo.find(
( { slug } ) => slug === taxonomySlug
);
if ( ! taxonomyInfo ) return [];

if ( ! taxonomyInfo ) {
return [];
}

return ( query.taxQuery?.[ taxonomySlug ] || [] ).reduce(
( accumulator, termId ) => {
const term = taxonomyInfo.terms.mapById[ termId ];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export const usePostTypes = () => {
return filteredPostTypes;
}, [] );
const postTypesTaxonomiesMap = useMemo( () => {
if ( ! postTypes?.length ) return;
if ( ! postTypes?.length ) {
return;
}

// eslint-disable-next-line consistent-return
return postTypes.reduce( ( accumulator, type ) => {
accumulator[ type.slug ] = type.taxonomies;
Expand Down
4 changes: 3 additions & 1 deletion src/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
'classifai-settings-watson_username'
);

if ( $toggler === null || $userField === null ) return;
if ( $toggler === null || $userField === null ) {
return;
}

const $userFieldWrapper = $userField.closest( 'tr' );
const [ $passwordFieldTitle ] = document
Expand Down
12 changes: 6 additions & 6 deletions src/js/gutenberg-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ const ClassifAIToggle = () => {
help={
'yes' === enabled
? __(
'ClassifAI language processing is enabled',
'classifai'
)
'ClassifAI language processing is enabled',
'classifai'
)
: __(
'ClassifAI language processing is disabled',
'classifai'
)
'ClassifAI language processing is disabled',
'classifai'
)
}
checked={ 'yes' === enabled }
onChange={ ( value ) => {
Expand Down
18 changes: 11 additions & 7 deletions src/js/language-processing.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ import '../scss/language-processing.scss';
name = item.label;
}

const width = 300 + 300 * rating;
const width = 300 + ( 300 * rating );
rating = ( rating * 100 ).toFixed( 2 );
name = name
.split( '/' )
Expand Down Expand Up @@ -325,30 +325,34 @@ import '../scss/language-processing.scss';
function debounce( func, wait, immediate ) {
let timeout;

return function () {
return function() {
const context = this,
args = arguments;

/** Debounced function. */
const later = function () {
const later = function() {
timeout = null;
if ( ! immediate ) func.apply( context, args );
if ( ! immediate ) {
func.apply( context, args );
}
};
const callNow = immediate && ! timeout;
clearTimeout( timeout );
timeout = setTimeout( later, wait );
if ( callNow ) func.apply( context, args );
if ( callNow ) {
func.apply( context, args );
}
};
}
} )();

// Display "Classify Post" button only when "Process content on update" is unchecked (Classic Editor).
document.addEventListener( 'DOMContentLoaded', function () {
document.addEventListener( 'DOMContentLoaded', function() {
const classifaiNLUCheckbox = document.getElementById(
'_classifai_process_content'
);
if ( classifaiNLUCheckbox ) {
classifaiNLUCheckbox.addEventListener( 'change', function () {
classifaiNLUCheckbox.addEventListener( 'change', function() {
const classifyButton = document.querySelector(
'.classifai-clasify-post-wrapper'
);
Expand Down
6 changes: 3 additions & 3 deletions src/js/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { handleClick } from './helpers';

( function ( $ ) {
( function( $ ) { // eslint-disable-line wrap-iife
const { __ } = wp.i18n;

/**
Expand Down Expand Up @@ -166,9 +166,9 @@ import { handleClick } from './helpers';
} );
};

$( document ).ready( function () {
$( document ).ready( function() {
if ( wp.media ) {
wp.media.view.Modal.prototype.on( 'open', function () {
wp.media.view.Modal.prototype.on( 'open', function() {
wp.media.frame.on( 'selection:toggle', handleButtonsClick );
wp.media.frame.on( 'selection:toggle', checkPdfReadStatus );
} );
Expand Down