forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sasslint precommit hook (elastic#28095)
* Remove SCSS linting from dev server * Add sasslint to precommit hook
- Loading branch information
1 parent
d745694
commit dd47972
Showing
6 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export { pickFilesToLint } from './pick_files_to_lint'; | ||
export { lintFiles } from './lint_files'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import sassLint from 'sass-lint'; | ||
import path from 'path'; | ||
import { createFailError } from '../run'; | ||
|
||
/** | ||
* Lints a list of files with eslint. eslint reports are written to the log | ||
* and a FailError is thrown when linting errors occur. | ||
* | ||
* @param {ToolingLog} log | ||
* @param {Array<File>} files | ||
* @return {undefined} | ||
*/ | ||
export function lintFiles(log, files) { | ||
const paths = files.map(file => file.getRelativePath()); | ||
|
||
const report = sassLint.lintFiles( | ||
paths.join(', '), | ||
{}, | ||
path.resolve(__dirname, '..', '..', '..', '.sass-lint.yml') | ||
); | ||
|
||
const failTypes = Object.keys( | ||
report.reduce( | ||
(failTypes, reportEntry) => { | ||
if (reportEntry.warningCount > 0) failTypes.warning = true; | ||
if (reportEntry.errorCount > 0) failTypes.errors = true; | ||
return failTypes; | ||
}, | ||
{} | ||
) | ||
); | ||
|
||
if (!failTypes.length) { | ||
log.success('[sasslint] %d files linted successfully', files.length); | ||
return; | ||
} | ||
|
||
log.error(sassLint.format(report)); | ||
throw createFailError(`[sasslint] ${failTypes.join(' & ')}`, 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import fs from 'fs'; | ||
import { safeLoad } from 'js-yaml'; | ||
import { makeRe } from 'minimatch'; | ||
import path from 'path'; | ||
|
||
// load the include globs from .sass-lint.yml and convert them to regular expressions for filtering files | ||
const sassLintPath = path.resolve(__dirname, '..', '..', '..', '.sass-lint.yml'); | ||
const sassLintConfig = safeLoad(fs.readFileSync(sassLintPath)); | ||
const { files: { include: includeGlobs } } = sassLintConfig; | ||
const includeRegex = includeGlobs.map(glob => makeRe(glob)); | ||
|
||
function matchesInclude(file) { | ||
for (let i = 0; i < includeRegex.length; i++) { | ||
if (includeRegex[i].test(file.relativePath)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
export function pickFilesToLint(log, files) { | ||
return files | ||
.filter(file => file.isSass()) | ||
.filter(matchesInclude); | ||
} |