Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is-language-code@^3.1.0 #9729

Merged
merged 1 commit into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
90 changes: 45 additions & 45 deletions lib/utilities/get-lang-arg.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
/*

This utility processes the argument passed with the `lang` option
This utility processes the argument passed with the `lang` option
in ember-cli, i.e. `ember (new||init||addon) app-name --lang=langArg`

Execution Context (usage, input, output, error handling, etc.):
Expand Down Expand Up @@ -35,11 +35,11 @@ Warning Messages (if necessary):

'use strict';

const getLangCodeInfo = require('is-language-code');
const { isLangCode } = require('is-language-code');

// Primary language code validation function (boolean)
function isValidLangCode(langArg) {
return getLangCodeInfo(langArg).res;
return isLangCode(langArg).res;
}

// Generates the result to pass back to `init`
Expand All @@ -48,29 +48,29 @@ function getResult(langArg) {
}

/*
Misuse case: attempt to set application programming language via `lang`
Misuse case: attempt to set application programming language via `lang`
AND
Edge case: valid language code AND a common programming language abbreviation
-------------------------------------------------------------------------------
It is possible that a user might mis-interpret the type of `language` that is
specified by the `--lang` flag. One notable potential `misuse case` is one in
which the user thinks `--lang` specifies the application's programming
language. For example, the user might call `ember new my-app --lang=typescript`
expecting to achieve an effect similar to the one provided by the
It is possible that a user might mis-interpret the type of `language` that is
specified by the `--lang` flag. One notable potential `misuse case` is one in
which the user thinks `--lang` specifies the application's programming
language. For example, the user might call `ember new my-app --lang=typescript`
expecting to achieve an effect similar to the one provided by the
`ember-cli-typescript` addon.

This misuse case is handled by checking the input `langArg` against an Array
containing notable programming language-related values: language names
(e.g. `JavaScript`), abbreviations (e.g. `js`), file extensions (e.g. `.js`),
or versions (e.g. `ES6`), etc. Specifically, if `langArg` is found within this
reference list, a WARNING message that describes correct `--lang` usage will
be emitted. The `lang` attribute will not be assigned in `index.html`, and the
This misuse case is handled by checking the input `langArg` against an Array
containing notable programming language-related values: language names
(e.g. `JavaScript`), abbreviations (e.g. `js`), file extensions (e.g. `.js`),
or versions (e.g. `ES6`), etc. Specifically, if `langArg` is found within this
reference list, a WARNING message that describes correct `--lang` usage will
be emitted. The `lang` attribute will not be assigned in `index.html`, and the
user will be notified with a corresponding STATUS message.

There are several edge cases (marked) where `langArg` is both a commonly-used
abbreviation for a programming language AND a valid language code. The behavior
for these cases is to assume the user has used `--lang` correctly and set the
`lang` attribute to the valid code in `index.html`. To cover for potential
abbreviation for a programming language AND a valid language code. The behavior
for these cases is to assume the user has used `--lang` correctly and set the
`lang` attribute to the valid code in `index.html`. To cover for potential
misuage, several helpful messages will also be emitted:
- `ts` is a valid language code AND a common programming language abbreviation
- the `lang` attribute will be set to `ts` in the application
Expand Down Expand Up @@ -141,44 +141,44 @@ function isValidCodeAndProg(langArg) {
return isValidLangCode(langArg) && isProgLang(langArg);
}

/*
/*
Misuse case: `--lang` called without `langArg` (NB: parser bug workaround)
-------------------------------------------------------------------------------
This is a workaround for handling an existing bug in the ember-cli parser
where the `--lang` option is specified in the command without a corresponding
value for `langArg`.
This is a workaround for handling an existing bug in the ember-cli parser
where the `--lang` option is specified in the command without a corresponding
value for `langArg`.

As examples, the parser behavior would likely affect the following usages:
As examples, the parser behavior would likely affect the following usages:
1. `ember new app-name --lang --skip-npm
2. `ember new app-name --lang`

In this context, the unintended parser behavior is that `langArg` will be
assingned to the String that immediately follows `--lang` in the command. If
`--lang` is the last explicitly defined part of the command (as in the second
example above), the first of any any `hidden` options pushed onto the command
after the initial parse (e.g. `--disable-analytics`, `--no-watcher`) will be
used when assigning `langArg`.
In this context, the unintended parser behavior is that `langArg` will be
assingned to the String that immediately follows `--lang` in the command. If
`--lang` is the last explicitly defined part of the command (as in the second
example above), the first of any any `hidden` options pushed onto the command
after the initial parse (e.g. `--disable-analytics`, `--no-watcher`) will be
used when assigning `langArg`.

In the above examples, `langArg` would likely be assigned as follows:
1. `ember new app-name --lang --skip-npm => `langArg='--skip-npm'`
2. `ember new app-name --lang` => `langArg='--disable-analytics'`

The workaround impelemented herein is to check whether or not the value of
`langArg` starts with a hyphen. The rationale for this approach is based on
The workaround impelemented herein is to check whether or not the value of
`langArg` starts with a hyphen. The rationale for this approach is based on
the following underlying assumptions:
- ALL CLI options start with (at least one) hyphen
- NO valid language codes start with a hyphen

If the leading hyphen is detected, the current behavior is to assume `--lang`
was declared without a corresponding specification. A WARNING message that
describes correct `--lang` usage will be emitted. The `lang` attribute will not
be assigned in `index.html`, and the user will be notified with a corresponding
If the leading hyphen is detected, the current behavior is to assume `--lang`
was declared without a corresponding specification. A WARNING message that
describes correct `--lang` usage will be emitted. The `lang` attribute will not
be assigned in `index.html`, and the user will be notified with a corresponding
STATUS message. Execution will not be halted.

Other complications related to this parser behavior are considered out-of-scope
and not handled here. In the first example above, this workaround would ensure
that `lang` is not assigned to `--skip-npm`, but it would not guarantee that
`--skip-npm` is correctly processed as a command option. That is, `npm` may or
Other complications related to this parser behavior are considered out-of-scope
and not handled here. In the first example above, this workaround would ensure
that `lang` is not assigned to `--skip-npm`, but it would not guarantee that
`--skip-npm` is correctly processed as a command option. That is, `npm` may or
may not get skipped during execution.
*/

