Skip to content

Commit

Permalink
🔨 chore: fix cosistent newline
Browse files Browse the repository at this point in the history
Signed-off-by: Pauline <[email protected]>
  • Loading branch information
pauliesnug committed Sep 10, 2024
1 parent f6127de commit bf62480
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
packages/eslint-config/src/types/typegen.d.ts
packages/eslint-config/.eslint-config-inspector
packages/eslint-config/_fixtures
packages/eslint-config/test/_fixtures

# Created by https://www.toptal.com/developers/gitignore/api/node,yarn,git,macos,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=node,yarn,git,macos,visualstudiocode
Expand Down
6 changes: 1 addition & 5 deletions packages/eslint-config/src/configs/gitignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ export async function gitignore(options: OptionsGitignore = {}): Promise<TypedFl

const content = fs.readFileSync(f, 'utf-8');
const relativePath = path.relative(cwd, path.dirname(f));
const globs = content.split(/\r?\n/u)
.filter(l => l && !l.startsWith('#'))
.map(l => convertIgnorePatternToMinimatch(l))
.map(g => relativeMinimatch(g, relativePath, cwd))
.filter(g => g !== null);
const globs = content.split(/\r?\n/u).filter(l => l && !l.startsWith('#')).map(l => convertIgnorePatternToMinimatch(l)).map(g => relativeMinimatch(g, relativePath, cwd)).filter(g => g !== null);

ignores.push(...globs);
});
Expand Down
53 changes: 42 additions & 11 deletions packages/eslint-plugin/src/rules/consistent-chaining.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@ const valids: ValidTestCase[] = [
.baz()
.boo()
`,
{
code: $`
Math.random()
.toString()
.split('')
.map(Number)
`,
options: [{ allowFirstPropertyAccess: true }],
},
$`
Math.random()
.toString()
.split('')
.map(Number)
`,
$`
foo.bar.baz()
.toString()
.split('')
.map(Number)
`,
$`
foo.bar.baz
.toString()
.split('')
.map(Number)
`,
];

const invalid: InvalidTestCase[] = [
Expand Down Expand Up @@ -93,7 +102,12 @@ const invalid: InvalidTestCase[] = [
.split('').map(Number)
`,
output: o => expect(o)
.toMatchInlineSnapshot(`" Math.random().toString().split('').map(Number)"`),
.toMatchInlineSnapshot(`
" Math.random()
.toString()
.split('')
.map(Number)"
`),
},
{
code: $`
Expand All @@ -102,7 +116,12 @@ const invalid: InvalidTestCase[] = [
.split('').map(Number)
`,
output: o => expect(o)
.toMatchInlineSnapshot(`" this.foo.toString().split('').map(Number)"`),
.toMatchInlineSnapshot(`
" this.foo
.toString()
.split('')
.map(Number)"
`),
},
{
code: $`
Expand Down Expand Up @@ -138,6 +157,18 @@ const invalid: InvalidTestCase[] = [
.filter(x => x)"
`),
},
{
code: $`
foo.bar.bar
.filter().map()
`,
output: o => expect(o)
.toMatchInlineSnapshot(`
" foo.bar.bar
.filter()
.map()"
`),
},
];

run({
Expand Down
22 changes: 12 additions & 10 deletions packages/eslint-plugin/src/rules/consistent-chaining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { createEslintRule } from '../utils';
export const RULE_NAME = 'consistent-chaining';
export type MessageIds = 'shouldWrap' | 'shouldNotWrap';
export type Options = [{
allowFirstPropertyAccess?: boolean;
allowLeadingPropertyAccess?: boolean;
}];

const defaultOptions: Options = [{
allowFirstPropertyAccess: true,
allowLeadingPropertyAccess: true,
}];

export default createEslintRule<Options, MessageIds>({
Expand All @@ -24,10 +24,10 @@ export default createEslintRule<Options, MessageIds>({
schema: [{
type: 'object',
properties: {
allowFirstPropertyAccess: {
allowLeadingPropertyAccess: {
type: 'boolean',
description: 'Allow first property access to be on the same line',
default: defaultOptions[0].allowFirstPropertyAccess,
description: 'Allow leading property access to be on the same line',
default: defaultOptions[0].allowLeadingPropertyAccess,
},
} satisfies Readonly<Record<keyof Options[0], JSONSchema4>>,
additionalProperties: false,
Expand All @@ -47,7 +47,8 @@ export default createEslintRule<Options, MessageIds>({
root: TSESTree.Node;
current: TSESTree.Node | undefined;
mode: 'single' | 'multi' | null;
} = { root: node, current: undefined, mode: null };
leadingPropertyAccess?: boolean;
} = { root: node, current: undefined, mode: null, leadingPropertyAccess: options.allowLeadingPropertyAccess ?? true };

while (store.root.parent && ['CallExpression', 'MemberExpression'].includes(store.root.parent.type))
store.root = store.root.parent;
Expand Down Expand Up @@ -77,20 +78,21 @@ export default createEslintRule<Options, MessageIds>({
}
}

members.forEach((m, i) => {
members.forEach((m) => {
const token = context.sourceCode.getTokenBefore(m.property)!;
const tokenBefore = context.sourceCode.getTokenBefore(token)!;
const currentMode: 'single' | 'multi'
= token.loc.start.line === tokenBefore.loc.end.line ? 'single' : 'multi';

if (
i === 0
&& options.allowFirstPropertyAccess
&& ['Identifier', 'ThisExpression'].includes(m.object.type)
store.leadingPropertyAccess
&& ['Identifier', 'Literal', 'MemberExpression', 'ThisExpression'].includes(m.object.type)
&& currentMode === 'single'
)
return;

store.leadingPropertyAccess = false;

if (store.mode === null) {
store.mode = currentMode;
return;
Expand Down
4 changes: 1 addition & 3 deletions packages/utils/src/core/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ const _ls = (path: string): Files => readdirSync(path).map(_stat);

function _f(f: Files, type?: string): string[] {
return type
? f.filter(l => l.stats.isFile())
.filter(l => filename(l.path).endsWith(`.${type}`))
.map(l => filename(l.path).slice(0, -(type.length + 1)))
? f.filter(l => l.stats.isFile()).filter(l => filename(l.path).endsWith(`.${type}`)).map(l => filename(l.path).slice(0, -(type.length + 1)))
: f.filter(l => l.stats.isFile()).map(l => filename(l.path));
}

Expand Down

0 comments on commit bf62480

Please sign in to comment.