Skip to content
This repository was archived by the owner on May 1, 2023. It is now read-only.

Commit 875dc94

Browse files
committed
Merge branch 'release/0.0.2'
2 parents 72e77cb + a900b9f commit 875dc94

File tree

6 files changed

+147
-21
lines changed

6 files changed

+147
-21
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
44

5+
## [0.0.2] - 2018-11-18
6+
7+
### Fixed
8+
9+
- Handle multiple multiline comments without newline in between correctly
10+
511
## [0.0.1] - 2018-11-08
612

713
### Added
@@ -23,6 +29,7 @@ All notable changes to this project will be documented in this file. The format
2329

2430
- Add initial `updateLicenseHeader` function supporting JavaScript, TypeScript, PHP, PHTML and Twig.
2531

26-
[unreleased]: https://github.com/splish-me/copyright-headers/compare/0.0.1...HEAD
32+
[unreleased]: https://github.com/splish-me/copyright-headers/compare/0.0.2...HEAD
33+
[0.0.2]: https://github.com/splish-me/copyright-headers/compare/0.0.1...0.0.2
2734
[0.0.1]: https://github.com/splish-me/copyright-headers/compare/0.0.0...0.0.1
2835
[0.0.0]: https://github.com/splish-me/copyright-headers/compare/df12fe2868efc66641034590c3ffd37e0896afbb...HEAD

