Skip to content

Commit

Permalink
Merge pull request #261 from sharecjs/fix-format-utils
Browse files Browse the repository at this point in the history
fix: format utils
  • Loading branch information
epszaw authored Apr 16, 2022
2 parents 74729ff + 24b1ee6 commit 198a763
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 134 deletions.
4 changes: 4 additions & 0 deletions packages/sharec-utils/.npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
src/**/*.test.js
.gitignore
tsconfig.json
lib/test
jest.setup.js
27 changes: 13 additions & 14 deletions packages/sharec-utils/lib/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ const indentWithTab = (str = '') => {
* @returns {string}
*/
const indentWithSpace = (str = '', size = 2) => {
const { type, indent } = detectIndent(str)

if (type !== 'tab') return str

const { indent } = detectIndent(str)
const newIndent = Array(size).fill(' ').join('')

return str.replace(new RegExp(indent, 'gm'), newIndent)
Expand All @@ -84,16 +81,18 @@ const applyFormat = ({ filename, content, rules }) => {
const isYaml = filename && /\.ya?ml/.test(filename)
let result = content

if (indentType === 'tab' && !isYaml) {
result = indentWithTab(result)
}

if (indentType === 'space' || isYaml) {
result = indentWithSpace(result, indentSize)
}

if (eof && !hasEOF(content)) {
result += '\n'
switch (true) {
case indentType === 'tab' && !isYaml:
result = indentWithTab(result)
break
case indentType === 'space' || isYaml:
result = indentWithSpace(result, indentSize)
break
case eof && !hasEOF(content):
result += EOL
break
default:
break
}

return result
Expand Down
10 changes: 3 additions & 7 deletions packages/sharec-utils/lib/fs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-check
const nanomatch = require('nanomatch')
const minimatch = require('minimatch')
const { makedir, readdir, lstat } = require('./std')
const { join } = require('./path')

Expand Down Expand Up @@ -37,12 +37,8 @@ const find = async (path, pattern) => {
const fullFilePath = join(path, file)
const stats = await lstat(fullFilePath)

if (stats.isFile()) {
result.push(
...nanomatch([fullFilePath], pattern, {
dot: true,
}),
)
if (stats.isFile() && minimatch(fullFilePath, pattern, { dot: true })) {
result.push(fullFilePath)
continue
}

Expand Down
27 changes: 11 additions & 16 deletions packages/sharec-utils/lib/test/__snapshots__/format.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,63 +1,58 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`utils > format applyFormat should apply format to given string 1`] = `
exports[`utils > format applyFormat applies format to given string 1`] = `
"{
\\"foo\\": \\"foo\\",
\\"bar\\": \\"bar\\",
\\"baz\\": \\"baz\\"
}"
`;

exports[`utils > format applyFormat should apply format to given string 2`] = `
exports[`utils > format applyFormat applies format to given string 2`] = `
"foo:
bar:
baz: baz"
`;

exports[`utils > format applyFormat should apply format to given string 3`] = `
exports[`utils > format applyFormat applies format to given string 3`] = `
"{
\\"foo\\": \\"foo\\",
\\"bar\\": \\"bar\\",
\\"baz\\": \\"baz\\"
}"
`;

exports[`utils > format applyFormat should apply format to given string 4`] = `
exports[`utils > format applyFormat applies format to given string 4`] = `
"foo:
bar:
baz: baz"
`;

exports[`utils > format applyFormat should apply format to given string 5`] = `
"foo
"
`;

exports[`utils > format indentWithSpace should not affect string with spaces 1`] = `
exports[`utils > format indentWithSpace increases space indent 1`] = `
"{
\\"foo\\": \\"foo\\",
\\"bar\\": \\"bar\\",
\\"baz\\": \\"baz\\"
\\"foo\\": \\"foo\\",
\\"bar\\": \\"bar\\",
\\"baz\\": \\"baz\\"
}"
`;

exports[`utils > format indentWithSpace should replace all tabs with spaces 1`] = `
exports[`utils > format indentWithSpace replaces all tabs with spaces 1`] = `
"{
\\"foo\\": \\"foo\\",
\\"bar\\": \\"bar\\",
\\"baz\\": \\"baz\\"
}"
`;

exports[`utils > format indentWithTab should not affect string with tabs 1`] = `
exports[`utils > format indentWithTab doesn't affect string with tabs 1`] = `
"{
\\"foo\\": \\"foo\\",
\\"bar\\": \\"bar\\",
\\"baz\\": \\"baz\\"
}"
`;

exports[`utils > format indentWithTab should replace all spaces with tabs 1`] = `
exports[`utils > format indentWithTab replaces all spaces with tabs 1`] = `
"{
\\"foo\\": \\"foo\\",
\\"bar\\": \\"bar\\",
Expand Down
34 changes: 13 additions & 21 deletions packages/sharec-utils/lib/test/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ describe('utils > format', () => {
const yamlFxt = fixtures('default/yaml/01-formatting')

describe('hasSpacesIndent', () => {
it('should correctly determine spaces indent in string', () => {
it('determines spaces indent', () => {
expect(hasSpacesIndent(jsonFxt.result)).toBe(true)
expect(hasSpacesIndent(yamlFxt.result)).toBe(true)
expect(hasSpacesIndent(jsonFxt.current)).toBe(false)
})
})

describe('hasTabsIndent', () => {
it('should correctly determine tabs indent in string', () => {
it('determines tabs', () => {
expect(hasTabsIndent(jsonFxt.result)).toBe(false)
expect(hasTabsIndent(yamlFxt.result)).toBe(false)
expect(hasTabsIndent(jsonFxt.current)).toBe(true)
})
})

describe('hasEOF', () => {
it('should correctly determine line wrap at the and of the all lines', () => {
it('determines line wrap at the and of the string', () => {
expect(hasEOF(`foo${EOL}${EOL}`)).toBe(true)
expect(hasEOF('foo')).toBe(false)
})
Expand All @@ -47,27 +47,27 @@ describe('utils > format', () => {
})

describe('indentWithTab', () => {
it('should replace all spaces with tabs', () => {
it('replaces all spaces with tabs', () => {
expect(indentWithTab(jsonFxt.result)).toMatchSnapshot()
})

it('should not affect string with tabs', () => {
it("doesn't affect string with tabs", () => {
expect(indentWithTab(jsonFxt.current)).toMatchSnapshot()
})
})

describe('indentWithSpace', () => {
it('should replace all tabs with spaces', () => {
it('replaces all tabs with spaces', () => {
expect(indentWithSpace(jsonFxt.current, 2)).toMatchSnapshot()
})

it('should not affect string with spaces', () => {
expect(indentWithSpace(jsonFxt.result, 2)).toMatchSnapshot()
it('increases space indent', () => {
expect(indentWithSpace(jsonFxt.result, 4)).toMatchSnapshot()
})
})

describe('applyFormat', () => {
it('should apply format to given string', () => {
it('applies format to given string', () => {
expect(
applyFormat({
content: jsonFxt.current,
Expand Down Expand Up @@ -108,22 +108,14 @@ describe('utils > format', () => {
},
}),
).toMatchSnapshot()
expect(
applyFormat({
content: 'foo',
rules: {
eof: true,
},
}),
).toMatchSnapshot()
})
})

describe('getFormatByFilename', () => {
const jsFormat = { foo: 'bar' }
const yamlFormat = { bar: 'baz' }

it('should return format by extname', () => {
it('formats by extname', () => {
const formats = {
'*.json': jsFormat,
'*.yaml': yamlFormat,
Expand All @@ -133,7 +125,7 @@ describe('utils > format', () => {
expect(getFormatByFilename(formats, 'foo.yaml')).toBe(yamlFormat)
})

it('should return format by extnames sequence', () => {
it('formats by extnames sequence', () => {
const formats = {
'*.{json,yaml}': jsFormat,
}
Expand All @@ -142,7 +134,7 @@ describe('utils > format', () => {
expect(getFormatByFilename(formats, 'foo.yaml')).toBe(jsFormat)
})

it('should return format by filename', () => {
it('formats by filename', () => {
const formats = {
'foo.json': jsFormat,
'bar.yaml': yamlFormat,
Expand All @@ -152,7 +144,7 @@ describe('utils > format', () => {
expect(getFormatByFilename(formats, 'bar.yaml')).toBe(yamlFormat)
})

it('should return format by filenames sequence', () => {
it('formats by filenames sequence', () => {
const formats = {
'{foo.json,bar.yaml}': jsFormat,
'{bar.json,baz.yaml}': yamlFormat,
Expand Down
Loading

0 comments on commit 198a763

Please sign in to comment.