Expand All @@ -194,12 +194,12 @@ const MSG_HEAD = `An issue with the \`--lang\` flag returned the following messa

// Message context from language code validation (valid: null, invalid: reason)
function getLangCodeMsg(langArg) {
return getLangCodeInfo(langArg).message;
return isLangCode(langArg).message;
}

// Edge case: valid language code AND a common programming language abbreviation
function getValidAndProgMsg(langArg) {
return `The \`--lang\` flag has been used with argument \`${langArg}\`,
return `The \`--lang\` flag has been used with argument \`${langArg}\`,
which is BOTH a valid language code AND an abbreviation for a programming language.
${getProgLangMsg(langArg)}`;
}
Expand All @@ -218,15 +218,15 @@ function getCliMsg() {

// 3. `STATUS` message: report if `lang` will be set in `index.html`
function getStatusMsg(langArg, willSet) {
return `The human language of the application will ${willSet ? `be set to ${langArg}` : `NOT be set`} in
return `The human language of the application will ${willSet ? `be set to ${langArg}` : `NOT be set`} in
the \`<html>\` element's \`lang\` attribute in \`index.html\`.`;
}

// 4. `HELP` message: template for all `--lang`-related warnings emitted
const MSG_HELP = `If this was not your intention, you may edit the \`<html>\` element's
const MSG_HELP = `If this was not your intention, you may edit the \`<html>\` element's
\`lang\` attribute in \`index.html\` manually after the process is complete.
Information about using the \`--lang\` flag:
The \`--lang\` flag sets the base human language of an app or test app:
The \`--lang\` flag sets the base human language of an app or test app:
- \`app/index.html\` (app)
- \`tests/dummy/app/index.html\` (addon test app)
If used, the lang option must specfify a valid language code.
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"http-proxy": "^1.18.1",
"inflection": "^1.13.1",
"is-git-url": "^1.0.0",
"is-language-code": "^2.0.0",
"is-language-code": "^3.1.0",
"isbinaryfile": "^4.0.8",
"js-yaml": "^3.14.0",
"json-stable-stringify": "^1.0.1",
Expand Down Expand Up @@ -147,7 +147,6 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.3.1",
"fixturify": "^2.1.0",
"is-language-code": "^2.0.0",
"jsdom": "^19.0.0",
"latest-version": "^5.1.0",
"mocha": "^8.3.2",
Expand Down
17 changes: 13 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@
core-js "^2.6.5"
regenerator-runtime "^0.13.4"

"@babel/runtime@^7.14.0":
version "7.16.5"
resolved "https://registry.npmjs.org/@babel/runtime/-/runtime-7.16.5.tgz#7f3e34bf8bdbbadf03fbb7b1ea0d929569c9487a"
integrity sha512-TXWihFIS3Pyv5hzR7j6ihmeLkZfrXGxAr5UfSl8CHf+6q/wpiYDkUau0czckpYG8QmnCIuPpdLtuA9VmuGGyMA==
dependencies:
regenerator-runtime "^0.13.4"

"@babel/template@^7.16.0", "@babel/template@^7.7.4":
version "7.16.0"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.16.0.tgz#d16a35ebf4cd74e202083356fab21dd89363ddd6"
Expand Down Expand Up @@ -4555,10 +4562,12 @@ is-interactive@^1.0.0:
resolved "https://registry.yarnpkg.com/is-interactive/-/is-interactive-1.0.0.tgz#cea6e6ae5c870a7b0a0004070b7b587e0252912e"
integrity sha512-2HvIEKRoqS62guEC+qBjpvRubdX910WCMuJTZ+I9yvqKU2/12eSL549HMwtabb4oupdj2sMP50k+XJfB/8JE6w==

is-language-code@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/is-language-code/-/is-language-code-2.0.0.tgz#6f4d59c551d73b98c45cf9f1d3ce65cee060e65b"
integrity sha512-6xKmRRcP2YdmMBZMVS3uiJRPQgcMYolkD6hFw2Y4KjqyIyaJlCGxUt56tuu0iIV8q9r8kMEo0Gjd/GFwKrgjbw==
is-language-code@^3.1.0:
version "3.1.0"
resolved "https://registry.npmjs.org/is-language-code/-/is-language-code-3.1.0.tgz#b2386b49227e7010636f16d0c2c681ca40136ab5"
integrity sha512-zJdQ3QTeLye+iphMeK3wks+vXSRFKh68/Pnlw7aOfApFSEIOhYa8P9vwwa6QrImNNBMJTiL1PpYF0f4BxDuEgA==
dependencies:
"@babel/runtime" "^7.14.0"

is-negative-zero@^2.0.0:
version "2.0.0"
Expand Down