__tests__/index.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ echo "foo bar";
4242
*
4343
* Copyright (c) 2018 Splish UG (haftungsbeschränkt)
4444
*/
45+
echo "foo bar";
46+
`)
47+
})
48+
49+
test('does start with <?php (w/ unnecessary whitespace after opening tag)', () => {
50+
const input = `<?php
51+
52+
53+
echo "foo bar";
54+
`
55+
const [status, output] = getUpdatedCopyrightHeader(input, php, options)
56+
57+
expect(status).toEqual(CopyrightHeaderStatus.Added)
58+
expect(output).toEqual(`<?php
59+
/**
60+
* This file is part of @splish-me/copyright-headers
61+
*
62+
* Copyright (c) 2018 Splish UG (haftungsbeschränkt)
63+
*/
64+
65+
4566
echo "foo bar";
4667
`)
4768
})
@@ -115,6 +136,37 @@ echo "foo bar";
115136
* Copyright (c) 2016 Max Mustermann Corporation
116137
*/
117138
echo "foo bar";
139+
`)
140+
})
141+
142+
test('multiple copyright headers (w/o newlines in between)', () => {
143+
const input = `<?php
144+
/**
145+
* This file is part of @splish-me/copyright-headers
146+
*
147+
* Copyright (c) 2016 Splish UG (haftungsbeschränkt)
148+
*/
149+
/**
150+
* Copyright (c) 2016 Max Mustermann Corporation
151+
*/
152+
echo "foo bar";
153+
`
154+
const [status, output] = getUpdatedCopyrightHeader(input, php, {
155+
...options,
156+
shouldUpdate: () => true
157+
})
158+
159+
expect(status).toEqual(CopyrightHeaderStatus.Changed)
160+
expect(output).toEqual(`<?php
161+
/**
162+
* This file is part of @splish-me/copyright-headers
163+
*
164+
* Copyright (c) 2018 Splish UG (haftungsbeschränkt)
165+
*/
166+
/**
167+
* Copyright (c) 2016 Max Mustermann Corporation
168+
*/
169+
echo "foo bar";
118170
`)
119171
})
120172
})
@@ -236,6 +288,39 @@ echo "foo bar";
236288
*/
237289
echo "foo bar";
238290
?>
291+
`)
292+
})
293+
294+
test('multiple copyright headers (w/o newlines in between)', () => {
295+
const input = `<?php
296+
/**
297+
* This file is part of @splish-me/copyright-headers
298+
*
299+
* Copyright (c) 2016 Splish UG (haftungsbeschränkt)
300+
*/
301+
/**
302+
* Copyright (c) 2016 Max Mustermann Corporation
303+
*/
304+
echo "foo bar";
305+
?>
306+
`
307+
const [status, output] = getUpdatedCopyrightHeader(input, php, {
308+
...options,
309+
shouldUpdate: () => true
310+
})
311+
312+
expect(status).toEqual(CopyrightHeaderStatus.Changed)
313+
expect(output).toEqual(`<?php
314+
/**
315+
* This file is part of @splish-me/copyright-headers
316+
*
317+
* Copyright (c) 2018 Splish UG (haftungsbeschränkt)
318+
*/
319+
/**
320+
* Copyright (c) 2016 Max Mustermann Corporation
321+
*/
322+
echo "foo bar";
323+
?>
239324
`)
240325
})
241326
})

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@splish-me/copyright-headers",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"homepage": "https://github.com/splish-me/copyright-headers",
55
"bugs": {
66
"url": "https://github.com/splish-me/copyright-headers/issues"
@@ -26,12 +26,14 @@
2626
"_prettier": "prettier \"{src/**/*,*}.{js,jsx,ts,tsx,css,scss,sass,less,json,md,markdown,yaml,yml}\""
2727
},
2828
"dependencies": {
29+
"ramda": "^0.26.0",
2930
"signale": "^1.3.0"
3031
},
3132
"devDependencies": {
3233
"@types/glob": "^7.0.0",
3334
"@types/jest": "^23.3.9",
3435
"@types/node": "^10.0.0",
36+
"@types/ramda": "^0.25.41",
3537
"@types/signale": "^1.2.0",
3638
"glob": "^7.0.0",
3739
"jest": "^23.6.0",

src/index.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* @link https://github.com/splish-me/copyright-headers for the canonical source repository
77
*/
88
import * as fs from 'fs'
9+
import * as R from 'ramda'
910
import * as signale from 'signale'
1011
import * as util from 'util'
1112

@@ -120,33 +121,42 @@ export function getUpdatedCopyrightHeader(
120121
options: CopyrightHeaderOptions
121122
): [CopyrightHeaderStatus, string] {
122123
const header = getLicenseHeader(language, options.lines)
123-
const re = getLicenseHeaderRegExp(language)
124-
const match = content.match(re)
124+
const existingCopyrightHeader = getFirstCopyrightHeader(content, language)
125125

126-
if (!match) {
126+
if (!existingCopyrightHeader) {
127127
let newHeader = header
128128
let newContent = content
129129

130-
if (language.before && newContent.startsWith(language.before)) {
131-
newContent = content.substring(language.before.length)
132-
} else {
133-
if (language.after) {
134-
newHeader += `${language.after}`
130+
const contentStartsWithOpeningTag =
131+
language.before && content.startsWith(language.before)
132+
133+
if (language.before) {
134+
newHeader = language.before + newHeader
135+
136+
if (contentStartsWithOpeningTag) {
137+
newContent = content.substring(language.before.length)
135138
}
136139
}
137140

141+
if (language.after && !contentStartsWithOpeningTag) {
142+
newHeader += language.after
143+
}
144+
138145
return [CopyrightHeaderStatus.Added, `${newHeader}\n${newContent}`]
139146
}
140147

141-
if (match[0] === header) {
148+
if (existingCopyrightHeader === header) {
142149
return [CopyrightHeaderStatus.Unchanged, '']
143150
}
144151

145152
if (
146153
typeof options.shouldUpdate === 'function' &&
147-
options.shouldUpdate(match[0])
154+
options.shouldUpdate(existingCopyrightHeader)
148155
) {
149-
return [CopyrightHeaderStatus.Changed, content.replace(re, header)]
156+
return [
157+
CopyrightHeaderStatus.Changed,
158+
content.replace(existingCopyrightHeader, header)
159+
]
150160
}
151161

152162
return [CopyrightHeaderStatus.External, content]
@@ -167,19 +177,20 @@ function getSourceLanguage(filePath: string) {
167177

168178
function getLicenseHeader(language: SourceLanguage, lines: string[]) {
169179
return (
170-
(language.before || '') +
171180
`${language.begin}\n` +
172181
lines.map(language.buildLine).join('\n') +
173182
`\n${language.end}`
174183
)
175184
}
176185

177-
function getLicenseHeaderRegExp(language: SourceLanguage) {
178-
const forRe = (s: string) => s.replace(/(\?|\/|\*)/g, match => `\\${match}`)
186+
function getFirstCopyrightHeader(content: string, language: SourceLanguage) {
187+
const after = R.tail(R.split(language.begin, content))
179188

180-
return new RegExp(
181-
`${forRe(language.before || '')}${forRe(language.begin)}\n(.+\n)*${forRe(
182-
language.end
183-
)}`
184-
)
189+
if (R.isEmpty(after)) {
190+
return ''
191+
}
192+
193+
const [comment] = R.split(language.end, after.join(''))
194+
195+
return `${language.begin}${comment}${language.end}`
185196
}

wallaby.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = () => {
2+
return {
3+
files: ['src/**/*.ts'],
4+
tests: ['__tests__/**/*.ts'],
5+
env: {
6+
type: 'node',
7+
runner: 'node'
8+
},
9+
testFramework: 'jest'
10+
}
11+
}

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.3.tgz#3918b73ceed484e58367be5acb79d1775239e393"
4848
integrity sha512-sfGmOtSMSbQ/AKG8V9xD1gmjquC9awIIZ/Kj309pHb2n3bcRAcGMQv5nJ6gCXZVsneGE4+ve8DXKRCsrg3TFzg==
4949

50+
"@types/ramda@^0.25.41":
51+
version "0.25.41"
52+
resolved "https://registry.yarnpkg.com/@types/ramda/-/ramda-0.25.41.tgz#c965160129d7e8345e40508858514f8ec7af706f"
53+
integrity sha512-R6IkySFkrCN6Xd3exKX1PI2hvkmpbDZBKYz7u4uuYas8NeUCmcHkccYo6OWKtWIkYipjGBQP2ZzlTT+R3bUY6Q==
54+
5055
"@types/signale@^1.2.0":
5156
version "1.2.0"
5257
resolved "https://registry.yarnpkg.com/@types/signale/-/signale-1.2.0.tgz#575719e586a1352cf1de04478b5fc0920e221000"
@@ -2960,6 +2965,11 @@ qs@~6.5.2:
29602965
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"
29612966
integrity sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==
29622967

2968+
ramda@^0.26.0:
2969+
version "0.26.0"
2970+
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.26.0.tgz#3cbe059f5c18fbc0eb86f2396ab1f8320705424c"
2971+
integrity sha512-maK1XqpgsOo5DwjhROjqDvpm1vkphLQbpleVv+b3t5Y9uOQ0t8hTHT582+mDs7RLrex1kd4lWYizNXWLVjsq9w==
2972+
29632973
randomatic@^3.0.0:
29642974
version "3.1.1"
29652975
resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-3.1.1.tgz#b776efc59375984e36c537b2f51a1f0aff0da1ed"

0 commit comments

Comments
 (0)