Skip to content

Commit

Permalink
Ensure absolute paths to processors (stylelint#2207)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtheclark authored and sergesemashko committed Mar 3, 2017
1 parent 32f11db commit 2846c11
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
2 changes: 2 additions & 0 deletions docs/user-guide/node-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ The difference between the `configOverrides` and `config` options is this: If an

A file glob, or array of file globs. Ultimately passed to [node-glob](https://github.com/isaacs/node-glob) to figure out what files you want to lint.

Relative globs are considered relative to `process.cwd()`.

`node_modules` and `bower_components` are always ignored.

### `formatter`
Expand Down
4 changes: 3 additions & 1 deletion lib/augmentConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,9 @@ function addProcessorFunctions(config/*: stylelint$config*/)/*: stylelint$config
if (!config.processors) return config

const codeProcessors = []
const resultProcessors = [];[].concat(config.processors).forEach(processorConfig => {
const resultProcessors = []

;[].concat(config.processors).forEach(processorConfig => {
const processorKey = JSON.stringify(processorConfig)

let initializedProcessor
Expand Down
23 changes: 17 additions & 6 deletions lib/lintSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,36 @@
const _ = require("lodash")
const assignDisabledRanges = require("./assignDisabledRanges")
const configurationError = require("./utils/configurationError")
const path = require("path")
const ruleDefinitions = require("./rules")

// Run stylelint on a PostCSS Result, either one that is provided
// or one that we create
module.exports = function (stylelint/*: stylelint$internalApi*/)/*: Promise<Object>*/ {
const options/*: {
module.exports = function (
stylelint/*: stylelint$internalApi*/,
options/*: {
code?: string,
codeFilename?: string,
filePath?: string,
codeFilename?: string, // Must be an absolute file path
filePath?: string, // Must be an absolute file path
existingPostcssResult?: Object,
}*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}
}*/
)/*: Promise<Object>*/ {
options = options || {}

if (!options.filePath && options.code === undefined && !options.existingPostcssResult) {
return Promise.reject("You must provide filePath, code, or existingPostcssResult")
return Promise.reject(new Error("You must provide filePath, code, or existingPostcssResult"))
}

const isCodeNotFile = options.code !== undefined

const inputFilePath = isCodeNotFile ? options.codeFilename : options.filePath
if (inputFilePath !== undefined && !path.isAbsolute(inputFilePath)) {
if (isCodeNotFile) {
return Promise.reject(new Error("codeFilename must be an absolute path"))
} else {
return Promise.reject(new Error("filePath must be an absolute path"))
}
}

const getIsIgnored = stylelint.isPathIgnored(inputFilePath).catch(err => {
if (isCodeNotFile && err.code === "ENOENT") return false
Expand Down
18 changes: 13 additions & 5 deletions lib/postcssPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
const _ = require("lodash")
const createStylelint = require("./createStylelint")
const postcss = require("postcss")
const path = require("path")

module.exports = postcss.plugin("stylelint", function () {
const options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}

const tailoredOptions/*: Object*/ = options.rules ? { config: options } : options
module.exports = postcss.plugin("stylelint", function (options) {
options = options || {}

const tailoredOptions/*: Object*/ = options.rules
? { config: options }
: options
const stylelint = createStylelint(tailoredOptions)

return (root, result) => {
let filePath = options.from || _.get(root, "source.input.file")
if (filePath !== undefined && !path.isAbsolute(filePath)) {
filePath = path.join(process.cwd(), filePath)
}

return stylelint._lintSource({
filePath: options.from || _.get(root, "source.input.file"),
filePath,
existingPostcssResult: result,
})
}
Expand Down
16 changes: 14 additions & 2 deletions lib/standalone.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* @flow */
"use strict"
const path = require("path")
const formatters = require("./formatters")
const createStylelint = require("./createStylelint")
const globby = require("globby")
Expand Down Expand Up @@ -51,7 +52,13 @@ module.exports = function (options/*: Object */)/*: Promise<stylelint$standalone
})

if (!files) {
return stylelint._lintSource({ code, codeFilename }).then(postcssResult => {
const absoluteCodeFilename = (codeFilename !== undefined && !path.isAbsolute(codeFilename))
? path.join(process.cwd(), codeFilename)
: codeFilename
return stylelint._lintSource({
code,
codeFilename: absoluteCodeFilename,
}).then(postcssResult => {
return stylelint._createStylelintResult(postcssResult)
}).catch(handleError).then(stylelintResult => {
return prepareReturnValue([stylelintResult])
Expand All @@ -70,7 +77,12 @@ module.exports = function (options/*: Object */)/*: Promise<stylelint$standalone
}

const getStylelintResults = filePaths.map(filePath => {
return stylelint._lintSource({ filePath }).then(postcssResult => {
const absoluteFilepath = (!path.isAbsolute(filePath))
? path.join(process.cwd(), filePath)
: filePath
return stylelint._lintSource({
filePath: absoluteFilepath,
}).then(postcssResult => {
return stylelint._createStylelintResult(postcssResult, filePath)
}).catch(handleError)
})
Expand Down

0 comments on commit 2846c11

Please sign in to comment.