Skip to content

Commit

Permalink
feat: change comment recognition and replace to regexp-based
Browse files Browse the repository at this point in the history
  • Loading branch information
e-cloud committed Oct 29, 2018
1 parent a07d291 commit ddfcec1
Show file tree
Hide file tree
Showing 46 changed files with 875 additions and 154 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Desktop.ini

# Build files
dist
build
coverage
lib/*.js
test/*.js
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
testMatch: ['**/test/*.test.ts'],
testEnvironment: 'node',
transform: {
'.ts': '<rootDir>/node_modules/ts-jest/preprocessor.js',
'^.+\\.ts$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'js'],
coveragePathIgnorePatterns: ['/node_modules/', '/test/'],
Expand Down
23 changes: 5 additions & 18 deletions lib/css-renderer.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
import * as gonzales from 'gonzales-pe'
import { generateBlockCommentContent } from './util'
import { generateBlockCommentContent, removeOldCopyrightWithinBlockComment, EOL } from './util'

function render(source: string, template: string, syntax = 'css') {
const parseTree = gonzales.parse(source, {
syntax,
})
function render(source: string, template: string) {
const desiredComment = `/*${generateBlockCommentContent(template)}*/${EOL + EOL}`

const commentNode = gonzales.createNode({
syntax,
type: 'multilineComment',
content: generateBlockCommentContent(template),
})
const blankLineNode = gonzales.createNode({
type: 'space',
content: '\n\n',
})
parseTree.insert(0, blankLineNode)
parseTree.insert(0, commentNode)
source = removeOldCopyrightWithinBlockComment(source)

return parseTree.toString()
return desiredComment + source.trimLeft()
}

export { render }
43 changes: 5 additions & 38 deletions lib/js-renderer.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,11 @@
import * as recast from 'recast'
import { generateBlockCommentContent } from './util'
import { generateBlockCommentContent, removeOldCopyrightWithinBlockComment, EOL } from './util'

const builders = recast.types.builders
function render(source: string, template: string) {
const desiredComment = `/*${generateBlockCommentContent(template)}*/${EOL + EOL}`

function parse(source: string, parserType = 'babel') {
return recast.parse(source, {
parser: require(`recast/parsers/${parserType}`),
})
}

function print(ast: any) {
return recast.print(ast, {
lineTerminator: '\n',
}).code
}

function appendCopyright(ast: any, crText: string) {
const comment = builders.commentBlock(generateBlockCommentContent(crText))

const firstStatement = ast.program.body[0]

if (firstStatement) {
firstStatement.comments = firstStatement.leadingComments || []

firstStatement.comments.unshift(comment)
}

return ast
}

function render(source: string, template: string, parserType?: 'babel' | 'typescript') {
const ast = parse(source, parserType)

const alteredAst = appendCopyright(ast, template)

return addBlankLineBetweenCopyrightAndCode(print(alteredAst))
}
source = removeOldCopyrightWithinBlockComment(source)

function addBlankLineBetweenCopyrightAndCode(fileString: string) {
return fileString.replace(/\*\/$/m, '*/\n')
return desiredComment + source.trimLeft()
}

export { render }
50 changes: 45 additions & 5 deletions lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
function generateBlockCommentContent(text: string, lineChar = '*', needTopChar = true) {
return `${needTopChar ? lineChar : ''}\n${text
export const EOL = '\n'

export function generateBlockCommentContent(text: string, lineChar = '*', needTopChar = true) {
return `${needTopChar ? lineChar : ''}${EOL}${text
.trim()
.split('\n')
.split(EOL)
.map(line => ` ${lineChar} ${line}`.trimRight())
.join('\n')}\n `
.join(EOL)}${EOL} `
}

export function removeOldCopyrightWithinBlockComment(source: string) {
return removeOldCopyright(source, blockCommentRegexp)
}

export function removeOldCopyrightWithinXML(source: string) {
return removeOldCopyright(source, xmlCommentRegexp)
}

export function removeOldCopyrightWithinYAML(source: string) {
return removeOldCopyright(source, yamlMultilineCommentRegexp)
}

export { generateBlockCommentContent }
export function removeOldCopyright(source: string, commentRegexp: RegExp, cpRegExp = copyrightRegexp) {
if (cpRegExp.test(source)) {
let matches
const existingVersions = []
const input = source.trim()
// tslint:disable-next-line:no-conditional-assignment
while (matches = commentRegexp.exec(input)) {
// find out the copyright comment
if (cpRegExp.test(matches[0])) {
existingVersions.push(matches[0])
}
}

// remove all existing copyrights
existingVersions.forEach(item => source = source.replace(item, EOL + EOL))
}

return source
}

export const copyrightRegexp = /copyright/i

export const xmlCommentRegexp = /\s*<!--([\s\S]*?)-->\s*/g

export const blockCommentRegexp = /\s*\/\*\*\s*\n([\S\s]*?)\*\/\s*/g

export const yamlMultilineCommentRegexp = /\s*(#\s*[^\n]+\n){2,}\s*/g
16 changes: 4 additions & 12 deletions lib/xml-renderer.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { generateBlockCommentContent } from './util'

const xmlCommentRegexp = /$(<!--([\s\S]*?)-->)/g
import { generateBlockCommentContent, EOL, removeOldCopyrightWithinXML } from './util'

function render(source: string, template: string) {
const comment = `<!--${generateBlockCommentContent(template, '-', false)}-->\n\n`

const matches = xmlCommentRegexp.exec(source.trim())

let result
const desiredComment = `<!--${generateBlockCommentContent(template, '-', false)}-->${EOL + EOL}`

if (!matches) {
result = comment + source
}
source = removeOldCopyrightWithinXML(source)

return result
return desiredComment + source.trimLeft()
}

export { render }
17 changes: 4 additions & 13 deletions lib/yaml-renderer.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import * as yaml from 'yaml'
import { EOL, removeOldCopyrightWithinYAML } from './util';

function render(source: string, template: string) {
const templateLines = template.split('\n')
const comment = templateLines.map(line => ` ${line}`.trimRight()).join('\n')
const desiredComment = template.split('\n').map(line => `# ${line}`.trimRight()).join(EOL) + EOL + EOL

const doc = yaml.parseDocument(source, {
keepCstNodes: true,
})
source = removeOldCopyrightWithinYAML(source)

doc.commentBefore = comment

const preout = doc.toString()

const lastTemplateLine = templateLines[templateLines.length - 1]

return preout.replace(lastTemplateLine, `${lastTemplateLine}\n`)
return desiredComment + source.trimLeft()
}

