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

Ensure absolute paths to processors #2207

Merged
merged 1 commit into from
Dec 29, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this pattern the same here and in standalone.js? If so, is it possible to create a util named something like absolutizePath() and use that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this pattern is util-worthy. It's just very straightforward JS.

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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment about creating a util.

? 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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment about creating a util.

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