diff --git a/fluent-bundle/makefile b/fluent-bundle/makefile index c7210775a..2ed518018 100644 --- a/fluent-bundle/makefile +++ b/fluent-bundle/makefile @@ -51,7 +51,7 @@ html: --excludePrivate \ --logger none \ --hideGenerator - @echo -e " $(OK) html build" + @echo -e " $(OK) html built" clean: @rm -f esm/*.js esm/*.d.ts esm/.compiled diff --git a/fluent-dedent/.esdoc.json b/fluent-dedent/.esdoc.json deleted file mode 100644 index 9f61dff1a..000000000 --- a/fluent-dedent/.esdoc.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "source": "./src", - "destination": "../html/dedent", - "plugins": [ - { - "name": "esdoc-standard-plugin" - }, - { - "name": "esdoc-ecmascript-proposal-plugin", - "option": { - "objectRestSpread": true, - "asyncGenerators": true - } - } - ] -} diff --git a/fluent-dedent/.gitignore b/fluent-dedent/.gitignore index 7d147bec1..99472bc4d 100644 --- a/fluent-dedent/.gitignore +++ b/fluent-dedent/.gitignore @@ -1,2 +1,3 @@ +esm/ /index.js /compat.js diff --git a/fluent-dedent/.npmignore b/fluent-dedent/.npmignore index f520b36f5..abad613ff 100644 --- a/fluent-dedent/.npmignore +++ b/fluent-dedent/.npmignore @@ -1,5 +1,7 @@ .nyc_output coverage -docs +esm/.compiled +src test makefile +tsconfig.json diff --git a/fluent-dedent/makefile b/fluent-dedent/makefile index c6585be16..dd474ba50 100644 --- a/fluent-dedent/makefile +++ b/fluent-dedent/makefile @@ -3,16 +3,30 @@ GLOBAL := FluentDedent include ../common.mk -build: index.js compat.js +lint: + @eslint --config $(ROOT)/eslint_ts.json --max-warnings 0 src/*.ts + @eslint --config $(ROOT)/eslint_test.json --max-warnings 0 test/ + @echo -e " $(OK) lint" + +.PHONY: compile +compile: esm/.compiled + +esm/.compiled: $(SOURCES) + @tsc + @touch $@ + @echo -e " $(OK) esm/ compiled" -test: +.PHONY: test +test: esm/.compiled @nyc --reporter=text --reporter=html mocha \ --recursive --ui tdd \ --require esm \ test/**/*_test.js -index.js: $(SOURCES) - @rollup $(CURDIR)/src/index.js \ +build: index.js compat.js + +index.js: esm/.compiled + @rollup $(CURDIR)/esm/index.js \ --config $(ROOT)/bundle_config.js \ --banner "/* $(PACKAGE)@$(VERSION) */" \ --amd.id $(PACKAGE) \ @@ -20,8 +34,8 @@ index.js: $(SOURCES) --output.file $@ @echo -e " $(OK) $@ built" -compat.js: $(SOURCES) - @rollup $(CURDIR)/src/index.js \ +compat.js: esm/.compiled + @rollup $(CURDIR)/esm/index.js \ --config $(ROOT)/compat_config.js \ --banner "/* $(PACKAGE)@$(VERSION) */" \ --amd.id $(PACKAGE) \ @@ -29,6 +43,18 @@ compat.js: $(SOURCES) --output.file $@ @echo -e " $(OK) $@ built" -lint: _lint -html: _html -clean: _clean +html: + @typedoc src \ + --out ../html/dedent \ + --mode file \ + --excludeNotExported \ + --excludePrivate \ + --logger none \ + --hideGenerator + @echo -e " $(OK) html built" + +clean: + @rm -f esm/*.js esm/*.d.ts esm/.compiled + @rm -f index.js compat.js + @rm -rf .nyc_output coverage + @echo -e " $(OK) clean" diff --git a/fluent-dedent/package.json b/fluent-dedent/package.json index 0c9ddd263..8c493f5aa 100644 --- a/fluent-dedent/package.json +++ b/fluent-dedent/package.json @@ -11,11 +11,10 @@ "email": "stas@mozilla.com" } ], - "directories": { - "lib": "./src" - }, + "type": "commonjs", "main": "./index.js", - "module": "./src/index.js", + "module": "./esm/index.js", + "types": "./esm/index.d.ts", "repository": { "type": "git", "url": "https://github.com/projectfluent/fluent.js.git" diff --git a/fluent-dedent/src/index.js b/fluent-dedent/src/index.ts similarity index 68% rename from fluent-dedent/src/index.js rename to fluent-dedent/src/index.ts index 7079f7119..f7fc61af6 100644 --- a/fluent-dedent/src/index.js +++ b/fluent-dedent/src/index.ts @@ -9,37 +9,46 @@ const RE_BLANK = /^[ \t]*$/; * line and it must end on a line of its own, with the closing delimiter on a * next line. * - * @param {Array} strings - * @param {...any} values - * @returns string + * @param strings + * @param values */ -export default function ftl(strings, ...values) { +export default function ftl( + strings: TemplateStringsArray, + ...values: Array +): string { let code = strings.reduce((acc, cur) => acc + values.shift() + cur); let lines = code.split("\n"); - let [first, commonIndent] = [lines.shift(), lines.pop()]; - if (!RE_BLANK.test(first)) { + let first = lines.shift(); + if (first === undefined || !RE_BLANK.test(first)) { throw new RangeError("Content must start on a new line."); } - if (!RE_BLANK.test(commonIndent)) { + + let commonIndent = lines.pop(); + if (commonIndent === undefined || !RE_BLANK.test(commonIndent)) { throw new RangeError("Closing delimiter must appear on a new line."); } - function dedent(line, idx) { + let dedented = []; + for (let i = 0; i < lines.length; i++) { + let line = lines[i]; let lineIndent = line.slice(0, commonIndent.length); if (lineIndent.length === 0) { // Empty blank lines are preserved even if technically they are not // indented at all. This also short-circuits the dedentation logic when // commonIndent.length is 0, i.e. when all indents should be kept. - return line; + dedented.push(line); + continue; } + if (lineIndent !== commonIndent) { // The indentation of the line must match commonIndent exacty. - throw new RangeError(`Insufficient indentation in line ${idx + 1}.`); + throw new RangeError(`Insufficient indentation in line ${i + 1}.`); } + // Strip commonIndent. - return line.slice(commonIndent.length); + dedented.push(line.slice(commonIndent.length)); } - return lines.map(dedent).join("\n"); + return dedented.join("\n"); } diff --git a/fluent-dedent/test/blank_test.js b/fluent-dedent/test/blank_test.js index 08a203e49..31e20013e 100644 --- a/fluent-dedent/test/blank_test.js +++ b/fluent-dedent/test/blank_test.js @@ -1,7 +1,7 @@ "use strict"; import assert from "assert"; -import ftl from "../src/index"; +import ftl from "../esm/index"; suite("blank lines", function() { test("leading", function() { diff --git a/fluent-dedent/test/content_test.js b/fluent-dedent/test/content_test.js index d94eaa461..df2037273 100644 --- a/fluent-dedent/test/content_test.js +++ b/fluent-dedent/test/content_test.js @@ -1,7 +1,7 @@ "use strict"; import assert from "assert"; -import ftl from "../src/index"; +import ftl from "../esm/index"; suite("content lines", function() { test("no indent", function () { diff --git a/fluent-dedent/test/eol_test.js b/fluent-dedent/test/eol_test.js index 14d886b7e..6b3896c9b 100644 --- a/fluent-dedent/test/eol_test.js +++ b/fluent-dedent/test/eol_test.js @@ -1,7 +1,7 @@ "use strict"; import assert from "assert"; -import ftl from "../src/index"; +import ftl from "../esm/index"; suite("EOL at extremes", function () { test("no EOLs", function () { diff --git a/fluent-dedent/test/interpolation_test.js b/fluent-dedent/test/interpolation_test.js index 45a495d37..0039911ce 100644 --- a/fluent-dedent/test/interpolation_test.js +++ b/fluent-dedent/test/interpolation_test.js @@ -1,7 +1,7 @@ "use strict"; import assert from "assert"; -import ftl from "../src/index"; +import ftl from "../esm/index"; suite("interpolation", function() { test("single", function() { diff --git a/fluent-dedent/test/mixed_test.js b/fluent-dedent/test/mixed_test.js index 340b4fe4d..259c36ab0 100644 --- a/fluent-dedent/test/mixed_test.js +++ b/fluent-dedent/test/mixed_test.js @@ -1,7 +1,7 @@ "use strict"; import assert from "assert"; -import ftl from "../src/index"; +import ftl from "../esm/index"; suite("mixed indent", function() { test("same amount", function() { diff --git a/fluent-dedent/test/tabs_test.js b/fluent-dedent/test/tabs_test.js index 87f07e60b..4246871ef 100644 --- a/fluent-dedent/test/tabs_test.js +++ b/fluent-dedent/test/tabs_test.js @@ -1,7 +1,7 @@ "use strict"; import assert from "assert"; -import ftl from "../src/index"; +import ftl from "../esm/index"; suite("tab indent", function() { test("same amount", function() { diff --git a/fluent-dedent/tsconfig.json b/fluent-dedent/tsconfig.json new file mode 100644 index 000000000..45fefaee8 --- /dev/null +++ b/fluent-dedent/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "target": "es2019", + "module": "es2015", + "strict": true, + "allowJs": false, + "esModuleInterop": true, + "moduleResolution": "node", + "noEmitHelpers": true, + "declaration": true, + "outDir": "./esm", + }, + "include": [ + "./src/**/*.ts" + ] +} diff --git a/fluent-sequence/makefile b/fluent-sequence/makefile index b7db9dc3b..0a255aa0f 100644 --- a/fluent-sequence/makefile +++ b/fluent-sequence/makefile @@ -25,7 +25,7 @@ test: esm/.compiled build: index.js compat.js -index.js: $(SOURCES) +index.js: esm/.compiled @rollup $(CURDIR)/esm/index.js \ --config $(ROOT)/bundle_config.js \ --banner "/* $(PACKAGE)@$(VERSION) */" \ @@ -34,7 +34,7 @@ index.js: $(SOURCES) --output.file $@ @echo -e " $(OK) $@ built" -compat.js: $(SOURCES) +compat.js: esm/.compiled @rollup $(CURDIR)/esm/index.js \ --config $(ROOT)/compat_config.js \ --banner "/* $(PACKAGE)@$(VERSION) */" \ @@ -45,13 +45,13 @@ compat.js: $(SOURCES) html: @typedoc src \ - --out ../html/bundle \ + --out ../html/sequence \ --mode file \ --excludeNotExported \ --excludePrivate \ --logger none \ --hideGenerator - @echo -e " $(OK) html build" + @echo -e " $(OK) html built" clean: @rm -f esm/*.js esm/*.d.ts esm/.compiled