Skip to content

Commit

Permalink
re-transformed rules to new format
Browse files Browse the repository at this point in the history
  • Loading branch information
benmosher committed Sep 24, 2016
1 parent a560d31 commit 1f7a9ca
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 88 deletions.
160 changes: 83 additions & 77 deletions src/rules/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,93 +6,99 @@ import assign from 'object-assign'
import resolve from 'eslint-module-utils/resolve'
import { isBuiltIn } from '../core/importType'

module.exports = function (context) {
const configuration = context.options[0] || 'never'
const defaultConfig = typeof configuration === 'string' ? configuration : null
const modifiers = assign(
{},
typeof configuration === 'object' ? configuration : context.options[1]
)
const enumValues = { enum: [ 'always', 'never' ] }
const patternProperties = {
type: 'object',
patternProperties: { '.*': enumValues },
}

function isUseOfExtensionRequired(extension) {
if (!has(modifiers, extension)) { modifiers[extension] = defaultConfig }
return modifiers[extension] === 'always'
}
module.exports = {
meta: {
docs: {},

function isUseOfExtensionForbidden(extension) {
if (!has(modifiers, extension)) { modifiers[extension] = defaultConfig }
return modifiers[extension] === 'never'
}
schema: {
anyOf: [
{
type: 'array',
items: [enumValues],
additionalItems: false,
},
{
type: 'array',
items: [patternProperties],
additionalItems: false,
},
{
type: 'array',
items: [
enumValues,
patternProperties,
],
additionalItems: false,
},
],
},
},

function isResolvableWithoutExtension(file) {
const extension = path.extname(file)
const fileWithoutExtension = file.slice(0, -extension.length)
const resolvedFileWithoutExtension = resolve(fileWithoutExtension, context)
create: function (context) {
const configuration = context.options[0] || 'never'
const defaultConfig = typeof configuration === 'string' ? configuration : null
const modifiers = assign(
{},
typeof configuration === 'object' ? configuration : context.options[1]
)

return resolvedFileWithoutExtension === resolve(file, context)
}
function isUseOfExtensionRequired(extension) {
if (!has(modifiers, extension)) { modifiers[extension] = defaultConfig }
return modifiers[extension] === 'always'
}

function checkFileExtension(node) {
const { source } = node
const importPath = source.value
function isUseOfExtensionForbidden(extension) {
if (!has(modifiers, extension)) { modifiers[extension] = defaultConfig }
return modifiers[extension] === 'never'
}

// don't enforce anything on builtins
if (isBuiltIn(importPath, context.settings)) return
function isResolvableWithoutExtension(file) {
const extension = path.extname(file)
const fileWithoutExtension = file.slice(0, -extension.length)
const resolvedFileWithoutExtension = resolve(fileWithoutExtension, context)

const resolvedPath = resolve(importPath, context)
return resolvedFileWithoutExtension === resolve(file, context)
}

// get extension from resolved path, if possible.
// for unresolved, use source value.
const extension = path.extname(resolvedPath || importPath).substring(1)
function checkFileExtension(node) {
const { source } = node
const importPath = source.value

if (!extension || !endsWith(importPath, extension)) {
if (isUseOfExtensionRequired(extension) && !isUseOfExtensionForbidden(extension)) {
context.report({
node: source,
message:
`Missing file extension ${extension ? `"${extension}" ` : ''}for "${importPath}"`,
})
}
} else if (extension) {
if (isUseOfExtensionForbidden(extension) && isResolvableWithoutExtension(importPath)) {
context.report({
node: source,
message: `Unexpected use of file extension "${extension}" for "${importPath}"`,
})
}
}
}
// don't enforce anything on builtins
if (isBuiltIn(importPath, context.settings)) return

return {
ImportDeclaration: checkFileExtension,
}
}
const resolvedPath = resolve(importPath, context)

const enumValues = { enum: [ 'always', 'never' ] }
const patternProperties = {
type: 'object',
patternProperties: { '.*': enumValues },
}
// get extension from resolved path, if possible.
// for unresolved, use source value.
const extension = path.extname(resolvedPath || importPath).substring(1)

module.exports.schema = {
anyOf: [
{
type: 'array',
items: [enumValues],
additionalItems: false,
},
{
type: 'array',
items: [patternProperties],
additionalItems: false,
},
{
type: 'array',
items: [
enumValues,
patternProperties,
],
additionalItems: false,
},
],
if (!extension || !endsWith(importPath, extension)) {
if (isUseOfExtensionRequired(extension) && !isUseOfExtensionForbidden(extension)) {
context.report({
node: source,
message:
`Missing file extension ${extension ? `"${extension}" ` : ''}for "${importPath}"`,
})
}
} else if (extension) {
if (isUseOfExtensionForbidden(extension) && isResolvableWithoutExtension(importPath)) {
context.report({
node: source,
message: `Unexpected use of file extension "${extension}" for "${importPath}"`,
})
}
}
}

return {
ImportDeclaration: checkFileExtension,
}
},
}
28 changes: 17 additions & 11 deletions src/rules/no-dynamic-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ function isStaticValue(arg) {
(arg.type === 'TemplateLiteral' && arg.expressions.length === 0)
}

module.exports = function (context) {
return {
CallExpression(node) {
if (isRequire(node) && !isStaticValue(node.arguments[0])) {
context.report({
node,
message: 'Calls to require() should use string literals',
})
}
},
}
module.exports = {
meta: {
docs: {},
},

create: function (context) {
return {
CallExpression(node) {
if (isRequire(node) && !isStaticValue(node.arguments[0])) {
context.report({
node,
message: 'Calls to require() should use string literals',
})
}
},
}
},
}

0 comments on commit 1f7a9ca

Please sign in to comment.