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

Lots of Flow work #2163

Merged
merged 1 commit into from Dec 4, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions decls/postcss.js
Expand Up @@ -21,10 +21,14 @@ export type postcss$atRule = {
afterName: string,
},
type: string,
parent: Object,
nodes: Array<Object>
}

export type postcss$rule = {
raws: Object,
selector: string,
type: string,
parent: Object,
nodes: Array<Object>,
}
19 changes: 15 additions & 4 deletions lib/assignDisabledRanges.js
Expand Up @@ -9,14 +9,18 @@ const disableLineCommand = COMMAND_PREFIX + "disable-line"
const disableNextLineCommand = COMMAND_PREFIX + "disable-next-line"
const ALL_RULES = "all"

// Run it like a plugin ...
/*:: type disabledRangeObject = {
[ruleName: string]: Array<{
start: number,
end?: number,
}>
}*/
module.exports = function (root/*: Object*/, result/*: Object*/) {

// Run it like a plugin ...
module.exports = function (
root/*: Object*/,
result/*: Object*/
) {
result.stylelint = result.stylelint || {}

// Most of the functions below work via side effects mutating
Expand All @@ -41,7 +45,11 @@ module.exports = function (root/*: Object*/, result/*: Object*/) {
})
}

function disableLine(line/*: number*/, ruleName/*: string*/, comment/*: postcss$comment*/) {
function disableLine(
line/*: number*/,
ruleName/*: string*/,
comment/*: postcss$comment*/
) {
if (ruleIsDisabled(ALL_RULES)) {
throw comment.error("All rules have already been disabled", { plugin: "stylelint" })
}
Expand Down Expand Up @@ -132,7 +140,10 @@ module.exports = function (root/*: Object*/, result/*: Object*/) {
}
}

function getCommandRules(command/*: string*/, fullText/*: string*/)/*: Array<string>*/ {
function getCommandRules(
command/*: string*/,
fullText/*: string*/
)/*: Array<string>*/ {
const rules = _.compact(fullText.slice(command.length).split(",")).map(r => r.trim())
if (_.isEmpty(rules)) {
return [ALL_RULES]
Expand Down
65 changes: 42 additions & 23 deletions lib/augmentConfig.js
Expand Up @@ -15,7 +15,11 @@ const FILE_NOT_FOUND_ERROR_CODE = "ENOENT"
// - Merges config and configOverrides
// - Makes all paths absolute
// - Merges extends
function augmentConfigBasic(stylelint/*: stylelint$internalApi*/, config/*: stylelint$config*/, configDir/*: string*/)/*: Promise<stylelint$config>*/ {
function augmentConfigBasic(
stylelint/*: stylelint$internalApi*/,
config/*: stylelint$config*/,
configDir/*: string*/
)/*: Promise<stylelint$config>*/ {
return Promise.resolve().then(() => {
return _.merge(config, stylelint._options.configOverrides)
}).then(augmentedConfig => {
Expand All @@ -28,13 +32,13 @@ function augmentConfigBasic(stylelint/*: stylelint$internalApi*/, config/*: styl
// Extended configs need to be run through augmentConfigBasic
// but do not need the full treatment. Things like pluginFunctions
// will be resolved and added by the parent config.
function augmentConfigExtended(stylelint/*: stylelint$internalApi*/, cosmiconfigResultArg/*: ?{
config: stylelint$config,
filepath: string,
}*/)/*: Promise<?{
config: stylelint$config,
filepath: string,
}>*/ {
function augmentConfigExtended(
stylelint/*: stylelint$internalApi*/,
cosmiconfigResultArg/*: ?{
config: stylelint$config,
filepath: string,
}*/
)/*: Promise<?{ config: stylelint$config, filepath: string }>*/ {
const cosmiconfigResult = cosmiconfigResultArg // Lock in for Flow
if (!cosmiconfigResult) return Promise.resolve(null)

Expand All @@ -48,13 +52,13 @@ function augmentConfigExtended(stylelint/*: stylelint$internalApi*/, cosmiconfig
})
}

function augmentConfigFull(stylelint/*: stylelint$internalApi*/, cosmiconfigResultArg/*: ?{
config: stylelint$config,
filepath: string,
}*/)/*: Promise<?{
config: stylelint$config,
filepath: string
}>*/ {
function augmentConfigFull(
stylelint/*: stylelint$internalApi*/,
cosmiconfigResultArg/*: ?{
config: stylelint$config,
filepath: string,
}*/
)/*: Promise<?{ config: stylelint$config, filepath: string }>*/ {
const cosmiconfigResult = cosmiconfigResultArg // Lock in for Flow
if (!cosmiconfigResult) return Promise.resolve(null)

Expand Down Expand Up @@ -85,7 +89,10 @@ function augmentConfigFull(stylelint/*: stylelint$internalApi*/, cosmiconfigResu

// Load a file ignore ignore patterns, if there is one;
// then add them to the config as an ignorePatterns property
function addIgnorePatterns(stylelint/*: stylelint$internalApi*/, config/*: stylelint$config*/)/*: Promise<stylelint$config>*/ {
function addIgnorePatterns(
stylelint/*: stylelint$internalApi*/,
config/*: stylelint$config*/
)/*: Promise<stylelint$config>*/ {
const ignoreFilePath = stylelint._options.ignorePath || DEFAULT_IGNORE_FILENAME
const absoluteIgnoreFilePath = path.isAbsolute(ignoreFilePath) ? ignoreFilePath : path.resolve(process.cwd(), ignoreFilePath)

Expand Down Expand Up @@ -114,7 +121,10 @@ function addIgnorePatterns(stylelint/*: stylelint$internalApi*/, config/*: style
// - plugins
// - processors
// (extends handled elsewhere)
function absolutizePaths(config/*: stylelint$config*/, configDir/*: string*/)/*: stylelint$config*/ {
function absolutizePaths(
config/*: stylelint$config*/,
configDir/*: string*/
)/*: stylelint$config*/ {
if (config.ignoreFiles) {
config.ignoreFiles = [].concat(config.ignoreFiles).map(glob => {
if (path.isAbsolute(glob.replace(/^!/, ""))) return glob
Expand All @@ -137,7 +147,10 @@ function absolutizePaths(config/*: stylelint$config*/, configDir/*: string*/)/*:

// Processors are absolutized in their own way because
// they can be and return a string or an array
function absolutizeProcessors(processors/*: stylelint$configProcessors*/, configDir/*: string*/)/*: stylelint$configProcessors*/ {
function absolutizeProcessors(
processors/*: stylelint$configProcessors*/,
configDir/*: string*/
)/*: stylelint$configProcessors*/ {
const normalizedProcessors = Array.isArray(processors) ? processors : [processors]

return normalizedProcessors.map(item => {
Expand All @@ -149,7 +162,11 @@ function absolutizeProcessors(processors/*: stylelint$configProcessors*/, config
})
}

function extendConfig(stylelint/*: stylelint$internalApi*/, config/*: stylelint$config*/, configDir/*: string*/)/*: Promise<stylelint$config>*/ {
function extendConfig(
stylelint/*: stylelint$internalApi*/,
config/*: stylelint$config*/,
configDir/*: string*/
)/*: Promise<stylelint$config>*/ {
if (config.extends === undefined) return Promise.resolve(config)
const normalizedExtends = Array.isArray(config.extends) ? config.extends : [config.extends]

Expand All @@ -168,10 +185,12 @@ function extendConfig(stylelint/*: stylelint$internalApi*/, config/*: stylelint$
})
}

function loadExtendedConfig(stylelint/*: stylelint$internalApi*/, config/*: stylelint$config*/, configDir/*: string*/, extendLookup/*: string*/)/*: Promise<?{
config: stylelint$config,
filepath: string,
}>*/ {
function loadExtendedConfig(
stylelint/*: stylelint$internalApi*/,
config/*: stylelint$config*/,
configDir/*: string*/,
extendLookup/*: string*/
)/*: Promise<?{ config: stylelint$config, filepath: string }>*/ {
const extendPath = getModulePath(configDir, extendLookup)
return stylelint._extendExplorer.load(null, extendPath)
}
Expand Down
6 changes: 5 additions & 1 deletion lib/createStylelintResult.js
Expand Up @@ -2,7 +2,11 @@
"use strict"
const _ = require("lodash")

module.exports = function (stylelint/*: stylelint$internalApi*/, postcssResult/*: Object*/, filePath/*:: ?: string*/)/*: Promise<stylelint$result>*/ {
module.exports = function (
stylelint/*: stylelint$internalApi*/,
postcssResult/*: Object*/,
filePath/*:: ?: string*/
)/*: Promise<stylelint$result>*/ {
const source = !postcssResult.root.source ? undefined : postcssResult.root.source.input.file || postcssResult.root.source.input.id

// Strip out deprecation warnings from the messages
Expand Down
8 changes: 4 additions & 4 deletions lib/getConfigForFile.js
Expand Up @@ -4,10 +4,10 @@ const augmentConfigFull = require("./augmentConfig").augmentConfigFull
const configurationError = require("./utils/configurationError")
const path = require("path")

module.exports = function (stylelint/*: stylelint$internalApi*/, searchPath/*:: ?: string*/)/*: Promise<?{
config: stylelint$config,
filepath: string
}>*/ {
module.exports = function (
stylelint/*: stylelint$internalApi*/,
searchPath/*:: ?: string*/
)/*: Promise<?{ config: stylelint$config, filepath: string }>*/ {
searchPath = searchPath || process.cwd()

const optionsConfig = stylelint._options.config
Expand Down
14 changes: 7 additions & 7 deletions lib/getPostcssResult.js
Expand Up @@ -11,13 +11,13 @@ const postcssProcessor = postcss()

module.exports = function (stylelint/*: stylelint$internalApi*/)/*: Promise<?Object>*/ {
const options/*: {
code?: string,
codeFilename?: string,
filePath?: string,
codeProcessors?: Array<Function>,
syntax?: stylelint$syntaxes,
customSyntax?: string
}*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}
code?: string,
codeFilename?: string,
filePath?: string,
codeProcessors?: Array<Function>,
syntax?: stylelint$syntaxes,
customSyntax?: string
}*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}

const cached = stylelint._postcssResultCache.get(options.filePath)
if (cached) return Promise.resolve(cached)
Expand Down
5 changes: 4 additions & 1 deletion lib/isPathIgnored.js
Expand Up @@ -9,7 +9,10 @@ const path = require("path")
// and will have incorporated any .stylelintignore file that was found
// into its ignorePatterns property. We then check the path
// against these.
module.exports = function (stylelint/*: stylelint$internalApi*/, filePathArg/*:: ?: string*/)/*: Promise<boolean>*/ {
module.exports = function (
stylelint/*: stylelint$internalApi*/,
filePathArg/*:: ?: string*/
)/*: Promise<boolean>*/ {
const filePath = filePathArg // to please Flow
if (!filePath) {
return Promise.resolve(false)
Expand Down
16 changes: 10 additions & 6 deletions lib/lintSource.js
Expand Up @@ -9,11 +9,11 @@ const ruleDefinitions = require("./rules")
// or one that we create
module.exports = function (stylelint/*: stylelint$internalApi*/)/*: Promise<Object>*/ {
const options/*: {
code?: string,
codeFilename?: string,
filePath?: string,
existingPostcssResult?: Object,
}*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}
code?: string,
codeFilename?: string,
filePath?: string,
existingPostcssResult?: Object,
}*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}

if (!options.filePath && options.code === undefined && !options.existingPostcssResult) {
return Promise.reject("You must provide filePath, code, or existingPostcssResult")
Expand Down Expand Up @@ -64,7 +64,11 @@ module.exports = function (stylelint/*: stylelint$internalApi*/)/*: Promise<Obje
})
}

function lintPostcssResult(stylelint/*: stylelint$internalApi*/, postcssResult/*: Object*/, config/*: stylelint$config*/)/*: Promise<>*/ {
function lintPostcssResult(
stylelint/*: stylelint$internalApi*/,
postcssResult/*: Object*/,
config/*: stylelint$config*/
)/*: Promise<>*/ {
postcssResult.stylelint = postcssResult.stylelint || {}
postcssResult.stylelint.ruleSeverities = {}
postcssResult.stylelint.customMessages = {}
Expand Down
19 changes: 11 additions & 8 deletions lib/needlessDisables.js
Expand Up @@ -71,14 +71,17 @@ module.exports = function (results/*: Array<stylelint$result>*/)/*: stylelint$ne
return report
}

function isWarningInRange(warning/*: {
rule: string,
line: number,
}*/, range/*: {
rules?: Array<string>,
start: number,
end?: number,
}*/)/*: boolean*/ {
function isWarningInRange(
warning/*: {
rule: string,
line: number
}*/,
range/*: {
rules?: Array<string>,
start: number,
end?: number,
}*/
)/*: boolean*/ {
const rule = warning.rule,
line = warning.line

Expand Down
6 changes: 5 additions & 1 deletion lib/normalizeRuleSettings.js
Expand Up @@ -14,7 +14,11 @@ const _ = require("lodash")
// standard form: [primaryOption, secondaryOption]
// Except in the cases with null, a & b, in which case
// null is returned
module.exports = function (rawSettings/*: stylelint$configRuleSettings*/, ruleName/*: string*/, primaryOptionArray/*:: ?: boolean*/)/*: ?Array<any | [any, Object]>*/ {
module.exports = function (
rawSettings/*: stylelint$configRuleSettings*/,
ruleName/*: string*/,
primaryOptionArray/*:: ?: boolean*/
)/*: ?Array<any | [any, Object]>*/ {
if (rawSettings === null) {
return null
}
Expand Down
5 changes: 4 additions & 1 deletion lib/utils/beforeBlockString.js
@@ -1,6 +1,9 @@
/* @flow */
"use strict"
module.exports = function (statement/*: Object*/, options/*:: ?: Object*/)/*: string*/ {
module.exports = function (
statement/*: Object*/,
options/*:: ?: Object*/
)/*: string*/ {
options = options || {}

let result = ""
Expand Down
8 changes: 3 additions & 5 deletions lib/utils/configurationError.js
@@ -1,13 +1,11 @@
/* @flow */
"use strict"

/**
* Create configurationError from text and set CLI exit code
*
* @param {string} text
* @return {Error} - The error, with text and exit code
*/
module.exports = function (text) {
const err = new Error(text)
module.exports = function (text/*: string */)/* Object */ {
const err/*: Object*/ = new Error(text)
err.code = 78
return err
}
13 changes: 5 additions & 8 deletions lib/utils/containsString.js
@@ -1,3 +1,4 @@
/* @flow */
"use strict"

/**
Expand All @@ -6,15 +7,11 @@
*
* Any strings starting and ending with `/` are ignored. Use the
* matchesStringOrRegExp() util to match regexes.
*
* @param {string} input
* @param {string|array} comparison
* @return {boolean|object} `false` if no match is found.
* If a match is found, returns an object with these properties:
* - `match`: the `input` value that had a match
* - `pattern`: the `comparison` pattern that had a match
*/
module.exports = function containsString(input, comparison) {
module.exports = function containsString(
input/*: string*/,
comparison/*: string | Array<string>*/
)/*: false | { match: string, pattern: string }*/ {
if (!Array.isArray(comparison)) {
return testAgainstString(input, comparison)
}
Expand Down
6 changes: 2 additions & 4 deletions lib/utils/declarationValueIndex.js
@@ -1,12 +1,10 @@
/* @flow */
"use strict"

/**
* Get the index of a declaration's value
*
* @param {Decl} decl
* @return {int} The index
*/
module.exports = function (decl) {
module.exports = function (decl/*: Object*/)/*: number*/ {
const beforeColon = decl.toString().indexOf(":")
const afterColon = decl.raw("between").length - decl.raw("between").indexOf(":")
return beforeColon + afterColon
Expand Down
6 changes: 2 additions & 4 deletions lib/utils/findAnimationName.js
@@ -1,3 +1,4 @@
/* @flow */
"use strict"

const keywordSets = require("../reference/keywordSets")
Expand All @@ -8,11 +9,8 @@ const postcssValueParser = require("postcss-value-parser")

/**
* Get the font-families within a `font` shorthand property value.
*
* @param {string} value
* @return {object} Collection font-family nodes
*/
module.exports = function findAnimationName(value) {
module.exports = function findAnimationName(value/*: string*/)/*: Array<Object>*/ {
const animationNames = []

const valueNodes = postcssValueParser(value)
Expand Down