Skip to content

Commit

Permalink
Merge pull request #133 from srowhani/fix/multiple-include-paths
Browse files Browse the repository at this point in the history
feat(fix): multiple include paths
  • Loading branch information
srowhani authored Nov 25, 2019
2 parents 69ab124 + 4791a91 commit 38aaa00
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 68 deletions.
13 changes: 5 additions & 8 deletions src/helpers/get-config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { Nullable, SlfParserOptions } from '@src/types';
import * as fs from 'fs';
import * as path from 'path';

const merge = require('merge');
const path = require('path');
const fs = require('fs');
const yaml = require('js-yaml');

type OptionParser = (filename: string) => Nullable<SlfParserOptions>;
interface MappedParserOptions {
[key: string]: OptionParser;
}
interface JSONObject {
[key: string]: any;
}

const _configurationProxy = new Proxy<MappedParserOptions>(
{
Expand Down Expand Up @@ -46,10 +43,10 @@ function parseModule(filename: string): Nullable<SlfParserOptions> {
return require(path.resolve(filename));
}

export const getConfig = (filename: string): JSONObject =>
export const getConfig = (filename: string): Record<string, any> =>
_configurationProxy[filename];

export const mergeConfig = (
baseConfig: JSONObject,
extendedConfig: JSONObject,
baseConfig: Record<string, any>,
extendedConfig: Record<string, any>,
) => merge.recursive(true, baseConfig, extendedConfig);
115 changes: 61 additions & 54 deletions src/sass-lint-auto-fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,77 +25,84 @@ export function autoFixSassFactory(config: ConfigOpts) {
const _slRules = config.slRules || slRules;
const _slConfig = config.slConfig || slConfig;

const files = glob.sync(config.files.include, {
ignore: config.files.ignore,
});
const patternsToInclude =
typeof config.files.include === 'string'
? [config.files.include]
: config.files.include;

return function* autoFixSass(
options: LintOpts,
): IterableIterator<Resolution> {
for (const filename of files) {
const content = fs.readFileSync(filename).toString();
if (content !== null && content !== undefined && content.length > 0) {
const fileExtension = path
.extname(filename)
.substr(1)
.toLowerCase();

if (isValidExtension(fileExtension)) {
let ast;

try {
ast = parse(content, {
syntax: fileExtension,
});
} catch (e) {
logger.warn('parse', filename, e);
return;
}

const rules = _slRules(_slConfig(options));

const filteredRules: SlRule[] = rules.filter(
(rule: SlRule) => config.resolvers[rule.rule.name],
);

for (const rule of filteredRules) {
const { name } = rule.rule;
let resolver: Resolver;
for (const pattern of patternsToInclude) {
const files = glob.sync(pattern, {
ignore: config.files.ignore,
});

for (const filename of files) {
const content = fs.readFileSync(filename).toString();
if (content !== null && content !== undefined && content.length > 0) {
const fileExtension = path
.extname(filename)
.substr(1)
.toLowerCase();

if (isValidExtension(fileExtension)) {
let ast;

try {
resolver = createModule({
name,
ast,
rule,
ast = parse(content, {
syntax: fileExtension,
});
} catch (e) {
SentryService.reportIncident(e);
logger.warn('resolver', `Module '${name}' doesn't exist.`);
logger.warn('parse', filename, e);
return;
}

try {
const detects = rule.rule.detect(ast, rule);
const rules = _slRules(_slConfig(options));

if (detects.length > 0) {
logger.verbose(
'fix',
`Running resolver "${name}" on "${filename}"`,
);
const filteredRules: SlRule[] = rules.filter(
(rule: SlRule) => config.resolvers[rule.rule.name],
);

ast = resolver.fix(detects);
yield {
for (const rule of filteredRules) {
const { name } = rule.rule;
let resolver: Resolver;

try {
resolver = createModule({
name,
ast,
filename,
rule,
};
}
} catch (e) {
if (!config.options.optOut) {
});
} catch (e) {
SentryService.reportIncident(e);
logger.warn('resolver', `Module '${name}' doesn't exist.`);
return;
}

try {
const detects = rule.rule.detect(ast, rule);

if (detects.length > 0) {
logger.verbose(
'fix',
`Running resolver "${name}" on "${filename}"`,
);

ast = resolver.fix(detects);
yield {
ast,
filename,
rule,
};
}
} catch (e) {
if (!config.options.optOut) {
SentryService.reportIncident(e);
}
// TODO: Friendly way to inform user that an unexpected error occured
logger.warn('error', e);
}
// TODO: Friendly way to inform user that an unexpected error occured
logger.warn('error', e);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface SlfParserOptions {
}

export interface SlfParserSyntaxOptions {
include: string[];
include: (keyof typeof ValidFileType)[];
}

export interface Resolution {
Expand All @@ -29,7 +29,7 @@ export interface ConfigOpts {
slRules?: any;
slConfig?: any;
files: {
include: string;
include: string | string[];
ignore?: string;
};
syntax: {
Expand Down
2 changes: 1 addition & 1 deletion src/types/sass-lint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ declare module 'sass-lint' {
'cache-config'?: boolean;
};
files: {
include: string;
include: string | string[];
};
rules: Ruleset;
}
Expand Down
6 changes: 3 additions & 3 deletions test/helpers/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const path = require('path');
import { Node, parse } from 'gonzales-pe-sl';
import { LintOpts, lintText, Ruleset } from 'sass-lint';
export interface MockLintConfigParams {
pattern?: string;
pattern?: string | string[];
lintRules: Ruleset;
}

Expand All @@ -35,14 +35,14 @@ export function createMockLintOptions({
}

export function* resolvePattern(
pattern: string,
pattern: string | string[],
lintRules: Ruleset,
logger: ILogger,
): IterableIterator<Resolution> {
const configOptions: ConfigOpts = {
logger,
files: {
include: pattern,
include: typeof pattern === 'string' ? [pattern] : pattern,
},
resolvers: { [Object.keys(lintRules)[0]]: 1 },
syntax: {
Expand Down
28 changes: 28 additions & 0 deletions test/src/sass-lint-auto-fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,32 @@ describe('sass-lint-auto-fix', () => {
const emptyResolutionSet = [...slaf(customSlConfig)];
expect(emptyResolutionSet.length).toEqual(0);
});

it('accepts a list of files for include with permissive lint pattern', () => {
const logger = createLogger({ silentEnabled: true });

const options: Partial<ConfigOpts> = {
logger,
files: {
include: [
'test/sass/attribute-quotes.sass',
'test/sass/attribute-quotes.scss',
],
},
resolvers: { 'attribute-quotes': 1 },
syntax: {
include: ['scss', 'sass'],
},
};

const slaf = autoFixSassFactory(options as ConfigOpts);
const resolutions = [
...slaf({
options: {},
files: { include: '**/*.s(a|c)ss' },
rules: { 'attribute-quotes': 1 },
} as LintOpts),
];
expect(resolutions.length).toEqual(2);
});
});

0 comments on commit 38aaa00

Please sign in to comment.