diff --git a/.eslintignore b/.eslintignore deleted file mode 100644 index 8fbc26b..0000000 --- a/.eslintignore +++ /dev/null @@ -1,7 +0,0 @@ -**/dist -**/coverage -**/test/expect -**/test/fixtures -**/node_modules/** -**/*.md -**/*.json diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 00d1d32..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1,159 +0,0 @@ -const OFF = 0 -const WARN = 1 -const ON = 2 -const YES = true -const USE = false - -module.exports = { - root: YES, - parser: 'typescript-eslint-parser', - - plugins: [ - 'typescript', - ], - - parserOptions: { - ecmaVersion: 2017, - impliedStrict: YES, - sourceType: 'module', - }, - - // https://github.com/sindresorhus/globals/blob/master/globals.json - env: { - node: YES, - }, - - // https://github.com/eslint/eslint/blob/master/conf/eslint-recommended.js - extends: [ - 'eslint:recommended', - ], - - rules: { - 'array-bracket-spacing': [ON, 'never'], - 'array-callback-return': ON, - 'arrow-parens': ON, - 'arrow-spacing': ON, - 'block-scoped-var': ON, - 'block-spacing': ON, - 'brace-style': [ON, '1tbs'], - 'comma-dangle': [ON, 'always-multiline'], - 'comma-spacing': ON, - 'comma-style': ON, - 'complexity': [ON, 13], // default is 20 - 'computed-property-spacing': ON, - 'consistent-return': ON, - 'consistent-this': [ON, '_self'], - 'curly': ON, - 'dot-location': [ON, 'property'], - 'dot-notation': WARN, - 'eol-last': ON, - 'eqeqeq': [ON, 'smart'], - 'func-call-spacing': ON, - 'guard-for-in': WARN, - 'handle-callback-err': [ON, '^err(or)?$'], - 'implicit-arrow-linebreak': ON, - 'indent': [ON, 2, { flatTernaryExpressions: YES, SwitchCase: 1 }], - 'jsx-quotes': [ON, 'prefer-double'], - 'key-spacing': [ON, { mode: 'minimum' }], - 'keyword-spacing': ON, - 'linebreak-style': [ON, 'unix'], - 'max-depth': ON, - 'max-len': [1, 120, 4, { ignoreUrls: YES, ignorePattern: '=\\s+/.+/' }], - 'max-nested-callbacks': [ON, 5], // default is 10 - 'new-parens': ON, - 'no-alert': ON, - 'no-array-constructor': ON, - 'no-caller': ON, - 'no-catch-shadow': ON, - 'no-confusing-arrow': [ON, { allowParens: YES }], - 'no-console': OFF, - 'no-div-regex': ON, - 'no-duplicate-imports': [ON, { includeExports: YES }], - 'no-eval': ON, - 'no-extend-native': ON, - 'no-extra-bind': ON, - 'no-floating-decimal': ON, - 'no-implicit-globals': ON, - 'no-implied-eval': ON, - 'no-iterator': ON, - 'no-label-var': ON, - 'no-lone-blocks': ON, - 'no-lonely-if': ON, - 'no-loop-func': ON, - 'no-multi-str': ON, - 'no-multiple-empty-lines': [ON, { max: 2 }], - 'no-native-reassign': ON, - 'no-new-func': ON, - 'no-new-object': ON, - 'no-new-wrappers': ON, - 'no-new': ON, - 'no-octal-escape': ON, - 'no-proto': ON, - 'no-prototype-builtins': WARN, - 'no-return-assign': [ON, 'except-parens'], - 'no-script-url': ON, - 'no-self-compare': ON, - 'no-sequences': ON, - 'no-shadow-restricted-names': ON, - 'no-template-curly-in-string': ON, - 'no-throw-literal': ON, - 'no-trailing-spaces': ON, - 'no-undef': OFF, // handled by TS - 'no-undef-init': ON, - 'no-unexpected-multiline': ON, - 'no-unmodified-loop-condition': WARN, - 'no-unneeded-ternary': ON, - 'no-unused-expressions': ON, - 'no-unused-vars': OFF, // handled by TS - 'no-use-before-define': [ON, { functions: USE, variables: USE }], - 'no-useless-call': ON, - 'no-useless-computed-key': ON, - 'no-useless-rename': ON, - 'no-useless-return': ON, - 'no-var': ON, - 'no-void': ON, - 'no-whitespace-before-property': ON, - 'no-with': ON, - 'object-curly-spacing': [ON, 'always'], - 'object-shorthand': ON, - 'one-var-declaration-per-line': ON, - 'operator-linebreak': ON, - 'prefer-const': [ON, { destructuring: 'all' }], - 'prefer-numeric-literals': ON, - 'prefer-promise-reject-errors': ON, - 'quote-props': [ON, 'consistent'], - 'quotes': [ON, 'single', 'avoid-escape'], - 'radix': ON, - 'require-await': ON, - 'require-yield': ON, - 'rest-spread-spacing': ON, - 'semi-spacing': ON, - 'semi': [ON, 'never'], - 'space-before-blocks': ON, - 'space-before-function-paren': [ON, 'always'], - 'space-in-parens': ON, - 'space-unary-ops': ON, - 'switch-colon-spacing': [ON, { after: YES }], - 'template-curly-spacing': ON, - 'unicode-bom': [ON, 'never'], - 'wrap-iife': [ON, 'inside'], - 'yield-star-spacing': ON, - 'yoda': [ON, 'never'], - - // Typescript ------------------------------------------------------------ - 'typescript/adjacent-overload-signatures': ON, - 'typescript/class-name-casing': ON, - 'typescript/member-delimiter-style': ON, - 'typescript/member-naming': [ON, { private: '^_', protected: '^_' }], - 'typescript/no-use-before-define': [ON, { functions: false, typedefs: false }], - }, - overrides: { - files: ['test/'], - env: { - mocha: YES, - }, - rules: { - 'no-console': OFF, - } - } -} diff --git a/.gitignore b/.gitignore index 090e7be..9adc040 100644 --- a/.gitignore +++ b/.gitignore @@ -25,7 +25,9 @@ pids *.pid *.seed *.tgz -*.nyc_output **/*.bak **/*.lcov **/*.log + +.nyc_output +.rpt2_cache diff --git a/.npmignore b/.npmignore index 6ee10b6..dbf396a 100644 --- a/.npmignore +++ b/.npmignore @@ -1,16 +1,12 @@ -# Exclude source and tests files from the package -src +# Exclude test files from the package, leave src test -# Include the ready to run code w/o sourcemaps +# Include the ready to run code !dist -**/*.map # Exclude VSCode, CI, and Dev related stuff .vscode -.eslint* *.yml -tsconfig* Makefile # Common git and npm ignored folders and files @@ -27,7 +23,9 @@ pids *.pid *.seed *.tgz -*.nyc_output **/*.bak **/*.lcov **/*.log + +.nyc_output +.rpt2_cache diff --git a/.travis.yml b/.travis.yml old mode 100755 new mode 100644 diff --git a/.vscode/launch.json b/.vscode/launch.json old mode 100755 new mode 100644 diff --git a/.vscode/settings.json b/.vscode/settings.json index cfe6245..bc4c69e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,8 +12,6 @@ "eslint.validate": [ "javascript", "javascriptreact", - "typescript", - "typescriptreact" ], "eslint.workingDirectories": [ "./src", diff --git a/CHANGELOG.md b/CHANGELOG.md index c121435..e397b62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,34 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased] +## \[1.1.0] - 2018-11-21 + +### Added + +- Option `escapeQuotes` to escape quotes in the output of strings (not wrapped by JSON output). +- TSLint instead of ESLint, for compatibility with CI services. +- Codacy quality and coverage services. + +### Changed + +- Convert `export.default` to `module.exports` in internal modules. Since it is a node.js library, it looks right and produces a cleaner code. +- The output of chained properties stops with a primitive value, to avoid some compile-time errors. + +### Removed + +- ESLint configuration. +- Coverity badge, get the right results with this service is a nightmare. + +### Fixed + +- Minor issues with linters. + +## \[1.0.0] - 2018-10-23 Major refactorization after two years, using TypeScript v3. ### Added + - Support for BOM mark in the source (it is preserved and does not affects the parsing). - Badges of the different services used to take care of the quality of the code. - Buy me a Coffee link. @@ -19,6 +42,7 @@ Major refactorization after two years, using TypeScript v3. - Support for replacement with more than one object property. ### Changed + - Revised Readme (WIP) - Make readonly the predefined variables `_FILE` and `_VERSION`. - Integrate Coverity (static analysis), CodeClimate (quality), Codebeat (quiality/analysis) and Codecov (coverage). @@ -39,37 +63,38 @@ Major refactorization after two years, using TypeScript v3. - Update devDependencies and minimum node.js version to 6. ### Removed + - The undocumented option `errorHandler` was removed, which makes the logic more simpler and secure. - The "dist" folder holding the transpilled code is excluded from CVS (slim PRs, simpler CI config) - (Internal) Remove babel, as the plugin now runs on node.js v6 or later. -## [0.3.5] - 2016-10-23 +## \[0.3.5] - 2016-10-23 - Add link to gulp plugin for jscc. Thanks to [@gucong3000](https://github.com/gucong3000). - Add test witj workaround to #3: "not work with eslint rule: comma-spacing" using the `prefixes` option. - Updated devDependencies. -## [0.3.4] - 2016-10-23 +## \[0.3.4] - 2016-10-23 - Added support for nested object and micro-like abilities (thanks to [@bergi9](https://github.com/bergi9)). - Updated devDependencies. -## [0.3.3] - 2016-10-23 +## \[0.3.3] - 2016-10-23 - Fixes issue with sourceMap generating incorrect output. -## [0.3.2] - 2016-10-22 +## \[0.3.2] - 2016-10-22 - Fixes an issue with losing location in sourceMap after replacing memvars. - Now JSCC always returns an object, even if there were no changes. - Updated `devDependencies`. -## [0.3.1] - 2016-10-14 +## \[0.3.1] - 2016-10-14 - Source map includes the source filename (needed by [jscc-brunch](https://www.npmjs.com/package/jscc-brunch)). - Removed jscc own source maps from the distribution. - The CommonJS version is validated by [Coverty Scan](https://scan.coverity.com/projects/amarcruz-jscc). -## [0.3.0] - 2016-10-06 +## \[0.3.0] - 2016-10-06 - Initial Release published as v0.3.0 in npm over an old `jscc` tool from Taketoshi Aono. diff --git a/Makefile b/Makefile index 289f7e9..a80b5c3 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ REQBUILD = "6.0.0" setup_cover: ifeq ($(CURBUILD),$(REQBUILD)) - @ npm i -g codecov + @ npm i -g codecov codacy-coverage @ curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter @ chmod +x ./cc-test-reporter @ ./cc-test-reporter before-build @@ -15,8 +15,9 @@ send_cover: ifeq ($(CURBUILD),$(REQBUILD)) @ echo Sending coverage report... @ nyc report -r=lcov - @ codecov -f coverage/lcov.info + @ codecov -f ./coverage/lcov.info @ ./cc-test-reporter after-build --exit-code $(TRAVIS_TEST_RESULT) + @ cat ./coverage/lcov.info | codacy-coverage -p . --language typescript @ echo The report was sent. else @ echo The coverage report will be sent in $(REQBUILD) diff --git a/README.md b/README.md old mode 100755 new mode 100644 index d05ca50..438fb89 --- a/README.md +++ b/README.md @@ -4,25 +4,24 @@ [![License][license-image]][license-url] [![AppVeyor][appveypr-image]][appveypr-url] [![Build Status][travis-image]][travis-url] -[![Coverity][coverity-image]][coverity-url] -[![codebeat][codebeat-image]][codebeat-url] +[![Codebeat][codebeat-image]][codebeat-url] [![Coverage][codecov-image]][codecov-url] Featuring some of the C preprocessor characteristics through special, configurable comments, jscc can be used in any type of files to build multiple versions of your software from the same code base. With jscc, you have: -* Conditional inclusion/exclusion of code, based on compile-time variables* -* Compile-time variables with all the power of JavaScript expressions -* Replacement of variables in the source code (by value at compile-time) -* Plugins for -* Source Map support +- Conditional inclusion/exclusion of blocks, based on compile-time variables* +- Compile-time variables with all the power of JavaScript expressions +- Replacement of variables in the sources, by its value at compile-time +- Sourcemap support, useful for JavaScript sources. +- TypeScript v3 definitions \* This feature allows you the conditional declaration of ES6 imports (See the [example](#example)). -jscc is derived on [jspreproc](http://amarcruz.github.io/jspreproc), the tiny source file preprocessor in JavaScript, enhanced with Source Map support but without the file importer nor the removal of comments ([rollup](https://rollupjs.org/guide/en) with [rollup-plugin-cleanup](https://www.npmjs.com/package/rollup-plugin-cleanup) does this better). +jscc is derived on [jspreproc](http://amarcruz.github.io/jspreproc), the tiny source file preprocessor in JavaScript, enhanced with sourcemap support but without the file importer nor the removal of comments ([rollup](https://rollupjs.org/guide/en) with [rollup-plugin-cleanup](https://www.npmjs.com/package/rollup-plugin-cleanup) does it better). -jscc works in NodeJS 6 or later, without external dependencies and a minimal footprint. It was designed to operate on small to medium pieces of code (like most nowadays) and, since the whole process is done in memory, it is _really fast_. +jscc works in NodeJS 6 or later, with minimal dependencies and footprint. It was designed to operate on small to medium pieces of code (like most nowadays) and, since the whole process is done in memory, it is _really fast_. jscc is **not** a minifier tool, but it does well what it does... @@ -50,20 +49,38 @@ npm i jscc -D ### Direct Usage ```js -import jscc from 'jscc'; - -const result = jscc(sourceCode, options) +const jscc = require('jscc'); + +const result = jscc(sourceCode, options); + +// or in async mode: +jscc(sourceCode, options, (err, result) => { + if (err) { + console.error(err); + } else { + console.log(result.code); + console.log(result.map); + } +}) ``` -## API +The result is a plain JS object with a property `code`, a string with the processed source, and a property `map`, with a raw sourcemap object, if required by the `sourcemap` option (its default is `true`). + +If a callback is provided, jscc will operate asynchronously and call the callback with an error object, if any, or `null` in the first parameter and the result in the second. + +## Directives + +jscc works with _directives_ inserted in the text files and prefixed with configurable character sequences, that defaults to `'/*'`, `'//'` and `' + [npm-image]: https://img.shields.io/npm/v/jscc.svg [npm-url]: https://www.npmjs.com/package/jscc [license-image]: https://img.shields.io/github/license/mashape/apistatus.svg @@ -186,23 +188,19 @@ Copyright (c) 2018, Alberto Martínez. [appveypr-url]: https://ci.appveyor.com/project/aMarCruz/jscc [travis-image]: https://img.shields.io/travis/aMarCruz/jscc.svg [travis-url]: https://travis-ci.org/aMarCruz/jscc -[coverity-image]: https://scan.coverity.com/projects/10389/badge.svg -[coverity-url]: https://scan.coverity.com/projects/amarcruz-jscc +[codacy-badge]: https://api.codacy.com/project/badge/Grade/30e8679fcd614227837ad250dd6c4030 +[codacy-url]: https://www.codacy.com/app/aMarCruz/jscc?utm_source=github.com&utm_medium=referral&utm_content=aMarCruz/jscc&utm_campaign=Badge_Grade +[codacyc-badge]: https://api.codacy.com/project/badge/Coverage/30e8679fcd614227837ad250dd6c4030 +[codacyc-url]: https://www.codacy.com/app/aMarCruz/jscc?utm_source=github.com&utm_medium=referral&utm_content=aMarCruz/jscc&utm_campaign=Badge_Coverage [codebeat-image]: https://codebeat.co/badges/7e15dc9d-42a8-4ea2-8bae-a21c09490fbe [codebeat-url]: https://codebeat.co/projects/github-com-amarcruz-jscc-dev [codecov-image]: https://codecov.io/gh/aMarCruz/jscc/branch/dev/graph/badge.svg [codecov-url]: https://codecov.io/gh/aMarCruz/jscc - - [climate-image]: https://codeclimate.com/github/aMarCruz/jscc/badges/gpa.svg [climate-url]: https://codeclimate.com/github/aMarCruz/jscc [issues-image]: https://codeclimate.com/github/aMarCruz/jscc/badges/issue_count.svg [issues-url]: https://codeclimate.com/github/aMarCruz/jscc -[cccover-image]: https://api.codeclimate.com/v1/badges/50d60a10ec7c9156b429/test_coverage -[cccover-url]: https://codeclimate.com/github/aMarCruz/jscc/test_coverage -[commits-image]: https://img.shields.io/github/commits-since/aMarCruz/jscc/latest.svg -[commits-url]: https://github.com/aMarCruz/jscc/commits/dev - - +[climatec-image]: https://api.codeclimate.com/v1/badges/50d60a10ec7c9156b429/test_coverage +[climatec-url]: https://codeclimate.com/github/aMarCruz/jscc/test_coverage [bmc-image]: https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png [bmc-url]: https://www.buymeacoffee.com/aMarCruz diff --git a/getpkgver.d.ts b/getpkgver.d.ts deleted file mode 100644 index 31ca3b7..0000000 --- a/getpkgver.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -/** - * Search for the package.json in the current or a parent directory that - * contains version information. - * - * @returns Version string or an empty string if the info was not found. - */ -declare function getPackageVersion (): string -export = getPackageVersion diff --git a/getpkgver.js b/getpkgver.js deleted file mode 100644 index 6d62aea..0000000 --- a/getpkgver.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./dist/lib/get-package-version').getPackageVersion diff --git a/index.d.ts b/index.d.ts index 95bc853..d46948f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,42 +1,90 @@ -/** +/* jscc v1.0 @author aMarCruz @license MIT */ -declare module 'jscc' { - //declare const jscc: Jscc - export = Jscc.jscc -} +export = Jscc + +/** + * Preprocessor for conditional comments and compile-time variable + * replacement replacement in text files (asynchronous version). + * + * @param source String to be preprocessed, encoded in utf8. + * @param filename Absolute or relative path to the current directory. + * @param options User options. + * @param callback NodeJS style callback that receives the error and result as parameters. + */ +declare function Jscc ( + source: string, + filename: string | null | undefined, + options: Jscc.Options | null | undefined, + callback: Jscc.Callback +): void +/** + * Preprocessor for conditional comments and compile-time variable + * replacement replacement in text files (synchronous version). + * + * @param source String to be preprocessed, encoded in utf8. + * @param filename Absolute or relative path to the current directory. + * @param options User options. + * @returns Object with `code` and `map` properties. + */ +declare function Jscc ( + source: string, + filename?: string | null, + options?: Jscc.Options | null +): Jscc.Result + +// tslint:disable:no-namespace declare namespace Jscc { + type QuoteType = 'single' | 'double' | 'both' + interface Options { /** - * Allows to preserve the empty lines of the directives and blocks that were - * removed. + * String with the type of quotes to escape in the output of strings: + * 'single', 'double' or 'both'. + * + * It does not affects the output of regexes or strings contained in the + * JSON output of objects. + */ + escapeQuotes?: QuoteType + + /** + * Allows to preserve the empty lines of the directives and blocks that + * were removed. * * Use this option with `sourceMap:false` if you are only interested in * keeping the line count. + * * @default false */ - keepLines?: boolean; + keepLines?: boolean + /** * Include the original source in the sourceMap. + * * @default false */ - mapContent?: boolean; + mapContent?: boolean + /** - * Make a hi-res sourceMap. + * Makes a hi-res sourceMap. + * * @default true */ - mapHires?: boolean; + mapHires?: boolean + /** * String, regex or array of strings or regex matching the start of a directive. * That is, the characters before the '#', usually the start of comments. + * * @default ['//','/*','