Skip to content

Commit

Permalink
MDL-81263 grunt: Respect local .eslintignore and .stylelintignore
Browse files Browse the repository at this point in the history
  • Loading branch information
PhMemmel committed Mar 18, 2024
1 parent 757be30 commit 7940c24
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions .grunt/tasks/ignorefiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ module.exports = grunt => {
}) + "\n");
};

/**
* Extracts ignore entries from a local ignore file.
*
* @param {string} componentPath the file path to the component, relative to the code base directory
* @param {string} ignoreFilePath the path to the ignore file
* @return {array} array of ignore paths to be included in the global ignore files
*/
const getEntriesFromLocalIgnoreFile = (componentPath, ignoreFilePath) => {
const ignorePaths = [];
if (grunt.file.exists(ignoreFilePath)) {
const ignoreFile = grunt.file.read(ignoreFilePath);
const entries = ignoreFile.split('\n');
entries.forEach(entry => {
entry = entry.trim();
if (entry.length > 0 && !entry.startsWith('#') && !entry.startsWith('!')) {
while (entry.startsWith('/')) {
entry = entry.substring(1);
}
ignorePaths.push(componentPath + '/' + entry);
}
});
}
return ignorePaths;
};

/**
* Generate ignore files (utilising thirdpartylibs.xml data)
*/
Expand All @@ -67,6 +92,20 @@ module.exports = grunt => {

// An array of paths to third party directories.
const thirdPartyPaths = ComponentList.getThirdPartyPaths();
const localStylelintIgnorePaths = [];
const localEslintIgnorePaths = [];
ComponentList.getComponentPaths(process.cwd() + '/').forEach(componentPath => {
const localEslintIgnorePath = process.cwd() + '/' + componentPath + '/.eslintignore';
const localEslintIgnoreEntries = getEntriesFromLocalIgnoreFile(componentPath, localEslintIgnorePath);
if (localEslintIgnoreEntries.length > 0) {
localEslintIgnorePaths.push(...localEslintIgnoreEntries);
}
const localStylelintIgnorePath = process.cwd() + '/' + componentPath + '/.stylelintignore';
const localStylelintIgnoreEntries = getEntriesFromLocalIgnoreFile(componentPath, localStylelintIgnorePath);
if (localStylelintIgnoreEntries.length > 0) {
localStylelintIgnorePaths.push(...localStylelintIgnoreEntries);
}
});

// Generate .eslintignore.
const eslintIgnores = [
Expand All @@ -77,7 +116,7 @@ module.exports = grunt => {
// Ignore all yui/src meta directories and build directories.
'*/**/yui/src/*/meta/',
'*/**/build/',
].concat(thirdPartyPaths);
].concat(thirdPartyPaths).concat(localEslintIgnorePaths);
grunt.file.write('.eslintignore', eslintIgnores.join('\n') + '\n');

// Generate .stylelintignore.
Expand All @@ -88,7 +127,7 @@ module.exports = grunt => {
'theme/classic/style/moodle.css',
'jsdoc/styles/*.css',
'admin/tool/componentlibrary/hugo/dist/css/docs.css',
].concat(thirdPartyPaths);
].concat(thirdPartyPaths).concat(localStylelintIgnorePaths);
grunt.file.write('.stylelintignore', stylelintIgnores.join('\n') + '\n');

phpcsIgnore(thirdPartyPaths);
Expand Down

0 comments on commit 7940c24

Please sign in to comment.