Skip to content

Commit

Permalink
Minor changes to single line comment cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulioRandall committed Jun 9, 2024
1 parent 131c951 commit d90b6ce
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ import p23, { cleanFileNode } from 'p23'
const fileDocs = p23().map(cleanFileNode)
```

Note that cleaning doesn't alter whitespace. Because I have no idea what kind of whitespace formatting someone may use. Multiline comments have a minor exception where the leading whitespace and prefix `//` are removed.
Note that cleaning doesn't alter whitespace. Because I have no idea what kind of whitespace formatting someone may use.

An unbroken series of single line comments are the exception. The leading whitespace and prefix `//` is removed from each line. The leading space in the
content is also removed if there is one, and only one, leading space.

```js
[
Expand All @@ -109,7 +112,7 @@ Note that cleaning doesn't alter whitespace. Because I have no idea what kind of
relPath: "./src/lib/BartSimpson.svelte",
absPath: "/home/esmerelda/github/my-project/src/lib/BartSimpson.svelte",
nodes: {
ay_caramba: [" A node with the name (path) 'ay_caramba'."],
ay_caramba: ["A node with the name (path) 'ay_caramba'."],
eat: {
my: {
shorts: [`
Expand All @@ -121,11 +124,11 @@ Note that cleaning doesn't alter whitespace. Because I have no idea what kind of
},
js: {
multiline: [`
An unbroken
An unbroken
series of
series of
single line comments.`]
single line comments.`]
},
html: {
line: [` P23 will parse HTML comments too. `],
Expand All @@ -143,7 +146,7 @@ Note that cleaning doesn't alter whitespace. Because I have no idea what kind of

1. Doc strings include the comment delimters unless cleaned with `cleanFileNode` or by your own means.
2. Cleaning and managing the whitespace in node values is your responsibility.
3. Path segments must adhere to: `^[$@a-zA-Z_][$@a-zA-Z0-9_\-]*$`. This list may be extended in future to include almost any string character.
3. Path segments must adhere to: `^[$@a-zA-Z_][$@a-zA-Z0-9_\-]*$`. This list may be extended in future to include **almost** any string character.
4. Nodes with the same name are in order of appearance within the file.
5. Yes, it will parse block comments in CSS nodes too.
6. "Don't have a cow, Man!" - Bart Simpson
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Simple tool for adding parsable notes as comments within Svelte components.",
"type": "module",
"license": "MIT",
"version": "0.11.0",
"version": "0.12.0",
"engines": {
"node": ">=18"
},
Expand Down
6 changes: 6 additions & 0 deletions src/clean-file-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,16 @@ const removeCommentPrefixFromLines = (s) => {
const lines = s.split('\n')

for (let i = 0; i < lines.length; i++) {
// Remove the indent and comment delimter.
const linePrefix = lines[i].match(/^\s*\/\//)
if (linePrefix) {
lines[i] = lines[i].slice(linePrefix[0].length)
}

// Remove the leading space if there is one, and only one, leading space.
if (/^ [^ ]/.test(lines[i])) {
lines[i] = lines[i].slice(1)
}
}

return lines.join('\n')
Expand Down
12 changes: 6 additions & 6 deletions src/clean-file-node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ describe('clean-file-node.js', () => {
describe('JavaScript line & line block', () => {
test('One liner', () => {
const act = parseNodeValue('//p23.name value ')
expect(act).toEqual(' value ')
expect(act).toEqual('value ')
})

test('Line block with content starting on initial line', () => {
const act = parseNodeValue(`//p23.name zero
// one`)
expect(act).toEqual(` zero
one`)
expect(act).toEqual(`zero
one`)
})

test('Line block with content starting on second line', () => {
Expand All @@ -61,9 +61,9 @@ describe('clean-file-node.js', () => {
//
// three`)
expect(act).toEqual(`
one
one
three`)
three`)
})
})
})
Expand All @@ -79,7 +79,7 @@ describe('clean-file-node.js', () => {

const exp = {
nodes: {
initial: [' Mr '],
initial: ['Mr '],
name: [' Smith '],
age: [' 24 '],
},
Expand Down

0 comments on commit d90b6ce

Please sign in to comment.