export { render }
25 changes: 25 additions & 0 deletions test/__baselines__/css/with-normal-top-comment.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (C) xxx Systems, Inc - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by John Doe <jdoe@heaven.com>, 1984
*
* From https://softwareengineering.stackexchange.com/a/68150/266765
*/

/**
* Lorem Ipsum is simply dummy text of the printing and typesetting industry.
* Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
* when an unknown printer took a galley of type and scrambled it to make a type
* specimen book. It has survived not only five centuries, but also the leap into
* electronic typesetting, remaining essentially unchanged.
*/

body {
background-color: lightblue;
}

h1 {
color: navy;
margin-left: 20px;
}
17 changes: 17 additions & 0 deletions test/__baselines__/css/with-same-copyright.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (C) xxx Systems, Inc - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by John Doe <jdoe@heaven.com>, 1984
*
* From https://softwareengineering.stackexchange.com/a/68150/266765
*/

body {
background-color: lightblue;
}

h1 {
color: navy;
margin-left: 20px;
}
17 changes: 17 additions & 0 deletions test/__baselines__/css/with-top-copyright.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (C) xxx Systems, Inc - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by John Doe <jdoe@heaven.com>, 1984
*
* From https://softwareengineering.stackexchange.com/a/68150/266765
*/

body {
background-color: lightblue;
}

h1 {
color: navy;
margin-left: 20px;
}
25 changes: 25 additions & 0 deletions test/__baselines__/html/with-inner-copyright.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!--
- Copyright (C) xxx Systems, Inc - All Rights Reserved
- Unauthorized copying of this file, via any medium is strictly prohibited
- Proprietary and confidential
- Written by John Doe <jdoe@heaven.com>, 1984
-
- From https://softwareengineering.stackexchange.com/a/68150/266765
-->

<!DOCTYPE html>

<html>

<head>
<title>Page Title</title>
</head>

<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>

</html>
32 changes: 32 additions & 0 deletions test/__baselines__/html/with-normal-top-comment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!--
- Copyright (C) xxx Systems, Inc - All Rights Reserved
- Unauthorized copying of this file, via any medium is strictly prohibited
- Proprietary and confidential
- Written by John Doe <jdoe@heaven.com>, 1984
-
- From https://softwareengineering.stackexchange.com/a/68150/266765
-->

<!--
- Lorem Ipsum is simply dummy text of the printing and typesetting industry.
- Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
- when an unknown printer took a galley of type and scrambled it to make a type
- specimen book. It has survived not only five centuries, but also the leap into
- electronic typesetting, remaining essentially unchanged.
-->

<!DOCTYPE html>
<html>

<head>
<title>Page Title</title>
</head>

<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>

</html>
24 changes: 24 additions & 0 deletions test/__baselines__/html/with-same-copyright.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
- Copyright (C) xxx Systems, Inc - All Rights Reserved
- Unauthorized copying of this file, via any medium is strictly prohibited
- Proprietary and confidential
- Written by John Doe <jdoe@heaven.com>, 1984
-
- From https://softwareengineering.stackexchange.com/a/68150/266765
-->

<!DOCTYPE html>
<html>

<head>
<title>Page Title</title>
</head>

<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>

</html>
24 changes: 24 additions & 0 deletions test/__baselines__/html/with-top-copyright.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
- Copyright (C) xxx Systems, Inc - All Rights Reserved
- Unauthorized copying of this file, via any medium is strictly prohibited
- Proprietary and confidential
- Written by John Doe <jdoe@heaven.com>, 1984
-
- From https://softwareengineering.stackexchange.com/a/68150/266765
-->

<!DOCTYPE html>
<html>

<head>
<title>Page Title</title>
</head>

<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>

</html>
22 changes: 22 additions & 0 deletions test/__baselines__/js/with-normal-top-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (C) xxx Systems, Inc - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by John Doe <jdoe@heaven.com>, 1984
*
* From https://softwareengineering.stackexchange.com/a/68150/266765
*/

/**
* Lorem Ipsum is simply dummy text of the printing and typesetting industry.
* Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
* when an unknown printer took a galley of type and scrambled it to make a type
* specimen book. It has survived not only five centuries, but also the leap into
* electronic typesetting, remaining essentially unchanged.
*/

function add(a, b) {
return a + b;
}

module.exports = add;

0 comments on commit ddfcec1

Please sign in to comment.